2016-12-28 11 views
0

아래와 같이 server.xml에 connector 태그가 설정되어 있습니다.Java 코드에서 server.xml의 구성된 커넥터 태그에 액세스하기

<Connector port="8080" bindOnInit="false" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> 

나는 자바 코드를 사용하여 런타임에 bindOnInit 매개 변수의 값을 읽고 싶어. XML을 구문 분석하고 싶지 않습니다. 내가 검색 한 결과 데이터를 얻을 수있는 것으로 나타났습니다 Tomcat tomcat = new Tomcat(); 바람둥이 개체. 새로운 바람둥이 객체를 생성하면 설정 값이 부여되지 않으므로 새 바람둥이 객체를 생성하는 대신 기존의 바람둥이 객체를 어떻게 얻을 수 있습니까?

bindOnInit 매개 변수는 자바 코드를 사용하여 런타임에 런타임에 참조하십시오.

미리 감사드립니다.

+0

이 값을 원하십니까? –

+0

이 값을 읽는 법을 알고 싶습니다. –

+0

bindOnInit = "false"일 때 Connector가 어떻게 작동하는지 알면 나에게 설명 할 수있다. –

답변

0

MBeanServer를 사용하여 org.apache.catalina.server.StandardServer을 복구해야합니다.

import java.lang.management.ManagementFactory; 
import java.util.Collection; 
import java.util.HashSet; 

import javax.management.MBeanServer; 
import javax.management.ObjectName; 
import org.apache.catalina.Service; 
import org.apache.catalina.connector.Connector; 
import org.apache.catalina.core.StandardServer; 
import org.apache.log4j.Logger; 

public class TomcatConnectors { 

    public static final String CATALINA_SERVICE_NAME = "Catalina"; 

    public static final String CONNECTOR_HTTP_PROTOCOL_NAME = "HTTP/1.1"; 

    private Logger logger = Logger.getLogger(this.getClass()); 

    private Collection<Connector> connectors; 

    /** 
    * 
    */ 
    public TomcatConnectors() { 
     super(); 
     this.connectors = new HashSet<Connector>(); 
     this.loadConnectors(); 
    } 

    /** 
    * 
    * @return 
    */ 
    protected StandardServer getServerInstance(){ 
     org.apache.catalina.core.StandardServer server = null; 
     try{ 
      MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); 
      server = (StandardServer)mbeanServer.getAttribute(
         new ObjectName("Catalina:type=Server"), 
         "managedResource" 
        ); 
      if(logger.isDebugEnabled()){ 
       logger.debug("Server found. Info: "); 
       logger.debug(" - address   : " + server.getAddress()); 
       logger.debug(" - domain   : " + server.getDomain()); 
       logger.debug(" - info    : " + server.getInfo()); 
       logger.debug(" - shutdown port : " + server.getPort()); 
       logger.debug(" - shutdown command : " + server.getShutdown()); 
       logger.debug(" - serverInfo  : " + server.getServerInfo()); 
       logger.debug(" - status   : " + server.getStateName()); 

      }    

     }catch(Throwable t){ 
      logger.fatal("Fatal Error Recovering StandardServer from MBeanServer : " + t.getClass().getName() + ": " + t.getMessage(), t); 
     } 
     return server; 
    } 

    /* 
    * 
    */ 
    protected Service getCatalinaService(){ 
     org.apache.catalina.core.StandardServer server = this.getServerInstance(); 
     Service[] services = server.findServices(); 
     for(Service aService : services){ 
      if(logger.isDebugEnabled()){ 
       logger.debug("Service: " + aService.getName() + 
         ", info: " + aService.getInfo() + 
         ", state: " + aService.getStateName()); 
      } 

      if(aService.getName().equalsIgnoreCase(CATALINA_SERVICE_NAME)){ 
       return aService;     
      } 
     } 
     return null; 
    } 

    protected void loadConnectors() { 
     Service catalinaService = this.getCatalinaService(); 
     if(catalinaService == null){ 
      throw new IllegalStateException("Service Catalina cannot be null"); 
     } 
     if(catalinaService.findConnectors() != null && catalinaService.findConnectors().length > 0){ 
      logger.debug("List of connectors: "); 
      for(Connector aConnector : catalinaService.findConnectors()){ 
       if(logger.isDebugEnabled()){ 
        logger.debug("Connector.getProtocol: " + aConnector.getProtocol()); 
        logger.debug("Connector.getPort: " + aConnector.getPort()); 
        logger.debug("Connector.getInfo: " + aConnector.getInfo()); 
        logger.debug("Connector.getStateName: " + aConnector.getStateName()); 
        logger.debug("Connector.property.bindOnInit: " + aConnector.getProperty("bindOnInit")); 
        logger.debug("Connector.attribute.bindOnInit: " + aConnector.getAttribute("bindOnInit")); 
        logger.debug("Connector.getState: " + aConnector.getState()); 
       } 
       this.connectors.add(aConnector); 
      } 
     } 
    } 

    /** 
    * @return the connectors 
    */ 
    public Collection<Connector> getConnectors() { 
     if(this.connectors.isEmpty()){ 
      this.loadConnectors(); 
     } 
     return connectors; 
    } 
} 

