Java Code Examples for org.apache.catalina.Service#findConnectors()

The following examples show how to use org.apache.catalina.Service#findConnectors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Get the default http connector. You can set more
 * parameters - the port is already initialized.
 *
 * <p>
 * Alternatively, you can construct a Connector and set any params,
 * then call addConnector(Connector)
 *
 * @return A connector object that can be customized
 */
public Connector getConnector() {
    Service service = getService();
    if (service.findConnectors().length > 0) {
        return service.findConnectors()[0];
    }

    if (defaultConnectorCreated) {
        return null;
    }
    // The same as in standard Tomcat configuration.
    // This creates an APR HTTP connector if AprLifecycleListener has been
    // configured (created) and Tomcat Native library is available.
    // Otherwise it creates a NIO HTTP connector.
    Connector connector = new Connector("HTTP/1.1");
    connector.setPort(port);
    service.addConnector(connector);
    defaultConnectorCreated = true;
    return connector;
}
 
Example 2
Source File: ApplicationContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void populateSessionTrackingModes() {
    // URL re-writing is always enabled by default
    defaultSessionTrackingModes = EnumSet.of(SessionTrackingMode.URL);
    supportedSessionTrackingModes = EnumSet.of(SessionTrackingMode.URL);

    if (context.getCookies()) {
        defaultSessionTrackingModes.add(SessionTrackingMode.COOKIE);
        supportedSessionTrackingModes.add(SessionTrackingMode.COOKIE);
    }

    // SSL not enabled by default as it can only used on its own
    // Context > Host > Engine > Service
    Service s = ((Engine) context.getParent().getParent()).getService();
    Connector[] connectors = s.findConnectors();
    // Need at least one SSL enabled connector to use the SSL session ID.
    for (Connector connector : connectors) {
        if (Boolean.TRUE.equals(connector.getAttribute("SSLEnabled"))) {
            supportedSessionTrackingModes.add(SessionTrackingMode.SSL);
            break;
        }
    }
}
 
Example 3
Source File: ApplicationContext.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private void populateSessionTrackingModes() {
    // URL re-writing is always enabled by default
    defaultSessionTrackingModes = EnumSet.of(SessionTrackingMode.URL);
    supportedSessionTrackingModes = EnumSet.of(SessionTrackingMode.URL);

    if (context.getCookies()) {
        defaultSessionTrackingModes.add(SessionTrackingMode.COOKIE);
        supportedSessionTrackingModes.add(SessionTrackingMode.COOKIE);
    }

    // SSL not enabled by default as it can only used on its own
    // Context > Host > Engine > Service
    Service s = ((Engine) context.getParent().getParent()).getService();
    Connector[] connectors = s.findConnectors();
    // Need at least one SSL enabled connector to use the SSL session ID.
    for (Connector connector : connectors) {
        if (Boolean.TRUE.equals(connector.getAttribute("SSLEnabled"))) {
            supportedSessionTrackingModes.add(SessionTrackingMode.SSL);
            break;
        }
    }
}
 
Example 4
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void setConnector(Connector connector) {
    defaultConnectorCreated = true;
    Service service = getService();
    boolean found = false;
    for (Connector serviceConnector : service.findConnectors()) {
        if (connector == serviceConnector) {
            found = true;
        }
    }
    if (!found) {
        service.addConnector(connector);
    }
}
 
Example 5
Source File: ServiceMBean.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Find and return the set of Connectors associated with this Service.
 * @return an array of string representations of the connectors
 * @throws MBeanException error accessing the associated service
 */
public String[] findConnectors() throws MBeanException {

    Service service = doGetManagedResource();

    Connector[] connectors = service.findConnectors();
    String[] str = new String[connectors.length];

    for(int i = 0; i < connectors.length; i++) {
        str[i] = connectors[i].toString();
    }

    return str;
}
 
Example 6
Source File: MBeanFactory.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Remove an existing Connector.
 *
 * @param name MBean Name of the component to remove
 *
 * @exception Exception if a component cannot be removed
 */
public void removeConnector(String name) throws Exception {

    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(name);
    Service service = getService(oname);
    String port = oname.getKeyProperty("port");
    String address = oname.getKeyProperty("address");
    if (address != null) {
        address = ObjectName.unquote(address);
    }

    Connector conns[] = service.findConnectors();

    for (int i = 0; i < conns.length; i++) {
        String connAddress = null;
        Object objConnAddress = conns[i].getProperty("address");
        if (objConnAddress != null) {
            connAddress = ((InetAddress) objConnAddress).getHostAddress();
        }
        String connPort = ""+conns[i].getPort();

        if (address == null) {
            // Don't combine this with outer if or we could get an NPE in
            // 'else if' below
            if (connAddress == null && port.equals(connPort)) {
                service.removeConnector(conns[i]);
                conns[i].destroy();
                break;
            }
        } else if (address.equals(connAddress) && port.equals(connPort)) {
            service.removeConnector(conns[i]);
            conns[i].destroy();
            break;
        }
    }
}
 
