📄 standardpipeline.java
字号:
lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, null);
}
private void registerValve(Valve valve) {
if( valve instanceof ValveBase &&
((ValveBase)valve).getObjectName()==null ) {
try {
String domain=((ContainerBase)container).getDomain();
if( container instanceof StandardContext ) {
domain=((StandardContext)container).getEngineName();
}
if( container instanceof StandardWrapper) {
Container ctx=((StandardWrapper)container).getParent();
domain=((StandardContext)ctx).getEngineName();
}
ObjectName vname=((ValveBase)valve).createObjectName(
domain,
((ContainerBase)container).getJmxName());
if( vname != null ) {
((ValveBase)valve).setObjectName(vname);
Registry.getRegistry(null, null).registerComponent
(valve, vname, valve.getClass().getName());
((ValveBase)valve).setController
(((ContainerBase)container).getJmxName());
}
} catch( Throwable t ) {
log.info( "Can't register valve " + valve , t );
}
}
}
private void unregisterValve(Valve valve) {
if( valve instanceof ValveBase ) {
try {
ValveBase vb=(ValveBase)valve;
if( vb.getController()!=null &&
vb.getController() ==
((ContainerBase)container).getJmxName() ) {
ObjectName vname=vb.getObjectName();
Registry.getRegistry(null, null).getMBeanServer()
.unregisterMBean(vname);
((ValveBase)valve).setObjectName(null);
}
} catch( Throwable t ) {
log.info( "Can't unregister valve " + valve , t );
}
}
}
// ------------------------------------------------------- Pipeline Methods
/**
* <p>Return the Valve instance that has been distinguished as the basic
* Valve for this Pipeline (if any).
*/
public Valve getBasic() {
return (this.basic);
}
/**
* <p>Set the Valve instance that has been distinguished as the basic
* Valve for this Pipeline (if any). Prioer to setting the basic Valve,
* the Valve's <code>setContainer()</code> will be called, if it
* implements <code>Contained</code>, with the owning Container as an
* argument. The method may throw an <code>IllegalArgumentException</code>
* if this Valve chooses not to be associated with this Container, or
* <code>IllegalStateException</code> if it is already associated with
* a different Container.</p>
*
* @param valve Valve to be distinguished as the basic Valve
*/
public void setBasic(Valve valve) {
// Change components if necessary
Valve oldBasic = this.basic;
if (oldBasic == valve)
return;
// Stop the old component if necessary
if (oldBasic != null) {
if (started && (oldBasic instanceof Lifecycle)) {
try {
((Lifecycle) oldBasic).stop();
} catch (LifecycleException e) {
log.error("StandardPipeline.setBasic: stop", e);
}
}
if (oldBasic instanceof Contained) {
try {
((Contained) oldBasic).setContainer(null);
} catch (Throwable t) {
;
}
}
}
// Start the new component if necessary
if (valve == null)
return;
if (valve instanceof Contained) {
((Contained) valve).setContainer(this.container);
}
if (valve instanceof Lifecycle) {
try {
((Lifecycle) valve).start();
} catch (LifecycleException e) {
log.error("StandardPipeline.setBasic: start", e);
return;
}
}
this.basic = valve;
}
/**
* <p>Add a new Valve to the end of the pipeline associated with this
* Container. Prior to adding the Valve, the Valve's
* <code>setContainer()</code> method will be called, if it implements
* <code>Contained</code>, with the owning Container as an argument.
* The method may throw an
* <code>IllegalArgumentException</code> if this Valve chooses not to
* be associated with this Container, or <code>IllegalStateException</code>
* if it is already associated with a different Container.</p>
*
* @param valve Valve to be added
*
* @exception IllegalArgumentException if this Container refused to
* accept the specified Valve
* @exception IllegalArgumentException if the specifie Valve refuses to be
* associated with this Container
* @exception IllegalStateException if the specified Valve is already
* associated with a different Container
*/
public void addValve(Valve valve) {
// Validate that we can add this Valve
if (valve instanceof Contained)
((Contained) valve).setContainer(this.container);
// Start the new component if necessary
if (started) {
if (valve instanceof Lifecycle) {
try {
((Lifecycle) valve).start();
} catch (LifecycleException e) {
log.error("StandardPipeline.addValve: start: ", e);
}
}
// Register the newly added valve
registerValve(valve);
}
// Add this Valve to the set associated with this Pipeline
synchronized (valves) {
Valve results[] = new Valve[valves.length +1];
System.arraycopy(valves, 0, results, 0, valves.length);
results[valves.length] = valve;
valves = results;
}
}
/**
* Return the set of Valves in the pipeline associated with this
* Container, including the basic Valve (if any). If there are no
* such Valves, a zero-length array is returned.
*/
public Valve[] getValves() {
if (basic == null)
return (valves);
synchronized (valves) {
Valve results[] = new Valve[valves.length + 1];
System.arraycopy(valves, 0, results, 0, valves.length);
results[valves.length] = basic;
return (results);
}
}
public ObjectName[] getValveObjectNames() {
ObjectName oname[]=new ObjectName[valves.length + 1];
for( int i=0; i<valves.length; i++ ) {
if( valves[i] instanceof ValveBase )
oname[i]=((ValveBase)valves[i]).getObjectName();
}
if( basic instanceof ValveBase )
oname[valves.length]=((ValveBase)basic).getObjectName();
return oname;
}
/**
* Cause the specified request and response to be processed by the Valves
* associated with this pipeline, until one of these valves causes the
* response to be created and returned. The implementation must ensure
* that multiple simultaneous requests (on different threads) can be
* processed through the same Pipeline without interfering with each
* other's control flow.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception is thrown
*/
public void invoke(Request request, Response response)
throws IOException, ServletException {
// Invoke the first Valve in this pipeline for this request
//(new StandardPipelineValveContext()).invokeNext(request, response);
StandardValveContext valveContext =
(StandardValveContext) request.getValveContext();
if (valveContext == null) {
valveContext = new StandardValveContext();
request.setValveContext(valveContext);
}
valveContext.set(basic, valves);
valveContext.invokeNext(request, response);
valveContext.set(null, null);
}
/**
* Remove the specified Valve from the pipeline associated with this
* Container, if it is found; otherwise, do nothing. If the Valve is
* found and removed, the Valve's <code>setContainer(null)</code> method
* will be called if it implements <code>Contained</code>.
*
* @param valve Valve to be removed
*/
public void removeValve(Valve valve) {
synchronized (valves) {
// Locate this Valve in our list
int j = -1;
for (int i = 0; i < valves.length; i++) {
if (valve == valves[i]) {
j = i;
break;
}
}
if (j < 0)
return;
// Remove this valve from our list
Valve results[] = new Valve[valves.length - 1];
int n = 0;
for (int i = 0; i < valves.length; i++) {
if (i == j)
continue;
results[n++] = valves[i];
}
valves = results;
try {
if (valve instanceof Contained)
((Contained) valve).setContainer(null);
} catch (Throwable t) {
;
}
}
// Stop this valve if necessary
if (started) {
if (valve instanceof Lifecycle) {
try {
((Lifecycle) valve).stop();
} catch (LifecycleException e) {
log.error("StandardPipeline.removeValve: stop: ", e);
}
}
// Unregister the removed valave
unregisterValve(valve);
}
}
// ------------------------------------------------------ Protected Methods
/**
* Log a message on the Logger associated with our Container (if any).
*
* @param message Message to be logged
*/
protected void log(String message) {
Logger logger = null;
if (container != null)
logger = container.getLogger();
if (logger != null)
logger.log("StandardPipeline[" + container.getName() + "]: " +
message);
else
System.out.println("StandardPipeline[" + container.getName() +
"]: " + message);
}
/**
* Log a message on the Logger associated with our Container (if any).
*
* @param message Message to be logged
* @param throwable Associated exception
*/
protected void log(String message, Throwable throwable) {
Logger logger = null;
if (container != null)
logger = container.getLogger();
if (logger != null)
logger.log("StandardPipeline[" + container.getName() + "]: " +
message, throwable);
else {
System.out.println("StandardPipeline[" + container.getName() +
"]: " + message);
throwable.printStackTrace(System.out);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -