Search in sources :

Example 21 with Resource

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

the class ConvertJMSDestinationDefinitions method toResource.

private Resource toResource(final JMSDestination factory) {
    final String name = cleanUpName(factory.getName());
    final Resource factoryResource = new Resource(name, javax.jms.ConnectionFactory.class.getName());
    factoryResource.setJndi(factory.getName().replaceFirst("java:", ""));
    factoryResource.setType(factory.getInterfaceName() != null && !factory.getInterfaceName().isEmpty() ? factory.getInterfaceName() : "javax.jms.Queue");
    if (factory.getClassName() != null && !factory.getClassName().isEmpty()) {
        factoryResource.setClassName(factory.getClassName());
    }
    final Properties p = factoryResource.getProperties();
    put(p, AutoConfig.ORIGIN_FLAG, AutoConfig.ORIGIN_ANNOTATION);
    put(p, "JndiName", factoryResource.getJndi());
    put(p, "Destination", factory.getDestinationName());
    if (factory.getResourceAdapter() != null && !factory.getResourceAdapter().isEmpty()) {
        put(p, "ResourceAdapter", factory.getResourceAdapter());
    }
    setProperties(factory, p);
    return factoryResource;
}
Also used : Resource(org.apache.openejb.config.sys.Resource) Properties(java.util.Properties)

Example 22 with Resource

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

the class AppInfoBuilder method buildConnectorModules.

private void buildConnectorModules(final AppModule appModule, final AppInfo appInfo) throws OpenEJBException {
    final String appId = appModule.getModuleId();
    for (final ConnectorModule connectorModule : appModule.getConnectorModules()) {
        // 
        // DEVELOPERS NOTE:  if you change the id generation code here, you must change
        // the id generation code in AutoConfig$AppResources
        // 
        final Connector connector = connectorModule.getConnector();
        final ConnectorInfo connectorInfo = new ConnectorInfo();
        connectorInfo.description = connector.getDescription();
        connectorInfo.displayName = connector.getDisplayName();
        connectorInfo.path = connectorModule.getJarLocation();
        connectorInfo.moduleId = connectorModule.getModuleId();
        connectorInfo.watchedResources.addAll(connectorModule.getWatchedResources());
        connectorInfo.validationInfo = ValidatorBuilder.getInfo(connectorModule.getValidationConfig());
        connectorInfo.uniqueId = connectorModule.getUniqueId();
        connectorInfo.mbeans = connectorModule.getMbeans();
        final List<URL> libraries = connectorModule.getLibraries();
        for (final URL url : libraries) {
            final File file = toFile(url);
            try {
                connectorInfo.libs.add(file.getCanonicalPath());
            } catch (final IOException e) {
                throw new IllegalArgumentException("Invalid application lib path " + file.getAbsolutePath());
            }
        }
        final ResourceAdapter resourceAdapter = connector.getResourceAdapter();
        if (resourceAdapter.getResourceAdapterClass() != null) {
            final String id = this.getId(connectorModule);
            final String className = resourceAdapter.getResourceAdapterClass();
            final ServiceProvider provider = new ServiceProvider(className, id, "Resource");
            provider.getTypes().add(className);
            ServiceUtils.registerServiceProvider(appId, provider);
            final Resource resource = new Resource(id, className, appId + "#" + id);
            for (final ConfigProperty property : resourceAdapter.getConfigProperty()) {
                final String name = property.getConfigPropertyName();
                final String value = property.getConfigPropertyValue();
                if (value != null) {
                    resource.getProperties().setProperty(name, value);
                }
            }
            connectorInfo.resourceAdapter = this.configFactory.configureService(resource, ResourceInfo.class);
        }
        final OutboundResourceAdapter outbound = resourceAdapter.getOutboundResourceAdapter();
        if (outbound != null) {
            String transactionSupport = "none";
            final TransactionSupportType transactionSupportType = outbound.getTransactionSupport();
            if (transactionSupportType != null) {
                switch(transactionSupportType) {
                    case LOCAL_TRANSACTION:
                        transactionSupport = "local";
                        break;
                    case NO_TRANSACTION:
                        transactionSupport = "none";
                        break;
                    case XA_TRANSACTION:
                        transactionSupport = "xa";
                        break;
                }
            }
            for (final ConnectionDefinition connection : outbound.getConnectionDefinition()) {
                final String id = this.getId(connection, outbound, connectorModule);
                final String className = connection.getManagedConnectionFactoryClass();
                final String type = connection.getConnectionFactoryInterface();
                final ServiceProvider provider = new ServiceProvider(className, id, "Resource");
                provider.getTypes().add(type);
                ServiceUtils.registerServiceProvider(appId, provider);
                final Resource resource = new Resource(id, type, appId + "#" + id);
                final Properties properties = resource.getProperties();
                for (final ConfigProperty property : connection.getConfigProperty()) {
                    final String name = property.getConfigPropertyName();
                    final String value = property.getConfigPropertyValue();
                    if (value != null) {
                        properties.setProperty(name, value);
                    }
                }
                properties.setProperty("TransactionSupport", transactionSupport);
                if (connectorInfo.resourceAdapter != null) {
                    properties.setProperty("ResourceAdapter", connectorInfo.resourceAdapter.id);
                }
                final ResourceInfo resourceInfo = this.configFactory.configureService(resource, ResourceInfo.class);
                connectorInfo.outbound.add(resourceInfo);
            }
        }
        final InboundResourceadapter inbound = resourceAdapter.getInboundResourceAdapter();
        if (inbound != null) {
            for (final MessageListener messageListener : inbound.getMessageAdapter().getMessageListener()) {
                final String id = this.getId(messageListener, inbound, connectorModule);
                final Container container = new Container(id, "MESSAGE", null);
                final Properties properties = container.getProperties();
                properties.setProperty("ResourceAdapter", connectorInfo.resourceAdapter.id);
                properties.setProperty("MessageListenerInterface", messageListener.getMessageListenerType());
                properties.setProperty("ActivationSpecClass", messageListener.getActivationSpec().getActivationSpecClass());
                final MdbContainerInfo mdbContainerInfo = this.configFactory.configureService(container, MdbContainerInfo.class);
                connectorInfo.inbound.add(mdbContainerInfo);
            }
        }
        for (final AdminObject adminObject : resourceAdapter.getAdminObject()) {
            final String id = this.getId(adminObject, resourceAdapter, connectorModule);
            final String className = adminObject.getAdminObjectClass();
            final String type = adminObject.getAdminObjectInterface();
            final ServiceProvider provider = new ServiceProvider(className, id, "Resource");
            provider.getTypes().add(type);
            ServiceUtils.registerServiceProvider(appId, provider);
            final Resource resource = new Resource(id, type, appId + "#" + id);
            final Properties properties = resource.getProperties();
            for (final ConfigProperty property : adminObject.getConfigProperty()) {
                final String name = property.getConfigPropertyName();
                final String value = property.getConfigPropertyValue();
                if (value != null) {
                    properties.setProperty(name, value);
                }
            }
            final ResourceInfo resourceInfo = this.configFactory.configureService(resource, ResourceInfo.class);
            connectorInfo.adminObject.add(resourceInfo);
        }
        appInfo.connectors.add(connectorInfo);
    }
}
Also used : Connector(org.apache.openejb.jee.Connector) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) ConnectionDefinition(org.apache.openejb.jee.ConnectionDefinition) ConnectorInfo(org.apache.openejb.assembler.classic.ConnectorInfo) TransactionSupportType(org.apache.openejb.jee.TransactionSupportType) Resource(org.apache.openejb.config.sys.Resource) MessageListener(org.apache.openejb.jee.MessageListener) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL) Container(org.apache.openejb.config.sys.Container) ServiceProvider(org.apache.openejb.config.sys.ServiceProvider) ConfigProperty(org.apache.openejb.jee.ConfigProperty) MdbContainerInfo(org.apache.openejb.assembler.classic.MdbContainerInfo) OutboundResourceAdapter(org.apache.openejb.jee.OutboundResourceAdapter) ResourceAdapter(org.apache.openejb.jee.ResourceAdapter) InboundResourceadapter(org.apache.openejb.jee.InboundResourceadapter) URLs.toFile(org.apache.openejb.util.URLs.toFile) File(java.io.File) OutboundResourceAdapter(org.apache.openejb.jee.OutboundResourceAdapter) AdminObject(org.apache.openejb.jee.AdminObject)

