Search in sources :

Example 66 with ResourceInfo

use of org.apache.openejb.assembler.classic.ResourceInfo 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<String>();
                    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)

Aggregations

ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)66 PersistenceUnitInfo (org.apache.openejb.assembler.classic.PersistenceUnitInfo)31 Resource (org.apache.openejb.config.sys.Resource)14 OpenEJBException (org.apache.openejb.OpenEJBException)12 Properties (java.util.Properties)8 AppInfo (org.apache.openejb.assembler.classic.AppInfo)8 Persistence (org.apache.openejb.jee.jpa.unit.Persistence)8 PersistenceUnit (org.apache.openejb.jee.jpa.unit.PersistenceUnit)8 ArrayList (java.util.ArrayList)6 Assembler (org.apache.openejb.assembler.classic.Assembler)6 WebApp (org.apache.openejb.jee.WebApp)6 SuperProperties (org.apache.openejb.util.SuperProperties)6 Test (org.junit.Test)6 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)5 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)5 File (java.io.File)4 DataSource (javax.sql.DataSource)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 AppContext (org.apache.openejb.AppContext)3