KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > EmbeddedWebContainer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web;
25
26 import java.io.File JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
30
31 import org.apache.catalina.Connector;
32 import org.apache.catalina.ContainerListener;
33 import org.apache.catalina.Context;
34 import org.apache.catalina.Host;
35 import org.apache.catalina.Engine;
36 import org.apache.catalina.InstanceListener;
37 import org.apache.catalina.Lifecycle;
38 import org.apache.catalina.Realm;
39 import org.apache.catalina.core.StandardContext;
40 import org.apache.catalina.core.StandardEngine;
41 import org.apache.catalina.startup.Embedded;
42 import org.apache.catalina.startup.ContextConfig;
43 import org.apache.catalina.mbeans.MBeanUtils;
44 import org.apache.catalina.net.ServerSocketFactory;
45 import org.apache.catalina.logger.FileLogger;
46 import org.apache.tomcat.util.IntrospectionUtils;
47
48 import com.sun.enterprise.deployment.WebBundleDescriptor;
49 import com.sun.enterprise.web.connector.coyote.PECoyoteConnector;
50 import com.sun.web.server.WebContainerListener;
51 import com.sun.enterprise.server.ServerContext;
52 import com.sun.enterprise.server.pluggable.WebContainerFeatureFactory;
53 import com.sun.enterprise.config.serverbeans.ElementProperty;
54 import com.sun.enterprise.config.ConfigBean;
55
56 /**
57  * Represents an embedded Catalina web container within the Application Server.
58  *
59  * This class is intended for use in versions of the application server
60  * that *do not* have web-core as well as in versions that do. When used
61  * with a web container that is integrated via the NSAPI j2ee-plugin, the
62  * virtual server implementation that <code>createHost</code> returns
63  * is customized to "<code>com.sun.enterprise.web.NSAPIVirtualServer</code>".
64  */

