📄 embedded.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.startup;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import org.apache.catalina.Authenticator;
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Loader;
import org.apache.catalina.Realm;
import org.apache.catalina.Valve;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.core.StandardService;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.security.SecurityConfig;
import org.apache.catalina.util.LifecycleSupport;
import org.apache.catalina.util.StringManager;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.IntrospectionUtils;
import org.apache.tomcat.util.log.SystemLogHandler;
/**
* Convenience class to embed a Catalina servlet container environment
* inside another application. You must call the methods of this class in the
* following order to ensure correct operation.
*
* <ul>
* <li>Instantiate a new instance of this class.</li>
* <li>Set the relevant properties of this object itself. In particular,
* you will want to establish the default Logger to be used, as well
* as the default Realm if you are using container-managed security.</li>
* <li>Call <code>createEngine()</code> to create an Engine object, and then
* call its property setters as desired.</li>
* <li>Call <code>createHost()</code> to create at least one virtual Host
* associated with the newly created Engine, and then call its property
* setters as desired. After you customize this Host, add it to the
* corresponding Engine with <code>engine.addChild(host)</code>.</li>
* <li>Call <code>createContext()</code> to create at least one Context
* associated with each newly created Host, and then call its property
* setters as desired. You <strong>SHOULD</strong> create a Context with
* a pathname equal to a zero-length string, which will be used to process
* all requests not mapped to some other Context. After you customize
* this Context, add it to the corresponding Host with
* <code>host.addChild(context)</code>.</li>
* <li>Call <code>addEngine()</code> to attach this Engine to the set of
* defined Engines for this object.</li>
* <li>Call <code>createConnector()</code> to create at least one TCP/IP
* connector, and then call its property setters as desired.</li>
* <li>Call <code>addConnector()</code> to attach this Connector to the set
* of defined Connectors for this object. The added Connector will use
* the most recently added Engine to process its received requests.</li>
* <li>Repeat the above series of steps as often as required (although there
* will typically be only one Engine instance created).</li>
* <li>Call <code>start()</code> to initiate normal operations of all the
* attached components.</li>
* </ul>
*
* After normal operations have begun, you can add and remove Connectors,
* Engines, Hosts, and Contexts on the fly. However, once you have removed
* a particular component, it must be thrown away -- you can create a new one
* with the same characteristics if you merely want to do a restart.
* <p>
* To initiate a normal shutdown, call the <code>stop()</code> method of
* this object.
* <p>
* @see org.apache.catalina.startup.Catalina#main For a complete example
* of how Tomcat is set up and launched as an Embedded application.
*
* @author Craig R. McClanahan
* @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
*/
public class Embedded extends StandardService implements Lifecycle {
private static Log log = LogFactory.getLog(Embedded.class);
// ----------------------------------------------------------- Constructors
/**
* Construct a new instance of this class with default properties.
*/
public Embedded() {
this(null);
}
/**
* Construct a new instance of this class with specified properties.
*
* @param realm Realm implementation to be inherited by all components
* (unless overridden further down the container hierarchy)
*/
public Embedded(Realm realm) {
super();
setRealm(realm);
setSecurityProtection();
}
// ----------------------------------------------------- Instance Variables
/**
* Is naming enabled ?
*/
protected boolean useNaming = true;
/**
* Is standard streams redirection enabled ?
*/
protected boolean redirectStreams = true;
/**
* The set of Engines that have been deployed in this server. Normally
* there will only be one.
*/
protected Engine engines[] = new Engine[0];
/**
* Custom mappings of login methods to authenticators
*/
protected HashMap authenticators;
/**
* Descriptive information about this server implementation.
*/
protected static final String info =
"org.apache.catalina.startup.Embedded/1.0";
/**
* The lifecycle event support for this component.
*/
protected LifecycleSupport lifecycle = new LifecycleSupport(this);
/**
* The default realm to be used by all containers associated with
* this compoennt.
*/
protected Realm realm = null;
/**
* The string manager for this package.
*/
protected static StringManager sm =
StringManager.getManager(Constants.Package);
/**
* Has this component been started yet?
*/
protected boolean started = false;
/**
* Use await.
*/
protected boolean await = false;
// ------------------------------------------------------------- Properties
/**
* Return true if naming is enabled.
*/
public boolean isUseNaming() {
return (this.useNaming);
}
/**
* Enables or disables naming support.
*
* @param useNaming The new use naming value
*/
public void setUseNaming(boolean useNaming) {
boolean oldUseNaming = this.useNaming;
this.useNaming = useNaming;
support.firePropertyChange("useNaming", new Boolean(oldUseNaming),
new Boolean(this.useNaming));
}
/**
* Return true if redirction of standard streams is enabled.
*/
public boolean isRedirectStreams() {
return (this.redirectStreams);
}
/**
* Enables or disables naming support.
*
* @param useNaming The new use naming value
*/
public void setRedirectStreams(boolean redirectStreams) {
boolean oldRedirectStreams = this.redirectStreams;
this.redirectStreams = redirectStreams;
support.firePropertyChange("redirectStreams", new Boolean(oldRedirectStreams),
new Boolean(this.redirectStreams));
}
/**
* Return the default Realm for our Containers.
*/
public Realm getRealm() {
return (this.realm);
}
/**
* Set the default Realm for our Containers.
*
* @param realm The new default realm
*/
public void setRealm(Realm realm) {
Realm oldRealm = this.realm;
this.realm = realm;
support.firePropertyChange("realm", oldRealm, this.realm);
}
public void setAwait(boolean b) {
await = b;
}
public boolean isAwait() {
return await;
}
public void setCatalinaHome( String s ) {
System.setProperty( "catalina.home", s);
}
public void setCatalinaBase( String s ) {
System.setProperty( "catalina.base", s);
}
public String getCatalinaHome() {
return System.getProperty("catalina.home");
}
public String getCatalinaBase() {
return System.getProperty("catalina.base");
}
// --------------------------------------------------------- Public Methods
/**
* Add a new Connector to the set of defined Connectors. The newly
* added Connector will be associated with the most recently added Engine.
*
* @param connector The connector to be added
*
* @exception IllegalStateException if no engines have been added yet
*/
public synchronized void addConnector(Connector connector) {
if( log.isDebugEnabled() ) {
log.debug("Adding connector (" + connector.getInfo() + ")");
}
// Make sure we have a Container to send requests to
if (engines.length < 1)
throw new IllegalStateException
(sm.getString("embedded.noEngines"));
/*
* Add the connector. This will set the connector's container to the
* most recently added Engine
*/
super.addConnector(connector);
}
/**
* Add a new Engine to the set of defined Engines.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -