Search in sources :

Example 1 with ServiceProvider

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

the class ConfigurationFactory method resolveServiceProvider.

@SuppressWarnings({ "unchecked" })
private ServiceProvider resolveServiceProvider(final org.apache.openejb.config.Service service, final Class infoType) throws OpenEJBException {
    if (service.getClassName() != null) {
        if (service.getType() == null) {
            service.setType(service.getClassName());
        }
        final ServiceProvider provider = new ServiceProvider();
        provider.setId(service.getId());
        provider.setService(getProviderType(service));
        provider.getTypes().add(service.getType());
        provider.setClassName(service.getClassName());
        provider.setConstructor(service.getConstructor());
        provider.setFactoryName(service.getFactoryName());
        return provider;
    }
    if (service.getProvider() != null) {
        return ServiceUtils.getServiceProvider(service.getProvider());
    }
    if (service.getType() == null && serviceTypeIsAdjustable) {
        // try to guess quickly for know type
        // -> DataSource
        // the algo is weird but works, don't try to simplify it too much
        // because we just have the service properties, not our defaults
        final Properties properties = service.getProperties();
        if ((properties.containsKey("JdbcDriver") || properties.containsKey("JdbcUrl") || properties.containsKey("url")) && (properties.containsKey("JtaManaged") || properties.containsKey("UserName") || properties.containsKey("Password"))) {
            service.setType("javax.sql.DataSource");
        }
    }
    if (service.getType() != null) {
        return ServiceUtils.getServiceProviderByType(getProviderType(service), service.getType());
    }
    if (service.getId() != null) {
        try {
            return ServiceUtils.getServiceProvider(service.getId());
        } catch (final NoSuchProviderException e) {
            logger.debug("resolveServiceProvider", e);
        }
    }
    if (infoType != null) {
        final org.apache.openejb.config.Service defaultService = getDefaultService(infoType);
        if (defaultService != null) {
            return resolveServiceProvider(defaultService, null);
        }
    }
    return null;
}
Also used : ServiceProvider(org.apache.openejb.config.sys.ServiceProvider) Properties(java.util.Properties) SuperProperties(org.apache.openejb.util.SuperProperties)

Example 2 with ServiceProvider

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

the class ConfigurationFactory method configureService.

/**
 * This is the major piece of code that configures services.
 * It merges the data from the <ServiceProvider> declaration
 * with the data from the openejb.xml file (say <Resource>)
 *
 * The end result is a canonical (i.e. flattened) ServiceInfo
 * The ServiceInfo will be of a specific type (ContainerInfo, ResourceInfo, etc)
 *
 * @param service  Service
 * @param infoType Class
 * @param <T>      infoType
 * @return ServiceInfo
 * @throws OpenEJBException On error
 */
