KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > startup > Embedded


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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
18
19 package org.apache.catalina.startup;
20
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import org.apache.catalina.Authenticator;
28 import org.apache.catalina.Container;
29 import org.apache.catalina.Context;
30 import org.apache.catalina.Engine;
31 import org.apache.catalina.Host;
32 import org.apache.catalina.Lifecycle;
33 import org.apache.catalina.LifecycleException;
34 import org.apache.catalina.LifecycleListener;
35 import org.apache.catalina.Loader;
36 import org.apache.catalina.Realm;
37 import org.apache.catalina.Valve;
38 import org.apache.catalina.connector.Connector;
39 import org.apache.catalina.core.StandardContext;
40 import org.apache.catalina.core.StandardEngine;
41 import org.apache.catalina.core.StandardHost;
42 import org.apache.catalina.core.StandardService;
43 import org.apache.catalina.loader.WebappLoader;
44 import org.apache.catalina.security.SecurityConfig;
45 import org.apache.catalina.util.LifecycleSupport;
46 import org.apache.catalina.util.StringManager;
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49 import org.apache.tomcat.util.IntrospectionUtils;
50 import org.apache.tomcat.util.log.SystemLogHandler;
51
52
53 /**
54  * Convenience class to embed a Catalina servlet container environment
55  * inside another application. You must call the methods of this class in the
56  * following order to ensure correct operation.
57  *
58  * <ul>
59  * <li>Instantiate a new instance of this class.</li>
60  * <li>Set the relevant properties of this object itself. In particular,
61  * you will want to establish the default Logger to be used, as well
62  * as the default Realm if you are using container-managed security.</li>
63  * <li>Call <code>createEngine()</code> to create an Engine object, and then
64  * call its property setters as desired.</li>
65  * <li>Call <code>createHost()</code> to create at least one virtual Host
66  * associated with the newly created Engine, and then call its property
67  * setters as desired. After you customize this Host, add it to the
68  * corresponding Engine with <code>engine.addChild(host)</code>.</li>
69  * <li>Call <code>createContext()</code> to create at least one Context
70  * associated with each newly created Host, and then call its property
71  * setters as desired. You <strong>SHOULD</strong> create a Context with
72  * a pathname equal to a zero-length string, which will be used to process
73  * all requests not mapped to some other Context. After you customize
74  * this Context, add it to the corresponding Host with
75  * <code>host.addChild(context)</code>.</li>
76  * <li>Call <code>addEngine()</code> to attach this Engine to the set of
77  * defined Engines for this object.</li>
78  * <li>Call <code>createConnector()</code> to create at least one TCP/IP
79  * connector, and then call its property setters as desired.</li>
80  * <li>Call <code>addConnector()</code> to attach this Connector to the set
81  * of defined Connectors for this object. The added Connector will use
82  * the most recently added Engine to process its received requests.</li>
83  * <li>Repeat the above series of steps as often as required (although there
84  * will typically be only one Engine instance created).</li>
85  * <li>Call <code>start()</code> to initiate normal operations of all the
86  * attached components.</li>
87  * </ul>
88  *
89  * After normal operations have begun, you can add and remove Connectors,
90  * Engines, Hosts, and Contexts on the fly. However, once you have removed
91  * a particular component, it must be thrown away -- you can create a new one
92  * with the same characteristics if you merely want to do a restart.
93  * <p>
94  * To initiate a normal shutdown, call the <code>stop()</code> method of
95  * this object.
96  * <p>
97  * @see org.apache.catalina.startup.Catalina#main For a complete example
98  * of how Tomcat is set up and launched as an Embedded application.
99  *
100  * @author Craig R. McClanahan
101  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
102  */