Example 23 with Resource

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

the class AppInfoBuilder method buildAppResources.

private void buildAppResources(final AppModule module, final AppInfo info) {
    for (final Resource def : module.getResources()) {
        // so we skip the undeployement skipping the id
        if (!def.getProperties().containsKey("ApplicationWide")) {
            info.resourceIds.add(def.getId());
            info.resourceAliases.addAll(def.getAliases());
        }
    }
}
Also used : Resource(org.apache.openejb.config.sys.Resource)

Example 24 with Resource

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

the class AutoConfig method deploy.

private void deploy(final AppModule app, final PersistenceModule persistenceModule) throws OpenEJBException {
    if (!autoCreateResources) {
        return;
    }
    final Persistence persistence = persistenceModule.getPersistence();
    for (final PersistenceUnit unit : persistence.getPersistenceUnit()) {
        if (unit.getProvider() != null) {
            logger.info("Configuring PersistenceUnit(name=" + unit.getName() + ", provider=" + unit.getProvider() + ")");
        } else {
            logger.info("Configuring PersistenceUnit(name=" + unit.getName() + ")");
        }
        if (unit.getJtaDataSource() == null && unit.getNonJtaDataSource() == null && "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.force-unit-type", unit.getProperty("openejb.force-unit-type", "true")))) {
            // 8.2.1.5 of JPA 2.0 spec
            unit.setTransactionType(TransactionType.JTA);
        }
        // if jta datasource is specified it can be used as model fo rnon jta datasource
        final boolean resourceLocal = TransactionType.RESOURCE_LOCAL.equals(unit.getTransactionType()) && unit.getJtaDataSource() == null;
        if (resourceLocal && unit.getNonJtaDataSource() == null && isDataSourcePropertiesConfigured(unit.getProperties())) {
            continue;
        }
        final Properties required = new Properties();
        if ("org.apache.openjpa.persistence.PersistenceProviderImpl".equals(unit.getProvider())) {
            if (unit.getJtaDataSource() == null) {
                unit.setJtaDataSource(unit.getProperty("openjpa.ConnectionFactoryName"));
            }
            if (unit.getNonJtaDataSource() == null) {
                unit.setNonJtaDataSource(unit.getProperty("openjpa.ConnectionFactory2Name"));
            }
        }
        logger.debug("raw <jta-data-source>" + unit.getJtaDataSource() + "</jta-datasource>");
        logger.debug("raw <non-jta-data-source>" + unit.getNonJtaDataSource() + "</non-jta-datasource>");
        // keep it can start with java:global for instance
        final String originalJtaDataSource = unit.getJtaDataSource();
        unit.setJtaDataSource(normalizeResourceId(originalJtaDataSource));
        final String originalNonJtaDataSource = unit.getNonJtaDataSource();
        unit.setNonJtaDataSource(normalizeResourceId(originalNonJtaDataSource));
        logger.debug("normalized <jta-data-source>" + unit.getJtaDataSource() + "</jta-datasource>");
        logger.debug("normalized <non-jta-data-source>" + unit.getNonJtaDataSource() + "</non-jta-datasource>");
        if (logger.isDebugEnabled()) {
            required.put("JtaManaged", "true");
            final List<String> managed = configFactory.getResourceIds("DataSource", required);
            required.put("JtaManaged", "false");
            final List<String> unmanaged = configFactory.getResourceIds("DataSource", required);
            required.clear();
            final List<String> unknown = configFactory.getResourceIds("DataSource", required);
            logger.debug("Available DataSources");
            for (final String name : managed) {
                logger.debug("DataSource(name=" + name + ", JtaManaged=true)");
            }
            for (final String name : unmanaged) {
                logger.debug("DataSource(name=" + name + ", JtaManaged=false)");
            }
            for (final String name : unknown) {
                if (managed.contains(name)) {
                    continue;
                }
                if (unmanaged.contains(name)) {
                    continue;
                }
                logger.debug("DataSource(name=" + name + ", JtaManaged=<unknown>)");
            }
        }
        final String prefix = app.getModuleId() + "/";
        String jtaDataSourceId = null;
        String nonJtaDataSourceId = null;
        // first try exact matching without JtaManaged which is not mandatory actually (custom DS + JTADataSourceWrapperFactory)
        final String jtaWithJavaAndSlash = replaceJavaAndSlash(unit.getJtaDataSource());
        for (final String potentialName : asList(prefix + jtaWithJavaAndSlash, originalJtaDataSource, jtaWithJavaAndSlash)) {
            if (potentialName == null) {
                // If unit.getJtaDataSource() is null, one of the potentialName is also null.
                continue;
            }
            final ResourceInfo jtaInfo = configFactory.getResourceInfo(potentialName);
            if (jtaInfo != null) {
                if (// don't test true since it can be missing
                !"false".equalsIgnoreCase(jtaInfo.properties.getProperty("JtaManaged")) && (jtaInfo.types.contains("DataSource") || jtaInfo.types.contains(DataSource.class.getName()))) {
                    jtaDataSourceId = jtaInfo.id;
                    break;
                } else {
                    logger.warning("Found matching datasource: " + jtaInfo.id + " but this one is not a JTA datasource");
                }
            }
        }
        final String nonJtaWithJavaAndSlash = replaceJavaAndSlash(unit.getNonJtaDataSource());
        for (final String potentialName : asList(prefix + nonJtaWithJavaAndSlash, originalNonJtaDataSource, nonJtaWithJavaAndSlash)) {
            if (potentialName == null) {
                // If unit.getNonJtaDataSource() is null, one of the potentialName is also null.
                continue;
            }
            final ResourceInfo info = configFactory.getResourceInfo(potentialName);
            if (info != null) {
                if (!"true".equalsIgnoreCase(info.properties.getProperty("JtaManaged")) && (info.types.contains("DataSource") || info.types.contains(DataSource.class.getName()))) {
                    nonJtaDataSourceId = info.id;
                    break;
                } else {
                    logger.warning("Found matching datasource: " + info.id + " but this one is a JTA datasource");
                }
            }
        }
        // then that's ok to force configuration
        if (jtaDataSourceId == null && !resourceLocal) {
            required.put("JtaManaged", "true");
            jtaDataSourceId = findResourceId(prefix + jtaWithJavaAndSlash, "DataSource", required, null);
            if (jtaDataSourceId == null) {
                // test with javax.sql.DataSource before DataSource since RA can register resources without our shortcut
                jtaDataSourceId = findResourceId(jtaWithJavaAndSlash, "javax.sql.DataSource", required, null);
            }
        /* this shouldn't be mandatory anymore since our DataSource has as alias javax.sql.DataSource
                if (jtaDataSourceId == null) {
                    jtaDataSourceId = findResourceId(replaceJavaAndSlash(unit.getJtaDataSource()), "DataSource", required, null);
                }
                */
        }
        if (nonJtaDataSourceId == null) {
            required.put("JtaManaged", "false");
            nonJtaDataSourceId = findResourceId(prefix + nonJtaWithJavaAndSlash, "DataSource", required, null);
            if (nonJtaDataSourceId == null) {
                nonJtaDataSourceId = findResourceId(nonJtaWithJavaAndSlash, "DataSource", required, null);
            }
        }
        if ((jtaDataSourceId != null || resourceLocal) && nonJtaDataSourceId != null) {
            // Both DataSources were explicitly configured.
            if (jtaDataSourceId != null) {
                setJtaDataSource(unit, jtaDataSourceId);
            }
            setNonJtaDataSource(unit, nonJtaDataSourceId);
            continue;
        }
        // 
        if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
            required.put("JtaManaged", ServiceUtils.NONE);
            if (!resourceLocal) {
                jtaDataSourceId = findResourceId(unit.getJtaDataSource(), "DataSource", required, null);
            }
            nonJtaDataSourceId = findResourceId(unit.getNonJtaDataSource(), "DataSource", required, null);
            if (jtaDataSourceId != null || nonJtaDataSourceId != null) {
                if (jtaDataSourceId != null) {
                    setJtaDataSource(unit, jtaDataSourceId);
                }
                if (nonJtaDataSourceId != null) {
                    setNonJtaDataSource(unit, nonJtaDataSourceId);
                }
                continue;
            }
        }
        // We are done with the most optimal configuration.
        // 
        // If both the jta-data-source and non-jta-data-source
        // references were explicitly and correctly configured
        // to existing datasource, we wouldn't get this far.
        // 
        // At this point we see if either we can't figure out
        // if there's an issue with their configuration or
        // if we can't intelligently complete their configuration.
        // 
        // Do both the jta-data-source and non-jta-data-source references
        // point to the same datasource?
        // 
        // If so, then unlink the invalid one so defaulting rules can
        // possibly fill in a good value.
        // 
        required.put("JtaManaged", ServiceUtils.ANY);
        final String possibleJta = findResourceId(jtaWithJavaAndSlash, "DataSource", required, null);
        final String possibleNonJta = findResourceId(nonJtaWithJavaAndSlash, "DataSource", required, null);
        if (possibleJta != null && possibleJta.equals(possibleNonJta)) {
            final ResourceInfo dataSource = configFactory.getResourceInfo(possibleJta);
            final String jtaManaged = (String) dataSource.properties.get("JtaManaged");
            logger.warning("PeristenceUnit(name=" + unit.getName() + ") invalidly refers to Resource(id=" + dataSource.id + ") as both its <jta-data-source> and <non-jta-data-source>.");
            if ("true".equalsIgnoreCase(jtaManaged)) {
                nonJtaDataSourceId = null;
                unit.setNonJtaDataSource(null);
            } else if ("false".equalsIgnoreCase(jtaManaged)) {
                jtaDataSourceId = null;
                unit.setJtaDataSource(null);
            }
        }
        // 
        // Do the jta-data-source and non-jta-data-source references
        // point to innapropriately configured Resources?
        // 
        checkUnitDataSourceRefs(unit);
        // 
        if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
            jtaDataSourceId = findResourceProviderId(unit.getJtaDataSource());
            nonJtaDataSourceId = findResourceProviderId(unit.getNonJtaDataSource());
            // we can just create the second resource using the first as a template
            if (jtaDataSourceId != null || nonJtaDataSourceId != null) {
                final Resource jtaResource = new Resource(jtaDataSourceId, "DataSource", jtaDataSourceId);
                jtaResource.getProperties().setProperty("JtaManaged", "true");
                final Resource nonJtaResource = new Resource(nonJtaDataSourceId, "DataSource", nonJtaDataSourceId);
                nonJtaResource.getProperties().setProperty("JtaManaged", "false");
                if (jtaDataSourceId == null) {
                    jtaResource.setId(nonJtaDataSourceId + "Jta");
                    jtaResource.setProvider(nonJtaDataSourceId);
                } else if (nonJtaDataSourceId == null) {
                    nonJtaResource.setId(jtaDataSourceId + "NonJta");
                    nonJtaResource.setProvider(jtaDataSourceId);
                }
                final ResourceInfo jtaResourceInfo = configFactory.configureService(jtaResource, ResourceInfo.class);
                final ResourceInfo nonJtaResourceInfo = configFactory.configureService(nonJtaResource, ResourceInfo.class);
                if (jtaDataSourceId != null && nonJtaDataSourceId == null) {
                    nonJtaResourceInfo.originAppName = jtaResourceInfo.originAppName;
                }
                logAutoCreateResource(jtaResourceInfo, "DataSource", unit.getName());
                jtaDataSourceId = installResource(unit.getName(), jtaResourceInfo);
                logAutoCreateResource(nonJtaResourceInfo, "DataSource", unit.getName());
                nonJtaDataSourceId = installResource(unit.getName(), nonJtaResourceInfo);
                setJtaDataSource(unit, jtaDataSourceId);
                setNonJtaDataSource(unit, nonJtaDataSourceId);
                continue;
            }
        }
        // Look for defaults, see https://issues.apache.org/jira/browse/OPENEJB-1027
        if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
            // We check for data sources matching the following names:
            // 1. The persistence unit id
            // 2. The web module id
            // 3. The web module context root
            // 4. The application module id
            final List<String> ids = new ArrayList<String>();
            ids.add(unit.getName());
            for (final WebModule webModule : app.getWebModules()) {
                ids.add(webModule.getModuleId());
                ids.add(webModule.getContextRoot());
            }
            ids.add(app.getModuleId());
            // Search for a matching data source
            for (final String id : ids) {
                // Try finding a jta managed data source
                required.put("JtaManaged", "true");
                jtaDataSourceId = findResourceId(id, "DataSource", required, null);
                if (jtaDataSourceId == null) {
                    // No jta managed data source found. Try finding a non-jta managed
                    required.clear();
                    required.put("JtaManaged", "false");
                    nonJtaDataSourceId = findResourceId(id, "DataSource", required, null);
                }
                if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
                    // Neither jta nor non-jta managed data sources were found. try to find one with it unset
                    required.clear();
                    required.put("JtaManaged", ServiceUtils.NONE);
                    jtaDataSourceId = findResourceId(id, "DataSource", required, null);
                }
                if (jtaDataSourceId != null || nonJtaDataSourceId != null) {
                    // We have found a default. Exit the loop
                    break;
                }
            }
        }
        // 
        if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
            required.clear();
            required.put("JtaManaged", "true");
            jtaDataSourceId = firstMatching(prefix, "DataSource", required, null);
            if (jtaDataSourceId == null) {
                required.clear();
                required.put("JtaManaged", "false");
                nonJtaDataSourceId = firstMatching(prefix, "DataSource", required, null);
            }
        }
        if (jtaDataSourceId != null && nonJtaDataSourceId == null) {
            final ResourceInfo jtaResourceInfo = configFactory.getResourceInfo(jtaDataSourceId);
            final Properties jtaProperties = jtaResourceInfo.properties;
            if (jtaProperties.containsKey("JtaManaged")) {
                // Strategy 1: Best match search
                required.clear();
                required.put("JtaManaged", "false");
                for (final String key : asList("JdbcDriver", "JdbcUrl")) {
                    if (jtaProperties.containsKey(key)) {
                        required.put(key, jtaProperties.get(key));
                    }
                }
                nonJtaDataSourceId = firstMatching(prefix, "DataSource", required, null);
                if (nonJtaDataSourceId == null) {
                    final ResourceInfo nonJtaResourceInfo = copy(jtaResourceInfo);
                    nonJtaResourceInfo.id = jtaResourceInfo.id + "NonJta";
                    nonJtaResourceInfo.originAppName = jtaResourceInfo.originAppName;
                    suffixAliases(nonJtaResourceInfo, "NonJta");
                    configureImplicitDataSource(nonJtaResourceInfo);
                    final Properties overrides = ConfigurationFactory.getSystemProperties(nonJtaResourceInfo.id, nonJtaResourceInfo.service);
                    nonJtaResourceInfo.properties.putAll(overrides);
                    nonJtaResourceInfo.properties.setProperty("JtaManaged", "false");
                    // if created from annotation we just want live config
                    nonJtaResourceInfo.properties.remove("Definition");
                    logAutoCreateResource(nonJtaResourceInfo, "DataSource", unit.getName());
                    logger.info("configureService.configuring", nonJtaResourceInfo.id, nonJtaResourceInfo.service, jtaResourceInfo.id);
                    nonJtaDataSourceId = installResource(unit.getName(), nonJtaResourceInfo);
                }
            }
        }
        // 
        // Does the jta-data-source reference point an existing
        // Resource in the system with JtaManaged=false?
        // 
        // If so, we can search for an existing datasource
        // configured with identical properties and use it.
        // 
        // If that doesn't work, we can copy the jta-data-source
        // and auto-create the missing non-jta-data-source
        // using it as a template, applying the overrides,
        // and finally setting JtaManaged=false
        // 
        final String deduceJtaFromNonJta = unit.getProperty(AUTOCREATE_JTA_DATASOURCE_FROM_NON_JTA_ONE_KEY, SystemInstance.get().getOptions().get(AUTOCREATE_JTA_DATASOURCE_FROM_NON_JTA_ONE_KEY, (String) null));
        if (nonJtaDataSourceId != null && jtaDataSourceId == null && // hibernate uses the fact that this ds is missing to get a non jta em instead of a JTA one
        (!resourceLocal || deduceJtaFromNonJta != null) && (deduceJtaFromNonJta == null || deduceJtaFromNonJta != null && Boolean.parseBoolean(deduceJtaFromNonJta))) {
            final ResourceInfo nonJtaResourceInfo = configFactory.getResourceInfo(nonJtaDataSourceId);
            final Properties nonJtaProperties = nonJtaResourceInfo.properties;
            if (nonJtaProperties.containsKey("JtaManaged")) {
                // Strategy 1: Best match search
                required.clear();
                required.put("JtaManaged", "true");
                for (final String key : asList("JdbcDriver", "JdbcUrl")) {
                    if (nonJtaProperties.containsKey(key)) {
                        required.put(key, nonJtaProperties.get(key));
                    }
                }
                jtaDataSourceId = firstMatching(prefix, "DataSource", required, null);
                if (jtaDataSourceId == null) {
                    final ResourceInfo jtaResourceInfo = copy(nonJtaResourceInfo);
                    jtaResourceInfo.id = nonJtaResourceInfo.id + "Jta";
                    suffixAliases(jtaResourceInfo, "Jta");
                    configureImplicitDataSource(jtaResourceInfo);
                    final Properties overrides = ConfigurationFactory.getSystemProperties(jtaResourceInfo.id, jtaResourceInfo.service);
                    jtaResourceInfo.properties.putAll(overrides);
                    jtaResourceInfo.properties.setProperty("JtaManaged", "true");
                    // if created from annotation we just want live config
                    jtaResourceInfo.properties.remove("Definition");
                    logAutoCreateResource(jtaResourceInfo, "DataSource", unit.getName());
                    logger.info("configureService.configuring", jtaResourceInfo.id, jtaResourceInfo.service, nonJtaResourceInfo.id);
                    jtaDataSourceId = installResource(unit.getName(), jtaResourceInfo);
                }
            }
        }
        // 
        if (jtaDataSourceId == null && nonJtaDataSourceId == null) {
            if (!resourceLocal) {
                required.put("JtaManaged", "true");
                jtaDataSourceId = autoCreateResource("DataSource", required, unit.getName());
            }
            required.put("JtaManaged", "false");
            nonJtaDataSourceId = autoCreateResource("DataSource", required, unit.getName());
        }
        if (jtaDataSourceId != null) {
            setJtaDataSource(unit, jtaDataSourceId);
        }
        if (nonJtaDataSourceId != null) {
            setNonJtaDataSource(unit, nonJtaDataSourceId);
        }
    }
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) Resource(org.apache.openejb.config.sys.Resource) ArrayList(java.util.ArrayList) Properties(java.util.Properties) SuperProperties(org.apache.openejb.util.SuperProperties)

