samedi 21 octobre 2017

Why Tomcat Context implements Pipeline interface and also contains a Pipeline field?

I am reading the book "How Tomcat Works". In Chapter 5, the SimpleContext implements Context and Pipeline interface. But at the same time SimpleContext holds a Pipeline field (SimplePipeline implements Pipeline interface), and use this to construct it.

// Context implements Pipeline interface
public class SimpleContext implements Context, Pipeline { 
    public SimpleContext() {
      pipeline.setBasic(new SimpleContextValve());
    }
    protected HashMap children = new HashMap();
    protected Loader loader = null;

    // also contains a Pipeline field, and use "this" to construct it
    protected SimplePipeline pipeline = new SimplePipeline(this); 

    protected HashMap servletMappings = new HashMap();
    protected Mapper mapper = null;
    protected HashMap mappers = new HashMap();
    private Container parent = null;

    // remainder omitted ...
}

SimplePipeline also holds the reference of Context with which this Pipeline is associated.

public class SimplePipeline implements Pipeline {

  public SimplePipeline(Container container) {
    setContainer(container);
  }

  protected Valve basic = null;

  // The Container with which this Pipeline is associated.
  protected Container container = null;

  protected Valve valves[] = new Valve[0];

  // remainder omitted ...
}

If I was the author, I will either let SimpleContext implements Pipeline interface, or keep Pipeline as a member field of Context, not both of them.

I know that maybe author do this for some "specific reason". Any one can tell me that reason?




Aucun commentaire:

Enregistrer un commentaire