65 public final class EmbeddedWebContainer extends Embedded {
66
67     /**
68      * The logger to use for logging ALL web container related messages.
69      */

70     private Logger JavaDoc _logger = null;
71
72     private WebContainerFeatureFactory webContainerFeatureFactory;
73
74     private WebContainer webContainer;
75
76     /*
77      * The value of the 'file' attribute of the log-service element
78      */

79     private String JavaDoc logServiceFile;
80
81  
82     // ------------------------------------------------------------ Constructor
83

84     public EmbeddedWebContainer(Logger JavaDoc webLogger,
85                                 ServerContext serverContext,
86                                 WebContainer webContainer,
87                                 String JavaDoc logServiceFile) {
88         super();
89         _logger = webLogger;
90         this.webContainer = webContainer;
91         this.logServiceFile = logServiceFile;
92         webContainerFeatureFactory =
93             serverContext.getPluggableFeatureFactory().getWebContainerFeatureFactory();
94     }
95     
96
97     // --------------------------------------------------------- Public Methods
98

99     /**
100      * Creates a virtual server.
101      *
102      * @param vsID Virtual server id
103      * @param vsBean Bean corresponding to virtual-server element in domain.xml
104      * @param vsDocRoot Virtual server docroot
105      * @param vsMimeMap Virtual server MIME mappings
106      *
107      * @return The generated virtual server instance
108      */

109     public Host createHost(
110                     String JavaDoc vsID,
111                     com.sun.enterprise.config.serverbeans.VirtualServer vsBean,
112                     String JavaDoc vsDocroot,
113                     String JavaDoc vsLogFile,
114                     MimeMap vsMimeMap) {
115
116         VirtualServer vs = webContainerFeatureFactory.getVirtualServer();
117         vs.setDebug(debug);
118         vs.setAppBase(vsDocroot);
119         vs.setName(vsID);
120         vs.setID(vsID);
121         vs.setBean(vsBean);
122         vs.setMimeMap(vsMimeMap);
123
124         String JavaDoc defaultWebXmlLocation = Constants.DEFAULT_WEB_XML;
125         boolean allowLinking = true;
126         String JavaDoc state = null;
127
128         if (vsBean != null) {
129
130             state = vsBean.getState();
131
132             ElementProperty prop = null;
133
134             //Begin EE: 4920692 Make the default-web.xml be relocatable
135
prop = vsBean.getElementPropertyByName("default-web-xml");
136             if (prop != null) {
137                 defaultWebXmlLocation = prop.getValue();
138             }
139             //End EE: 4920692 Make the default-web.xml be relocatable
140

141             // allowLinking
142
prop = vsBean.getElementPropertyByName("allowLinking");
143             if (prop != null) {
144                 allowLinking = ConfigBean.toBoolean(prop.getValue());
145             }
146         }
147
148         vs.setDefaultWebXmlLocation(defaultWebXmlLocation);
149
150         // Set vs state
151
if (state == null) {
152             state = WebContainer.ON;
153         }
154         if (WebContainer.DISABLED.equalsIgnoreCase(state)) {
155             vs.setIsActive(false);
156         } else {
157             vs.setIsActive(ConfigBean.toBoolean(state));
158         }
159         
160         vs.setAllowLinking(allowLinking);
161
162         if (vsLogFile != null && !vsLogFile.equals(logServiceFile)) {
163             /*
164              * Configure separate logger for this virtual server only if
165              * 'log-file' attribute of this <virtual-server> and 'file'
166              * attribute of <log-service> are different (See 6189219).
167              */

168             setLogFile(vs, vsLogFile);
169         }
170          
171         ContainerListener listener = loadListener
172                 ("com.sun.enterprise.web.connector.grizzly.CatalinaListener");
173         if ( listener != null )
174             vs.addContainerListener(listener);
175         
176         return vs;
177     }
178
179     
180     /**
181      * Create a web module/application.
182      *
183      * @param ctxPath Context path for the web module
184      * @param location Absolute pathname to the web module directory
185      * @param defaultWebXmlLocation Location of default-web.xml
186      * @param hasWebXml true if the context to be created has a deployment
187      * descriptor, false otherwise
188      */

189     public Context createContext(String JavaDoc ctxPath, String JavaDoc location,
190                                  String JavaDoc defaultWebXmlLocation,
191                                  boolean useDOLforDeployment,
192                                  WebBundleDescriptor wbd) {
193
194         boolean hasWebXml = (wbd == null ? false : true);
195
196         WebModule context = new WebModule(webContainer);
197         context.setDebug(debug);
198         context.setPath(ctxPath);
199         context.setDocBase(location);
200         context.setCrossContext(true);
201         context.setUseNaming(isUseNaming());
202         context.setHasWebXml(hasWebXml);
203         context.setManagerChecksFrequency(1);
204             
205         ContextConfig config;
206         WebModule defaultWebModule;
207         if (useDOLforDeployment) {
208             config = new WebModuleContextConfig();
209             ((WebModuleContextConfig)config).setDescriptor(wbd);
210         } else {
211             config = new ContextConfig();
212         }
213         
214         config.setDefaultWebXml(defaultWebXmlLocation);
215         ((Lifecycle) context).addLifecycleListener(config);
216
217         context.addLifecycleListener(new WebModuleListener(
218                 webContainer.getInstanceClassPath(), location, wbd));
219
220         context.addInstanceListener(Constants.J2EE_INSTANCE_LISTENER);
221         
222         context.addContainerListener(new WebContainerListener());
223
224         context.addInstanceListener(
225             "com.sun.enterprise.admin.monitor.callflow.WebContainerListener");
226         
227         return context;
228     }
229
230          
231     /**
232      * Util method to load classes that might get compiled after this class is
233      * compiled.
234      */

235     private ContainerListener loadListener(String JavaDoc className){
236         try{
237             Class JavaDoc clazz = Class.forName(className);
238             return (ContainerListener)clazz.newInstance();
239         } catch (Throwable JavaDoc ex){
240             _logger.log(Level.SEVERE,ex.getMessage() + ":" + className, ex);
241         }
242         return null;
243     }
244     
245    
246     /**
247      * Return the list of engines created (from Embedded API)
248      */

249     public Engine[] getEngines() {
250         return engines;
251     }
252
253     /**
254      * Returns the list of Connector objects associated with this
255      * EmbeddedWebContainer.
256      *
257      * @return The list of Connector objects associated with this
258      * EmbeddedWebContainer
259      */

260     public Connector[] getConnectors() {
261         return connectors;
262     }
263
264     /*
265      * Configures the given virtual server with the specified log file.
266      *
267      * @param vs The virtual server
268      * @param logFile The value of the virtual server's log-file attribute in
269      * the domain.xml
270      */

271     protected void setLogFile(Host vs, String JavaDoc logFile) {
272
273         String JavaDoc logPrefix = logFile;
274         String JavaDoc logDir = null;
275         String JavaDoc logSuffix = null;
276
277         if (logPrefix == null || logPrefix.equals("")) {
278             return;
279         }
280
281         int index = logPrefix.lastIndexOf(File.separatorChar);
282         if (index != -1) {
283             logDir = logPrefix.substring(0, index);
284             logPrefix = logPrefix.substring(index+1);
285         }
286         
287         index = logPrefix.indexOf('.');
288         if (index != -1) {
289             logSuffix = logPrefix.substring(index);
290             logPrefix = logPrefix.substring(0, index);
291         }
292
293         logPrefix += "_";
294
295         FileLogger contextLogger = new FileLogger();
296         if (logDir != null) {
297             contextLogger.setDirectory(logDir);
298         }
299         contextLogger.setPrefix(logPrefix);
300         if (logSuffix != null) {
301             contextLogger.setSuffix(logSuffix);
302         }
303         contextLogger.setTimestamp(true);
304
305         vs.setLogger(contextLogger);
306     }
307
308
309     /**
310      * Create a customized version of the Tomcat's 5 Coyote Connector. This
311      * connector is required in order to support PE Web Programmatic login
312      * functionality.
313      * @param address InetAddress to bind to, or <code>null</code> if the
314      * connector is supposed to bind to all addresses on this server
315      * @param port Port number to listen to
316      * @param protocol the http protocol to use.
317      */

318     public Connector createConnector(String JavaDoc address, int port,
319                      String JavaDoc protocol) {
320
321         if (address != null) {
322             /*
323              * InetAddress.toString() returns a string of the form
324              * "<hostname>/<literal_IP>". Get the latter part, so that the
325              * address can be parsed (back) into an InetAddress using
326              * InetAddress.getByName().
327              */

328             int index = address.indexOf('/');
329             if (index != -1) {
330                 address = address.substring(index + 1);
331             }
332         }
333
334         _logger.log(Level.FINE,"Creating connector for address='" +
335                   ((address == null) ? "ALL" : address) +
336                   "' port='" + port + "' protocol='" + protocol + "'");
337
338         PECoyoteConnector connector = new PECoyoteConnector();
339
340         if (address != null) {
341             connector.setAddress(address);
342         }
343
344         connector.setPort(port);
345
346         if (protocol.equals("ajp")) {
347             connector.setProtocolHandlerClassName(
348                  "org.apache.jk.server.JkCoyoteHandler");
349         } else if (protocol.equals("memory")) {
350             connector.setProtocolHandlerClassName(
351                  "org.apache.coyote.memory.MemoryProtocolHandler");
352         } else if (protocol.equals("https")) {
353             connector.setScheme("https");
354             connector.setSecure(true);
355         }
356
357         return (connector);
358
359     }
360     
361
362     /**
363      * Create, configure, and return an Engine that will process all
364      * HTTP requests received from one of the associated Connectors,
365      * based on the specified properties.
366      *
367      * Do not create the JAAS default realm since all children will
368      * have their own.
369      */

370     public Engine createEngine() {
371
372         StandardEngine engine = new StandardEngine(){
373             public Realm getRealm(){
374                 return null;
375             }
376         };
377
378         engine.setDebug(debug);
379         // Default host will be set to the first host added
380
engine.setLogger(logger); // Inherited by all children
381
engine.setRealm(null); // Inherited by all children
382

383         ContainerListener listener = loadListener
384             ("com.sun.enterprise.admin.monitor.callflow.WebContainerListener");
385         if ( listener != null ) {
386             engine.addContainerListener(listener);
387         }
388         return (engine);
389
390     }
391 }
392
Popular Tags