/** * Get the {@link Container} with which this instance is associated. * * @return The Container with which this instance is associated or * <code>null</code> if not associated with a Container */ Container getContainer();
/** * Set the <code>Container</code> with which this instance is associated. * * @param container The Container instance with which this instance is to * be associated, or <code>null</code> to disassociate this instance * from any Container */ void setContainer(Container container); }
public abstract class ValveBase extends LifecycleMBeanBase implements Contained, Valve { // 国际化管理器,可以支持多国语言 protected static final StringManager sm = StringManager.getManager(ValveBase.class);
// -------------------- JMX and Registration --------------------
// 设置获取MBean对象的keyProperties,格式如:a=b,c=d,e=f... @Override public String getObjectNameKeyProperties() { StringBuilder name = new StringBuilder("type=Valve");
Container container = getContainer();
name.append(container.getMBeanKeyProperties());
int seq = 0;
// Pipeline may not be present in unit testing Pipeline p = container.getPipeline(); if (p != null) { for (Valve valve : p.getValves()) { // Skip null valves if (valve == null) { continue; } // Only compare valves in pipeline until we find this valve if (valve == this) { break; } if (valve.getClass() == this.getClass()) { // Duplicate valve earlier in pipeline // increment sequence number seq ++; } } }
if (seq > 0) { name.append(",seq="); name.append(seq); }
String className = this.getClass().getName(); int period = className.lastIndexOf('.'); if (period >= 0) { className = className.substring(period + 1); } name.append(",name="); name.append(className);
return name.toString(); } // 获取所属域,从container获取 @Override public String getDomainInternal() { Container c = getContainer(); if (c == null) { return null; } else { return c.getDomain(); } } }
// 开始逻辑,调用所有阀门的start方法 @Override protected synchronized void startInternal() throws LifecycleException { // Start the Valves in our pipeline (including the basic), if any Valve current = first; if (current == null) { current = basic; } while (current != null) { if (current instanceof Lifecycle) ((Lifecycle) current).start(); current = current.getNext(); }
// Stop the Valves in our pipeline (including the basic), if any Valve current = first; if (current == null) { current = basic; } while (current != null) { if (current instanceof Lifecycle) ((Lifecycle) current).stop(); current = current.getNext(); } }
// 获取基础阀门 @Override public Valve getBasic() { return (this.basic); }
// 设置基础阀门 @Override public void setBasic(Valve valve) { // Change components if necessary Valve oldBasic = this.basic; if (oldBasic == valve) return;
// Stop the old component if necessary // 老的基础阀门会被调用stop方法且所属容器置为null if (oldBasic != null) { if (getState().isAvailable() && (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) { ExceptionUtils.handleThrowable(t); } } }
// Start the new component if necessary // 新的阀门会设置所属容器,并调用start方法 if (valve == null) return; if (valve instanceof Contained) { ((Contained) valve).setContainer(this.container); } if (getState().isAvailable() && valve instanceof Lifecycle) { try { ((Lifecycle) valve).start(); } catch (LifecycleException e) { log.error("StandardPipeline.setBasic: start", e); return; } }
// Update the pipeline // 替换pipeline中的基础阀门,就是讲基础阀门的前一个阀门的next指向当前阀门 Valve current = first; while (current != null) { if (current.getNext() == oldBasic) { current.setNext(valve); break; } current = current.getNext(); }
this.basic = valve; }
// 添加阀门 @Override 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 // 调用阀门的start方法 if (getState().isAvailable()) { if (valve instanceof Lifecycle) { try { ((Lifecycle) valve).start(); } catch (LifecycleException e) { log.error("StandardPipeline.addValve: start: ", e); } } }
// Add this Valve to the set associated with this Pipeline // 设置阀门,将阀门添加到基础阀门的前一个 if (first == null) { first = valve; valve.setNext(basic); } else { Valve current = first; while (current != null) { if (current.getNext() == basic) { current.setNext(valve); valve.setNext(basic); break; } current = current.getNext(); } }
// 获取阀门数组 @Override public Valve[] getValves() { ArrayList<Valve> valveList = new ArrayList<>(); Valve current = first; if (current == null) { current = basic; } while (current != null) { valveList.add(current); current = current.getNext(); }
return valveList.toArray(new Valve[0]); }
// JMX方法,在此忽略 public ObjectName[] getValveObjectNames() { ArrayList<ObjectName> valveList = new ArrayList<>(); Valve current = first; if (current == null) { current = basic; } while (current != null) { if (current instanceof JmxEnabled) { valveList.add(((JmxEnabled) current).getObjectName()); } current = current.getNext(); }
return valveList.toArray(new ObjectName[0]); }
// 移除阀门 @Override public void removeValve(Valve valve) { Valve current; if(first == valve) { // 如果待移出的阀门是首个阀门,则首个阀门的下一个阀门变成首个阀门 first = first.getNext(); current = null; } else { current = first; } // 遍历阀门集合,并进行移除 while (current != null) { if (current.getNext() == valve) { current.setNext(valve.getNext()); break; } current = current.getNext(); }
if (first == basic) first = null;
// 设置阀门所属容器为null if (valve instanceof Contained) ((Contained) valve).setContainer(null);