Example 25 with Resource

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

the class AutoConfig method getResourceEnvId.

private String getResourceEnvId(final String beanName, String resourceId, final String type, AppResources appResources) throws OpenEJBException {
    if (resourceId == null) {
        return null;
    }
    if (appResources == null) {
        appResources = EMPTY_APP_RESOURCES;
    }
    // skip references such as URLs which are automatically handled by the server
    if (ignoredReferenceTypes.contains(type)) {
        return null;
    }
    resourceId = normalizeResourceId(resourceId);
    // check for existing resource with specified resourceId
    final List<String> resourceEnvIds = getResourceIds(appResources, type, null);
    for (final String id : resourceEnvIds) {
        if (id.equalsIgnoreCase(resourceId)) {
            return id;
        }
    }
    // throw an exception or log an error
    final String message = "No existing resource found while attempting to Auto-link unmapped resource-env-ref '" + resourceId + "' of type '" + type + "' for '" + beanName + "'.  Looked for Resource(id=" + resourceId + ")";
    if (!autoCreateResources) {
        throw new OpenEJBException(message);
    }
    logger.debug(message);
    // Auto create a resource using the first provider that can supply a resource of the desired type
    final String providerId = ServiceUtils.getServiceProviderId(type);
    if (providerId == null) {
        // if there are any existing resources of the desired type, use the first one
        if (resourceEnvIds.size() > 0) {
            return resourceEnvIds.get(0);
        }
        throw new OpenEJBException("No provider available for resource-env-ref '" + resourceId + "' of type '" + type + "' for '" + beanName + "'.");
    }
    final Resource resource = new Resource(resourceId, null, providerId);
    resource.getProperties().setProperty("destination", resourceId);
    final ResourceInfo resourceInfo = configFactory.configureService(resource, ResourceInfo.class);
    logAutoCreateResource(resourceInfo, type, beanName);
    return installResource(beanName, resourceInfo);
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) 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