Search in sources :

Example 26 with Resource

use of org.apache.openejb.config.sys.Resource in project tomee by apache.

the class AutoConfig method processApplicationResources.

private void processApplicationResources(final AppModule module) throws OpenEJBException {
    final Collection<Resource> resources = module.getResources();
    if (resources.size() == 0) {
        return;
    }
    final List<JndiConsumer> jndiConsumers = new ArrayList<JndiConsumer>();
    for (final WebModule webModule : module.getWebModules()) {
        final JndiConsumer consumer = webModule.getWebApp();
        jndiConsumers.add(consumer);
    }
    for (final EjbModule ejbModule : module.getEjbModules()) {
        Collections.addAll(jndiConsumers, ejbModule.getEjbJar().getEnterpriseBeans());
    }
    List<ResourceInfo> resourceInfos = new ArrayList<ResourceInfo>();
    final Map<ResourceInfo, Resource> resourcesMap = new HashMap<ResourceInfo, Resource>(resources.size());
    for (final Resource resource : resources) {
        final String originalId = PropertyPlaceHolderHelper.value(resource.getId());
        final String modulePrefix = module.getModuleId() + "/";
        if ("/".equals(modulePrefix) || originalId.startsWith("global") || originalId.startsWith("/global")) {
            resource.setId(replaceJavaAndSlash(originalId));
        } else {
            resource.getProperties().setProperty(ORIGINAL_ID, originalId);
            resource.setId(modulePrefix + replaceJavaAndSlash(originalId));
        }
        resource.setJndi(PropertyPlaceHolderHelper.value(resource.getJndi()));
        final Thread thread = Thread.currentThread();
        final ClassLoader oldCl = thread.getContextClassLoader();
        thread.setContextClassLoader(module.getClassLoader());
        try {
            resource.getProperties().putAll(PropertyPlaceHolderHelper.holds(resource.getProperties()));
        } finally {
            thread.setContextClassLoader(oldCl);
        }
        final Collection<String> aliases = resource.getAliases();
        if (!aliases.isEmpty()) {
            final Collection<String> newAliases = new ArrayList<String>();
            for (final String s : aliases) {
                newAliases.add(module.getModuleId() + "/" + s);
            }
            resource.getAliases().clear();
            resource.getAliases().addAll(newAliases);
        }
        final Properties properties = resource.getProperties();
        if (DataSource.class.getName().equals(resource.getType()) || DataSource.class.getSimpleName().equals(resource.getType())) {
            DataSourceFactory.trimNotSupportedDataSourceProperties(properties);
        }
        final boolean shouldGenerateJdbcUrl = DataSource.class.getName().equals(resource.getType()) && resource.getProperties().containsKey(ORIGIN_FLAG) && resource.getProperties().getProperty(ORIGIN_FLAG).equals(ORIGIN_ANNOTATION);
        if (shouldGenerateJdbcUrl && properties.get("JdbcUrl") == null) {
            final String url = getVendorUrl(properties);
            if (url != null) {
                properties.put("JdbcUrl", url);
            }
        }
        final ResourceInfo resourceInfo = configFactory.configureService(resource, ResourceInfo.class);
        resourceInfo.originAppName = module.getModuleId();
        final ResourceRef resourceRef = new ResourceRef();
        resourceRef.setResType(chooseType(module.getClassLoader(), resourceInfo, resource.getType()));
        if (shouldGenerateJdbcUrl) {
            properties.remove(ORIGIN_FLAG);
            resourceRef.setResRefName(dataSourceLookupName(resource));
        } else {
            resourceRef.setResRefName(OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id);
        }
        resourceRef.setMappedName(resourceInfo.id);
        final ResourceRef strictRef = new ResourceRef(OPENEJB_RESOURCE_JNDI_PREFIX + originalId, resourceRef.getResType(), resourceRef.getResAuth(), resourceRef.getResSharingScope());
        strictRef.setMappedName(resourceInfo.id);
        for (final JndiConsumer consumer : jndiConsumers) {
            // for injections etc...
            addResource(consumer, resourceRef);
            if (!"/".equals(modulePrefix)) {
                // for lookups (without prefix)
                addResource(consumer, strictRef);
            }
        }
        resourceInfos.add(resourceInfo);
        resourcesMap.put(resourceInfo, resource);
    }
    resourceInfos = ConfigurationFactory.sort(resourceInfos, module.getModuleId() + "/");
    for (final ResourceInfo resourceInfo : resourceInfos) {
        final int originalSize = resourceInfo.aliases.size();
        final String id = installResource(module.getModuleId(), resourceInfo);
        final Resource resource = resourcesMap.remove(resourceInfo);
        resource.setId(id);
        if (resourceInfo.aliases.size() > originalSize) {
            // an aliases is generally added to be able to bind in global jndi tree
            resource.getAliases().add(resourceInfo.aliases.get(resourceInfo.aliases.size() - 1));
        }
    }
    resourceInfos.clear();
// resources.clear(); // don't clear it since we want to keep this to be able to undeploy resources with the app
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) HashMap(java.util.HashMap) Resource(org.apache.openejb.config.sys.Resource) ArrayList(java.util.ArrayList) Properties(java.util.Properties) SuperProperties(org.apache.openejb.util.SuperProperties) DataSource(javax.sql.DataSource) ResourceRef(org.apache.openejb.jee.ResourceRef) JndiConsumer(org.apache.openejb.jee.JndiConsumer)

Example 27 with Resource

use of org.apache.openejb.config.sys.Resource in project tomee by apache.

the class ConfigurationFactory method getOpenEjbConfiguration.

public OpenEjbConfiguration getOpenEjbConfiguration(final Openejb providedConf) throws OpenEJBException {
    if (sys != null) {
        return sys;
    }
    if (providedConf != null) {
        openejb = providedConf;
    } else if (configLocation != null) {
        openejb = JaxbOpenejb.readConfig(configLocation);
    } else {
        openejb = JaxbOpenejb.createOpenejb();
    }
    for (final SystemProperty sp : openejb.getSystemProperties()) {
        final String name = sp.getName();
        final String value = sp.getValue();
        SystemInstance.get().setProperty(name, value);
        JavaSecurityManagers.setSystemProperty(name, value);
    }
    loadPropertiesDeclaredConfiguration(openejb);
    sys = new OpenEjbConfiguration();
    sys.containerSystem = new ContainerSystemInfo();
    sys.facilities = new FacilitiesInfo();
    // listener + some config can be defined as service
    for (final Service service : openejb.getServices()) {
        final ServiceInfo info = configureService(service, ServiceInfo.class);
        sys.facilities.services.add(info);
    }
    for (final JndiProvider provider : openejb.getJndiProvider()) {
        final JndiContextInfo info = configureService(provider, JndiContextInfo.class);
        sys.facilities.remoteJndiContexts.add(info);
    }
    sys.facilities.securityService = configureService(openejb.getSecurityService(), SecurityServiceInfo.class);
    sys.facilities.transactionService = configureService(openejb.getTransactionManager(), TransactionServiceInfo.class);
    List<ResourceInfo> resources = new ArrayList<>();
    for (final Resource resource : openejb.getResource()) {
        final ResourceInfo resourceInfo = configureService(resource, ResourceInfo.class);
        resources.add(resourceInfo);
    }
    resources = sort(resources, null);
    sys.facilities.resources.addAll(resources);
    if (openejb.getProxyFactory() != null) {
        sys.facilities.intraVmServer = configureService(openejb.getProxyFactory(), ProxyFactoryInfo.class);
    }
    for (final Container declaration : openejb.getContainer()) {
        final ContainerInfo info = createContainerInfo(declaration);
        sys.containerSystem.containers.add(info);
    }
    final List<File> declaredApps = getDeclaredApps();
    for (final File jarFile : declaredApps) {
        try {
            final AppInfo appInfo = configureApplication(jarFile);
            sys.containerSystem.applications.add(appInfo);
        } catch (final OpenEJBException alreadyHandled) {
            final DeploymentExceptionManager exceptionManager = SystemInstance.get().getComponent(DeploymentExceptionManager.class);
            if (exceptionManager != null) {
                exceptionManager.pushDelpoymentException(alreadyHandled);
            }
        }
    }
    final boolean embedded = SystemInstance.get().hasProperty(EJBContainer.class.getName());
    final Options options = SystemInstance.get().getOptions();
    if (options.get("openejb.system.apps", false)) {
        try {
            final boolean extended = SystemApps.isExtended();
            final AppInfo appInfo;
            if (!extended) {
                // do it manually, we know what we need and can skip a bunch of processing
                appInfo = SystemAppInfo.preComputedInfo(this);
            } else {
                appInfo = configureApplication(new AppModule(SystemApps.getSystemModule()));
            }
            // they doesn't use CDI so no need to activate it
            // 1) will be faster
            // 2) will let embedded containers (tomee-embedded mainly) not be noised by it in our singleton service
            appInfo.properties.put("openejb.cdi.activated", "false");
            sys.containerSystem.applications.add(appInfo);
        } catch (final OpenEJBException e) {
            logger.error("Unable to load the system applications.", e);
        }
    } else if (options.get(DEPLOYMENTS_CLASSPATH_PROPERTY, !embedded)) {
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        final ArrayList<File> jarFiles = getModulesFromClassPath(declaredApps, classLoader);
        final String appId = "classpath.ear";
        final boolean classpathAsEar = options.get(CLASSPATH_AS_EAR, true);
        try {
            if (classpathAsEar && !jarFiles.isEmpty()) {
                final AppInfo appInfo = configureApplication(classLoader, appId, jarFiles);
                sys.containerSystem.applications.add(appInfo);
            } else {
                for (final File jarFile : jarFiles) {
                    final AppInfo appInfo = configureApplication(jarFile);
                    sys.containerSystem.applications.add(appInfo);
                }
            }
            if (jarFiles.size() == 0) {
                logger.warning("config.noModulesFoundToDeploy");
            }
        } catch (final OpenEJBException alreadyHandled) {
            logger.debug("config.alreadyHandled");
        }
    }
    for (final Deployments deployments : openejb.getDeployments()) {
        if (deployments.isAutoDeploy()) {
            if (deployments.getDir() != null) {
                sys.containerSystem.autoDeploy.add(deployments.getDir());
            }
        }
    }
    final OpenEjbConfiguration finished = sys;
    sys = null;
    openejb = null;
    return finished;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) Options(org.apache.openejb.loader.Options) ArrayList(java.util.ArrayList) AdditionalDeployments(org.apache.openejb.config.sys.AdditionalDeployments) Deployments(org.apache.openejb.config.sys.Deployments) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) ServiceInfo(org.apache.openejb.assembler.classic.ServiceInfo) EJBContainer(javax.ejb.embeddable.EJBContainer) Container(org.apache.openejb.config.sys.Container) JndiContextInfo(org.apache.openejb.assembler.classic.JndiContextInfo) EJBContainer(javax.ejb.embeddable.EJBContainer) ContainerSystemInfo(org.apache.openejb.assembler.classic.ContainerSystemInfo) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) DeploymentExceptionManager(org.apache.openejb.assembler.classic.DeploymentExceptionManager) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) JndiProvider(org.apache.openejb.config.sys.JndiProvider) Resource(org.apache.openejb.config.sys.Resource) AbstractService(org.apache.openejb.config.sys.AbstractService) SecurityService(org.apache.openejb.config.sys.SecurityService) Service(org.apache.openejb.config.sys.Service) AppInfo(org.apache.openejb.assembler.classic.AppInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) FacilitiesInfo(org.apache.openejb.assembler.classic.FacilitiesInfo) ProxyFactoryInfo(org.apache.openejb.assembler.classic.ProxyFactoryInfo) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) ManagedContainerInfo(org.apache.openejb.assembler.classic.ManagedContainerInfo) BmpEntityContainerInfo(org.apache.openejb.assembler.classic.BmpEntityContainerInfo) StatelessSessionContainerInfo(org.apache.openejb.assembler.classic.StatelessSessionContainerInfo) CmpEntityContainerInfo(org.apache.openejb.assembler.classic.CmpEntityContainerInfo) StatefulSessionContainerInfo(org.apache.openejb.assembler.classic.StatefulSessionContainerInfo) SingletonSessionContainerInfo(org.apache.openejb.assembler.classic.SingletonSessionContainerInfo) MdbContainerInfo(org.apache.openejb.assembler.classic.MdbContainerInfo) ContainerInfo(org.apache.openejb.assembler.classic.ContainerInfo) File(java.io.File)