public <T extends ServiceInfo> T configureService(org.apache.openejb.config.Service service, final Class<? extends T> infoType) throws OpenEJBException {
    try {
        if (infoType == null) {
            throw new NullPointerException("type");
        }
        if (service == null) {
            service = getDefaultService(infoType);
            if (service == null) {
                throw new OpenEJBException(messages.format("configureService.noDefaultService", infoType.getName()));
            }
        }
        {
            String template = service.getTemplate();
            if (template == null) {
                template = SystemInstance.get().getProperty(Template.class.getName());
            }
            if (template != null) {
                template = unaliasPropertiesProvider(template);
                // don't trim them, user wants to handle it himself, let him do it
                final ObjectRecipe recipe = newObjectRecipe(template);
                recipe.setProperty("serviceId", service.getId());
                // note: we can also use reflection if needed to limit the dependency
                Template.class.cast(recipe.create()).configure(service);
            }
        }
        final ServiceProvider provider = getServiceProvider(service, infoType);
        if (service.getId() == null) {
            service.setId(provider.getId());
        }
        final Properties overrides = trim(getSystemProperties(overrideKey(service), provider.getService()));
        final Properties serviceProperties = service.getProperties();
        trim(serviceProperties);
        trim(provider.getProperties());
        logger.info("configureService.configuring", service.getId(), provider.getService(), provider.getId());
        if (logger.isDebugEnabled()) {
            for (final Map.Entry<Object, Object> entry : serviceProperties.entrySet()) {
                final Object key = entry.getKey();
                Object value = entry.getValue();
                if (key instanceof String && "password".equalsIgnoreCase((String) key)) {
                    value = "<hidden>";
                }
                logger.debug("[" + key + "=" + value + "]");
            }
            for (final Map.Entry<Object, Object> entry : overrides.entrySet()) {
                final Object key = entry.getKey();
                Object value = entry.getValue();
                if (key instanceof String && "password".equalsIgnoreCase((String) key)) {
                    value = "<hidden>";
                }
                logger.debug("Override [" + key + "=" + value + "]");
            }
        }
        final Properties props = new SuperProperties().caseInsensitive(true);
        // weird hack but sometimes we don't want default values when we want null for instance
        if (serviceProperties == null || "false".equals(serviceProperties.getProperty(IGNORE_DEFAULT_VALUES_PROP, "false"))) {
            props.putAll(provider.getProperties());
        }
        if (serviceProperties != null) {
            props.putAll(serviceProperties);
        }
        props.putAll(overrides);
        {
            // force user properties last
            String propertiesProvider = service.getPropertiesProvider();
            if (propertiesProvider == null) {
                propertiesProvider = SystemInstance.get().getProperty(PropertiesResourceProvider.class.getName());
            }
            if (propertiesProvider != null) {
                propertiesProvider = unaliasPropertiesProvider(propertiesProvider);
                // don't trim them, user wants to handle it himself, let him do it
                final ObjectRecipe recipe = newObjectRecipe(propertiesProvider);
                recipe.setFactoryMethod("provides");
                recipe.setProperty("serviceId", service.getId());
                recipe.setProperties(props);
                // let user get all config
                recipe.setProperty("properties", props);
                final Properties p = Properties.class.cast(recipe.create());
                props.putAll(p);
            }
        }
        props.remove(IGNORE_DEFAULT_VALUES_PROP);
        final T info;
        try {
            info = infoType.newInstance();
        } catch (final Exception e) {
            throw new OpenEJBException(messages.format("configureService.cannotInstantiateClass", infoType.getName()), e);
        }
        // some jndi adjustment
        if (service.getId().startsWith("java:/")) {
            service.setId(service.getId().substring("java:/".length()));
        }
        info.service = provider.getService();
        info.types.addAll(provider.getTypes());
        info.description = provider.getDescription();
        info.displayName = provider.getDisplayName();
        info.className = provider.getClassName();
        info.factoryMethod = provider.getFactoryName();
        info.id = service.getId();
        info.properties = props;
        info.constructorArgs.addAll(parseConstructorArgs(provider));
        if (info instanceof ResourceInfo && service instanceof Resource) {
            final ResourceInfo ri = ResourceInfo.class.cast(info);
            final Resource resource = Resource.class.cast(service);
            ri.jndiName = resource.getJndi();
            ri.postConstruct = resource.getPostConstruct();
            ri.preDestroy = resource.getPreDestroy();
            ri.aliases.addAll(resource.getAliases());
            ri.dependsOn.addAll(resource.getDependsOn());
        }
        if (service.getClasspath() != null && service.getClasspath().length() > 0) {
            info.classpath = resolveClasspath(service.getClasspath());
        }
        info.classpathAPI = service.getClasspathAPI();
        specialProcessing(info);
        return info;
    } catch (final NoSuchProviderException e) {
        final String message = logger.fatal("configureService.failed", e, (null != service ? service.getId() : ""));
        throw new OpenEJBException(message + ": " + e.getMessage());
    } catch (final Throwable e) {
        final String message = logger.fatal("configureService.failed", e, (null != service ? service.getId() : ""));
        throw new OpenEJBException(message, e);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) Resource(org.apache.openejb.config.sys.Resource) Properties(java.util.Properties) SuperProperties(org.apache.openejb.util.SuperProperties) SuperProperties(org.apache.openejb.util.SuperProperties) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) Template(org.apache.openejb.api.resource.Template) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) ServiceProvider(org.apache.openejb.config.sys.ServiceProvider) PropertiesResourceProvider(org.apache.openejb.api.resource.PropertiesResourceProvider) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ServiceProvider

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

the class ServiceUtils method hasServiceProvider.

public static boolean hasServiceProvider(final String id) {
    try {
        final ProviderInfo info = getProviderInfo(id);
        final List<ServiceProvider> services = getServiceProviders(info.getPackageName());
        for (final ServiceProvider service : services) {
            if (service.getId().equals(id)) {
                return true;
            }
        }
    } catch (final OpenEJBException | IllegalStateException ignored) {
    // someone else will load the file and get the exception
    }
    return false;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ServiceProvider(org.apache.openejb.config.sys.ServiceProvider)

Example 4 with ServiceProvider

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

the class ServiceUtils method getServiceProvidersByServiceType.

public static List<ServiceProvider> getServiceProvidersByServiceType(final String type) throws OpenEJBException {
    final ArrayList<ServiceProvider> providers = new ArrayList<>();
    if (type == null) {
        return providers;
    }
    final List<ServiceProvider> services = getServiceProviders(currentDefaultProviderUrl(DEFAULT_PROVIDER_URL));
    for (final ServiceProvider service : services) {
        if (service.getService().equals(type)) {
            providers.add(service);
        }
    }
    return providers;
}
Also used : ServiceProvider(org.apache.openejb.config.sys.ServiceProvider) ArrayList(java.util.ArrayList)

Example 5 with ServiceProvider

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

the class ServiceJarXmlLoader method parse.

private void parse(final String namespace) {
    try {
        // Load and try again
        final ServicesJar servicesJar = JaxbOpenejb.readServicesJar(namespace);
        for (final ServiceProvider provider : servicesJar.getServiceProvider()) {
            final ID found = new ID(namespace, provider.getId());
            loaded.put(found, provider);
        }
    } catch (final OpenEJBException e) {
        throw new IllegalStateException(e);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ServiceProvider(org.apache.openejb.config.sys.ServiceProvider) ServicesJar(org.apache.openejb.config.sys.ServicesJar)

Aggregations

ServiceProvider (org.apache.openejb.config.sys.ServiceProvider)14 ArrayList (java.util.ArrayList)6 ProviderManager (org.apache.openejb.config.provider.ProviderManager)5 List (java.util.List)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 ID (org.apache.openejb.config.provider.ID)4 ProviderLoader (org.apache.openejb.config.provider.ProviderLoader)4 Properties (java.util.Properties)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)2 Resource (org.apache.openejb.config.sys.Resource)2 SuperProperties (org.apache.openejb.util.SuperProperties)2 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1