Search in sources :

Example 6 with ContextResourceLink

use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomcat by apache.

the class ContextResourceLinkMBean method setAttribute.

/**
 * Set the value of a specific attribute of this MBean.
 *
 * @param attribute The identification of the attribute to be set
 *  and the new value
 *
 * @exception AttributeNotFoundException if this attribute is not
 *  supported by this MBean
 * @exception MBeanException if the initializer of an object
 *  throws an exception
 * @exception ReflectionException if a Java reflection exception
 *  occurs when invoking the getter
 */
@Override
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
    // Validate the input parameters
    if (attribute == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException(sm.getString("mBean.nullAttribute")), sm.getString("mBean.nullAttribute"));
    }
    String name = attribute.getName();
    Object value = attribute.getValue();
    if (name == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException(sm.getString("mBean.nullName")), sm.getString("mBean.nullName"));
    }
    ContextResourceLink crl = doGetManagedResource();
    if ("global".equals(name)) {
        crl.setGlobal((String) value);
    } else if ("description".equals(name)) {
        crl.setDescription((String) value);
    } else if ("name".equals(name)) {
        crl.setName((String) value);
    } else if ("type".equals(name)) {
        crl.setType((String) value);
    } else {
        crl.setProperty(name, "" + value);
    }
    // cannot use side-effects.  It's removed and added back each time
    // there is a modification in a resource.
    NamingResources nr = crl.getNamingResources();
    nr.removeResourceLink(crl.getName());
    nr.addResourceLink(crl);
}
Also used : ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) NamingResources(org.apache.tomcat.util.descriptor.web.NamingResources) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 7 with ContextResourceLink

use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomcat by apache.

the class NamingResourcesSF method storeChildren.

/**
 * Store the specified NamingResources properties.
 *
 * @param aWriter
 *            PrintWriter to which we are storing
 * @param indent
 *            Number of spaces to indent this element
 * @param aElement
 *            Object whose properties are being stored
 * @param elementDesc
 *            element descriptor
 *
 * @exception Exception
 *                if an exception occurs while storing
 *
 * @see org.apache.catalina.storeconfig.StoreFactoryBase#storeChildren(java.io.PrintWriter,
 *      int, java.lang.Object, StoreDescription)
 */
@Override
public void storeChildren(PrintWriter aWriter, int indent, Object aElement, StoreDescription elementDesc) throws Exception {
    if (aElement instanceof NamingResourcesImpl) {
        NamingResourcesImpl resources = (NamingResourcesImpl) aElement;
        // Store nested <Ejb> elements
        ContextEjb[] ejbs = resources.findEjbs();
        storeElementArray(aWriter, indent, ejbs);
        // Store nested <Environment> elements
        ContextEnvironment[] envs = resources.findEnvironments();
        storeElementArray(aWriter, indent, envs);
        // Store nested <LocalEjb> elements
        ContextLocalEjb[] lejbs = resources.findLocalEjbs();
        storeElementArray(aWriter, indent, lejbs);
        // Store nested <Resource> elements
        ContextResource[] dresources = resources.findResources();
        storeElementArray(aWriter, indent, dresources);
        // Store nested <ResourceEnvRef> elements
        ContextResourceEnvRef[] resEnv = resources.findResourceEnvRefs();
        storeElementArray(aWriter, indent, resEnv);
        // Store nested <ResourceLink> elements
        ContextResourceLink[] resourceLinks = resources.findResourceLinks();
        storeElementArray(aWriter, indent, resourceLinks);
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) ContextEjb(org.apache.tomcat.util.descriptor.web.ContextEjb) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) ContextLocalEjb(org.apache.tomcat.util.descriptor.web.ContextLocalEjb) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResourceEnvRef(org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)

Example 8 with ContextResourceLink

use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomcat by apache.

the class NamingContextListener method containerEvent.

// ---------------------------------------------- ContainerListener Methods
/**
     * Acknowledge the occurrence of the specified event.
     * Note: Will never be called when the listener is associated to a Server,
     * since it is not a Container.
     *
     * @param event ContainerEvent that has occurred
     */
