Source Code Cross Referenced for Embedded.java in  » Sevlet-Container » apache-tomcat-6.0.14 » org » apache » catalina » startup » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Sevlet Container » apache tomcat 6.0.14 » org.apache.catalina.startup 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.catalina.startup;
019:
020:        import java.io.File;
021:        import java.io.IOException;
022:        import java.net.InetAddress;
023:        import java.util.HashMap;
024:
025:        import org.apache.catalina.Authenticator;
026:        import org.apache.catalina.Container;
027:        import org.apache.catalina.Context;
028:        import org.apache.catalina.Engine;
029:        import org.apache.catalina.Host;
030:        import org.apache.catalina.Lifecycle;
031:        import org.apache.catalina.LifecycleException;
032:        import org.apache.catalina.LifecycleListener;
033:        import org.apache.catalina.Loader;
034:        import org.apache.catalina.Realm;
035:        import org.apache.catalina.Valve;
036:        import org.apache.catalina.connector.Connector;
037:        import org.apache.catalina.core.StandardContext;
038:        import org.apache.catalina.core.StandardEngine;
039:        import org.apache.catalina.core.StandardHost;
040:        import org.apache.catalina.core.StandardService;
041:        import org.apache.catalina.loader.WebappLoader;
042:        import org.apache.catalina.security.SecurityConfig;
043:        import org.apache.catalina.util.LifecycleSupport;
044:        import org.apache.catalina.util.StringManager;
045:        import org.apache.juli.logging.Log;
046:        import org.apache.juli.logging.LogFactory;
047:        import org.apache.tomcat.util.IntrospectionUtils;
048:        import org.apache.tomcat.util.log.SystemLogHandler;
049:
050:        /**
051:         * Convenience class to embed a Catalina servlet container environment
052:         * inside another application.  You must call the methods of this class in the
053:         * following order to ensure correct operation.
054:         *
055:         * <ul>
056:         * <li>Instantiate a new instance of this class.</li>
057:         * <li>Set the relevant properties of this object itself.  In particular,
058:         *     you will want to establish the default Logger to be used, as well
059:         *     as the default Realm if you are using container-managed security.</li>
060:         * <li>Call <code>createEngine()</code> to create an Engine object, and then
061:         *     call its property setters as desired.</li>
062:         * <li>Call <code>createHost()</code> to create at least one virtual Host
063:         *     associated with the newly created Engine, and then call its property
064:         *     setters as desired.  After you customize this Host, add it to the
065:         *     corresponding Engine with <code>engine.addChild(host)</code>.</li>
066:         * <li>Call <code>createContext()</code> to create at least one Context
067:         *     associated with each newly created Host, and then call its property
068:         *     setters as desired.  You <strong>SHOULD</strong> create a Context with
069:         *     a pathname equal to a zero-length string, which will be used to process
070:         *     all requests not mapped to some other Context.  After you customize
071:         *     this Context, add it to the corresponding Host with
072:         *     <code>host.addChild(context)</code>.</li>
073:         * <li>Call <code>addEngine()</code> to attach this Engine to the set of
074:         *     defined Engines for this object.</li>
075:         * <li>Call <code>createConnector()</code> to create at least one TCP/IP
076:         *     connector, and then call its property setters as desired.</li>
077:         * <li>Call <code>addConnector()</code> to attach this Connector to the set
078:         *     of defined Connectors for this object.  The added Connector will use
079:         *     the most recently added Engine to process its received requests.</li>
080:         * <li>Repeat the above series of steps as often as required (although there
081:         *     will typically be only one Engine instance created).</li>
082:         * <li>Call <code>start()</code> to initiate normal operations of all the
083:         *     attached components.</li>
084:         * </ul>
085:         *
086:         * After normal operations have begun, you can add and remove Connectors,
087:         * Engines, Hosts, and Contexts on the fly.  However, once you have removed
088:         * a particular component, it must be thrown away -- you can create a new one
089:         * with the same characteristics if you merely want to do a restart.
090:         * <p>
091:         * To initiate a normal shutdown, call the <code>stop()</code> method of
092:         * this object.
093:         * <p>
094:         * @see org.apache.catalina.startup.Catalina#main For a complete example
095:         * of how Tomcat is set up and launched as an Embedded application.
096:         *
097:         * @author Craig R. McClanahan
098:         * @version $Revision: 534913 $ $Date: 2007-05-03 17:49:09 +0200 (jeu., 03 mai 2007) $
099:         */
100:
101:        public class Embedded extends StandardService implements  Lifecycle {
102:            private static Log log = LogFactory.getLog(Embedded.class);
103:
104:            // ----------------------------------------------------------- Constructors
105:
106:            /**
107:             * Construct a new instance of this class with default properties.
108:             */
109:            public Embedded() {
110:
111:                this (null);
112:
113:            }
114:
115:            /**
116:             * Construct a new instance of this class with specified properties.
117:             *
118:             * @param realm Realm implementation to be inherited by all components
119:             *  (unless overridden further down the container hierarchy)
120:             */
121:            public Embedded(Realm realm) {
122:
123:                super ();
124:                setRealm(realm);
125:                setSecurityProtection();
126:
127:            }
128:
129:            // ----------------------------------------------------- Instance Variables
130:
131:            /**
132:             * Is naming enabled ?
133:             */
134:            protected boolean useNaming = true;
135:
136:            /**
137:             * Is standard streams redirection enabled ?
138:             */
139:            protected boolean redirectStreams = true;
140:
141:            /**
142:             * The set of Engines that have been deployed in this server.  Normally
143:             * there will only be one.
144:             */
145:            protected Engine engines[] = new Engine[0];
146:
147:            /**
148:             * Custom mappings of login methods to authenticators
149:             */
150:            protected HashMap authenticators;
151:
152:            /**
153:             * Descriptive information about this server implementation.
154:             */
155:            protected static final String info = "org.apache.catalina.startup.Embedded/1.0";
156:
157:            /**
158:             * The lifecycle event support for this component.
159:             */
160:            protected LifecycleSupport lifecycle = new LifecycleSupport(this );
161:
162:            /**
163:             * The default realm to be used by all containers associated with
164:             * this compoennt.
165:             */
166:            protected Realm realm = null;
167:
168:            /**
169:             * The string manager for this package.
170:             */
171:            protected static StringManager sm = StringManager
172:                    .getManager(Constants.Package);
173:
174:            /**
175:             * Has this component been started yet?
176:             */
177:            protected boolean started = false;
178:
179:            /**
180:             * Use await.
181:             */
182:            protected boolean await = false;
183:
184:            // ------------------------------------------------------------- Properties
185:
186:            /**
187:             * Return true if naming is enabled.
188:             */
189:            public boolean isUseNaming() {
190:
191:                return (this .useNaming);
192:
193:            }
194:
195:            /**
196:             * Enables or disables naming support.
197:             *
198:             * @param useNaming The new use naming value
199:             */
200:            public void setUseNaming(boolean useNaming) {
201:
202:                boolean oldUseNaming = this .useNaming;
203:                this .useNaming = useNaming;
204:                support.firePropertyChange("useNaming", new Boolean(
205:                        oldUseNaming), new Boolean(this .useNaming));
206:
207:            }
208:
209:            /**
210:             * Return true if redirction of standard streams is enabled.
211:             */
212:            public boolean isRedirectStreams() {
213:
214:                return (this .redirectStreams);
215:
216:            }
217:
218:            /**
219:             * Enables or disables naming support.
220:             *
221:             * @param useNaming The new use naming value
222:             */
223:            public void setRedirectStreams(boolean redirectStreams) {
224:
225:                boolean oldRedirectStreams = this .redirectStreams;
226:                this .redirectStreams = redirectStreams;
227:                support.firePropertyChange("redirectStreams", new Boolean(
228:                        oldRedirectStreams), new Boolean(this .redirectStreams));
229:
230:            }
231:
232:            /**
233:             * Return the default Realm for our Containers.
234:             */
235:            public Realm getRealm() {
236:
237:                return (this .realm);
238:
239:            }
240:
241:            /**
242:             * Set the default Realm for our Containers.
243:             *
244:             * @param realm The new default realm
245:             */
246:            public void setRealm(Realm realm) {
247:
248:                Realm oldRealm = this .realm;
249:                this .realm = realm;
250:                support.firePropertyChange("realm", oldRealm, this .realm);
251:
252:            }
253:
254:            public void setAwait(boolean b) {
255:                await = b;
256:            }
257:
258:            public boolean isAwait() {
259:                return await;
260:            }
261:
262:            public void setCatalinaHome(String s) {
263:                System.setProperty("catalina.home", s);
264:            }
265:
266:            public void setCatalinaBase(String s) {
267:                System.setProperty("catalina.base", s);
268:            }
269:
270:            public String getCatalinaHome() {
271:                return System.getProperty("catalina.home");
272:            }
273:
274:            public String getCatalinaBase() {
275:                return System.getProperty("catalina.base");
276:            }
277:
278:            // --------------------------------------------------------- Public Methods
279:
280:            /**
281:             * Add a new Connector to the set of defined Connectors.  The newly
282:             * added Connector will be associated with the most recently added Engine.
283:             *
284:             * @param connector The connector to be added
285:             *
286:             * @exception IllegalStateException if no engines have been added yet
287:             */
288:            public synchronized void addConnector(Connector connector) {
289:
290:                if (log.isDebugEnabled()) {
291:                    log.debug("Adding connector (" + connector.getInfo() + ")");
292:                }
293:
294:                // Make sure we have a Container to send requests to
295:                if (engines.length < 1)
296:                    throw new IllegalStateException(sm
297:                            .getString("embedded.noEngines"));
298:
299:                /*
300:                 * Add the connector. This will set the connector's container to the
301:                 * most recently added Engine
302:                 */
303:                super .addConnector(connector);
304:            }
305:
306:            /**
307:             * Add a new Engine to the set of defined Engines.
308:             *
309:             * @param engine The engine to be added
310:             */
311:            public synchronized void addEngine(Engine engine) {
312:
313:                if (log.isDebugEnabled())
314:                    log.debug("Adding engine (" + engine.getInfo() + ")");
315:
316:                // Add this Engine to our set of defined Engines
317:                Engine results[] = new Engine[engines.length + 1];
318:                for (int i = 0; i < engines.length; i++)
319:                    results[i] = engines[i];
320:                results[engines.length] = engine;
321:                engines = results;
322:
323:                // Start this Engine if necessary
324:                if (started && (engine instanceof  Lifecycle)) {
325:                    try {
326:                        ((Lifecycle) engine).start();
327:                    } catch (LifecycleException e) {
328:                        log.error("Engine.start", e);
329:                    }
330:                }
331:
332:                this .container = engine;
333:            }
334:
335:            /**
336:             * Create, configure, and return a new TCP/IP socket connector
337:             * based on the specified properties.
338:             *
339:             * @param address InetAddress to bind to, or <code>null</code> if the
340:             * connector is supposed to bind to all addresses on this server
341:             * @param port Port number to listen to
342:             * @param secure true if the generated connector is supposed to be
343:             * SSL-enabled, and false otherwise
344:             */
345:            public Connector createConnector(InetAddress address, int port,
346:                    boolean secure) {
347:                return createConnector(address != null ? address.toString()
348:                        : null, port, secure);
349:            }
350:
351:            public Connector createConnector(String address, int port,
352:                    boolean secure) {
353:                String protocol = "http";
354:                if (secure) {
355:                    protocol = "https";
356:                }
357:
358:                return createConnector(address, port, protocol);
359:            }
360:
361:            public Connector createConnector(InetAddress address, int port,
362:                    String protocol) {
363:                return createConnector(address != null ? address.toString()
364:                        : null, port, protocol);
365:            }
366:
367:            public Connector createConnector(String address, int port,
368:                    String protocol) {
369:
370:                Connector connector = null;
371:
372:                if (address != null) {
373:                    /*
374:                     * InetAddress.toString() returns a string of the form
375:                     * "<hostname>/<literal_IP>". Get the latter part, so that the
376:                     * address can be parsed (back) into an InetAddress using
377:                     * InetAddress.getByName().
378:                     */
379:                    int index = address.indexOf('/');
380:                    if (index != -1) {
381:                        address = address.substring(index + 1);
382:                    }
383:                }
384:
385:                if (log.isDebugEnabled()) {
386:                    log.debug("Creating connector for address='"
387:                            + ((address == null) ? "ALL" : address)
388:                            + "' port='" + port + "' protocol='" + protocol
389:                            + "'");
390:                }
391:
392:                try {
393:
394:                    if (protocol.equals("ajp")) {
395:                        connector = new Connector(
396:                                "org.apache.jk.server.JkCoyoteHandler");
397:                    } else if (protocol.equals("memory")) {
398:                        connector = new Connector(
399:                                "org.apache.coyote.memory.MemoryProtocolHandler");
400:                    } else if (protocol.equals("http")) {
401:                        connector = new Connector();
402:                    } else if (protocol.equals("https")) {
403:                        connector = new Connector();
404:                        connector.setScheme("https");
405:                        connector.setSecure(true);
406:                        connector.setProperty("SSLEnabled", "true");
407:                        // FIXME !!!! SET SSL PROPERTIES
408:                    } else {
409:                        connector = new Connector(protocol);
410:                    }
411:
412:                    if (address != null) {
413:                        IntrospectionUtils.setProperty(connector, "address", ""
414:                                + address);
415:                    }
416:                    IntrospectionUtils
417:                            .setProperty(connector, "port", "" + port);
418:
419:                } catch (Exception e) {
420:                    log.error("Couldn't create connector.");
421:                }
422:
423:                return (connector);
424:
425:            }
426:
427:            /**
428:             * Create, configure, and return a Context that will process all
429:             * HTTP requests received from one of the associated Connectors,
430:             * and directed to the specified context path on the virtual host
431:             * to which this Context is connected.
432:             * <p>
433:             * After you have customized the properties, listeners, and Valves
434:             * for this Context, you must attach it to the corresponding Host
435:             * by calling:
436:             * <pre>
437:             *   host.addChild(context);
438:             * </pre>
439:             * which will also cause the Context to be started if the Host has
440:             * already been started.
441:             *
442:             * @param path Context path of this application ("" for the default
443:             *  application for this host, must start with a slash otherwise)
444:             * @param docBase Absolute pathname to the document base directory
445:             *  for this web application
446:             *
447:             * @exception IllegalArgumentException if an invalid parameter
448:             *  is specified
449:             */
450:            public Context createContext(String path, String docBase) {
451:
452:                if (log.isDebugEnabled())
453:                    log.debug("Creating context '" + path + "' with docBase '"
454:                            + docBase + "'");
455:
456:                StandardContext context = new StandardContext();
457:
458:                context.setDocBase(docBase);
459:                context.setPath(path);
460:
461:                ContextConfig config = new ContextConfig();
462:                config.setCustomAuthenticators(authenticators);
463:                ((Lifecycle) context).addLifecycleListener(config);
464:
465:                return (context);
466:
467:            }
468:
469:            /**
470:             * Create, configure, and return an Engine that will process all
471:             * HTTP requests received from one of the associated Connectors,
472:             * based on the specified properties.
473:             */
474:            public Engine createEngine() {
475:
476:                if (log.isDebugEnabled())
477:                    log.debug("Creating engine");
478:
479:                StandardEngine engine = new StandardEngine();
480:
481:                // Default host will be set to the first host added
482:                engine.setRealm(realm); // Inherited by all children
483:
484:                return (engine);
485:
486:            }
487:
488:            /**
489:             * Create, configure, and return a Host that will process all
490:             * HTTP requests received from one of the associated Connectors,
491:             * and directed to the specified virtual host.
492:             * <p>
493:             * After you have customized the properties, listeners, and Valves
494:             * for this Host, you must attach it to the corresponding Engine
495:             * by calling:
496:             * <pre>
497:             *   engine.addChild(host);
498:             * </pre>
499:             * which will also cause the Host to be started if the Engine has
500:             * already been started.  If this is the default (or only) Host you
501:             * will be defining, you may also tell the Engine to pass all requests
502:             * not assigned to another virtual host to this one:
503:             * <pre>
504:             *   engine.setDefaultHost(host.getName());
505:             * </pre>
506:             *
507:             * @param name Canonical name of this virtual host
508:             * @param appBase Absolute pathname to the application base directory
509:             *  for this virtual host
510:             *
511:             * @exception IllegalArgumentException if an invalid parameter
512:             *  is specified
513:             */
514:            public Host createHost(String name, String appBase) {
515:
516:                if (log.isDebugEnabled())
517:                    log.debug("Creating host '" + name + "' with appBase '"
518:                            + appBase + "'");
519:
520:                StandardHost host = new StandardHost();
521:
522:                host.setAppBase(appBase);
523:                host.setName(name);
524:
525:                return (host);
526:
527:            }
528:
529:            /**
530:             * Create and return a class loader manager that can be customized, and
531:             * then attached to a Context, before it is started.
532:             *
533:             * @param parent ClassLoader that will be the parent of the one
534:             *  created by this Loader
535:             */
536:            public Loader createLoader(ClassLoader parent) {
537:
538:                if (log.isDebugEnabled())
539:                    log.debug("Creating Loader with parent class loader '"
540:                            + parent + "'");
541:
542:                WebappLoader loader = new WebappLoader(parent);
543:                return (loader);
544:
545:            }
546:
547:            /**
548:             * Return descriptive information about this Server implementation and
549:             * the corresponding version number, in the format
550:             * <code>&lt;description&gt;/&lt;version&gt;</code>.
551:             */
552:            public String getInfo() {
553:
554:                return (info);
555:
556:            }
557:
558:            /**
559:             * Remove the specified Context from the set of defined Contexts for its
560:             * associated Host.  If this is the last Context for this Host, the Host
561:             * will also be removed.
562:             *
563:             * @param context The Context to be removed
564:             */
565:            public synchronized void removeContext(Context context) {
566:
567:                if (log.isDebugEnabled())
568:                    log.debug("Removing context[" + context.getPath() + "]");
569:
570:                // Is this Context actually among those that are defined?
571:                boolean found = false;
572:                for (int i = 0; i < engines.length; i++) {
573:                    Container hosts[] = engines[i].findChildren();
574:                    for (int j = 0; j < hosts.length; j++) {
575:                        Container contexts[] = hosts[j].findChildren();
576:                        for (int k = 0; k < contexts.length; k++) {
577:                            if (context == (Context) contexts[k]) {
578:                                found = true;
579:                                break;
580:                            }
581:                        }
582:                        if (found)
583:                            break;
584:                    }
585:                    if (found)
586:                        break;
587:                }
588:                if (!found)
589:                    return;
590:
591:                // Remove this Context from the associated Host
592:                if (log.isDebugEnabled())
593:                    log.debug(" Removing this Context");
594:                context.getParent().removeChild(context);
595:
596:            }
597:
598:            /**
599:             * Remove the specified Engine from the set of defined Engines, along with
600:             * all of its related Hosts and Contexts.  All associated Connectors are
601:             * also removed.
602:             *
603:             * @param engine The Engine to be removed
604:             */
605:            public synchronized void removeEngine(Engine engine) {
606:
607:                if (log.isDebugEnabled())
608:                    log.debug("Removing engine (" + engine.getInfo() + ")");
609:
610:                // Is the specified Engine actually defined?
611:                int j = -1;
612:                for (int i = 0; i < engines.length; i++) {
613:                    if (engine == engines[i]) {
614:                        j = i;
615:                        break;
616:                    }
617:                }
618:                if (j < 0)
619:                    return;
620:
621:                // Remove any Connector that is using this Engine
622:                if (log.isDebugEnabled())
623:                    log.debug(" Removing related Containers");
624:                while (true) {
625:                    int n = -1;
626:                    for (int i = 0; i < connectors.length; i++) {
627:                        if (connectors[i].getContainer() == (Container) engine) {
628:                            n = i;
629:                            break;
630:                        }
631:                    }
632:                    if (n < 0)
633:                        break;
634:                    removeConnector(connectors[n]);
635:                }
636:
637:                // Stop this Engine if necessary
638:                if (engine instanceof  Lifecycle) {
639:                    if (log.isDebugEnabled())
640:                        log.debug(" Stopping this Engine");
641:                    try {
642:                        ((Lifecycle) engine).stop();
643:                    } catch (LifecycleException e) {
644:                        log.error("Engine.stop", e);
645:                    }
646:                }
647:
648:                // Remove this Engine from our set of defined Engines
649:                if (log.isDebugEnabled())
650:                    log.debug(" Removing this Engine");
651:                int k = 0;
652:                Engine results[] = new Engine[engines.length - 1];
653:                for (int i = 0; i < engines.length; i++) {
654:                    if (i != j)
655:                        results[k++] = engines[i];
656:                }
657:                engines = results;
658:
659:            }
660:
661:            /**
662:             * Remove the specified Host, along with all of its related Contexts,
663:             * from the set of defined Hosts for its associated Engine.  If this is
664:             * the last Host for this Engine, the Engine will also be removed.
665:             *
666:             * @param host The Host to be removed
667:             */
668:            public synchronized void removeHost(Host host) {
669:
670:                if (log.isDebugEnabled())
671:                    log.debug("Removing host[" + host.getName() + "]");
672:
673:                // Is this Host actually among those that are defined?
674:                boolean found = false;
675:                for (int i = 0; i < engines.length; i++) {
676:                    Container hosts[] = engines[i].findChildren();
677:                    for (int j = 0; j < hosts.length; j++) {
678:                        if (host == (Host) hosts[j]) {
679:                            found = true;
680:                            break;
681:
682:                        }
683:                    }
684:                    if (found)
685:                        break;
686:                }
687:                if (!found)
688:                    return;
689:
690:                // Remove this Host from the associated Engine
691:                if (log.isDebugEnabled())
692:                    log.debug(" Removing this Host");
693:                host.getParent().removeChild(host);
694:
695:            }
696:
697:            /*
698:             * Maps the specified login method to the specified authenticator, allowing
699:             * the mappings in org/apache/catalina/startup/Authenticators.properties
700:             * to be overridden.
701:             *
702:             * @param authenticator Authenticator to handle authentication for the
703:             * specified login method
704:             * @param loginMethod Login method that maps to the specified authenticator
705:             *
706:             * @throws IllegalArgumentException if the specified authenticator does not
707:             * implement the org.apache.catalina.Valve interface
708:             */
709:            public void addAuthenticator(Authenticator authenticator,
710:                    String loginMethod) {
711:                if (!(authenticator instanceof  Valve)) {
712:                    throw new IllegalArgumentException(
713:                            sm
714:                                    .getString("embedded.authenticatorNotInstanceOfValve"));
715:                }
716:                if (authenticators == null) {
717:                    synchronized (this ) {
718:                        if (authenticators == null) {
719:                            authenticators = new HashMap();
720:                        }
721:                    }
722:                }
723:                authenticators.put(loginMethod, authenticator);
724:            }
725:
726:            // ------------------------------------------------------ Lifecycle Methods
727:
728:            /**
729:             * Add a lifecycle event listener to this component.
730:             *
731:             * @param listener The listener to add
732:             */
733:            public void addLifecycleListener(LifecycleListener listener) {
734:
735:                lifecycle.addLifecycleListener(listener);
736:
737:            }
738:
739:            /**
740:             * Get the lifecycle listeners associated with this lifecycle. If this 
741:             * Lifecycle has no listeners registered, a zero-length array is returned.
742:             */
743:            public LifecycleListener[] findLifecycleListeners() {
744:
745:                return lifecycle.findLifecycleListeners();
746:
747:            }
748:
749:            /**
750:             * Remove a lifecycle event listener from this component.
751:             *
752:             * @param listener The listener to remove
753:             */
754:            public void removeLifecycleListener(LifecycleListener listener) {
755:
756:                lifecycle.removeLifecycleListener(listener);
757:
758:            }
759:
760:            /**
761:             * Prepare for the beginning of active use of the public methods of this
762:             * component.  This method should be called after <code>configure()</code>,
763:             * and before any of the public methods of the component are utilized.
764:             *
765:             * @exception LifecycleException if this component detects a fatal error
766:             *  that prevents this component from being used
767:             */
768:            public void start() throws LifecycleException {
769:
770:                if (log.isInfoEnabled())
771:                    log.info("Starting tomcat server");
772:
773:                // Validate the setup of our required system properties
774:                initDirs();
775:
776:                // Initialize some naming specific properties
777:                initNaming();
778:
779:                // Validate and update our current component state
780:                if (started)
781:                    throw new LifecycleException(sm
782:                            .getString("embedded.alreadyStarted"));
783:                lifecycle.fireLifecycleEvent(START_EVENT, null);
784:                started = true;
785:                initialized = true;
786:
787:                // Start our defined Engines first
788:                for (int i = 0; i < engines.length; i++) {
789:                    if (engines[i] instanceof  Lifecycle)
790:                        ((Lifecycle) engines[i]).start();
791:                }
792:
793:                // Start our defined Connectors second
794:                for (int i = 0; i < connectors.length; i++) {
795:                    connectors[i].initialize();
796:                    if (connectors[i] instanceof  Lifecycle)
797:                        ((Lifecycle) connectors[i]).start();
798:                }
799:
800:            }
801:
802:            /**
803:             * Gracefully terminate the active use of the public methods of this
804:             * component.  This method should be the last one called on a given
805:             * instance of this component.
806:             *
807:             * @exception LifecycleException if this component detects a fatal error
808:             *  that needs to be reported
809:             */
810:            public void stop() throws LifecycleException {
811:
812:                if (log.isDebugEnabled())
813:                    log.debug("Stopping embedded server");
814:
815:                // Validate and update our current component state
816:                if (!started)
817:                    throw new LifecycleException(sm
818:                            .getString("embedded.notStarted"));
819:                lifecycle.fireLifecycleEvent(STOP_EVENT, null);
820:                started = false;
821:
822:                // Stop our defined Connectors first
823:                for (int i = 0; i < connectors.length; i++) {
824:                    if (connectors[i] instanceof  Lifecycle)
825:                        ((Lifecycle) connectors[i]).stop();
826:                }
827:
828:                // Stop our defined Engines second
829:                for (int i = 0; i < engines.length; i++) {
830:                    if (engines[i] instanceof  Lifecycle)
831:                        ((Lifecycle) engines[i]).stop();
832:                }
833:
834:            }
835:
836:            // ------------------------------------------------------ Protected Methods
837:
838:            /** Initialize naming - this should only enable java:env and root naming.
839:             * If tomcat is embeded in an application that already defines those -
840:             * it shouldn't do it.
841:             *
842:             * XXX The 2 should be separated, you may want to enable java: but not
843:             * the initial context and the reverse
844:             * XXX Can we "guess" - i.e. lookup java: and if something is returned assume
845:             * false ?
846:             * XXX We have a major problem with the current setting for java: url
847:             */
848:            protected void initNaming() {
849:                // Setting additional variables
850:                if (!useNaming) {
851:                    log.info("Catalina naming disabled");
852:                    System.setProperty("catalina.useNaming", "false");
853:                } else {
854:                    System.setProperty("catalina.useNaming", "true");
855:                    String value = "org.apache.naming";
856:                    String oldValue = System
857:                            .getProperty(javax.naming.Context.URL_PKG_PREFIXES);
858:                    if (oldValue != null) {
859:                        value = value + ":" + oldValue;
860:                    }
861:                    System.setProperty(javax.naming.Context.URL_PKG_PREFIXES,
862:                            value);
863:                    if (log.isDebugEnabled())
864:                        log.debug("Setting naming prefix=" + value);
865:                    value = System
866:                            .getProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY);
867:                    if (value == null) {
868:                        System.setProperty(
869:                                javax.naming.Context.INITIAL_CONTEXT_FACTORY,
870:                                "org.apache.naming.java.javaURLContextFactory");
871:                    } else {
872:                        log
873:                                .debug("INITIAL_CONTEXT_FACTORY alread set "
874:                                        + value);
875:                    }
876:                }
877:            }
878:
879:            protected void initDirs() {
880:
881:                String catalinaHome = System.getProperty("catalina.home");
882:                if (catalinaHome == null) {
883:                    // Backwards compatibility patch for J2EE RI 1.3
884:                    String j2eeHome = System
885:                            .getProperty("com.sun.enterprise.home");
886:                    if (j2eeHome != null) {
887:                        catalinaHome = System
888:                                .getProperty("com.sun.enterprise.home");
889:                    } else if (System.getProperty("catalina.base") != null) {
890:                        catalinaHome = System.getProperty("catalina.base");
891:                    } else {
892:                        // Use IntrospectionUtils and guess the dir
893:                        catalinaHome = IntrospectionUtils.guessInstall(
894:                                "catalina.home", "catalina.base",
895:                                "catalina.jar");
896:                        if (catalinaHome == null) {
897:                            catalinaHome = IntrospectionUtils.guessInstall(
898:                                    "tomcat.install", "catalina.home",
899:                                    "tomcat.jar");
900:                        }
901:                    }
902:                }
903:                // last resort - for minimal/embedded cases. 
904:                if (catalinaHome == null) {
905:                    catalinaHome = System.getProperty("user.dir");
906:                }
907:                if (catalinaHome != null) {
908:                    File home = new File(catalinaHome);
909:                    if (!home.isAbsolute()) {
910:                        try {
911:                            catalinaHome = home.getCanonicalPath();
912:                        } catch (IOException e) {
913:                            catalinaHome = home.getAbsolutePath();
914:                        }
915:                    }
916:                    System.setProperty("catalina.home", catalinaHome);
917:                }
918:
919:                if (System.getProperty("catalina.base") == null) {
920:                    System.setProperty("catalina.base", catalinaHome);
921:                } else {
922:                    String catalinaBase = System.getProperty("catalina.base");
923:                    File base = new File(catalinaBase);
924:                    if (!base.isAbsolute()) {
925:                        try {
926:                            catalinaBase = base.getCanonicalPath();
927:                        } catch (IOException e) {
928:                            catalinaBase = base.getAbsolutePath();
929:                        }
930:                    }
931:                    System.setProperty("catalina.base", catalinaBase);
932:                }
933:
934:                String temp = System.getProperty("java.io.tmpdir");
935:                if (temp == null || (!(new File(temp)).exists())
936:                        || (!(new File(temp)).isDirectory())) {
937:                    log.error(sm.getString("embedded.notmp", temp));
938:                }
939:
940:            }
941:
942:            protected void initStreams() {
943:                if (redirectStreams) {
944:                    // Replace System.out and System.err with a custom PrintStream
945:                    SystemLogHandler systemlog = new SystemLogHandler(
946:                            System.out);
947:                    System.setOut(systemlog);
948:                    System.setErr(systemlog);
949:                }
950:            }
951:
952:            // -------------------------------------------------------- Private Methods
953:
954:            /**
955:             * Set the security package access/protection.
956:             */
957:            protected void setSecurityProtection() {
958:                SecurityConfig securityConfig = SecurityConfig.newInstance();
959:                securityConfig.setPackageDefinition();
960:                securityConfig.setPackageAccess();
961:            }
962:
963:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.