org.apache.catalina.Container Java Examples

The following examples show how to use org.apache.catalina.Container. 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: MBeanFactory.java    From Tomcat8-Source-Read with MIT License 9 votes vote down vote up
/**
 * Create a new JNDI Realm.
 *
 * @param parent MBean Name of the associated parent component
 * @return the object name of the created realm
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createJNDIRealm(String parent) throws Exception {

     // Create a new JNDIRealm instance
    JNDIRealm realm = new JNDIRealm();

    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    Container container = getParentContainerFromParent(pname);
    // Add the new instance to its parent component
    container.setRealm(realm);
    // Return the corresponding MBean name
    ObjectName oname = realm.getObjectName();

    if (oname != null) {
        return (oname.toString());
    } else {
        return null;
    }
}
 
Example #2
Source File: StandardHost.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Add a child Container, only if the proposed child is an implementation
 * of Context.
 *
 * @param child Child container to be added
 */
@Override
public void addChild(Container child) {

    if (!(child instanceof Context))
        throw new IllegalArgumentException
            (sm.getString("standardHost.notContext"));

    child.addLifecycleListener(new MemoryLeakTrackingListener());

    // Avoid NPE for case where Context is defined in server.xml with only a
    // docBase
    Context context = (Context) child;
    if (context.getPath() == null) {
        ContextName cn = new ContextName(context.getDocBase(), true);
        context.setPath(cn.getPath());
    }

    super.addChild(child);

}
 
Example #3
Source File: MBeanFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Create a new Web Application Loader.
 *
 * @param parent MBean Name of the associated parent component
 * @return the object name of the created loader
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createWebappLoader(String parent)
    throws Exception {

    // Create a new WebappLoader instance
    WebappLoader loader = new WebappLoader();

    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    Container container = getParentContainerFromParent(pname);
    if (container instanceof Context) {
        ((Context) container).setLoader(loader);
    }
    // FIXME add Loader.getObjectName
    //ObjectName oname = loader.getObjectName();
    ObjectName oname =
        MBeanUtils.createObjectName(pname.getDomain(), loader);
    return oname.toString();

}
 
Example #4
Source File: CopyParentClassLoaderRule.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Handle the beginning of an XML element.
 *
 * @param attributes The attributes of this element
 *
 * @exception Exception if a processing error occurs
 */
@Override
public void begin(String namespace, String name, Attributes attributes)
    throws Exception {

    if (digester.getLogger().isDebugEnabled())
        digester.getLogger().debug("Copying parent class loader");
    Container child = (Container) digester.peek(0);
    Object parent = digester.peek(1);
    Method method =
        parent.getClass().getMethod("getParentClassLoader", new Class[0]);
    ClassLoader classLoader =
        (ClassLoader) method.invoke(parent, new Object[0]);
    child.setParentClassLoader(classLoader);

}
 
Example #5
Source File: MBeanFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Create a new StandardManager.
 *
 * @param parent MBean Name of the associated parent component
 * @return the object name of the created manager
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createStandardManager(String parent)
    throws Exception {

    // Create a new StandardManager instance
    StandardManager manager = new StandardManager();

    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    Container container = getParentContainerFromParent(pname);
    if (container instanceof Context) {
        ((Context) container).setManager(manager);
    } else {
        throw new Exception(sm.getString("mBeanFactory.managerContext"));
    }
    ObjectName oname = manager.getObjectName();
    if (oname != null) {
        return oname.toString();
    } else {
        return null;
    }

}
 
Example #6
Source File: MapperListener.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Register host.
 */
private void registerHost(Host host) {

    String[] aliases = host.findAliases();
    mapper.addHost(host.getName(), aliases, host);

    for (Container container : host.findChildren()) {
        if (container.getState().isAvailable()) {
            registerContext((Context) container);
        }
    }
    if(log.isDebugEnabled()) {
        log.debug(sm.getString("mapperListener.registerHost",
                host.getName(), domain, connector));
    }
}
 
Example #7
Source File: MBeanFactory.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Create a new DataSource Realm.
 *
 * @param parent MBean Name of the associated parent component
 * @param dataSourceName the datasource name
 * @param roleNameCol the column name for the role names
 * @param userCredCol the column name for the user credentials
 * @param userNameCol the column name for the user names
 * @param userRoleTable the table name for the roles table
 * @param userTable the table name for the users
 * @return the object name of the created realm
 * @exception Exception if an MBean cannot be created or registered
 */
