Search in sources :

Example 21 with ContextResource

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

the class NamingResourcesMBean method getResources.

/**
 * Return the MBean Names of all the defined resource references for this
 * application.
 * @return an array of object names as strings
 */
public String[] getResources() {
    ContextResource[] resources = ((NamingResourcesImpl) this.resource).findResources();
    List<String> results = new ArrayList<>();
    for (ContextResource contextResource : resources) {
        try {
            ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), contextResource);
            results.add(oname.toString());
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException(sm.getString("namingResourcesMBean.createObjectNameError.resource", contextResource), e);
        }
    }
    return results.toArray(new String[0]);
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) ArrayList(java.util.ArrayList) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) ObjectName(javax.management.ObjectName)

Example 22 with ContextResource

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

the class NamingResourcesMBean method addResource.

/**
 * Add a resource reference for this web application.
 *
 * @param resourceName New resource reference name
 * @param type New resource reference type
 * @return the object name of the new resource
 * @throws MalformedObjectNameException if the object name was invalid
 */
public String addResource(String resourceName, String type) throws MalformedObjectNameException {
    NamingResourcesImpl nresources = (NamingResourcesImpl) this.resource;
    if (nresources == null) {
        return null;
    }
    ContextResource resource = nresources.findResource(resourceName);
    if (resource != null) {
        throw new IllegalArgumentException(sm.getString("namingResourcesMBean.addAlreadyExists.resource", resourceName));
    }
    resource = new ContextResource();
    resource.setName(resourceName);
    resource.setType(type);
    nresources.addResource(resource);
    // Return the corresponding MBean name
    ManagedBean managed = registry.findManagedBean("ContextResource");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), resource);
    return oname.toString();
}
Also used : NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ManagedBean(org.apache.tomcat.util.modeler.ManagedBean) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) ObjectName(javax.management.ObjectName)

Example 23 with ContextResource

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

the class TomEEDefaultIdentityStore method init.

@PostConstruct
private void init() throws Exception {
    definition = definitionSupplier.get();
    final StandardServer server = TomcatHelper.getServer();
    final NamingResourcesImpl resources = server.getGlobalNamingResources();
    final ContextResource userDataBaseResource = resources.findResource(definition.resource());
    userDatabase = (UserDatabase) server.getGlobalNamingContext().lookup(userDataBaseResource.getName());
}
Also used : StandardServer(org.apache.catalina.core.StandardServer) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) PostConstruct(javax.annotation.PostConstruct)

Example 24 with ContextResource

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

the class OpenEJBContextConfig method adjustDataSourceNameIfNecessary.

private void adjustDataSourceNameIfNecessary() {
    if (context == null || "false".equalsIgnoreCase(ADJUST_DATASOURCE_JNDI_NAMES)) {
        return;
    }
    final NamingResourcesImpl resources = context.getNamingResources();
    if (resources == null) {
        return;
    }
    final ContextResource[] foundResources = resources.findResources();
    String[] ids = null;
    if (foundResources != null) {
        for (final ContextResource resource : foundResources) {
            if ("javax.sql.DataSource".equals(resource.getType()) && !ResourceFactory.class.getName().equals(resource.getProperty(Constants.FACTORY))) {
                String jndiName = (String) resource.getProperty("mappedName");
                if (jndiName == null) {
                    jndiName = resource.getName();
                }
                if (jndiName == null) {
                    continue;
                }
                if (ids == null) {
                    final Properties props = new Properties();
                    final OpenEjbConfiguration runningConfig = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
                    final List<String> resourceIds = new ArrayList<>();
                    if (runningConfig != null) {
                        for (final ResourceInfo resourceInfo : runningConfig.facilities.resources) {
                            if (ConfigurationFactory.isResourceType(resourceInfo.service, resourceInfo.types, "javax.sql.DataSource") && ServiceUtils.implies(props, resourceInfo.properties)) {
                                resourceIds.add(resourceInfo.id);
                            }
                        }
                    }
                    ids = resourceIds.toArray(new String[resourceIds.size()]);
                }
                String mostMatchingId = null;
                for (final String id : ids) {
                    if (id.equals(jndiName)) {
                        mostMatchingId = jndiName;
                        break;
                    } else if (jndiName.endsWith("/" + id) && mostMatchingId == null) {
                        mostMatchingId = id;
                    } else if (id.endsWith("/" + jndiName) && mostMatchingId == null) {
                        mostMatchingId = "openejb/Resource/" + id;
                    }
                }
                if (mostMatchingId != null) {
                    resource.setProperty("mappedName", "java:" + mostMatchingId);
                    resource.setProperty(NamingUtil.RESOURCE_ID, "java:" + mostMatchingId);
                    resource.setProperty(Constants.FACTORY, ResourceFactory.class.getName());
                }
            }
        }
    }
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) ArrayList(java.util.ArrayList) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ResourceFactory(org.apache.tomee.common.ResourceFactory) Properties(java.util.Properties) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration)

