📄 connector.java
字号:
/**
* Return the true if the entity body encoding should be used for the URI.
*/
public boolean getUseBodyEncodingForURI() {
return (this.useBodyEncodingForURI);
}
/**
* Set if the entity body encoding should be used for the URI.
*
* @param useBodyEncodingForURI The new value for the flag.
*/
public void setUseBodyEncodingForURI(boolean useBodyEncodingForURI) {
this.useBodyEncodingForURI = useBodyEncodingForURI;
setProperty
("useBodyEncodingForURI", String.valueOf(useBodyEncodingForURI));
}
/**
* Indicates whether the generation of an X-Powered-By response header for
* servlet-generated responses is enabled or disabled for this Connector.
*
* @return true if generation of X-Powered-By response header is enabled,
* false otherwise
*/
public boolean getXpoweredBy() {
return xpoweredBy;
}
/**
* Enables or disables the generation of an X-Powered-By header (with value
* Servlet/2.4) for all servlet-generated responses returned by this
* Connector.
*
* @param xpoweredBy true if generation of X-Powered-By response header is
* to be enabled, false otherwise
*/
public void setXpoweredBy(boolean xpoweredBy) {
this.xpoweredBy = xpoweredBy;
setProperty("xpoweredBy", String.valueOf(xpoweredBy));
}
/**
* Enable the use of IP-based virtual hosting.
*
* @param useIPVHosts <code>true</code> if Hosts are identified by IP,
* <code>false/code> if Hosts are identified by name.
*/
public void setUseIPVHosts(boolean useIPVHosts) {
this.useIPVHosts = useIPVHosts;
setProperty("useIPVHosts", String.valueOf(useIPVHosts));
}
/**
* Test if IP-based virtual hosting is enabled.
*/
public boolean getUseIPVHosts() {
return useIPVHosts;
}
// --------------------------------------------------------- Public Methods
/**
* Create (or allocate) and return a Request object suitable for
* specifying the contents of a Request to the responsible Container.
*/
public Request createRequest() {
Request request = new Request();
request.setConnector(this);
return (request);
}
/**
* Create (or allocate) and return a Response object suitable for
* receiving the contents of a Response from the responsible Container.
*/
public Response createResponse() {
Response response = new Response();
response.setConnector(this);
return (response);
}
// ------------------------------------------------------ Lifecycle Methods
/**
* Add a lifecycle event listener to this component.
*
* @param listener The listener to add
*/
public void addLifecycleListener(LifecycleListener listener) {
lifecycle.addLifecycleListener(listener);
}
/**
* Get the lifecycle listeners associated with this lifecycle. If this
* Lifecycle has no listeners registered, a zero-length array is returned.
*/
public LifecycleListener[] findLifecycleListeners() {
return lifecycle.findLifecycleListeners();
}
/**
* Remove a lifecycle event listener from this component.
*
* @param listener The listener to add
*/
public void removeLifecycleListener(LifecycleListener listener) {
lifecycle.removeLifecycleListener(listener);
}
protected ObjectName createObjectName(String domain, String type)
throws MalformedObjectNameException {
String encodedAddr = null;
if (getProperty("address") != null) {
encodedAddr = URLEncoder.encode(getProperty("address").toString());
}
String addSuffix = (getProperty("address") == null) ? "" : ",address="
+ encodedAddr;
ObjectName _oname = new ObjectName(domain + ":type=" + type + ",port="
+ getPort() + addSuffix);
return _oname;
}
/**
* Initialize this connector (create ServerSocket here!)
*/
public void initialize()
throws LifecycleException
{
if (initialized) {
if(log.isInfoEnabled())
log.info(sm.getString("coyoteConnector.alreadyInitialized"));
return;
}
this.initialized = true;
if( oname == null && (container instanceof StandardEngine)) {
try {
// we are loaded directly, via API - and no name was given to us
StandardEngine cb=(StandardEngine)container;
oname = createObjectName(cb.getName(), "Connector");
Registry.getRegistry(null, null)
.registerComponent(this, oname, null);
controller=oname;
} catch (Exception e) {
log.error( "Error registering connector ", e);
}
if(log.isDebugEnabled())
log.debug("Creating name for connector " + oname);
}
// Initializa adapter
adapter = new CoyoteAdapter(this);
protocolHandler.setAdapter(adapter);
IntrospectionUtils.setProperty(protocolHandler, "jkHome",
System.getProperty("catalina.base"));
try {
protocolHandler.init();
} catch (Exception e) {
throw new LifecycleException
(sm.getString
("coyoteConnector.protocolHandlerInitializationFailed", e));
}
}
/**
* Pause the connector.
*/
public void pause()
throws LifecycleException {
try {
protocolHandler.pause();
} catch (Exception e) {
log.error(sm.getString
("coyoteConnector.protocolHandlerPauseFailed"), e);
}
}
/**
* Pause the connector.
*/
public void resume()
throws LifecycleException {
try {
protocolHandler.resume();
} catch (Exception e) {
log.error(sm.getString
("coyoteConnector.protocolHandlerResumeFailed"), e);
}
}
/**
* Begin processing requests via this Connector.
*
* @exception LifecycleException if a fatal startup error occurs
*/
public void start() throws LifecycleException {
if( !initialized )
initialize();
// Validate and update our current state
if (started ) {
if(log.isInfoEnabled())
log.info(sm.getString("coyoteConnector.alreadyStarted"));
return;
}
lifecycle.fireLifecycleEvent(START_EVENT, null);
started = true;
// We can't register earlier - the JMX registration of this happens
// in Server.start callback
if ( this.oname != null ) {
// We are registred - register the adapter as well.
try {
Registry.getRegistry(null, null).registerComponent
(protocolHandler, createObjectName(this.domain,"ProtocolHandler"), null);
} catch (Exception ex) {
log.error(sm.getString
("coyoteConnector.protocolRegistrationFailed"), ex);
}
} else {
if(log.isInfoEnabled())
log.info(sm.getString
("coyoteConnector.cannotRegisterProtocol"));
}
try {
protocolHandler.start();
} catch (Exception e) {
String errPrefix = "";
if(this.service != null) {
errPrefix += "service.getName(): \"" + this.service.getName() + "\"; ";
}
throw new LifecycleException
(errPrefix + " " + sm.getString
("coyoteConnector.protocolHandlerStartFailed", e));
}
if( this.domain != null ) {
mapperListener.setDomain( domain );
//mapperListener.setEngine( service.getContainer().getName() );
mapperListener.init();
try {
ObjectName mapperOname = createObjectName(this.domain,"Mapper");
if (log.isDebugEnabled())
log.debug(sm.getString(
"coyoteConnector.MapperRegistration", mapperOname));
Registry.getRegistry(null, null).registerComponent
(mapper, mapperOname, "Mapper");
} catch (Exception ex) {
log.error(sm.getString
("coyoteConnector.protocolRegistrationFailed"), ex);
}
}
}
/**
* Terminate processing requests via this Connector.
*
* @exception LifecycleException if a fatal shutdown error occurs
*/
public void stop() throws LifecycleException {
// Validate and update our current state
if (!started) {
log.error(sm.getString("coyoteConnector.notStarted"));
return;
}
lifecycle.fireLifecycleEvent(STOP_EVENT, null);
started = false;
try {
mapperListener.destroy();
Registry.getRegistry(null, null).unregisterComponent
(createObjectName(this.domain,"Mapper"));
Registry.getRegistry(null, null).unregisterComponent
(createObjectName(this.domain,"ProtocolHandler"));
} catch (MalformedObjectNameException e) {
log.error( sm.getString
("coyoteConnector.protocolUnregistrationFailed"), e);
}
try {
protocolHandler.destroy();
} catch (Exception e) {
throw new LifecycleException
(sm.getString
("coyoteConnector.protocolHandlerDestroyFailed", e));
}
}
// -------------------- JMX registration --------------------
protected String domain;
protected ObjectName oname;
protected MBeanServer mserver;
ObjectName controller;
public ObjectName getController() {
return controller;
}
public void setController(ObjectName controller) {
this.controller = controller;
}
public ObjectName getObjectName() {
return oname;
}
public String getDomain() {
return domain;
}
public ObjectName preRegister(MBeanServer server,
ObjectName name) throws Exception {
oname=name;
mserver=server;
domain=name.getDomain();
return name;
}
public void postRegister(Boolean registrationDone) {
}
public void preDeregister() throws Exception {
}
public void postDeregister() {
try {
if( started ) {
stop();
}
} catch( Throwable t ) {
log.error( "Unregistering - can't stop", t);
}
}
protected void findContainer() {
try {
// Register to the service
ObjectName parentName=new ObjectName( domain + ":" +
"type=Service");
if(log.isDebugEnabled())
log.debug("Adding to " + parentName );
if( mserver.isRegistered(parentName )) {
mserver.invoke(parentName, "addConnector", new Object[] { this },
new String[] {"org.apache.catalina.connector.Connector"});
// As a side effect we'll get the container field set
// Also initialize will be called
//return;
}
// XXX Go directly to the Engine
// initialize(); - is called by addConnector
ObjectName engName=new ObjectName( domain + ":" + "type=Engine");
if( mserver.isRegistered(engName )) {
Object obj=mserver.getAttribute(engName, "managedResource");
if(log.isDebugEnabled())
log.debug("Found engine " + obj + " " + obj.getClass());
container=(Container)obj;
// Internal initialize - we now have the Engine
initialize();
if(log.isDebugEnabled())
log.debug("Initialized");
// As a side effect we'll get the container field set
// Also initialize will be called
return;
}
} catch( Exception ex ) {
log.error( "Error finding container " + ex);
}
}
public void init() throws Exception {
if( this.getService() != null ) {
if(log.isDebugEnabled())
log.debug( "Already configured" );
return;
}
if( container==null ) {
findContainer();
}
}
public void destroy() throws Exception {
if( oname!=null && controller==oname ) {
if(log.isDebugEnabled())
log.debug("Unregister itself " + oname );
Registry.getRegistry(null, null).unregisterComponent(oname);
}
if( getService() == null)
return;
getService().removeConnector(this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -