您的位置:首页 > 大数据 > 人工智能

知识库--StandardService+Container+Connectors(64)

2016-12-29 08:44 204 查看
Container and Connectors

A StandardService instance contains two types of components: a container and one or more connectors. Being able to have multiple connectors enable Tomcat to service multiple protocols. One connector can be used to service HTTP requests, and another for servicing HTTPS requests.

The StandardService class uses the container variable as an object reference for the Container instance and the connectors array for all the connectors.

private Container container = null;
private Connector connector[] = new Connector[0];


To associate a container with this service, you use the setContainer method, which is given following:

public void setContainer(Container container){
Container oldContainer = this.container;
if((oldContainer != null) &&(oldContainer instanceof Engine))//消除原关联
((Engine)oldContainer).setService(null);

this.container = container;
if((this.container != null) && (this.container instanceof Engine))//建立新的关联关系
((Engine)this.container).setService(this);
if(started && (this.container != null) && (this.container instanceof Lifecyle)){//start()
try{
((Lifecycle)this.container).start();
}catch(LifecycleException  e){
;
}
}
synchronized(connectors){//建立连接
for(int i = 0; i < connectors.length; i++){
connectors[i].setContainer(this.container);
}
}
if(started && (oldContainer != nul) && (oldContainer instanceof Lifecycle)){//关闭历史容器
try{
((Lifecycle)oldContainer).stop();
}catch(LifecycleException e){
;
}
}
//report this property change to interested listeners
support.firePropertyChange("container",oldContainer,this.container);//通知事件
}


The container associated with this service will be passed to the setContainer method on each Connector object that is available(数组元素–引用) in this service to create the association between the container and each individual connector.

To add a connector to a Service object, use the addConnector method. To remove a connector, call the removeConnector method.

public void addConnector(Connector connector){
synchronized(connectors){
connector.setContainer(this.container);
connector.setService(this);
Connector results[] = new Connector[connectors.length + 1];
System.arraycopy(connectoes,0,results,0,connector.length);
results[connector.length] = connector;
connectors = results;
//初始化
if(initialized){
try{
connector.initialize();
}catch(LifecycleException e){
e.printStackTrace(System.err);
}
}

if(started && (connector instanceof Lifecyle)){
try{
((Lifecycle)connector).start();
}catch(LifecycleException e){;}
}
//Report this property change to interested listeners 原对象--改变后的对象
support.firePropertyChange("connector",null,connector);
}
}


//remove connector

public void removeConnector(Connector connector){
synchronized(connectors){
int j = -1;
for(int i = 0; i < connectors.length;i++){
if(connector == connectors[i]){
j = i;
break;
}
}
if(j < 0)
return;
if(started && (connectors[j] instanceof Lifecycle)){
try{
((Lifecycle)connectors[j]).stop();
}catch(LifecycleException e){
;
}
}
connectors[j].setContainer(null);
connector.setService(null);

int k = 0;
Connector result[] = new Connector[connectors.length - 1];
for(int i = 0; i< connectors.length; i++){
if(i!=j)
results[k++] = connectors[i];
}
connectors = results;

//report this property change to interested listeners
support.firePropertyChange("connector",connector,null);
}
}


Have your attention :

addConnector method initializes and starts the added connector.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tomcat