Index: /home/bes/dev/jboss/trunk/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployer.java
===================================================================
--- /home/bes/dev/jboss/trunk/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployer.java	(revision 69352)
+++ /home/bes/dev/jboss/trunk/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployer.java	(working copy)
@@ -26,10 +26,17 @@
 import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Properties;
 
 import javax.management.Attribute;
+import javax.management.AttributeNotFoundException;
+import javax.management.InstanceNotFoundException;
+import javax.management.InvalidAttributeValueException;
+import javax.management.MBeanException;
+import javax.management.MBeanRegistrationException;
 import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
 import javax.management.Notification;
 import javax.management.NotificationListener;
 import javax.management.ObjectInstance;
@@ -34,9 +41,11 @@
 import javax.management.NotificationListener;
 import javax.management.ObjectInstance;
 import javax.management.ObjectName;
+import javax.management.ReflectionException;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import javax.security.jacc.PolicyContext;
+import javax.security.jacc.PolicyContextException;
 
 import org.apache.catalina.Lifecycle;
 import org.apache.catalina.connector.Connector;
@@ -57,6 +66,7 @@
 import org.jboss.web.deployers.AbstractWarDeployment;
 import org.jboss.web.tomcat.security.HttpServletRequestPolicyContextHandler;
 import org.jboss.web.tomcat.service.session.SessionIDGenerator;
+import org.jboss.xb.binding.JBossXBException;
 import org.jboss.xb.binding.Unmarshaller;
 import org.jboss.xb.binding.UnmarshallerFactory;
 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
@@ -161,6 +171,8 @@
    /** */
    private String[] filteredPackages;
 
+   private boolean runtimeLifecycleCoupled = false;
+   
    public TomcatDeployer()
    {
    }