public String createDataSourceRealm(String parent, String dataSourceName,
    String roleNameCol, String userCredCol, String userNameCol,
    String userRoleTable, String userTable) throws Exception {

    // Create a new DataSourceRealm instance
    DataSourceRealm realm = new DataSourceRealm();
    realm.setDataSourceName(dataSourceName);
    realm.setRoleNameCol(roleNameCol);
    realm.setUserCredCol(userCredCol);
    realm.setUserNameCol(userNameCol);
    realm.setUserRoleTable(userRoleTable);
    realm.setUserTable(userTable);

    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    Container container = getParentContainerFromParent(pname);
    // Add the new instance to its parent component
    container.setRealm(realm);
    // Return the corresponding MBean name
    ObjectName oname = realm.getObjectName();
    if (oname != null) {
        return oname.toString();
    } else {
        return null;
    }
}
 
Example #8
Source File: CopyParentClassLoaderRule.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Handle the beginning of an XML element.
 *
 * @param attributes The attributes of this element
 *
 * @exception Exception if a processing error occurs
 */
@Override
public void begin(String namespace, String name, Attributes attributes)
    throws Exception {

    if (digester.getLogger().isDebugEnabled())
        digester.getLogger().debug("Copying parent class loader");
    Container child = (Container) digester.peek(0);
    Object parent = digester.peek(1);
    Method method =
        parent.getClass().getMethod("getParentClassLoader", new Class[0]);
    ClassLoader classLoader =
        (ClassLoader) method.invoke(parent, new Object[0]);
    child.setParentClassLoader(classLoader);

}
 
Example #9
Source File: ThreadLocalLeakPreventionListener.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void containerEvent(ContainerEvent event) {
    try {
        String type = event.getType();
        if (Container.ADD_CHILD_EVENT.equals(type)) {
            processContainerAddChild(event.getContainer(),
                (Container) event.getData());
        } else if (Container.REMOVE_CHILD_EVENT.equals(type)) {
            processContainerRemoveChild(event.getContainer(),
                (Container) event.getData());
        }
    } catch (Exception e) {
        String msg =
            sm.getString(
                "threadLocalLeakPreventionListener.containerEvent.error",
                event);
        log.error(msg, e);
    }

}
 
Example #10
Source File: RewriteValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Find the configuration path where the rewrite configuration file
 * will be stored.
 * @param resourceName The rewrite configuration file name
 * @return the full rewrite configuration path
 */
protected String getHostConfigPath(String resourceName) {
    StringBuffer result = new StringBuffer();
    Container container = getContainer();
    Container host = null;
    Container engine = null;
    while (container != null) {
        if (container instanceof Host)
            host = container;
        if (container instanceof Engine)
            engine = container;
        container = container.getParent();
    }
    if (engine != null) {
        result.append(engine.getName()).append('/');
    }
    if (host != null) {
        result.append(host.getName()).append('/');
    }
    result.append(resourceName);
    return result.toString();
}
 
Example #11
Source File: MBeanUtils.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the name of the domain to register MBeans for from a given
 * Container.
 * 
 * @param container
 *
 * @deprecated  To be removed since to creates a circular dependency. Will
 *              be replaced in Tomcat 8 by a new method on {@link
 *              Container}.
 */
@Deprecated
public static String getDomain(Container container) {
    
    String domain = null;
    
    Container c = container;
    
    while (!(c instanceof Engine) && c != null) {
        c = c.getParent();
    }
    
    if (c != null) {
        domain = c.getName();
    }
    
    return domain;
}
 
Example #12
Source File: HostManagerServlet.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Render a list of the currently active Contexts in our virtual host.
 *
 * @param writer Writer to render to
 */
protected void list(PrintWriter writer, StringManager smClient) {

    if (debug >= 1) {
        log(sm.getString("hostManagerServlet.list", engine.getName()));
    }

    writer.println(smClient.getString("hostManagerServlet.listed",
            engine.getName()));
    Container[] hosts = engine.findChildren();
    for (int i = 0; i < hosts.length; i++) {
        Host host = (Host) hosts[i];
        String name = host.getName();
        String[] aliases = host.findAliases();
        StringBuilder buf = new StringBuilder();
        if (aliases.length > 0) {
            buf.append(aliases[0]);
            for (int j = 1; j < aliases.length; j++) {
                buf.append(',').append(aliases[j]);
            }
        }
        writer.println(smClient.getString("hostManagerServlet.listitem",
                                    name, buf.toString()));
    }
}
 
Example #13
Source File: MBeanUtils.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the name of the domain to register MBeans for from a given
 * Service.
 * 
 * @param service 
 *
 * @deprecated  To be removed since to creates a circular dependency. Will
 *              be replaced in Tomcat 8 by a new method on {@link
 *              Service}.
 */
@Deprecated
public static String getDomain(Service service) {
    
    // Null service -> return null
    if (service == null) {
        return null;
    }
    
    String domain = null;
    
    Container engine = service.getContainer();
    
    // Use the engine name first
    if (engine != null) {
        domain = engine.getName();
    }
    
    // No engine or no engine name, use the service name 
    if (domain == null) {
        domain = service.getName();
    }
    
    // No service name, use null
    return domain;
}
 