Example 25 with ContextResource

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

the class TomcatJndiBuilder method mergeRef.

@SuppressWarnings({ "UnusedDeclaration" })
public void mergeRef(final NamingResourcesImpl naming, final PersistenceUnitReferenceInfo ref, final URI moduleUri) {
    if (isLookupRef(naming, ref)) {
        return;
    }
    ContextResource resource = naming.findResource(ref.referenceName.replaceAll("^comp/env/", ""));
    boolean addEntry = false;
    if (resource == null) {
        resource = new ContextResource();
        resource.setName(ref.referenceName.replaceAll("^comp/env/", ""));
        addEntry = true;
    }
    resource.setProperty(Constants.FACTORY, PersistenceUnitFactory.class.getName());
    resource.setProperty(NamingUtil.NAME, ref.referenceName.replaceAll("^comp/env/", ""));
    resource.setType(EntityManagerFactory.class.getName());
    if (ref.persistenceUnitName != null) {
        resource.setProperty(NamingUtil.UNIT, ref.persistenceUnitName);
    }
    if (ref.location != null) {
        resource.setProperty(NamingUtil.JNDI_NAME, ref.location.jndiName);
        resource.setProperty(NamingUtil.JNDI_PROVIDER_ID, ref.location.jndiProviderId);
    } else {
        // TODO: This will not work if webapps don't use AutoConfi
        final Context context = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
        final EntityManagerFactory factory;
        try {
            factory = (EntityManagerFactory) context.lookup("openejb/PersistenceUnit/" + ref.unitId);
        } catch (final NamingException e) {
            throw new IllegalStateException("PersistenceUnit '" + ref.unitId + "' not found for EXTENDED ref '" + ref.referenceName.replaceAll("^comp/env/", "") + "'");
        }
        setResource(resource, factory);
    }
    if (addEntry) {
        naming.addResource(resource);
    }
    if (replaceEntry) {
        ContextAccessController.setWritable(namingContextListener.getName(), standardContext.getNamingToken());
        if (!addEntry) {
            namingContextListener.removeResource(resource.getName());
        }
        namingContextListener.addResource(resource);
        ContextAccessController.setReadOnly(namingContextListener.getName());
    }
}
Also used : WebContext(org.apache.openejb.core.WebContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) StandardContext(org.apache.catalina.core.StandardContext) Context(javax.naming.Context) ContainerSystem(org.apache.openejb.spi.ContainerSystem) PersistenceUnitFactory(org.apache.tomee.common.PersistenceUnitFactory) EntityManagerFactory(javax.persistence.EntityManagerFactory) NamingException(javax.naming.NamingException) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Aggregations

ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)28 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)10 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)9 NamingException (javax.naming.NamingException)7 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)7 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)6 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)5 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 LifecycleException (org.apache.catalina.LifecycleException)3 Tomcat (org.apache.catalina.startup.Tomcat)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 ObjectName (javax.management.ObjectName)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 Context (javax.naming.Context)2 Reference (javax.naming.Reference)2 StringRefAddr (javax.naming.StringRefAddr)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 Server (org.apache.catalina.Server)2