Example 28 with Resource

use of org.apache.openejb.config.sys.Resource in project tomee by apache.

the class ReadDescriptors method check.

public static Resources check(final Resources resources) {
    final List<Resource> resourceList = resources.getResource();
    for (final Resource resource : resourceList) {
        if (resource.getClassName() != null) {
            try {
                ParentClassLoaderFinder.Helper.get().loadClass(resource.getClassName());
                continue;
            } catch (Exception e) {
            // ignore if this class is not found in the classloader
            }
            // if the resource class cannot be loaded,
            // set the lazy property to true
            // and the app classloader property to true
            final Boolean lazySpecified = Boolean.valueOf(resource.getProperties().getProperty("Lazy", "false"));
            resource.getProperties().setProperty("Lazy", "true");
            resource.getProperties().setProperty("UseAppClassLoader", "true");
            if (!lazySpecified) {
                resource.getProperties().setProperty("InitializeAfterDeployment", "true");
            }
        }
    }
    return resources;
}
Also used : Resource(org.apache.openejb.config.sys.Resource) OpenEJBException(org.apache.openejb.OpenEJBException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 29 with Resource

use of org.apache.openejb.config.sys.Resource in project tomee by apache.

the class AutoConfigPersistenceUnitsTest method testFromUnitNameJtaWithClasspath.

/**
 * Existing data source "orange-unit", jta-managed
 * <p/>
 * Persistence xml like so:
 * <p/>
 * <persistence-unit name="orange-unit" />
 * <p/>
 * The orange-unit app should automatically use orange-unit data source and create a new non-JtaManaged datasource
 *
 * @throws Exception
 */
public void testFromUnitNameJtaWithClasspath() throws Exception {
    final Resource resource = new Resource("orange-unit", "DataSource");
    final File file = new File("target/" + getClass().getName());
    file.mkdirs();
    resource.setClasspath(file.getPath());
    final ResourceInfo supplied = addDataSource(OrangeDriver.class, "jdbc:orange:some:stuff", true, resource);
    assertSame(supplied, resources.get(0));
    final PersistenceUnitInfo unitInfo = addPersistenceUnit("orange-unit", null, null);
    assertNotNull(unitInfo);
    // Check results
    final ResourceInfo generated = resources.get(1);
    assertEquals(supplied.id + "NonJta", generated.id);
    assertEquals(supplied.service, generated.service);
    assertEquals(supplied.className, generated.className);
    assertEquals(supplied.properties.get("JdbcDriver"), generated.properties.get("JdbcDriver"));
    assertEquals(supplied.properties.get("JdbcUrl"), generated.properties.get("JdbcUrl"));
    assertEquals("false", generated.properties.get("JtaManaged"));
    final String expected = Join.join("\n", supplied.classpath);
    final String actual = Join.join("\n", generated.classpath);
    assertEquals(expected, actual);
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) Resource(org.apache.openejb.config.sys.Resource) File(java.io.File) PersistenceUnitInfo(org.apache.openejb.assembler.classic.PersistenceUnitInfo)

Example 30 with Resource

use of org.apache.openejb.config.sys.Resource in project tomee by apache.

the class ConfigureServiceTest method testQueue.

public void testQueue() throws Exception {
    final ConfigurationFactory factory = new ConfigurationFactory();
    final ResourceInfo resourceInfo = factory.configureService(new Resource("myQueue", "Queue"), ResourceInfo.class);
    assertNotNull(resourceInfo);
    assertEquals("myQueue", resourceInfo.id);
    assertNotNull(resourceInfo.constructorArgs);
    assertNotNull(resourceInfo.properties);
    assertEquals("myQueue", resourceInfo.properties.getProperty("destination"));
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) Resource(org.apache.openejb.config.sys.Resource)

Aggregations

Resource (org.apache.openejb.config.sys.Resource)30 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)15 Test (org.junit.Test)12 Properties (java.util.Properties)11 ArrayList (java.util.ArrayList)7 OpenEJBException (org.apache.openejb.OpenEJBException)6 File (java.io.File)5 IOException (java.io.IOException)5 Resources (org.apache.openejb.config.sys.Resources)5 DataSource (javax.sql.DataSource)4 Container (org.apache.openejb.config.sys.Container)4 Openejb (org.apache.openejb.config.sys.Openejb)4 URL (java.net.URL)3 HashMap (java.util.HashMap)3 InitialContext (javax.naming.InitialContext)3 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)3 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)3 SuperProperties (org.apache.openejb.util.SuperProperties)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MalformedURLException (java.net.MalformedURLException)2