Example 7
Source File: ThreadLocalLeakPreventionListener.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Updates each ThreadPoolExecutor with the current time, which is the time
 * when a context is being stopped.
 *
 * @param context
 *            the context being stopped, used to discover all the Connectors
 *            of its parent Service.
 */
private void stopIdleThreads(Context context) {
    if (serverStopping) return;

    if (!(context instanceof StandardContext) ||
        !((StandardContext) context).getRenewThreadsWhenStoppingContext()) {
        log.debug("Not renewing threads when the context is stopping. "
            + "It is not configured to do it.");
        return;
    }

    Engine engine = (Engine) context.getParent().getParent();
    Service service = engine.getService();
    Connector[] connectors = service.findConnectors();
    if (connectors != null) {
        for (Connector connector : connectors) {
            ProtocolHandler handler = connector.getProtocolHandler();
            Executor executor = null;
            if (handler != null) {
                executor = handler.getExecutor();
            }

            if (executor instanceof ThreadPoolExecutor) {
                ThreadPoolExecutor threadPoolExecutor =
                    (ThreadPoolExecutor) executor;
                threadPoolExecutor.contextStopping();
            } else if (executor instanceof StandardThreadExecutor) {
                StandardThreadExecutor stdThreadExecutor =
                    (StandardThreadExecutor) executor;
                stdThreadExecutor.contextStopping();
            }

        }
    }
}
 
Example 8
Source File: SpringBootWebTwoConnectorsApplicationTests.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
    Service service = ((TomcatWebServer) event.getWebServer()).getTomcat().getService();
    for (Connector connector : service.findConnectors()) {
        if (connector.getSecure()) {
            this.httpsPort = connector.getLocalPort();
        } else {
            this.httpPort = connector.getLocalPort();
        }
    }
}
 
Example 9
Source File: TomcatServerFactory.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
static int getLocalPort(Tomcat tomcat) {
    Service[] services = tomcat.getServer().findServices();
    for (Service service : services) {
        for (Connector connector : service.findConnectors()) {
            ProtocolHandler protocolHandler = connector.getProtocolHandler();
            if (protocolHandler instanceof Http11AprProtocol || protocolHandler instanceof Http11NioProtocol) {
                return connector.getLocalPort();
            }
        }
    }
    return 0;
}
 
Example 10
Source File: MBeanFactory.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Remove an existing Connector.
 *
 * @param name MBean Name of the component to remove
 *
 * @exception Exception if a component cannot be removed
 */
public void removeConnector(String name) throws Exception {

    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(name);
    Service service = getService(oname);
    String port = oname.getKeyProperty("port");
    //String address = oname.getKeyProperty("address");

    Connector conns[] = service.findConnectors();

    for (int i = 0; i < conns.length; i++) {
        String connAddress = String.valueOf(conns[i].getProperty("address"));
        String connPort = ""+conns[i].getPort();

        // if (((address.equals("null")) &&
        if ((connAddress==null) && port.equals(connPort)) {
            service.removeConnector(conns[i]);
            conns[i].destroy();
            break;
        }
        // } else if (address.equals(connAddress))
        if (port.equals(connPort)) {
            // Remove this component from its parent component
            service.removeConnector(conns[i]);
            conns[i].destroy();
            break;
        }
    }

}
 
Example 11
Source File: ThreadLocalLeakPreventionListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Updates each ThreadPoolExecutor with the current time, which is the time
 * when a context is being stopped.
 * 
 * @param context
 *            the context being stopped, used to discover all the Connectors
 *            of its parent Service.
 */
private void stopIdleThreads(Context context) {
    if (serverStopping) return;

    if (!(context instanceof StandardContext) ||
        !((StandardContext) context).getRenewThreadsWhenStoppingContext()) {
        log.debug("Not renewing threads when the context is stopping. "
            + "It is not configured to do it.");
        return;
    }

    Engine engine = (Engine) context.getParent().getParent();
    Service service = engine.getService();
    Connector[] connectors = service.findConnectors();
    if (connectors != null) {
        for (Connector connector : connectors) {
            ProtocolHandler handler = connector.getProtocolHandler();
            Executor executor = null;
            if (handler != null) {
                executor = handler.getExecutor();
            }

            if (executor instanceof ThreadPoolExecutor) {
                ThreadPoolExecutor threadPoolExecutor =
                    (ThreadPoolExecutor) executor;
                threadPoolExecutor.contextStopping();
            } else if (executor instanceof StandardThreadExecutor) {
                StandardThreadExecutor stdThreadExecutor =
                    (StandardThreadExecutor) executor;
                stdThreadExecutor.contextStopping();
            }

        }
    }
}
 
Example 12
Source File: MBeanFactory.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Remove an existing Connector.
 *
 * @param name MBean Name of the component to remove
 *
 * @exception Exception if a component cannot be removed
 */