@@ -337,6 +349,36 @@
    public void setDeploymentClass(Class deploymentClass)
    {
       this.deploymentClass = deploymentClass;
+   }   
+   
+   /**
+    * Gets whether this object should start/stop the JBoss Web runtime
+    * during execution of its own start/stop lifecycle callbacks.
+    * 
+    * @return <code>true</code> if a call to {@link #start()} should
+    *        trigger a call to {@link #startWebServer()} and a call to
+    *        {@link #stop()} should trigger a call to {@link #stopWebServer()};
+    *        <code>false</code> if the webserver runtime lifecycle will
+    *        be separately managed.  Default is <code>false</code>.
+    */
+   public boolean isRuntimeLifecycleCoupled()
+   {
+      return runtimeLifecycleCoupled;
+   }
+
+   /**
+    * Sets whether this object should start/stop the JBoss Web runtime
+    * during execution of its own start/stop lifecycle callbacks.
+    * 
+    * @param coupled <code>true</code> if a call to {@link #start()} should
+    *        trigger a call to {@link #startWebServer()} and a call to
+    *        {@link #stop()} should trigger a call to {@link #stopWebServer()};
+    *        <code>false</code> if the webserver runtime lifecycle will
+    *        be separately managed. Default is <code>false</code>.
+    */
+   public void setRuntimeLifecycleCoupled(boolean coupled)
+   {
+      runtimeLifecycleCoupled = coupled;
    }
 
    /**
@@ -346,7 +388,30 @@
       throws Exception
    {
       super.start();
+      
+      // Parse shared web.xml
+      Unmarshaller unmarshaller = factory.newUnmarshaller();
+      URL webXml = this.getClass().getClassLoader().getResource("web.xml");
+      if (webXml == null)
+      {
+         webXml = this.getClass().getClassLoader().getResource("conf/web.xml");;
+      }
+      if (webXml == null)
+         throw new IllegalStateException("Unable to find shared web.xml or conf/web.xml");
+      SchemaBinding schema = JBossXBBuilder.build(Web25MetaData.class);
+      Web25MetaData confWebMD = (Web25MetaData) unmarshaller.unmarshal(webXml.toString(), schema);
+      sharedMetaData = new JBoss50WebMetaData();
+      sharedMetaData.merge(null, confWebMD);
+      
+      if (isRuntimeLifecycleCoupled())
+         startWebServer();
+   }
 
+   /**
+    * Initializes and starts the webserver runtime. 
+    */
+   public void startWebServer() throws Exception
+   {
       // Load Catalina properties
       CatalinaProperties.getProperty("");
       
@@ -412,19 +477,19 @@
       PolicyContext.registerHandler(HttpServletRequestPolicyContextHandler.WEB_REQUEST_KEY,
          handler, true);
 
-      // Parse shared web.xml
-      Unmarshaller unmarshaller = factory.newUnmarshaller();
-      URL webXml = this.getClass().getClassLoader().getResource("web.xml");
-      if (webXml == null)
-      {
-         webXml = this.getClass().getClassLoader().getResource("conf/web.xml");;
-      }
-      if (webXml == null)
-         throw new IllegalStateException("Unable to find shared web.xml or conf/web.xml");
-      SchemaBinding schema = JBossXBBuilder.build(Web25MetaData.class);
-      Web25MetaData confWebMD = (Web25MetaData) unmarshaller.unmarshal(webXml.toString(), schema);
-      sharedMetaData = new JBoss50WebMetaData();
-      sharedMetaData.merge(null, confWebMD);
+//      // Parse shared web.xml
+//      Unmarshaller unmarshaller = factory.newUnmarshaller();
+//      URL webXml = this.getClass().getClassLoader().getResource("web.xml");
+//      if (webXml == null)
+//      {
+//         webXml = this.getClass().getClassLoader().getResource("conf/web.xml");;
+//      }
+//      if (webXml == null)
+//         throw new IllegalStateException("Unable to find shared web.xml or conf/web.xml");
+//      SchemaBinding schema = JBossXBBuilder.build(Web25MetaData.class);
+//      Web25MetaData confWebMD = (Web25MetaData) unmarshaller.unmarshal(webXml.toString(), schema);
+//      sharedMetaData = new JBoss50WebMetaData();
+//      sharedMetaData.merge(null, confWebMD);
 
       // If we are hot-deployed *after* the overall server is started
       // we'll never receive Server.START_NOTIFICATION_TYPE, so check
@@ -452,6 +517,17 @@
    public void stop()
       throws Exception
    {
+      if (isRuntimeLifecycleCoupled())
+         stopWebServer();
+      
+      super.stop();
+   }
+
+   /**
+    * Stops the webserver runtime.
+    */
+   public void stopWebServer() throws Exception
+   {
       MBeanServer server = super.getServer();
       String objectNameS = catalinaDomain + ":type=server";
       ObjectName objectName = new ObjectName(objectNameS);
@@ -492,7 +568,6 @@
          ObjectName name = oi.getObjectName();
          server2.unregisterMBean(name);
       }
-      super.stop();
    }
 
    /**
@@ -657,6 +732,13 @@
       if(vf != null)
         config.setXacmlPolicyURL(vf.toURL());
       
+      // Add a dependency on the webserver itself
+      List<String> depends = metaData.getDepends();
+      if (depends == null)
+         depends = new ArrayList<String>();
+      depends.add(TomcatDeployerMBean.OBJECT_NAME.getCanonicalName());
+      metaData.setDepends(depends);     
+      
       deployment.setServer(super.getServer());
       deployment.init(config);
 
@@ -665,9 +747,9 @@
    
    public void create() throws Exception
    {
-      MBeanServer server = MBeanServerLocator.locateJBoss();
-      if (server != null)
-         server.registerMBean(this, OBJECT_NAME);
+//      MBeanServer server = MBeanServerLocator.locateJBoss();
+//      if (server != null)
+//         server.registerMBean(this, OBJECT_NAME);
    }
 
    public void destroy() throws Exception
@@ -672,8 +754,8 @@
 
    public void destroy() throws Exception
    {
-      MBeanServer server = MBeanServerLocator.locateJBoss();
-      if (server != null)
-         server.unregisterMBean(OBJECT_NAME);
+//      MBeanServer server = MBeanServerLocator.locateJBoss();
+//      if (server != null)
+//         server.unregisterMBean(OBJECT_NAME);
    } 
 }
Index: /home/bes/dev/jboss/trunk/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployerWrapper.java
===================================================================
--- /home/bes/dev/jboss/trunk/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployerWrapper.java	(revision 0)
+++ /home/bes/dev/jboss/trunk/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployerWrapper.java	(revision 0)
@@ -0,0 +1,246 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.web.tomcat.service.deployers;
+
+import org.jboss.security.plugins.JaasSecurityManagerServiceMBean;
+import org.jboss.system.ServiceMBeanSupport;
+
+/**
+ * Temporary workaround to support controlling the lifecycle of the webserver 
+ * runtime portion of TomcatDeployer via a JMX service in the deploy directory.  
+ * We want it in deploy so dependencies on services in deploy can be properly 
+ * expressed. We want it as a JMX service so the ServiceBindingManager can 
+ * alter the connector ports.
+ * <p>
+ * A more long term solution involves:
+ * <ol>
+ * <li>separating out the JBossWeb runtime aspects from TomcatDeployer and 
+ * putting them in a separate class</li>
+ * <li>developing a ProfileService-based alternative to ServiceBindingManager</li>
+ * </ol>
+ * </p> 
+ *  
+ * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
+ * @version $Revision: 1.1 $
+ */
+public class TomcatDeployerWrapper 
+    extends ServiceMBeanSupport
+    implements TomcatDeployerWrapperMBean
+{
+   private TomcatDeployer tomcatDeployer;
+   
+   
+   // Dependency inject the TomcatDeployer pojo
+   
+   public TomcatDeployer getTomcatDeployer()
+   {
+      return tomcatDeployer;
+   }
+
+   public void setTomcatDeployer(TomcatDeployer tomcatDeployer)
+   {
+      this.tomcatDeployer = tomcatDeployer;
+   }
+   
+   // In our lifecycle, we invoke the webserver lifecycle-related operations
+   // in the TomcatDeployer
+
+   @Override
+   protected void startService() throws Exception
+   {
+      if (tomcatDeployer == null)
+         throw new IllegalStateException("Must set TomcatDeployer before starting");
+      tomcatDeployer.startWebServer();
+   }
+
+   @Override
+   protected void stopService() throws Exception
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.stopWebServer();
+   }
+   
+   // Expose the TomcatDeployer MBean interface
+
+   public String getConfigFile()
+   {
+      return tomcatDeployer == null ? null : tomcatDeployer.getConfigFile();
+   }
+
+   public String getContextMBeanCode()
+   {
+      return tomcatDeployer == null ? null : tomcatDeployer.getContextMBeanCode();
+   }
+
+   public String getDomain()
+   {
+      return tomcatDeployer == null ? null : tomcatDeployer.getDomain();
+   }
+
+   public String[] getFilteredPackages()
+   {
+      return tomcatDeployer == null ? null : tomcatDeployer.getFilteredPackages();
+   }
+
+   public String getManagerClass()
+   {
+      return tomcatDeployer == null ? null : tomcatDeployer.getManagerClass();
+   }
+
+   public String getSessionIdAlphabet()
+   {
+      return tomcatDeployer == null ? null : tomcatDeployer.getSessionIdAlphabet();
+   }
+
+   public String getSubjectAttributeName()
+   {
+      return tomcatDeployer == null ? null : tomcatDeployer.getSubjectAttributeName();
+   }
+
+   public boolean getUseJBossWebLoader()
+   {
+      return tomcatDeployer == null ? false : tomcatDeployer.getUseJBossWebLoader();
+   }
+
+   public boolean isAllowSelfPrivilegedWebApps()
+   {
+      return tomcatDeployer == null ? false : tomcatDeployer.isAllowSelfPrivilegedWebApps();
+   }
+
+   public void setAllowSelfPrivilegedWebApps(boolean flag)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setAllowSelfPrivilegedWebApps(flag);
+   }
+
+   public void setConfigFile(String configFile)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setConfigFile(configFile);
+   }
+
+   public void setContextMBeanCode(String className)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setContextMBeanCode(className);
+   }
+
+   public void setDomain(String domainName)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setDomain(domainName);
+   }
+
+   public void setFilteredPackages(String[] pkgs)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setFilteredPackages(pkgs);
+   }
+
+   public void setManagerClass(String managerClass)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setManagerClass(managerClass);
+   }
+
+   public void setSecurityManagerService(JaasSecurityManagerServiceMBean mgr)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setSecurityManagerService(mgr);
+   }
+
+   public void setSessionIdAlphabet(String sessionIdAlphabet)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setSessionIdAlphabet(sessionIdAlphabet);
+   }
+
+   public void setSubjectAttributeName(String name)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setSubjectAttributeName(name);
+   }
+
+   public void setUseJBossWebLoader(boolean flag)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setUseJBossWebLoader(flag);
+   }
+
+   public void startConnectors() throws Exception
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.startConnectors();
+   }
+
+   public void stopConnectors() throws Exception
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.stopConnectors();
+   }
+
+   public String getDefaultSecurityDomain()
+   {
+      return tomcatDeployer == null ? null : tomcatDeployer.getDefaultSecurityDomain();
+   }
+
+   public boolean getJava2ClassLoadingCompliance()
+   {
+      return tomcatDeployer == null ? false : tomcatDeployer.getJava2ClassLoadingCompliance();
+   }
+
+   public boolean getLenientEjbLink()
+   {
+      return tomcatDeployer == null ? false : tomcatDeployer.getLenientEjbLink();
+   }
+
+   public boolean getUnpackWars()
+   {
+      return tomcatDeployer == null ? false : tomcatDeployer.getUnpackWars();
+   }
+
+   public void setDefaultSecurityDomain(String defaultSecurityDomain)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setDefaultSecurityDomain(defaultSecurityDomain);
+   }
+
+   public void setJava2ClassLoadingCompliance(boolean flag)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setJava2ClassLoadingCompliance(flag);
+   }
+
+   public void setLenientEjbLink(boolean flag)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setLenientEjbLink(flag);
+   }
+
+   public void setUnpackWars(boolean flag)
+   {
+      if (tomcatDeployer != null)
+         tomcatDeployer.setUnpackWars(flag);
+   }
+
+}
Index: /home/bes/dev/jboss/trunk/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployerWrapperMBean.java
===================================================================
--- /home/bes/dev/jboss/trunk/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployerWrapperMBean.java	(revision 0)
+++ /home/bes/dev/jboss/trunk/tomcat/src/main/org/jboss/web/tomcat/service/deployers/TomcatDeployerWrapperMBean.java	(revision 0)
@@ -0,0 +1,36 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.web.tomcat.service.deployers;
+
+import org.jboss.system.ServiceMBean;
+
+/**
+ * StandardMBean interface for the TomcatDeployerWrapper.
+ * 
+ * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
+ * @version $Revision: 1.1 $
+ */
+public interface TomcatDeployerWrapperMBean extends TomcatDeployerMBean, ServiceMBean
+{
+   public void setTomcatDeployer(TomcatDeployer tomcatDeployer);
+}
Index: /home/bes/dev/jboss/trunk/tomcat/src/resources/jboss-web-service.xml
===================================================================
--- /home/bes/dev/jboss/trunk/tomcat/src/resources/jboss-web-service.xml	(revision 0)
+++ /home/bes/dev/jboss/trunk/tomcat/src/resources/jboss-web-service.xml	(revision 0)
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- $Id: jboss-service-all.xml 60489 2007-02-12 08:22:28Z scott.stark@jboss.org $ -->
+
+<server> 
+   <!-- 
+       JBoss Web Service
+       
+       This is a wrapper around the WarDeployer bean deployed in
+       deployers/jboss-web.deployer/war-deployers-beans.xml.  This wrapper
+       controls the lifecycle of the JBoss Web runtime exposed by the
+       WarDeployer bean.
+   -->   
+   <mbean code="org.jboss.web.tomcat.service.deployers.TomcatDeployerWrapper"
+      name="jboss.web:service=WebServer">
+      
+    <!-- Inject the TomcatDeployer -->
+    <attribute name="TomcatDeployer">
+       <inject bean="WarDeployer"/>
+    </attribute> 
+    
+    <!-- 
+       Do not configure other JMX attributes via this file.
+       Use the WarDeployer bean in deployers/jboss-web.deployer/war-deployers-beans.xml
+    -->
+      
+   </mbean>
+</server>
\ No newline at end of file