103
104 public class Embedded extends StandardService implements Lifecycle {
105     private static Log log = LogFactory.getLog(Embedded.class);
106
107     // ----------------------------------------------------------- Constructors
108

109
110     /**
111      * Construct a new instance of this class with default properties.
112      */

113     public Embedded() {
114
115         this(null);
116
117     }
118
119
120     /**
121      * Construct a new instance of this class with specified properties.
122      *
123      * @param realm Realm implementation to be inherited by all components
124      * (unless overridden further down the container hierarchy)
125      */

126     public Embedded(Realm realm) {
127
128         super();
129         setRealm(realm);
130         setSecurityProtection();
131         
132     }
133
134
135     // ----------------------------------------------------- Instance Variables
136

137
138     /**
139      * Is naming enabled ?
140      */

141     protected boolean useNaming = true;
142
143
144     /**
145      * Is standard streams redirection enabled ?
146      */

147     protected boolean redirectStreams = true;
148
149
150     /**
151      * The set of Engines that have been deployed in this server. Normally
152      * there will only be one.
153      */

154     protected Engine engines[] = new Engine[0];
155
156
157     /**
158      * Custom mappings of login methods to authenticators
159      */

160     protected HashMap JavaDoc authenticators;
161
162
163     /**
164      * Descriptive information about this server implementation.
165      */

166     protected static final String JavaDoc info =
167         "org.apache.catalina.startup.Embedded/1.0";
168
169
170     /**
171      * The lifecycle event support for this component.
172      */

173     protected LifecycleSupport lifecycle = new LifecycleSupport(this);
174
175
176     /**
177      * The default realm to be used by all containers associated with
178      * this compoennt.
179      */

180     protected Realm realm = null;
181
182
183     /**
184      * The string manager for this package.
185      */

186     protected static StringManager sm =
187         StringManager.getManager(Constants.Package);
188
189
190     /**
191      * Has this component been started yet?
192      */

193     protected boolean started = false;
194
195     /**
196      * Use await.
197      */

198     protected boolean await = false;
199
200
201     // ------------------------------------------------------------- Properties
202

203
204     /**
205      * Return true if naming is enabled.
206      */

207     public boolean isUseNaming() {
208
209         return (this.useNaming);
210
211     }
212
213
214     /**
215      * Enables or disables naming support.
216      *
217      * @param useNaming The new use naming value
218      */

219     public void setUseNaming(boolean useNaming) {
220
221         boolean oldUseNaming = this.useNaming;
222         this.useNaming = useNaming;
223         support.firePropertyChange("useNaming", new Boolean JavaDoc(oldUseNaming),
224                                    new Boolean JavaDoc(this.useNaming));
225
226     }
227
228
229     /**
230      * Return true if redirction of standard streams is enabled.
231      */

232     public boolean isRedirectStreams() {
233
234         return (this.redirectStreams);
235
236     }
237
238
239     /**
240      * Enables or disables naming support.
241      *
242      * @param useNaming The new use naming value
243      */

244     public void setRedirectStreams(boolean redirectStreams) {
245
246         boolean oldRedirectStreams = this.redirectStreams;
247         this.redirectStreams = redirectStreams;
248         support.firePropertyChange("redirectStreams", new Boolean JavaDoc(oldRedirectStreams),
249                                    new Boolean JavaDoc(this.redirectStreams));
250
251     }
252
253
254     /**
255      * Return the default Realm for our Containers.
256      */

257     public Realm getRealm() {
258
259         return (this.realm);
260
261     }
262
263
264     /**
265      * Set the default Realm for our Containers.
266      *
267      * @param realm The new default realm
268      */

269     public void setRealm(Realm realm) {
270
271         Realm oldRealm = this.realm;
272         this.realm = realm;
273         support.firePropertyChange("realm", oldRealm, this.realm);
274
275     }
276
277     public void setAwait(boolean b) {
278         await = b;
279     }
280
281     public boolean isAwait() {
282         return await;
283     }
284
285     public void setCatalinaHome( String JavaDoc s ) {
286         System.setProperty( "catalina.home", s);
287     }
288
289     public void setCatalinaBase( String JavaDoc s ) {
290         System.setProperty( "catalina.base", s);
291     }
292
293     public String JavaDoc getCatalinaHome() {
294         return System.getProperty("catalina.home");
295     }
296
297     public String JavaDoc getCatalinaBase() {
298         return System.getProperty("catalina.base");
299     }
300
301
302     // --------------------------------------------------------- Public Methods
303

304     /**
305      * Add a new Connector to the set of defined Connectors. The newly
306      * added Connector will be associated with the most recently added Engine.
307      *
308      * @param connector The connector to be added
309      *
310      * @exception IllegalStateException if no engines have been added yet
311      */

312     public synchronized void addConnector(Connector connector) {
313
314         if( log.isDebugEnabled() ) {
315             log.debug("Adding connector (" + connector.getInfo() + ")");
316         }
317
318         // Make sure we have a Container to send requests to
319
if (engines.length < 1)
320             throw new IllegalStateException JavaDoc
321                 (sm.getString("embedded.noEngines"));
322
323         /*
324          * Add the connector. This will set the connector's container to the
325          * most recently added Engine
326          */

327         super.addConnector(connector);
328     }
329
330
331     /**
332      * Add a new Engine to the set of defined Engines.
333      *
334      * @param engine The engine to be added
335      */

336     public synchronized void addEngine(Engine engine) {
337
338         if( log.isDebugEnabled() )
339             log.debug("Adding engine (" + engine.getInfo() + ")");
340
341         // Add this Engine to our set of defined Engines
342
Engine results[] = new Engine[engines.length + 1];
343         for (int i = 0; i < engines.length; i++)
344             results[i] = engines[i];
345         results[engines.length] = engine;
346         engines = results;
347
348         // Start this Engine if necessary
349
if (started && (engine instanceof Lifecycle)) {
350             try {
351                 ((Lifecycle) engine).start();
352             } catch (LifecycleException e) {
353                 log.error("Engine.start", e);
354             }
355         }
356
357         this.container = engine;
358     }
359
360
361     /**
362      * Create, configure, and return a new TCP/IP socket connector
363      * based on the specified properties.
364      *
365      * @param address InetAddress to bind to, or <code>null</code> if the
366      * connector is supposed to bind to all addresses on this server
367      * @param port Port number to listen to
368      * @param secure true if the generated connector is supposed to be
369      * SSL-enabled, and false otherwise
370      */

371     public Connector createConnector(InetAddress JavaDoc address, int port,
372                                      boolean secure) {
373     return createConnector(address != null? address.toString() : null,
374                    port, secure);
375     }
376
377     public Connector createConnector(String JavaDoc address, int port,
378                                      boolean secure) {
379         String JavaDoc protocol = "http";
380         if (secure) {
381             protocol = "https";
382         }
383
384         return createConnector(address, port, protocol);
385     }
386
387
388     public Connector createConnector(InetAddress JavaDoc address, int port,
389                                      String JavaDoc protocol) {
390     return createConnector(address != null? address.toString() : null,
391                    port, protocol);
392     }
393
394     public Connector createConnector(String JavaDoc address, int port,
395                      String JavaDoc protocol) {
396
397         Connector connector = null;
398
399     if (address != null) {
400         /*
401          * InetAddress.toString() returns a string of the form
402          * "<hostname>/<literal_IP>". Get the latter part, so that the
403          * address can be parsed (back) into an InetAddress using
404          * InetAddress.getByName().
405          */

406         int index = address.indexOf('/');
407         if (index != -1) {
408         address = address.substring(index + 1);
409         }
410     }
411
412     if (log.isDebugEnabled()) {
413             log.debug("Creating connector for address='" +
414               ((address == null) ? "ALL" : address) +
415               "' port='" + port + "' protocol='" + protocol + "'");
416     }
417
418         try {
419
420             if (protocol.equals("ajp")) {
421                 connector = new Connector("org.apache.jk.server.JkCoyoteHandler");
422             } else if (protocol.equals("memory")) {
423                 connector = new Connector("org.apache.coyote.memory.MemoryProtocolHandler");
424             } else if (protocol.equals("http")) {
425                 connector = new Connector();
426             } else if (protocol.equals("https")) {
427                 connector = new Connector();
428                 connector.setScheme("https");
429                 connector.setSecure(true);
430                 // FIXME !!!! SET SSL PROPERTIES
431
}
432
433             if (address != null) {
434                 IntrospectionUtils.setProperty(connector, "address",
435                                                "" + address);
436             }
437             IntrospectionUtils.setProperty(connector, "port", "" + port);
438
439         } catch (Exception JavaDoc e) {
440             log.error("Couldn't create connector.");
441         }
442
443         return (connector);
444
445     }
446
447     /**
448      * Create, configure, and return a Context that will process all
449      * HTTP requests received from one of the associated Connectors,
450      * and directed to the specified context path on the virtual host
451      * to which this Context is connected.
452      * <p>
453      * After you have customized the properties, listeners, and Valves
454      * for this Context, you must attach it to the corresponding Host
455      * by calling:
456      * <pre>
457      * host.addChild(context);
458      * </pre>
459      * which will also cause the Context to be started if the Host has
460      * already been started.
461      *
462      * @param path Context path of this application ("" for the default
463      * application for this host, must start with a slash otherwise)
464      * @param docBase Absolute pathname to the document base directory
465      * for this web application
466      *
467      * @exception IllegalArgumentException if an invalid parameter
468      * is specified
469      */

470     public Context createContext(String JavaDoc path, String JavaDoc docBase) {
471
472         if( log.isDebugEnabled() )
473             log.debug("Creating context '" + path + "' with docBase '" +
474                        docBase + "'");
475
476         StandardContext context = new StandardContext();
477
478         context.setDocBase(docBase);
479         context.setPath(path);
480
481         ContextConfig config = new ContextConfig();
482         config.setCustomAuthenticators(authenticators);
483         ((Lifecycle) context).addLifecycleListener(config);
484
485         return (context);
486
487     }
488
489
490     /**
491      * Create, configure, and return an Engine that will process all
492      * HTTP requests received from one of the associated Connectors,
493      * based on the specified properties.
494      */

495     public Engine createEngine() {
496
497         if( log.isDebugEnabled() )
498             log.debug("Creating engine");
499
500         StandardEngine engine = new StandardEngine();
501
502         // Default host will be set to the first host added
503
engine.setRealm(realm); // Inherited by all children
504

505         return (engine);
506
507     }
508
509
510     /**
511      * Create, configure, and return a Host that will process all
512      * HTTP requests received from one of the associated Connectors,
513      * and directed to the specified virtual host.
514      * <p>
515      * After you have customized the properties, listeners, and Valves
516      * for this Host, you must attach it to the corresponding Engine
517      * by calling:
518      * <pre>
519      * engine.addChild(host);
520      * </pre>
521      * which will also cause the Host to be started if the Engine has
522      * already been started. If this is the default (or only) Host you
523      * will be defining, you may also tell the Engine to pass all requests
524      * not assigned to another virtual host to this one:
525      * <pre>
526      * engine.setDefaultHost(host.getName());
527      * </pre>
528      *
529      * @param name Canonical name of this virtual host
530      * @param appBase Absolute pathname to the application base directory
531      * for this virtual host
532      *
533      * @exception IllegalArgumentException if an invalid parameter
534      * is specified
535      */

536     public Host createHost(String JavaDoc name, String JavaDoc appBase) {
537
538         if( log.isDebugEnabled() )
539             log.debug("Creating host '" + name + "' with appBase '" +
540                        appBase + "'");
541
542         StandardHost host = new StandardHost();
543
544         host.setAppBase(appBase);
545         host.setName(name);
546
547         return (host);
548
549     }
550
551
552     /**
553      * Create and return a class loader manager that can be customized, and
554      * then attached to a Context, before it is started.
555      *
556      * @param parent ClassLoader that will be the parent of the one
557      * created by this Loader
558      */

559     public Loader createLoader(ClassLoader JavaDoc parent) {
560
561         if( log.isDebugEnabled() )
562             log.debug("Creating Loader with parent class loader '" +
563                        parent + "'");
564
565         WebappLoader loader = new WebappLoader(parent);
566         return (loader);
567
568     }
569
570
571     /**
572      * Return descriptive information about this Server implementation and
573      * the corresponding version number, in the format
574      * <code>&lt;description&gt;/&lt;version&gt;</code>.
575      */

576     public String JavaDoc getInfo() {
577
578         return (info);
579
580     }
581
582
583     /**
584      * Remove the specified Context from the set of defined Contexts for its
585      * associated Host. If this is the last Context for this Host, the Host
586      * will also be removed.
587      *
588      * @param context The Context to be removed
589      */

590     public synchronized void removeContext(Context context) {
591
592         if( log.isDebugEnabled() )
593             log.debug("Removing context[" + context.getPath() + "]");
594
595         // Is this Context actually among those that are defined?
596
boolean found = false;
597         for (int i = 0; i < engines.length; i++) {
598             Container hosts[] = engines[i].findChildren();
599             for (int j = 0; j < hosts.length; j++) {
600                 Container contexts[] = hosts[j].findChildren();
601                 for (int k = 0; k < contexts.length; k++) {
602                     if (context == (Context) contexts[k]) {
603                         found = true;
604                         break;
605                     }
606                 }
607                 if (found)
608                     break;
609             }
610             if (found)
611                 break;
612         }
613         if (!found)
614             return;
615
616         // Remove this Context from the associated Host
617
if( log.isDebugEnabled() )
618             log.debug(" Removing this Context");
619         context.getParent().removeChild(context);
620
621     }
622
623
624     /**
625      * Remove the specified Engine from the set of defined Engines, along with
626      * all of its related Hosts and Contexts. All associated Connectors are
627      * also removed.
628      *
629      * @param engine The Engine to be removed
630      */

631     public synchronized void removeEngine(Engine engine) {
632
633         if( log.isDebugEnabled() )
634             log.debug("Removing engine (" + engine.getInfo() + ")");
635
636         // Is the specified Engine actually defined?
637
int j = -1;
638         for (int i = 0; i < engines.length; i++) {
639             if (engine == engines[i]) {
640                 j = i;
641                 break;
642             }
643         }
644         if (j < 0)
645             return;
646
647         // Remove any Connector that is using this Engine
648
if( log.isDebugEnabled() )
649             log.debug(" Removing related Containers");
650         while (true) {
651             int n = -1;
652             for (int i = 0; i < connectors.length; i++) {
653                 if (connectors[i].getContainer() == (Container) engine) {
654                     n = i;
655                     break;
656                 }
657             }
658             if (n < 0)
659                 break;
660             removeConnector(connectors[n]);
661         }
662
663         // Stop this Engine if necessary
664
if (engine instanceof Lifecycle) {
665             if( log.isDebugEnabled() )
666                 log.debug(" Stopping this Engine");
667             try {
668                 ((Lifecycle) engine).stop();
669             } catch (LifecycleException e) {
670                 log.error("Engine.stop", e);
671             }
672         }
673
674         // Remove this Engine from our set of defined Engines
675
if( log.isDebugEnabled() )
676             log.debug(" Removing this Engine");
677         int k = 0;
678         Engine results[] = new Engine[engines.length - 1];
679         for (int i = 0; i < engines.length; i++) {
680             if (i != j)
681                 results[k++] = engines[i];
682         }
683         engines = results;
684
685     }
686
687
688     /**
689      * Remove the specified Host, along with all of its related Contexts,
690      * from the set of defined Hosts for its associated Engine. If this is
691      * the last Host for this Engine, the Engine will also be removed.
692      *
693      * @param host The Host to be removed
694      */

695     public synchronized void removeHost(Host host) {
696
697         if( log.isDebugEnabled() )
698             log.debug("Removing host[" + host.getName() + "]");
699
700         // Is this Host actually among those that are defined?
701
boolean found = false;
702         for (int i = 0; i < engines.length; i++) {
703             Container hosts[] = engines[i].findChildren();
704             for (int j = 0; j < hosts.length; j++) {
705                 if (host == (Host) hosts[j]) {
706                     found = true;
707                     break;
708
709                 }
710             }
711             if (found)
712                 break;
713         }
714         if (!found)
715             return;
716
717         // Remove this Host from the associated Engine
718
if( log.isDebugEnabled() )
719             log.debug(" Removing this Host");
720         host.getParent().removeChild(host);
721
722     }
723
724
725     /*
726      * Maps the specified login method to the specified authenticator, allowing
727      * the mappings in org/apache/catalina/startup/Authenticators.properties
728      * to be overridden.
729      *
730      * @param authenticator Authenticator to handle authentication for the
731      * specified login method
732      * @param loginMethod Login method that maps to the specified authenticator
733      *
734      * @throws IllegalArgumentException if the specified authenticator does not
735      * implement the org.apache.catalina.Valve interface
736      */

737     public void addAuthenticator(Authenticator authenticator,
738                                  String JavaDoc loginMethod) {
739         if (!(authenticator instanceof Valve)) {
740             throw new IllegalArgumentException JavaDoc(
741                 sm.getString("embedded.authenticatorNotInstanceOfValve"));
742         }
743         if (authenticators == null) {
744             synchronized (this) {
745                 if (authenticators == null) {
746                     authenticators = new HashMap JavaDoc();
747                 }
748             }
749         }
750         authenticators.put(loginMethod, authenticator);
751     }
752
753
754     // ------------------------------------------------------ Lifecycle Methods
755

756
757     /**
758      * Add a lifecycle event listener to this component.
759      *
760      * @param listener The listener to add
761      */

762     public void addLifecycleListener(LifecycleListener listener) {
763
764         lifecycle.addLifecycleListener(listener);
765
766     }
767
768
769     /**
770      * Get the lifecycle listeners associated with this lifecycle. If this
771      * Lifecycle has no listeners registered, a zero-length array is returned.
772      */

773     public LifecycleListener[] findLifecycleListeners() {
774
775         return lifecycle.findLifecycleListeners();
776
777     }
778
779
780     /**
781      * Remove a lifecycle event listener from this component.
782      *
783      * @param listener The listener to remove
784      */

785     public void removeLifecycleListener(LifecycleListener listener) {
786
787         lifecycle.removeLifecycleListener(listener);
788
789     }
790
791
792     /**
793      * Prepare for the beginning of active use of the public methods of this
794      * component. This method should be called after <code>configure()</code>,
795      * and before any of the public methods of the component are utilized.
796      *
797      * @exception LifecycleException if this component detects a fatal error
798      * that prevents this component from being used
799      */

800     public void start() throws LifecycleException {
801
802         if( log.isInfoEnabled() )
803             log.info("Starting tomcat server");
804
805         // Validate the setup of our required system properties
806
initDirs();
807
808         // Initialize some naming specific properties
809
initNaming();
810
811         // Validate and update our current component state
812
if (started)
813             throw new LifecycleException
814                 (sm.getString("embedded.alreadyStarted"));
815         lifecycle.fireLifecycleEvent(START_EVENT, null);
816         started = true;
817         initialized = true;
818
819         // Start our defined Engines first
820
for (int i = 0; i < engines.length; i++) {
821             if (engines[i] instanceof Lifecycle)
822                 ((Lifecycle) engines[i]).start();
823         }
824
825         // Start our defined Connectors second
826
for (int i = 0; i < connectors.length; i++) {
827             connectors[i].initialize();
828             if (connectors[i] instanceof Lifecycle)
829                 ((Lifecycle) connectors[i]).start();
830         }
831
832     }
833
834
835     /**
836      * Gracefully terminate the active use of the public methods of this
837      * component. This method should be the last one called on a given
838      * instance of this component.
839      *
840      * @exception LifecycleException if this component detects a fatal error
841      * that needs to be reported
842      */

843     public void stop() throws LifecycleException {
844
845         if( log.isDebugEnabled() )
846             log.debug("Stopping embedded server");
847
848         // Validate and update our current component state
849
if (!started)
850             throw new LifecycleException
851                 (sm.getString("embedded.notStarted"));
852         lifecycle.fireLifecycleEvent(STOP_EVENT, null);
853         started = false;
854
855         // Stop our defined Connectors first
856
for (int i = 0; i < connectors.length; i++) {
857             if (connectors[i] instanceof Lifecycle)
858                 ((Lifecycle) connectors[i]).stop();
859         }
860
861         // Stop our defined Engines second
862
for (int i = 0; i < engines.length; i++) {
863             if (engines[i] instanceof Lifecycle)
864                 ((Lifecycle) engines[i]).stop();
865         }
866
867     }
868
869
870     // ------------------------------------------------------ Protected Methods
871

872
873     /** Initialize naming - this should only enable java:env and root naming.
874      * If tomcat is embeded in an application that already defines those -
875      * it shouldn't do it.
876      *
877      * XXX The 2 should be separated, you may want to enable java: but not
878      * the initial context and the reverse
879      * XXX Can we "guess" - i.e. lookup java: and if something is returned assume
880      * false ?
881      * XXX We have a major problem with the current setting for java: url
882      */

883     protected void initNaming() {
884         // Setting additional variables
885
if (!useNaming) {
886             log.info( "Catalina naming disabled");
887             System.setProperty("catalina.useNaming", "false");
888         } else {
889             System.setProperty("catalina.useNaming", "true");
890             String JavaDoc value = "org.apache.naming";
891             String JavaDoc oldValue =
892                 System.getProperty(javax.naming.Context.URL_PKG_PREFIXES);
893             if (oldValue != null) {
894                 value = value + ":" + oldValue;
895             }
896             System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value);
897             if( log.isDebugEnabled() )
898                 log.debug("Setting naming prefix=" + value);
899             value = System.getProperty
900                 (javax.naming.Context.INITIAL_CONTEXT_FACTORY);
901             if (value == null) {
902                 System.setProperty
903                     (javax.naming.Context.INITIAL_CONTEXT_FACTORY,
904                      "org.apache.naming.java.javaURLContextFactory");
905             } else {
906                 log.debug( "INITIAL_CONTEXT_FACTORY alread set " + value );
907             }
908         }
909     }
910
911
912     protected void initDirs() {
913
914         String JavaDoc catalinaHome = System.getProperty("catalina.home");
915         if (catalinaHome == null) {
916             // Backwards compatibility patch for J2EE RI 1.3
917
String JavaDoc j2eeHome = System.getProperty("com.sun.enterprise.home");
918             if (j2eeHome != null) {
919                 catalinaHome=System.getProperty("com.sun.enterprise.home");
920             } else if (System.getProperty("catalina.base") != null) {
921                 catalinaHome = System.getProperty("catalina.base");
922             } else {
923                 // Use IntrospectionUtils and guess the dir
924
catalinaHome = IntrospectionUtils.guessInstall
925                     ("catalina.home", "catalina.base", "catalina.jar");
926                 if (catalinaHome == null) {
927                     catalinaHome = IntrospectionUtils.guessInstall
928                         ("tomcat.install", "catalina.home", "tomcat.jar");
929                 }
930             }
931         }
932         // last resort - for minimal/embedded cases.
933
if(catalinaHome==null) {
934             catalinaHome=System.getProperty("user.dir");
935         }
936         if (catalinaHome != null) {
937             File JavaDoc home = new File JavaDoc(catalinaHome);
938             if (!home.isAbsolute()) {
939                 try {
940                     catalinaHome = home.getCanonicalPath();
941                 } catch (IOException JavaDoc e) {
942                     catalinaHome = home.getAbsolutePath();
943                 }
944             }
945             System.setProperty("catalina.home", catalinaHome);
946         }
947
948         if (System.getProperty("catalina.base") == null) {
949             System.setProperty("catalina.base",
950                                catalinaHome);
951         } else {
952             String JavaDoc catalinaBase = System.getProperty("catalina.base");
953             File JavaDoc base = new File JavaDoc(catalinaBase);
954             if (!base.isAbsolute()) {
955                 try {
956                     catalinaBase = base.getCanonicalPath();
957                 } catch (IOException JavaDoc e) {
958                     catalinaBase = base.getAbsolutePath();
959                 }
960             }
961             System.setProperty("catalina.base", catalinaBase);
962         }
963         
964         String JavaDoc temp = System.getProperty("java.io.tmpdir");
965         if (temp == null || (!(new File JavaDoc(temp)).exists())
966                 || (!(new File JavaDoc(temp)).isDirectory())) {
967             log.error(sm.getString("embedded.notmp", temp));
968         }
969
970     }
971
972     
973     protected void initStreams() {
974         if (redirectStreams) {
975             // Replace System.out and System.err with a custom PrintStream
976
SystemLogHandler systemlog = new SystemLogHandler(System.out);
977             System.setOut(systemlog);
978             System.setErr(systemlog);
979         }
980     }
981     
982
983     // -------------------------------------------------------- Private Methods
984

985     /**
986      * Set the security package access/protection.
987      */

988     protected void setSecurityProtection(){
989         SecurityConfig securityConfig = SecurityConfig.newInstance();
990         securityConfig.setPackageDefinition();
991         securityConfig.setPackageAccess();
992     }
993
994 }
995
Popular Tags