@Override
public void containerEvent(ContainerEvent event) {
    if (!initialized)
        return;
    // Setting the context in read/write mode
    ContextAccessController.setWritable(getName(), token);
    String type = event.getType();
    if (type.equals("addEjb")) {
        String ejbName = (String) event.getData();
        if (ejbName != null) {
            ContextEjb ejb = namingResources.findEjb(ejbName);
            addEjb(ejb);
        }
    } else if (type.equals("addEnvironment")) {
        String environmentName = (String) event.getData();
        if (environmentName != null) {
            ContextEnvironment env = namingResources.findEnvironment(environmentName);
            addEnvironment(env);
        }
    } else if (type.equals("addLocalEjb")) {
        String localEjbName = (String) event.getData();
        if (localEjbName != null) {
            ContextLocalEjb localEjb = namingResources.findLocalEjb(localEjbName);
            addLocalEjb(localEjb);
        }
    } else if (type.equals("addResource")) {
        String resourceName = (String) event.getData();
        if (resourceName != null) {
            ContextResource resource = namingResources.findResource(resourceName);
            addResource(resource);
        }
    } else if (type.equals("addResourceLink")) {
        String resourceLinkName = (String) event.getData();
        if (resourceLinkName != null) {
            ContextResourceLink resourceLink = namingResources.findResourceLink(resourceLinkName);
            addResourceLink(resourceLink);
        }
    } else if (type.equals("addResourceEnvRef")) {
        String resourceEnvRefName = (String) event.getData();
        if (resourceEnvRefName != null) {
            ContextResourceEnvRef resourceEnvRef = namingResources.findResourceEnvRef(resourceEnvRefName);
            addResourceEnvRef(resourceEnvRef);
        }
    } else if (type.equals("addService")) {
        String serviceName = (String) event.getData();
        if (serviceName != null) {
            ContextService service = namingResources.findService(serviceName);
            addService(service);
        }
    } else if (type.equals("removeEjb")) {
        String ejbName = (String) event.getData();
        if (ejbName != null) {
            removeEjb(ejbName);
        }
    } else if (type.equals("removeEnvironment")) {
        String environmentName = (String) event.getData();
        if (environmentName != null) {
            removeEnvironment(environmentName);
        }
    } else if (type.equals("removeLocalEjb")) {
        String localEjbName = (String) event.getData();
        if (localEjbName != null) {
            removeLocalEjb(localEjbName);
        }
    } else if (type.equals("removeResource")) {
        String resourceName = (String) event.getData();
        if (resourceName != null) {
            removeResource(resourceName);
        }
    } else if (type.equals("removeResourceLink")) {
        String resourceLinkName = (String) event.getData();
        if (resourceLinkName != null) {
            removeResourceLink(resourceLinkName);
        }
    } else if (type.equals("removeResourceEnvRef")) {
        String resourceEnvRefName = (String) event.getData();
        if (resourceEnvRefName != null) {
            removeResourceEnvRef(resourceEnvRefName);
        }
    } else if (type.equals("removeService")) {
        String serviceName = (String) event.getData();
        if (serviceName != null) {
            removeService(serviceName);
        }
    }
    // Setting the context in read only mode
    ContextAccessController.setReadOnly(getName());
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextService(org.apache.tomcat.util.descriptor.web.ContextService) ContextLocalEjb(org.apache.tomcat.util.descriptor.web.ContextLocalEjb) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) ContextResourceEnvRef(org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef) ContextEjb(org.apache.tomcat.util.descriptor.web.ContextEjb) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 9 with ContextResourceLink

use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomcat by apache.

the class NamingContextListener method createNamingContext.

/**
 * Create and initialize the JNDI naming context.
 */