Example #14
Source File: MBeanFactory.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new Valve and associate it with a {@link Container}.
 *
 * @param className The fully qualified class name of the {@link Valve} to
 *                  create
 * @param parent    The MBean name of the associated parent
 *                  {@link Container}.
 *
 * @return  The MBean name of the {@link Valve} that was created or
 *          <code>null</code> if the {@link Valve} does not implement
 *          {@link LifecycleMBeanBase}.
 */
public String createValve(String className, String parent)
        throws Exception {

    // Look for the parent
    ObjectName parentName = new ObjectName(parent);
    Container container = getParentContainerFromParent(parentName);

    if (container == null) {
        // TODO
        throw new IllegalArgumentException();
    }

    Valve valve = (Valve) Class.forName(className).newInstance();

    container.getPipeline().addValve(valve);

    if (valve instanceof LifecycleMBeanBase) {
        return ((LifecycleMBeanBase) valve).getObjectName().toString();
    } else {
        return null;
    }
}
 
Example #15
Source File: MBeanUtils.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Deregister the MBean for this
 * <code>Valve</code> object.
 *
 * @param valve The Valve to be managed
 *
 * @exception Exception if an MBean cannot be deregistered
 * @deprecated  Unused. Will be removed in Tomcat 8.0.x
 */
@Deprecated
static void destroyMBean(Valve valve, Container container)
    throws Exception {

    ((Contained)valve).setContainer(container);
    String mname = createManagedName(valve);
    ManagedBean managed = registry.findManagedBean(mname);
    if (managed == null) {
        return;
    }
    String domain = managed.getDomain();
    if (domain == null)
        domain = mserver.getDefaultDomain();
    ObjectName oname = createObjectName(domain, valve);
    try {
        ((Contained)valve).setContainer(null);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
    }
    if( mserver.isRegistered(oname) ) {
        mserver.unregisterMBean(oname);
    }

}
 
Example #16
Source File: MBeanUtils.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Deregister the MBean for this
 * <code>Valve</code> object.
 *
 * @param valve The Valve to be managed
 *
 * @exception Exception if an MBean cannot be deregistered
 * @deprecated  Unused. Will be removed in Tomcat 8.0.x
 */
@Deprecated
static void destroyMBean(Valve valve, Container container)
    throws Exception {

    ((Contained)valve).setContainer(container);
    String mname = createManagedName(valve);
    ManagedBean managed = registry.findManagedBean(mname);
    if (managed == null) {
        return;
    }
    String domain = managed.getDomain();
    if (domain == null)
        domain = mserver.getDefaultDomain();
    ObjectName oname = createObjectName(domain, valve);
    try {
        ((Contained)valve).setContainer(null);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
    }
    if( mserver.isRegistered(oname) ) {
        mserver.unregisterMBean(oname);
    }

}
 
Example #17
Source File: EmbeddedTomcatTest.java    From junit-servers with MIT License 6 votes vote down vote up
@Test
void it_should_add_parent_classloader(@TempDir File tmpDir) throws Exception {
	final File tmpFile = Files.createTempFile(tmpDir.toPath(), null, null).toFile();
	final File dir = tmpFile.getParentFile();

	tomcat = new EmbeddedTomcat(EmbeddedTomcatConfiguration.builder()
		.withWebapp(dir)
		.withParentClasspath(dir.toURI().toURL())
		.build());

	tomcat.start();

	final Container[] containers = tomcat.getDelegate().getHost().findChildren();
	final ClassLoader cl = containers[0].getParentClassLoader();

	assertThat(cl).isNotNull();
	assertThat(cl.getResource("hello-world.html")).isNotNull();
	assertThat(cl.getResource(tmpFile.getName())).isNotNull();
}
 
Example #18
Source File: ContainerBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public String getMBeanKeyProperties() {
    Container c = this;
    StringBuilder keyProperties = new StringBuilder();
    int containerCount = 0;

    // Work up container hierarchy, add a component to the name for
    // each container
    while (!(c instanceof Engine)) {
        if (c instanceof Wrapper) {
            keyProperties.insert(0, ",servlet=");
            keyProperties.insert(9, c.getName());
        } else if (c instanceof Context) {
            keyProperties.insert(0, ",context=");
            ContextName cn = new ContextName(c.getName(), false);
            keyProperties.insert(9,cn.getDisplayName());
        } else if (c instanceof Host) {
            keyProperties.insert(0, ",host=");
            keyProperties.insert(6, c.getName());
        } else if (c == null) {
            // May happen in unit testing and/or some embedding scenarios
            keyProperties.append(",container");
            keyProperties.append(containerCount++);
            keyProperties.append("=null");
            break;
        } else {
            // Should never happen...
            keyProperties.append(",container");
            keyProperties.append(containerCount++);
            keyProperties.append('=');
            keyProperties.append(c.getName());
        }
        c = c.getParent();
    }
    return keyProperties.toString();
}
 