public void removeConnector(String name) throws Exception {

    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(name);
    Service service = getService(oname);
    String port = oname.getKeyProperty("port");
    //String address = oname.getKeyProperty("address");

    Connector conns[] = service.findConnectors();

    for (int i = 0; i < conns.length; i++) {
        String connAddress = String.valueOf(conns[i].getProperty("address"));
        String connPort = ""+conns[i].getPort();

        // if (((address.equals("null")) &&
        if ((connAddress==null) && port.equals(connPort)) {
            service.removeConnector(conns[i]);
            conns[i].destroy();
            break;
        }
        // } else if (address.equals(connAddress))
        if (port.equals(connPort)) {
            // Remove this component from its parent component
            service.removeConnector(conns[i]);
            conns[i].destroy();
            break;
        }
    }

}
 
Example 13
Source File: ThreadLocalLeakPreventionListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Updates each ThreadPoolExecutor with the current time, which is the time
 * when a context is being stopped.
 * 
 * @param context
 *            the context being stopped, used to discover all the Connectors
 *            of its parent Service.
 */
private void stopIdleThreads(Context context) {
    if (serverStopping) return;

    if (!(context instanceof StandardContext) ||
        !((StandardContext) context).getRenewThreadsWhenStoppingContext()) {
        log.debug("Not renewing threads when the context is stopping. "
            + "It is not configured to do it.");
        return;
    }

    Engine engine = (Engine) context.getParent().getParent();
    Service service = engine.getService();
    Connector[] connectors = service.findConnectors();
    if (connectors != null) {
        for (Connector connector : connectors) {
            ProtocolHandler handler = connector.getProtocolHandler();
            Executor executor = null;
            if (handler != null) {
                executor = handler.getExecutor();
            }

            if (executor instanceof ThreadPoolExecutor) {
                ThreadPoolExecutor threadPoolExecutor =
                    (ThreadPoolExecutor) executor;
                threadPoolExecutor.contextStopping();
            } else if (executor instanceof StandardThreadExecutor) {
                StandardThreadExecutor stdThreadExecutor =
                    (StandardThreadExecutor) executor;
                stdThreadExecutor.contextStopping();
            }

        }
    }
}
 
Example 14
Source File: ManagerServlet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private Connector[] getConnectors() {
    Engine e = (Engine) host.getParent();
    Service s = e.getService();
    return s.findConnectors();
}
 
Example 15
Source File: ManagedConnectorFactory.java    From armeria with Apache License 2.0 4 votes vote down vote up
private void checkConfiguration(StandardServer server,
                                Service expectedService, Connector expectedConnector,
                                Engine expectedEngine, StandardHost expectedHost, Context expectedContext) {

    // Check if Catalina base and home directories have not been changed.
    final File expectedBaseDir = config.baseDir().toFile();
    if (!Objects.equals(server.getCatalinaBase(), expectedBaseDir) ||
        !Objects.equals(server.getCatalinaHome(), expectedBaseDir)) {
        throw new TomcatServiceException("A configurator should never change the Catalina base and home.");
    }

    // Check if the server's port has not been changed.
    if (server.getPort() != EMBEDDED_TOMCAT_PORT) {
        throw new TomcatServiceException("A configurator should never change the port of the server.");
    }

    // Check if the default service has not been removed and a new service has not been added.
    final Service[] services = server.findServices();
    if (services == null || services.length != 1 || services[0] != expectedService) {
        throw new TomcatServiceException(
                "A configurator should never remove the default service or add a new service.");
    }

    // Check if the name of the default service has not been changed.
    if (!config.serviceName().equals(expectedService.getName())) {
        throw new TomcatServiceException(
                "A configurator should never change the name of the default service.");
    }

    // Check if the default connector has not been removed
    final Connector[] connectors = expectedService.findConnectors();
    if (connectors == null || Arrays.stream(connectors).noneMatch(c -> c == expectedConnector)) {
        throw new TomcatServiceException("A configurator should never remove the default connector.");
    }

    // Check if the engine has not been changed.
    final Container actualEngine = TomcatUtil.engine(expectedService, expectedHost.getName());
    if (actualEngine != expectedEngine) {
        throw new TomcatServiceException(
                "A configurator should never change the engine of the default service.");
    }

    // Check if the engine's name has not been changed.
    if (!config.engineName().equals(expectedEngine.getName())) {
        throw new TomcatServiceException(
                "A configurator should never change the name of the default engine.");
    }

    // Check if the default realm has not been changed.
    if (expectedEngine.getRealm() != config.realm()) {
        throw new TomcatServiceException("A configurator should never change the default realm.");
    }

    // Check if the default host has not been removed.
    final Container[] engineChildren = expectedEngine.findChildren();
    if (engineChildren == null || Arrays.stream(engineChildren).noneMatch(c -> c == expectedHost)) {
        throw new TomcatServiceException("A configurator should never remove the default host.");
    }

    // Check if the default context has not been removed.
    final Container[] contextChildren = expectedHost.findChildren();
    if (contextChildren == null || Arrays.stream(contextChildren).noneMatch(c -> c == expectedContext)) {
        throw new TomcatServiceException("A configurator should never remove the default context.");
    }

    // Check if the docBase of the default context has not been changed.
    if (!config.docBase().toString().equals(expectedContext.getDocBase())) {
        throw new TomcatServiceException(
                "A configurator should never change the docBase of the default context.");
    }
}