private void createNamingContext() throws NamingException {
    // Creating the comp subcontext
    if (container instanceof Server) {
        compCtx = namingContext;
        envCtx = namingContext;
    } else {
        compCtx = namingContext.createSubcontext("comp");
        envCtx = compCtx.createSubcontext("env");
    }
    int i;
    if (log.isDebugEnabled()) {
        log.debug("Creating JNDI naming context");
    }
    if (namingResources == null) {
        namingResources = new NamingResourcesImpl();
        namingResources.setContainer(container);
    }
    // Resource links
    ContextResourceLink[] resourceLinks = namingResources.findResourceLinks();
    for (i = 0; i < resourceLinks.length; i++) {
        addResourceLink(resourceLinks[i]);
    }
    // Resources
    ContextResource[] resources = namingResources.findResources();
    for (i = 0; i < resources.length; i++) {
        addResource(resources[i]);
    }
    // Resources Env
    ContextResourceEnvRef[] resourceEnvRefs = namingResources.findResourceEnvRefs();
    for (i = 0; i < resourceEnvRefs.length; i++) {
        addResourceEnvRef(resourceEnvRefs[i]);
    }
    // Environment entries
    ContextEnvironment[] contextEnvironments = namingResources.findEnvironments();
    for (i = 0; i < contextEnvironments.length; i++) {
        addEnvironment(contextEnvironments[i]);
    }
    // EJB references
    ContextEjb[] ejbs = namingResources.findEjbs();
    for (i = 0; i < ejbs.length; i++) {
        addEjb(ejbs[i]);
    }
    // Message Destination References
    MessageDestinationRef[] mdrs = namingResources.findMessageDestinationRefs();
    for (i = 0; i < mdrs.length; i++) {
        addMessageDestinationRef(mdrs[i]);
    }
    // WebServices references
    ContextService[] services = namingResources.findServices();
    for (i = 0; i < services.length; i++) {
        addService(services[i]);
    }
    // Binding a User Transaction reference
    if (container instanceof Context) {
        try {
            Reference ref = new TransactionRef();
            compCtx.bind("UserTransaction", ref);
            ContextTransaction transaction = namingResources.getTransaction();
            if (transaction != null) {
                Iterator<String> params = transaction.listProperties();
                while (params.hasNext()) {
                    String paramName = params.next();
                    String paramValue = (String) transaction.getProperty(paramName);
                    StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
                    ref.add(refAddr);
                }
            }
        } catch (NameAlreadyBoundException e) {
        // Ignore because UserTransaction was obviously
        // added via ResourceLink
        } catch (NamingException e) {
            log.error(sm.getString("naming.bindFailed", e));
        }
    }
    // Binding the resources directory context
    if (container instanceof Context) {
        try {
            compCtx.bind("Resources", ((Context) container).getResources());
        } catch (NamingException e) {
            log.error(sm.getString("naming.bindFailed", e));
        }
    }
}
Also used : ContextService(org.apache.tomcat.util.descriptor.web.ContextService) Server(org.apache.catalina.Server) MessageDestinationRef(org.apache.tomcat.util.descriptor.web.MessageDestinationRef) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NamingException(javax.naming.NamingException) ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) NamingContext(org.apache.naming.NamingContext) Context(org.apache.catalina.Context) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) Reference(javax.naming.Reference) ContextTransaction(org.apache.tomcat.util.descriptor.web.ContextTransaction) ContextEjb(org.apache.tomcat.util.descriptor.web.ContextEjb) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) StringRefAddr(javax.naming.StringRefAddr) TransactionRef(org.apache.naming.TransactionRef) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResourceEnvRef(org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)

Example 10 with ContextResourceLink

use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomcat by apache.

the class NamingResourcesImpl method initInternal.

// ------------------------------------------------------- Lifecycle methods
@Override
protected void initInternal() throws LifecycleException {
    super.initInternal();
    // Set this before we register currently known naming resources to avoid
    // timing issues. Duplication registration is not an issue.
    resourceRequireExplicitRegistration = true;
    for (ContextResource cr : resources.values()) {
        try {
            MBeanUtils.createMBean(cr);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", cr.getName()), e);
        }
    }
    for (ContextEnvironment ce : envs.values()) {
        try {
            MBeanUtils.createMBean(ce);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", ce.getName()), e);
        }
    }
    for (ContextResourceLink crl : resourceLinks.values()) {
        try {
            MBeanUtils.createMBean(crl);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", crl.getName()), e);
        }
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) NamingException(javax.naming.NamingException) LifecycleException(org.apache.catalina.LifecycleException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Aggregations

ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)16 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)10 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)7 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)6 NamingException (javax.naming.NamingException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 LifecycleException (org.apache.catalina.LifecycleException)4 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)4 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)4 ObjectName (javax.management.ObjectName)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 Context (org.apache.catalina.Context)2 ContextLocalEjb (org.apache.tomcat.util.descriptor.web.ContextLocalEjb)2 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)2 Test (org.junit.Test)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1