Example #19
Source File: LazyRealm.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public Container getContainer() {
    if (delegate != null) {
        return delegate.getContainer();
    }
    return container;
}
 
Example #20
Source File: Contexts.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static String getHostname(final StandardContext ctx) {
    String hostName = null;
    final Container parentHost = ctx.getParent();
    if (parentHost != null) {
        hostName = parentHost.getName();
    }
    if ((hostName == null) || (hostName.length() < 1)) {
        hostName = "_";
    }
    return hostName;
}
 
Example #21
Source File: ApplicationContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
    Map<String, ApplicationServletRegistration> result = new HashMap<>();

    Container[] wrappers = context.findChildren();
    for (Container wrapper : wrappers) {
        result.put(((Wrapper) wrapper).getName(),
                new ApplicationServletRegistration(
                        (Wrapper) wrapper, context));
    }

    return result;
}
 
Example #22
Source File: StandardWrapper.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Set the parent Container of this Wrapper, but only if it is a Context.
 *
 * @param container Proposed parent Container
 */
@Override
public void setParent(Container container) {

    if ((container != null) &&
        !(container instanceof Context))
        throw new IllegalArgumentException
            (sm.getString("standardWrapper.notContext"));
    if (container instanceof StandardContext) {
        swallowOutput = ((StandardContext)container).getSwallowOutput();
        unloadDelay = ((StandardContext)container).getUnloadDelay();
    }
    super.setParent(container);

}
 
Example #23
Source File: RealmBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Set the Container with which this Realm has been associated.
 *
 * @param container The associated Container
 */
@Override
public void setContainer(Container container) {

    Container oldContainer = this.container;
    this.container = container;
    support.firePropertyChange("container", oldContainer, this.container);

}
 
Example #24
Source File: SimpleTcpCluster.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Set the Container associated with our Cluster
 * 
 * @param container
 *            The Container to use
 */
@Override
public void setContainer(Container container) {
    Container oldContainer = this.container;
    this.container = container;
    support.firePropertyChange("container", oldContainer, this.container);
}
 
Example #25
Source File: SimpleTcpCluster.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
protected void initInternal() throws LifecycleException {
    super.initInternal();
    if (clusterDeployer != null) {
        StringBuilder name = new StringBuilder("type=Cluster");
        Container container = getContainer();
        name.append(MBeanUtils.getContainerKeyProperties(container));
        name.append(",component=Deployer");
        onameClusterDeployer = register(clusterDeployer, name.toString());
    }
}
 
Example #26
Source File: MapperListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Add this mapper to the container and all child containers
 *
 * @param container
 */
private void addListeners(Container container) {
    container.addContainerListener(this);
    container.addLifecycleListener(this);
    for (Container child : container.findChildren()) {
        addListeners(child);
    }
}
 
Example #27
Source File: ContainerMBean.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Remove a LifecycleEvent listeners from this component.
 *
 * @param type The ClassName of the listeners to be removed.
 * Note that all the listeners having given ClassName will be removed.
 * @throws MBeanException propagated from the managed resource access
 */
public void removeLifecycleListeners(String type) throws MBeanException{
    Container container = doGetManagedResource();

    LifecycleListener[] listeners = container.findLifecycleListeners();
    for (LifecycleListener listener : listeners){
        if (listener.getClass().getName().equals(type)) {
            container.removeLifecycleListener(listener);
        }
    }
}
 
Example #28
Source File: ContainerMBean.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Adds a valve to this Container instance.
 *
 * @param valveType ClassName of the valve to be added
 * @return the MBean name of the new valve
 * @throws MBeanException if adding the valve failed
 */
public String addValve(String valveType) throws MBeanException{
    Valve valve = (Valve) newInstance(valveType);

    Container container = doGetManagedResource();
    container.getPipeline().addValve(valve);

    if (valve instanceof JmxEnabled) {
        return ((JmxEnabled)valve).getObjectName().toString();
    } else {
        return null;
    }
}
 
Example #29
Source File: MapperListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Add this mapper to the container and all child containers
 *
 * @param container
 */
private void addListeners(Container container) {
    container.addContainerListener(this);
    container.addLifecycleListener(this);
    for (Container child : container.findChildren()) {
        addListeners(child);
    }
}
 
Example #30
Source File: AuthenticatorBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Set the Container to which this Valve is attached.
 *
 * @param container The container to which we are attached
 */
@Override
public void setContainer(Container container) {

    if (container != null && !(container instanceof Context))
        throw new IllegalArgumentException
            (sm.getString("authenticator.notContext"));

    super.setContainer(container);
    this.context = (Context) container;

}