Search in sources :

Example 36 with OpenEjbConfiguration

use of org.apache.openejb.assembler.classic.OpenEjbConfiguration in project tomee by apache.

the class ServiceManager method initServer.

protected ServerService initServer(final String serviceName, final Properties serviceProperties) throws IOException {
    final DiscoveryRegistry registry = SystemInstance.get().getComponent(DiscoveryRegistry.class);
    final OpenEjbConfiguration conf = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
    logger.debug("Processing ServerService(id=" + serviceName + ")");
    overrideProperties(serviceName, serviceProperties);
    serviceProperties.setProperty("name", serviceName);
    if (conf != null && conf.facilities != null) {
        final ServiceInfo info = new ServiceInfo();
        info.className = ((Class) serviceProperties.get(ServerService.class)).getName();
        info.service = "ServerService";
        info.id = serviceName;
        info.properties = serviceProperties;
        conf.facilities.services.add(info);
    }
    final boolean enabled = isEnabled(serviceProperties);
    logger.debug("Found ServerService(id=" + serviceName + ", disabled=" + (!enabled) + ")");
    if (enabled) {
        final Class serviceClass = (Class) serviceProperties.get(ServerService.class);
        logger.info("Creating ServerService(id=" + serviceName + ")");
        // log all properties on debug
        if (logger.isDebugEnabled()) {
            for (final Map.Entry<Object, Object> entry : serviceProperties.entrySet()) {
                logger.debug(entry.getKey() + " = " + entry.getValue());
            }
        }
        try {
            // Create Service
            ServerService service;
            ObjectRecipe recipe = new ObjectRecipe(serviceClass);
            try {
                // Do not import.  This class is not available in xbean-reflect-3.3
                final ReflectionUtil.StaticFactory factory = ReflectionUtil.findStaticFactory(serviceClass, "createServerService", null, null, serviceProperties.stringPropertyNames(), Collections.singleton(Option.NAMED_PARAMETERS));
                if (factory != null) {
                    // can throw an exception so call it before next line
                    recipe.setConstructorArgNames(factory.getParameterNames());
                    recipe.setFactoryMethod("createServerService");
                } else if (ReflectionUtil.findStaticFactory(serviceClass, "createServerService", null, null) != null) {
                    // old behavior, remove when sure previous check is ok
                    recipe.setFactoryMethod("createServerService");
                }
            } catch (final Throwable e) {
            //Ignore
            }
            recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
            recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
            service = (ServerService) recipe.create(serviceClass.getClassLoader());
            if (!(service instanceof SelfManaging)) {
                service = manage(serviceName, serviceProperties, service);
            }
            service.init(serviceProperties);
            if (service instanceof DiscoveryAgent) {
                final DiscoveryAgent agent = (DiscoveryAgent) service;
                registry.addDiscoveryAgent(agent);
            }
            if (LocalMBeanServer.isJMXActive()) {
                final MBeanServer server = LocalMBeanServer.get();
                register(serviceName, service, server);
            }
            return service;
        } catch (Throwable t) {
            t.printStackTrace();
            logger.error("service.instantiation.err", t, serviceClass.getName(), t.getClass().getName(), t.getMessage());
        }
    }
    return null;
}
Also used : ReflectionUtil(org.apache.xbean.recipe.ReflectionUtil) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration) ServiceInfo(org.apache.openejb.assembler.classic.ServiceInfo) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Map(java.util.Map) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer)

Example 37 with OpenEjbConfiguration

use of org.apache.openejb.assembler.classic.OpenEjbConfiguration 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

OpenEjbConfiguration (org.apache.openejb.assembler.classic.OpenEjbConfiguration)37 Test (org.junit.Test)24 File (java.io.File)23 Map (java.util.Map)9 Properties (java.util.Properties)9 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)7 Assembler (org.apache.openejb.assembler.classic.Assembler)7 AppInfo (org.apache.openejb.assembler.classic.AppInfo)6 List (java.util.List)5 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)4 BmpEntityContainerInfo (org.apache.openejb.assembler.classic.BmpEntityContainerInfo)3 CmpEntityContainerInfo (org.apache.openejb.assembler.classic.CmpEntityContainerInfo)3 ContainerInfo (org.apache.openejb.assembler.classic.ContainerInfo)3 ManagedContainerInfo (org.apache.openejb.assembler.classic.ManagedContainerInfo)3 MdbContainerInfo (org.apache.openejb.assembler.classic.MdbContainerInfo)3 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)3 ServiceInfo (org.apache.openejb.assembler.classic.ServiceInfo)3 SingletonSessionContainerInfo (org.apache.openejb.assembler.classic.SingletonSessionContainerInfo)3 StatefulSessionContainerInfo (org.apache.openejb.assembler.classic.StatefulSessionContainerInfo)3