WebApplications.cs

// Copyright � 2007 Jeffrey Bazinet, http://www.vwd-cms.com/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using System.Web.Hosting;

namespace VwdCms.Admin
{
    public class WebApplications : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Table tblItems;
        protected System.Web.UI.WebControls.Label lblError;
        protected System.Web.UI.WebControls.Panel pnlError;

        protected void Page_Load(object sender, EventArgs e)
        {
            VwdCms.Configuration.Utilities.ProtectAdminPages();

            ApplicationManager appMgr = ApplicationManager.GetApplicationManager();

            ApplicationInfo[] apps = appMgr.GetRunningApplications();

            string[] titles = { "ID", "VirtualPath", "PhysicalPath" };

            Table tb = this.tblItems;
            tb.Rows.Clear();
            tb.Controls.Clear();

            AddTitleRow(tblItems, titles);
            foreach (ApplicationInfo ai in apps)
            {
                AddRow(tb, ai);
            }
        }

        //[AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand,System.Web.AspNetHostingPermissionLevel.Minimal)]
        //[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand, System.Security.Permissions.PermissionState.Unrestricted)]
        protected void lbRestartApp_Command(object sender, CommandEventArgs e)
        {
            bool hostPerm = false;
            bool secPerm = false;

            try
            {
                AspNetHostingPermission permission = new AspNetHostingPermission(AspNetHostingPermissionLevel.Minimal);
                permission.Demand();
                hostPerm = true;

            }
            catch (Exception ex1)
            {
                string err = "<h2>You do not have the ASP Hosting permissions required "
                    + " to perform the Application Restart."
                + "<br/><br/>Error details:<br/><br/>" + ex1.ToString();
                this.lblError.Text = err;
                this.pnlError.Visible = true;
            }


            try
            {
                System.Security.Permissions.SecurityPermission permission =
                    new System.Security.Permissions.SecurityPermission(
                    System.Security.Permissions.PermissionState.Unrestricted);
                permission.Demand();
                secPerm = true;
            }
            catch (Exception ex2)
            {
                string err = "<h2>You do not have the security permissions required "
                    + " to perform the Application Restart."
                + "<br/><br/>Error details:<br/><br/>" + ex2.ToString();
                this.lblError.Text = err;
                this.pnlError.Visible = true;
            }

            if (hostPerm  secPerm)
            {
                string appID = Convert.ToString(e.CommandArgument);

                ApplicationManager appMgr = ApplicationManager.GetApplicationManager();

                appMgr.ShutdownApplication(appID);
            }
        }

        private void AddRow(Table tb, ApplicationInfo appInfo)
        {
            TableRow tr = null;
            TableCell tc = null;

            tr = new TableRow();

            LinkButton lb = null;

            string[] columnData = new string[3];
            columnData[0] = appInfo.ID;
            columnData[1] = appInfo.VirtualPath;
            columnData[2] = appInfo.PhysicalPath;

            foreach (string data in columnData)
            {
                tc = new TableCell();
                tc.Text = data;
                tc.HorizontalAlign = HorizontalAlign.Left;
                tr.Cells.Add(tc);
            }

            lb = new LinkButton();
            lb.ID = "lbRestartApp_" + appInfo.ID;
            lb.Text = "Restart";
            lb.CommandArgument = appInfo.ID;
            lb.Command += new CommandEventHandler(lbRestartApp_Command);
            tc = new TableCell();
            tc.Controls.Add(lb);
            tr.Cells.Add(tc);


            tb.Rows.Add(tr);

        }



        private void AddSpannedCell(Table tb, TableRow tr, string text, int colspan)
        {
            bool newrow = (tr == null);

            if (newrow)
            {
                tr = new TableRow();
            }

            TableCell tc = null;

            tc = new TableCell();
            tc.Text = text;
            tc.ColumnSpan = colspan;
            tc.HorizontalAlign = HorizontalAlign.Left;
            tr.Cells.Add(tc);

            if (newrow)
            {
                tb.Rows.Add(tr);
            }

        }
        private void AddTitleRow(Table tb, params string[] columnData)
        {
            TableRow tr = null;
            TableCell tc = null;

            tr = new TableRow();

            foreach (string data in columnData)
            {
                tc = new TableCell();
                tc.Text = data;
                tc.HorizontalAlign = HorizontalAlign.Left;
                tc.Style.Add("font-weight", "bold");
                tr.Cells.Add(tc);
            }

            tc = new TableCell();
            tc.Text = "Actions";
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.Style.Add("font-weight", "bold");
            tr.Cells.Add(tc);

            tb.Rows.Add(tr);

        }
    }
}