PageRenderTime 70ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/tomcat-7.0.2/java/org/apache/catalina/startup/Embedded.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 985 lines | 455 code | 196 blank | 334 comment | 104 complexity | e724f4a9fc9379c8ade98179697f3ca0 MD5 | raw file
  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. package org.apache.catalina.startup;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.net.InetAddress;
  21. import java.util.HashMap;
  22. import org.apache.catalina.Authenticator;
  23. import org.apache.catalina.Container;
  24. import org.apache.catalina.Context;
  25. import org.apache.catalina.Engine;
  26. import org.apache.catalina.Host;
  27. import org.apache.catalina.Lifecycle;
  28. import org.apache.catalina.LifecycleException;
  29. import org.apache.catalina.LifecycleListener;
  30. import org.apache.catalina.LifecycleState;
  31. import org.apache.catalina.Loader;
  32. import org.apache.catalina.Realm;
  33. import org.apache.catalina.Valve;
  34. import org.apache.catalina.connector.Connector;
  35. import org.apache.catalina.core.StandardContext;
  36. import org.apache.catalina.core.StandardEngine;
  37. import org.apache.catalina.core.StandardHost;
  38. import org.apache.catalina.core.StandardService;
  39. import org.apache.catalina.loader.WebappLoader;
  40. import org.apache.catalina.security.SecurityConfig;
  41. import org.apache.catalina.util.LifecycleBase;
  42. import org.apache.catalina.util.LifecycleSupport;
  43. import org.apache.tomcat.util.res.StringManager;
  44. import org.apache.juli.logging.Log;
  45. import org.apache.juli.logging.LogFactory;
  46. import org.apache.tomcat.util.IntrospectionUtils;
  47. import org.apache.tomcat.util.log.SystemLogHandler;
  48. /**
  49. * Convenience class to embed a Catalina servlet container environment
  50. * inside another application. You must call the methods of this class in the
  51. * following order to ensure correct operation.
  52. *
  53. * <ul>
  54. * <li>Instantiate a new instance of this class.</li>
  55. * <li>Set the relevant properties of this object itself. In particular,
  56. * you will want to establish the default Logger to be used, as well
  57. * as the default Realm if you are using container-managed security.</li>
  58. * <li>Call <code>createEngine()</code> to create an Engine object, and then
  59. * call its property setters as desired.</li>
  60. * <li>Call <code>createHost()</code> to create at least one virtual Host
  61. * associated with the newly created Engine, and then call its property
  62. * setters as desired. After you customize this Host, add it to the
  63. * corresponding Engine with <code>engine.addChild(host)</code>.</li>
  64. * <li>Call <code>createContext()</code> to create at least one Context
  65. * associated with each newly created Host, and then call its property
  66. * setters as desired. You <strong>SHOULD</strong> create a Context with
  67. * a pathname equal to a zero-length string, which will be used to process
  68. * all requests not mapped to some other Context. After you customize
  69. * this Context, add it to the corresponding Host with
  70. * <code>host.addChild(context)</code>.</li>
  71. * <li>Call <code>addEngine()</code> to attach this Engine to the set of
  72. * defined Engines for this object.</li>
  73. * <li>Call <code>createConnector()</code> to create at least one TCP/IP
  74. * connector, and then call its property setters as desired.</li>
  75. * <li>Call <code>addConnector()</code> to attach this Connector to the set
  76. * of defined Connectors for this object. The added Connector will use
  77. * the most recently added Engine to process its received requests.</li>
  78. * <li>Repeat the above series of steps as often as required (although there
  79. * will typically be only one Engine instance created).</li>
  80. * <li>Call <code>start()</code> to initiate normal operations of all the
  81. * attached components.</li>
  82. * </ul>
  83. *
  84. * After normal operations have begun, you can add and remove Connectors,
  85. * Engines, Hosts, and Contexts on the fly. However, once you have removed
  86. * a particular component, it must be thrown away -- you can create a new one
  87. * with the same characteristics if you merely want to do a restart.
  88. * <p>
  89. * To initiate a normal shutdown, call the <code>stop()</code> method of
  90. * this object.
  91. * <p>
  92. * @see org.apache.catalina.startup.Bootstrap#main For a complete example
  93. * of how Tomcat is set up and launched as an Embedded application.
  94. *
  95. * @author Craig R. McClanahan
  96. * @version $Id: Embedded.java 981816 2010-08-03 10:44:58Z markt $
  97. *
  98. * @deprecated Use {@link Tomcat} instead.
  99. */
  100. @Deprecated
  101. public class Embedded extends StandardService {
  102. private static final Log log = LogFactory.getLog(Embedded.class);
  103. // ----------------------------------------------------------- Constructors
  104. /**
  105. * Construct a new instance of this class with default properties.
  106. */
  107. public Embedded() {
  108. this(null);
  109. }
  110. /**
  111. * Construct a new instance of this class with specified properties.
  112. *
  113. * @param realm Realm implementation to be inherited by all components
  114. * (unless overridden further down the container hierarchy)
  115. */
  116. public Embedded(Realm realm) {
  117. super();
  118. setRealm(realm);
  119. setSecurityProtection();
  120. }
  121. // ----------------------------------------------------- Instance Variables
  122. /**
  123. * Is naming enabled ?
  124. */
  125. protected boolean useNaming = true;
  126. /**
  127. * Is standard streams redirection enabled ?
  128. */
  129. protected boolean redirectStreams = true;
  130. /**
  131. * The set of Engines that have been deployed in this server. Normally
  132. * there will only be one.
  133. */
  134. protected Engine engines[] = new Engine[0];
  135. /**
  136. * Custom mappings of login methods to authenticators
  137. */
  138. protected HashMap<String,Authenticator> authenticators;
  139. /**
  140. * Descriptive information about this server implementation.
  141. */
  142. protected static final String info =
  143. "org.apache.catalina.startup.Embedded/1.0";
  144. /**
  145. * The lifecycle event support for this component.
  146. */
  147. protected LifecycleSupport lifecycle = new LifecycleSupport(this);
  148. /**
  149. * The default realm to be used by all containers associated with
  150. * this component.
  151. */
  152. protected Realm realm = null;
  153. /**
  154. * The string manager for this package.
  155. */
  156. protected static final StringManager sm =
  157. StringManager.getManager(Constants.Package);
  158. /**
  159. * Use await.
  160. */
  161. protected boolean await = false;
  162. // ------------------------------------------------------------- Properties
  163. /**
  164. * Return true if naming is enabled.
  165. */
  166. public boolean isUseNaming() {
  167. return (this.useNaming);
  168. }
  169. /**
  170. * Enables or disables naming support.
  171. *
  172. * @param useNaming The new use naming value
  173. */
  174. public void setUseNaming(boolean useNaming) {
  175. boolean oldUseNaming = this.useNaming;
  176. this.useNaming = useNaming;
  177. support.firePropertyChange("useNaming", new Boolean(oldUseNaming),
  178. new Boolean(this.useNaming));
  179. }
  180. /**
  181. * Return true if redirection of standard streams is enabled.
  182. */
  183. public boolean isRedirectStreams() {
  184. return (this.redirectStreams);
  185. }
  186. /**
  187. * Enables or disables redirection.
  188. *
  189. * @param redirectStreams The new redirection value
  190. */
  191. public void setRedirectStreams(boolean redirectStreams) {
  192. boolean oldRedirectStreams = this.redirectStreams;
  193. this.redirectStreams = redirectStreams;
  194. support.firePropertyChange("redirectStreams", new Boolean(oldRedirectStreams),
  195. new Boolean(this.redirectStreams));
  196. }
  197. /**
  198. * Return the default Realm for our Containers.
  199. */
  200. public Realm getRealm() {
  201. return (this.realm);
  202. }
  203. /**
  204. * Set the default Realm for our Containers.
  205. *
  206. * @param realm The new default realm
  207. */
  208. public void setRealm(Realm realm) {
  209. Realm oldRealm = this.realm;
  210. this.realm = realm;
  211. support.firePropertyChange("realm", oldRealm, this.realm);
  212. }
  213. public void setAwait(boolean b) {
  214. await = b;
  215. }
  216. public boolean isAwait() {
  217. return await;
  218. }
  219. public void setCatalinaHome( String s ) {
  220. System.setProperty( "catalina.home", s);
  221. }
  222. public void setCatalinaBase( String s ) {
  223. System.setProperty( "catalina.base", s);
  224. }
  225. public String getCatalinaHome() {
  226. return System.getProperty("catalina.home");
  227. }
  228. public String getCatalinaBase() {
  229. return System.getProperty("catalina.base");
  230. }
  231. // --------------------------------------------------------- Public Methods
  232. /**
  233. * Add a new Connector to the set of defined Connectors. The newly
  234. * added Connector will be associated with the most recently added Engine.
  235. *
  236. * @param connector The connector to be added
  237. *
  238. * @exception IllegalStateException if no engines have been added yet
  239. */
  240. @Override
  241. public synchronized void addConnector(Connector connector) {
  242. if( log.isDebugEnabled() ) {
  243. log.debug("Adding connector (" + connector.getInfo() + ")");
  244. }
  245. // Make sure we have a Container to send requests to
  246. if (engines.length < 1)
  247. throw new IllegalStateException
  248. (sm.getString("embedded.noEngines"));
  249. /*
  250. * Add the connector. This will set the connector's container to the
  251. * most recently added Engine
  252. */
  253. super.addConnector(connector);
  254. }
  255. /**
  256. * Add a new Engine to the set of defined Engines.
  257. *
  258. * @param engine The engine to be added
  259. */
  260. public synchronized void addEngine(Engine engine) {
  261. if( log.isDebugEnabled() )
  262. log.debug("Adding engine (" + engine.getInfo() + ")");
  263. // Add this Engine to our set of defined Engines
  264. Engine results[] = new Engine[engines.length + 1];
  265. for (int i = 0; i < engines.length; i++)
  266. results[i] = engines[i];
  267. results[engines.length] = engine;
  268. engines = results;
  269. // Start this Engine if necessary
  270. if (getState().isAvailable()) {
  271. try {
  272. engine.start();
  273. } catch (LifecycleException e) {
  274. log.error("Engine.start", e);
  275. }
  276. }
  277. this.container = engine;
  278. }
  279. /**
  280. * Create, configure, and return a new TCP/IP socket connector
  281. * based on the specified properties.
  282. *
  283. * @param address InetAddress to bind to, or <code>null</code> if the
  284. * connector is supposed to bind to all addresses on this server
  285. * @param port Port number to listen to
  286. * @param secure true if the generated connector is supposed to be
  287. * SSL-enabled, and false otherwise
  288. */
  289. public Connector createConnector(InetAddress address, int port,
  290. boolean secure) {
  291. return createConnector(address != null? address.toString() : null,
  292. port, secure);
  293. }
  294. public Connector createConnector(String address, int port,
  295. boolean secure) {
  296. String protocol = "http";
  297. if (secure) {
  298. protocol = "https";
  299. }
  300. return createConnector(address, port, protocol);
  301. }
  302. public Connector createConnector(InetAddress address, int port,
  303. String protocol) {
  304. return createConnector(address != null? address.toString() : null,
  305. port, protocol);
  306. }
  307. public Connector createConnector(String address, int port,
  308. String protocol) {
  309. Connector connector = null;
  310. if (address != null) {
  311. /*
  312. * InetAddress.toString() returns a string of the form
  313. * "<hostname>/<literal_IP>". Get the latter part, so that the
  314. * address can be parsed (back) into an InetAddress using
  315. * InetAddress.getByName().
  316. */
  317. int index = address.indexOf('/');
  318. if (index != -1) {
  319. address = address.substring(index + 1);
  320. }
  321. }
  322. if (log.isDebugEnabled()) {
  323. log.debug("Creating connector for address='" +
  324. ((address == null) ? "ALL" : address) +
  325. "' port='" + port + "' protocol='" + protocol + "'");
  326. }
  327. try {
  328. if (protocol.equals("ajp")) {
  329. connector = new Connector("org.apache.coyote.ajp.AjpProtocol");
  330. } else if (protocol.equals("memory")) {
  331. connector = new Connector("org.apache.coyote.memory.MemoryProtocolHandler");
  332. } else if (protocol.equals("http")) {
  333. connector = new Connector();
  334. } else if (protocol.equals("https")) {
  335. connector = new Connector();
  336. connector.setScheme("https");
  337. connector.setSecure(true);
  338. connector.setProperty("SSLEnabled","true");
  339. // FIXME !!!! SET SSL PROPERTIES
  340. } else {
  341. connector = new Connector(protocol);
  342. }
  343. if (address != null) {
  344. IntrospectionUtils.setProperty(connector, "address",
  345. "" + address);
  346. }
  347. IntrospectionUtils.setProperty(connector, "port", "" + port);
  348. } catch (Exception e) {
  349. log.error("Couldn't create connector.");
  350. }
  351. return (connector);
  352. }
  353. /**
  354. * Create, configure, and return a Context that will process all
  355. * HTTP requests received from one of the associated Connectors,
  356. * and directed to the specified context path on the virtual host
  357. * to which this Context is connected.
  358. * <p>
  359. * After you have customized the properties, listeners, and Valves
  360. * for this Context, you must attach it to the corresponding Host
  361. * by calling:
  362. * <pre>
  363. * host.addChild(context);
  364. * </pre>
  365. * which will also cause the Context to be started if the Host has
  366. * already been started.
  367. *
  368. * @param path Context path of this application ("" for the default
  369. * application for this host, must start with a slash otherwise)
  370. * @param docBase Absolute pathname to the document base directory
  371. * for this web application
  372. *
  373. * @exception IllegalArgumentException if an invalid parameter
  374. * is specified
  375. */
  376. public Context createContext(String path, String docBase) {
  377. if( log.isDebugEnabled() )
  378. log.debug("Creating context '" + path + "' with docBase '" +
  379. docBase + "'");
  380. StandardContext context = new StandardContext();
  381. context.setDocBase(docBase);
  382. context.setPath(path);
  383. ContextConfig config = new ContextConfig();
  384. config.setCustomAuthenticators(authenticators);
  385. context.addLifecycleListener(config);
  386. return (context);
  387. }
  388. /**
  389. * Create, configure, and return an Engine that will process all
  390. * HTTP requests received from one of the associated Connectors,
  391. * based on the specified properties.
  392. */
  393. public Engine createEngine() {
  394. if( log.isDebugEnabled() )
  395. log.debug("Creating engine");
  396. StandardEngine engine = new StandardEngine();
  397. // Default host will be set to the first host added
  398. engine.setRealm(realm); // Inherited by all children
  399. return (engine);
  400. }
  401. /**
  402. * Create, configure, and return a Host that will process all
  403. * HTTP requests received from one of the associated Connectors,
  404. * and directed to the specified virtual host.
  405. * <p>
  406. * After you have customized the properties, listeners, and Valves
  407. * for this Host, you must attach it to the corresponding Engine
  408. * by calling:
  409. * <pre>
  410. * engine.addChild(host);
  411. * </pre>
  412. * which will also cause the Host to be started if the Engine has
  413. * already been started. If this is the default (or only) Host you
  414. * will be defining, you may also tell the Engine to pass all requests
  415. * not assigned to another virtual host to this one:
  416. * <pre>
  417. * engine.setDefaultHost(host.getName());
  418. * </pre>
  419. *
  420. * @param name Canonical name of this virtual host
  421. * @param appBase Absolute pathname to the application base directory
  422. * for this virtual host
  423. *
  424. * @exception IllegalArgumentException if an invalid parameter
  425. * is specified
  426. */
  427. public Host createHost(String name, String appBase) {
  428. if( log.isDebugEnabled() )
  429. log.debug("Creating host '" + name + "' with appBase '" +
  430. appBase + "'");
  431. StandardHost host = new StandardHost();
  432. host.setAppBase(appBase);
  433. host.setName(name);
  434. return (host);
  435. }
  436. /**
  437. * Create and return a class loader manager that can be customized, and
  438. * then attached to a Context, before it is started.
  439. *
  440. * @param parent ClassLoader that will be the parent of the one
  441. * created by this Loader
  442. */
  443. public Loader createLoader(ClassLoader parent) {
  444. if( log.isDebugEnabled() )
  445. log.debug("Creating Loader with parent class loader '" +
  446. parent + "'");
  447. WebappLoader loader = new WebappLoader(parent);
  448. return (loader);
  449. }
  450. /**
  451. * Return descriptive information about this Server implementation and
  452. * the corresponding version number, in the format
  453. * <code>&lt;description&gt;/&lt;version&gt;</code>.
  454. */
  455. @Override
  456. public String getInfo() {
  457. return (info);
  458. }
  459. /**
  460. * Remove the specified Context from the set of defined Contexts for its
  461. * associated Host. If this is the last Context for this Host, the Host
  462. * will also be removed.
  463. *
  464. * @param context The Context to be removed
  465. */
  466. public synchronized void removeContext(Context context) {
  467. if( log.isDebugEnabled() )
  468. log.debug("Removing context[" + context.getPath() + "]");
  469. // Is this Context actually among those that are defined?
  470. boolean found = false;
  471. for (int i = 0; i < engines.length; i++) {
  472. Container hosts[] = engines[i].findChildren();
  473. for (int j = 0; j < hosts.length; j++) {
  474. Container contexts[] = hosts[j].findChildren();
  475. for (int k = 0; k < contexts.length; k++) {
  476. if (context == (Context) contexts[k]) {
  477. found = true;
  478. break;
  479. }
  480. }
  481. if (found)
  482. break;
  483. }
  484. if (found)
  485. break;
  486. }
  487. if (!found)
  488. return;
  489. // Remove this Context from the associated Host
  490. if( log.isDebugEnabled() )
  491. log.debug(" Removing this Context");
  492. context.getParent().removeChild(context);
  493. }
  494. /**
  495. * Remove the specified Engine from the set of defined Engines, along with
  496. * all of its related Hosts and Contexts. All associated Connectors are
  497. * also removed.
  498. *
  499. * @param engine The Engine to be removed
  500. */
  501. public synchronized void removeEngine(Engine engine) {
  502. if( log.isDebugEnabled() )
  503. log.debug("Removing engine (" + engine.getInfo() + ")");
  504. // Is the specified Engine actually defined?
  505. int j = -1;
  506. for (int i = 0; i < engines.length; i++) {
  507. if (engine == engines[i]) {
  508. j = i;
  509. break;
  510. }
  511. }
  512. if (j < 0)
  513. return;
  514. // Remove any Connector that is using this Engine
  515. if( log.isDebugEnabled() )
  516. log.debug(" Removing related Containers");
  517. while (true) {
  518. int n = -1;
  519. for (int i = 0; i < connectors.length; i++) {
  520. if (connectors[i].getService().getContainer() == engine) {
  521. n = i;
  522. break;
  523. }
  524. }
  525. if (n < 0)
  526. break;
  527. removeConnector(connectors[n]);
  528. }
  529. // Stop this Engine if necessary
  530. if( log.isDebugEnabled() )
  531. log.debug(" Stopping this Engine");
  532. try {
  533. engine.stop();
  534. } catch (LifecycleException e) {
  535. log.error("Engine.stop", e);
  536. }
  537. // Remove this Engine from our set of defined Engines
  538. if( log.isDebugEnabled() )
  539. log.debug(" Removing this Engine");
  540. int k = 0;
  541. Engine results[] = new Engine[engines.length - 1];
  542. for (int i = 0; i < engines.length; i++) {
  543. if (i != j)
  544. results[k++] = engines[i];
  545. }
  546. engines = results;
  547. }
  548. /**
  549. * Remove the specified Host, along with all of its related Contexts,
  550. * from the set of defined Hosts for its associated Engine. If this is
  551. * the last Host for this Engine, the Engine will also be removed.
  552. *
  553. * @param host The Host to be removed
  554. */
  555. public synchronized void removeHost(Host host) {
  556. if( log.isDebugEnabled() )
  557. log.debug("Removing host[" + host.getName() + "]");
  558. // Is this Host actually among those that are defined?
  559. boolean found = false;
  560. for (int i = 0; i < engines.length; i++) {
  561. Container hosts[] = engines[i].findChildren();
  562. for (int j = 0; j < hosts.length; j++) {
  563. if (host == (Host) hosts[j]) {
  564. found = true;
  565. break;
  566. }
  567. }
  568. if (found)
  569. break;
  570. }
  571. if (!found)
  572. return;
  573. // Remove this Host from the associated Engine
  574. if( log.isDebugEnabled() )
  575. log.debug(" Removing this Host");
  576. host.getParent().removeChild(host);
  577. }
  578. /*
  579. * Maps the specified login method to the specified authenticator, allowing
  580. * the mappings in org/apache/catalina/startup/Authenticators.properties
  581. * to be overridden.
  582. *
  583. * @param authenticator Authenticator to handle authentication for the
  584. * specified login method
  585. * @param loginMethod Login method that maps to the specified authenticator
  586. *
  587. * @throws IllegalArgumentException if the specified authenticator does not
  588. * implement the org.apache.catalina.Valve interface
  589. */
  590. public void addAuthenticator(Authenticator authenticator,
  591. String loginMethod) {
  592. if (!(authenticator instanceof Valve)) {
  593. throw new IllegalArgumentException(
  594. sm.getString("embedded.authenticatorNotInstanceOfValve"));
  595. }
  596. if (authenticators == null) {
  597. synchronized (this) {
  598. if (authenticators == null) {
  599. authenticators = new HashMap<String,Authenticator>();
  600. }
  601. }
  602. }
  603. authenticators.put(loginMethod, authenticator);
  604. }
  605. // ------------------------------------------------------ Lifecycle Methods
  606. /**
  607. * Add a lifecycle event listener to this component.
  608. *
  609. * @param listener The listener to add
  610. */
  611. @Override
  612. public void addLifecycleListener(LifecycleListener listener) {
  613. lifecycle.addLifecycleListener(listener);
  614. }
  615. /**
  616. * Get the lifecycle listeners associated with this lifecycle. If this
  617. * Lifecycle has no listeners registered, a zero-length array is returned.
  618. */
  619. @Override
  620. public LifecycleListener[] findLifecycleListeners() {
  621. return lifecycle.findLifecycleListeners();
  622. }
  623. /**
  624. * Remove a lifecycle event listener from this component.
  625. *
  626. * @param listener The listener to remove
  627. */
  628. @Override
  629. public void removeLifecycleListener(LifecycleListener listener) {
  630. lifecycle.removeLifecycleListener(listener);
  631. }
  632. /**
  633. * Start nested components ({@link Connector}s and {@link Engine}s) and
  634. * implement the requirements of {@link LifecycleBase#startInternal()}.
  635. *
  636. * @exception LifecycleException if this component detects a fatal error
  637. * that prevents this component from being used
  638. */
  639. @Override
  640. protected void startInternal() throws LifecycleException {
  641. if( log.isInfoEnabled() )
  642. log.info("Starting tomcat server");
  643. // Validate the setup of our required system properties
  644. initDirs();
  645. // Initialize some naming specific properties
  646. initNaming();
  647. setState(LifecycleState.STARTING);
  648. // Start our defined Engines first
  649. for (int i = 0; i < engines.length; i++) {
  650. engines[i].start();
  651. }
  652. // Start our defined Connectors second
  653. for (int i = 0; i < connectors.length; i++) {
  654. ((Lifecycle) connectors[i]).start();
  655. }
  656. }
  657. /**
  658. * Stop nested components ({@link Connector}s and {@link Engine}s) and
  659. * implement the requirements of {@link LifecycleBase#stopInternal()}.
  660. *
  661. * @exception LifecycleException if this component detects a fatal error
  662. * that needs to be reported
  663. */
  664. @Override
  665. protected void stopInternal() throws LifecycleException {
  666. if( log.isDebugEnabled() )
  667. log.debug("Stopping embedded server");
  668. fireLifecycleEvent(STOP_EVENT, null);
  669. setState(LifecycleState.STARTING);
  670. // Stop our defined Connectors first
  671. for (int i = 0; i < connectors.length; i++) {
  672. ((Lifecycle) connectors[i]).stop();
  673. }
  674. // Stop our defined Engines second
  675. for (int i = 0; i < engines.length; i++) {
  676. engines[i].stop();
  677. }
  678. }
  679. // ------------------------------------------------------ Protected Methods
  680. /** Initialize naming - this should only enable java:env and root naming.
  681. * If tomcat is embedded in an application that already defines those -
  682. * it shouldn't do it.
  683. *
  684. * XXX The 2 should be separated, you may want to enable java: but not
  685. * the initial context and the reverse
  686. * XXX Can we "guess" - i.e. lookup java: and if something is returned assume
  687. * false ?
  688. * XXX We have a major problem with the current setting for java: url
  689. */
  690. protected void initNaming() {
  691. // Setting additional variables
  692. if (!useNaming) {
  693. log.info( "Catalina naming disabled");
  694. System.setProperty("catalina.useNaming", "false");
  695. } else {
  696. System.setProperty("catalina.useNaming", "true");
  697. String value = "org.apache.naming";
  698. String oldValue =
  699. System.getProperty(javax.naming.Context.URL_PKG_PREFIXES);
  700. if (oldValue != null) {
  701. value = value + ":" + oldValue;
  702. }
  703. System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value);
  704. if( log.isDebugEnabled() )
  705. log.debug("Setting naming prefix=" + value);
  706. value = System.getProperty
  707. (javax.naming.Context.INITIAL_CONTEXT_FACTORY);
  708. if (value == null) {
  709. System.setProperty
  710. (javax.naming.Context.INITIAL_CONTEXT_FACTORY,
  711. "org.apache.naming.java.javaURLContextFactory");
  712. } else {
  713. log.debug( "INITIAL_CONTEXT_FACTORY alread set " + value );
  714. }
  715. }
  716. }
  717. protected void initDirs() {
  718. String catalinaHome = System.getProperty("catalina.home");
  719. if (catalinaHome == null) {
  720. // Backwards compatibility patch for J2EE RI 1.3
  721. String j2eeHome = System.getProperty("com.sun.enterprise.home");
  722. if (j2eeHome != null) {
  723. catalinaHome=System.getProperty("com.sun.enterprise.home");
  724. } else if (System.getProperty("catalina.base") != null) {
  725. catalinaHome = System.getProperty("catalina.base");
  726. } else {
  727. // Use IntrospectionUtils and guess the dir
  728. catalinaHome = IntrospectionUtils.guessInstall
  729. ("catalina.home", "catalina.base", "catalina.jar");
  730. if (catalinaHome == null) {
  731. catalinaHome = IntrospectionUtils.guessInstall
  732. ("tomcat.install", "catalina.home", "tomcat.jar");
  733. }
  734. }
  735. }
  736. // last resort - for minimal/embedded cases.
  737. if(catalinaHome==null) {
  738. catalinaHome=System.getProperty("user.dir");
  739. }
  740. if (catalinaHome != null) {
  741. File home = new File(catalinaHome);
  742. if (!home.isAbsolute()) {
  743. try {
  744. catalinaHome = home.getCanonicalPath();
  745. } catch (IOException e) {
  746. catalinaHome = home.getAbsolutePath();
  747. }
  748. }
  749. System.setProperty("catalina.home", catalinaHome);
  750. }
  751. if (System.getProperty("catalina.base") == null) {
  752. System.setProperty("catalina.base",
  753. catalinaHome);
  754. } else {
  755. String catalinaBase = System.getProperty("catalina.base");
  756. File base = new File(catalinaBase);
  757. if (!base.isAbsolute()) {
  758. try {
  759. catalinaBase = base.getCanonicalPath();
  760. } catch (IOException e) {
  761. catalinaBase = base.getAbsolutePath();
  762. }
  763. }
  764. System.setProperty("catalina.base", catalinaBase);
  765. }
  766. String temp = System.getProperty("java.io.tmpdir");
  767. if (temp == null || (!(new File(temp)).exists())
  768. || (!(new File(temp)).isDirectory())) {
  769. log.error(sm.getString("embedded.notmp", temp));
  770. }
  771. }
  772. protected void initStreams() {
  773. if (redirectStreams) {
  774. // Replace System.out and System.err with a custom PrintStream
  775. SystemLogHandler systemlog = new SystemLogHandler(System.out);
  776. System.setOut(systemlog);
  777. System.setErr(systemlog);
  778. }
  779. }
  780. // -------------------------------------------------------- Private Methods
  781. /**
  782. * Set the security package access/protection.
  783. */
  784. protected void setSecurityProtection(){
  785. SecurityConfig securityConfig = SecurityConfig.newInstance();
  786. securityConfig.setPackageDefinition();
  787. securityConfig.setPackageAccess();
  788. }
  789. }