Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/geronimo/tomcat/TomcatContainer.java


1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.tomcat;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.apache.catalina.Container;
23  import org.apache.catalina.Context;
24  import org.apache.catalina.Engine;
25  import org.apache.catalina.connector.Connector;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.geronimo.gbean.GBeanInfo;
29  import org.apache.geronimo.gbean.GBeanInfoBuilder;
30  import org.apache.geronimo.gbean.GBeanLifecycle;
31  import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
32  import org.apache.geronimo.system.serverinfo.ServerInfo;
33  
34  /**
35   * Apache Tomcat GBean
36   * 
37   * @see http://wiki.apache.org/geronimo/Tomcat
38   * @see http://nagoya.apache.org/jira/browse/GERONIMO-215
39   * 
40   * @version $Rev: 46019 $ $Date: 2004-09-14 11:56:06 +0200 (Tue, 14 Sep 2004) $
41   */
42  public class TomcatContainer implements GBeanLifecycle {
43  
44      private static final Log log = LogFactory.getLog(TomcatContainer.class);
45  
46      /**
47       * The default value of CATALINA_HOME variable
48       */
49      private static final String DEFAULT_CATALINA_HOME = "var/catalina";
50  
51      /**
52       * Reference to the org.apache.catalina.Embedded embedded.
53       */
54      private TomcatGeronimoEmbedded embedded;
55  
56      /**
57       * Tomcat Engine that will contain the host
58       */
59      private Engine engine;
60  
61      /**
62       * Tomcat default Context
63       * 
64       * TODO: Make it a gbean
65       */
66      private Context defaultContext;
67  
68      /**
69       * Geronimo class loader
70       **/
71      private ClassLoader classLoader;
72  
73      /**
74       * Used only to resolve the paths
75       */
76      private ServerInfo serverInfo;
77  
78      // Required as it's referenced by deployed webapps
79      public TomcatContainer() {
80          setCatalinaHome(DEFAULT_CATALINA_HOME);
81      }
82  
83      /**
84       * GBean constructor (invoked dynamically when the gbean is declared in a plan)
85       */
86      public TomcatContainer(ClassLoader classLoader, String catalinaHome, ObjectRetriever engineGBean, ServerInfo serverInfo) {
87          setCatalinaHome(catalinaHome);
88  
89          if (classLoader == null){
90              throw new IllegalArgumentException("classLoader cannot be null.");
91          }
92  
93          if (engineGBean == null){
94              throw new IllegalArgumentException("engineGBean cannot be null.");
95          }
96  
97          this.classLoader = classLoader;
98          
99          this.engine = (Engine)engineGBean.getInternalObject();
100         this.serverInfo = serverInfo;
101     }
102 
103     public void doFail() {
104         try {
105             doStop();
106         } catch (Exception ignored) {
107         }
108     }
109 
110     /**
111      * Instantiate and start up Tomcat's Embedded class
112      * 
113      * See org.apache.catalina.startup.Embedded for details (TODO: provide the link to the javadoc)
114      */
115     public void doStart() throws Exception {
116         log.debug("doStart()");
117 
118         log.info("Endorsed Dirs set to:" + System.getProperty("java.endorsed.dirs"));
119  
120         // The comments are from the javadoc of the Embedded class
121 
122         // 1. Instantiate a new instance of this class.
123         if (embedded == null) {
124             embedded = new TomcatGeronimoEmbedded();
125         }
126 
127         // Assemble FileLogger as a gbean
128         /*
129          * FileLogger fileLog = new FileLogger(); fileLog.setDirectory("."); fileLog.setPrefix("vsjMbedTC5");
130          * fileLog.setSuffix(".log"); fileLog.setTimestamp(true);
131          */
132 
133         // 2. Set the relevant properties of this object itself. In particular,
134         // you will want to establish the default Logger to be used, as well as
135         // the default Realm if you are using container-managed security.
136         embedded.setUseNaming(false);
137 
138         //Add default contexts
139         Container[] hosts = engine.findChildren();
140         for(int i = 0; i < hosts.length; i++){
141             Context defaultContext = embedded.createContext("","");
142             defaultContext.setParentClassLoader(classLoader);
143             hosts[i].addChild(defaultContext);
144         }
145         
146         // 6. Call addEngine() to attach this Engine to the set of defined
147         // Engines for this object.
148         embedded.addEngine(engine);
149 
150         // 9. Call start() to initiate normal operations of all the attached
151         // components.
152         embedded.start();
153     }
154 
155     public void doStop() throws Exception {
156         if (embedded != null) {
157             embedded.stop();
158             embedded = null;
159         }
160     }
161 
162     /**
163      * Creates and adds the context to the running host
164      * 
165      * It simply delegates the call to Tomcat's Embedded and Host classes
166      * 
167      * @param ctx the context to be added
168      * 
169      * @see org.apache.catalina.startup.Embedded
170      * @see org.apache.catalina.Host
171      */
172     public void addContext(TomcatContext ctx) throws Exception{
173         Context anotherCtxObj = embedded.createContext(ctx.getPath(), ctx.getDocBase());
174         anotherCtxObj.setParentClassLoader(ctx.getWebClassLoader());
175         //anotherCtxObj.setParentClassLoader(this.getClass().getClassLoader());
176 
177         // Set the context for the Tomcat implementation
178         ctx.setContext(anotherCtxObj);
179         
180         // Have the context to set its properties if its a GeronimoStandardContext
181         if (anotherCtxObj instanceof GeronimoStandardContext) 
182             ((GeronimoStandardContext)anotherCtxObj).setContextProperties(ctx);
183 
184         //Was a virtual server defined?
185         String virtualServer = ctx.getVirtualServer();
186         if (virtualServer == null)
187             virtualServer = engine.getDefaultHost();
188         
189         Container host = engine.findChild(virtualServer);
190         if (host == null){
191             throw new IllegalArgumentException("Invalid virtual host '" + virtualServer +"'.  Do you have a matchiing Host entry in the plan?");
192         }
193         
194         if (ctx.getRealm() != null)
195             anotherCtxObj.setRealm(ctx.getRealm());
196         else
197             anotherCtxObj.setRealm(host.getRealm());
198             
199 
200         host.addChild(anotherCtxObj);
201     }
202 
203     public void removeContext(TomcatContext ctx) {
204         Context context = ctx.getContext();
205 
206         if (context != null)
207             embedded.removeContext(context);
208 
209     }
210     
211     public void setCatalinaHome(String catalinaHome) {
212         System.setProperty("catalina.home", catalinaHome);
213     }
214 
215     public void addConnector(Connector connector) {
216         embedded.addConnector(connector);
217     }
218 
219     public void removeConnector(Connector connector) {
220         embedded.removeConnector(connector);
221     }
222 
223     public static final GBeanInfo GBEAN_INFO;
224 
225     static {
226         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("Tomcat Web Container", TomcatContainer.class);
227 
228         infoFactory.setConstructor(new String[] { "classLoader", "catalinaHome", "engineGBean", "ServerInfo" });
229 
230         infoFactory.addAttribute("classLoader", ClassLoader.class, false);
231 
232         infoFactory.addAttribute("catalinaHome", String.class, true);
233 
234         infoFactory.addReference("engineGBean", ObjectRetriever.class, NameFactory.GERONIMO_SERVICE);
235 
236         infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
237 
238         infoFactory.addOperation("addContext", new Class[] { TomcatContext.class });
239         infoFactory.addOperation("removeContext", new Class[] { TomcatContext.class });
240 
241         infoFactory.addOperation("addConnector", new Class[] { Connector.class });
242         infoFactory.addOperation("removeConnector", new Class[] { Connector.class });
243 
244         GBEAN_INFO = infoFactory.getBeanInfo();
245     }
246 
247     public static GBeanInfo getGBeanInfo() {
248         return GBEAN_INFO;
249     }
250 }