나는이 커넥터 구성을 테스트 한 :이 모든 톰캣 시스템 요소의 루트 항목입니다

<Service name="Catalina"> 

    <Connector connectionTimeout="20000" port="8787" protocol="HTTP/1.1" redirectPort="8443"/> 

    <Connector bindOnInit="false" port="8009" protocol="AJP/1.3" redirectPort="8443"/> 

    ... 

그리고 이것은 로그 출력 :

HH:mm:ss,046 DEBUG TomcatConnectors:46 - Server found. Info: 
HH:mm:ss,046 DEBUG TomcatConnectors:47 - - address   : localhost 
HH:mm:ss,046 DEBUG TomcatConnectors:48 - - domain   : Catalina 
HH:mm:ss,046 DEBUG TomcatConnectors:49 - - info    : org.apache.catalina.core.StandardServer/1.0 
HH:mm:ss,046 DEBUG TomcatConnectors:50 - - shutdown port : 8005 
HH:mm:ss,046 DEBUG TomcatConnectors:51 - - shutdown command : SHUTDOWN 
HH:mm:ss,046 DEBUG TomcatConnectors:52 - - serverInfo  : Apache Tomcat/7.0.29 
HH:mm:ss,046 DEBUG TomcatConnectors:53 - - status   : STARTING 
HH:mm:ss,047 DEBUG TomcatConnectors:71 - Service: Catalina, info: org.apache.catalina.core.StandardService/1.0, state: STARTING 
HH:mm:ss,047 DEBUG TomcatConnectors:89 - List of connectors: 
HH:mm:ss,047 DEBUG TomcatConnectors:92 - Connector.getProtocol: HTTP/1.1 
HH:mm:ss,047 DEBUG TomcatConnectors:93 - Connector.getPort: 8787 
HH:mm:ss,047 DEBUG TomcatConnectors:94 - Connector.getInfo: org.apache.catalina.connector.Connector/2.1 
HH:mm:ss,047 DEBUG TomcatConnectors:95 - Connector.getStateName: INITIALIZED 
HH:mm:ss,047 DEBUG TomcatConnectors:96 - Connector.property.bindOnInit: null 
HH:mm:ss,047 DEBUG TomcatConnectors:97 - Connector.attribute.bindOnInit: null 
HH:mm:ss,047 DEBUG TomcatConnectors:98 - Connector.getState: INITIALIZED 
HH:mm:ss,048 DEBUG TomcatConnectors:92 - Connector.getProtocol: AJP/1.3 
HH:mm:ss,048 DEBUG TomcatConnectors:93 - Connector.getPort: 8009 
HH:mm:ss,048 DEBUG TomcatConnectors:94 - Connector.getInfo: org.apache.catalina.connector.Connector/2.1 
HH:mm:ss,048 DEBUG TomcatConnectors:95 - Connector.getStateName: INITIALIZED 
HH:mm:ss,048 DEBUG TomcatConnectors:96 - Connector.property.bindOnInit: false 
HH:mm:ss,048 DEBUG TomcatConnectors:97 - Connector.attribute.bindOnInit: false 
HH:mm:ss,048 DEBUG TomcatConnectors:98 - Connector.getState: INITIALIZED 

당신이 할 수있는 커넥터의 getProperty() 또는 getAttribute() 메서드를 통해 binOnInit 속성에 액세스 할 수 있고 실제로을 반환하는지 확인합니다AJP 커넥터의 값 :

HH:mm:ss,048 DEBUG TomcatConnectors:92 - Connector.getProtocol: AJP/1.3 
HH:mm:ss,048 DEBUG TomcatConnectors:96 - Connector.property.bindOnInit: false 
HH:mm:ss,048 DEBUG TomcatConnectors:97 - Connector.attribute.bindOnInit: false