Search in sources :

Example 11 with ServiceProvider

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

the class ProviderManager method register.

private void register(final ID id, final ServiceProvider provider, final Set<ID> seen) {
    if (providers.containsKey(id)) {
        return;
    }
    if (provider.getParent() != null) {
        final ID parentId = ID.parse(provider.getParent(), id);
        // Pass in a stack on this call to prevent circular reference
        final ServiceProvider parent = getProvider(parentId, seen);
        if (parent == null) {
            throw new NoSuchParentProviderException(provider, parentId);
        }
        inherit(provider, parent);
    }
    validate(id, provider);
    providers.put(id, provider);
}
Also used : ServiceProvider(org.apache.openejb.config.sys.ServiceProvider)

Example 12 with ServiceProvider

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

the class ProviderManagerTest method testCircularDependency.

public void testCircularDependency() throws Exception {
    final ProviderManager manager = new ProviderManager(new ProviderLoader() {

        @Override
        public ServiceProvider load(final ID id) {
            if ("color".equalsIgnoreCase(id.getName())) {
                final ServiceProvider color = new ServiceProvider(Color.class, "Color", "Resource");
                color.setParent("Orange");
                color.getProperties().setProperty("red", "0");
                color.getProperties().setProperty("green", "0");
                color.getProperties().setProperty("blue", "0");
                return color;
            }
            if ("red".equalsIgnoreCase(id.getName())) {
                final ServiceProvider red = new ServiceProvider();
                red.setId("Red");
                red.setParent("Color");
                red.getProperties().setProperty("red", "255");
                return red;
            }
            if ("orange".equalsIgnoreCase(id.getName())) {
                final ServiceProvider orange = new ServiceProvider();
                orange.setId("Orange");
                orange.setParent("Red");
                orange.getProperties().setProperty("green", "200");
                return orange;
            }
            throw new IllegalStateException(id.toString());
        }

        @Override
        public List<ServiceProvider> load(final String namespace) {
            return null;
        }
    });
    assertEquals(0, manager.getAll().size());
    try {
        manager.get("dEFAUlT", "orAngE");
        fail("ProviderCircularReferenceException should have been thrown");
    } catch (final ProviderCircularReferenceException e) {
    // pass
    }
}
Also used : ProviderManager(org.apache.openejb.config.provider.ProviderManager) ServiceProvider(org.apache.openejb.config.sys.ServiceProvider) ProviderCircularReferenceException(org.apache.openejb.config.provider.ProviderCircularReferenceException) List(java.util.List) ArrayList(java.util.ArrayList) ID(org.apache.openejb.config.provider.ID) ProviderLoader(org.apache.openejb.config.provider.ProviderLoader)

Example 13 with ServiceProvider

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

the class ProviderManagerTest method testLoaderLoadNamespace.

public void testLoaderLoadNamespace() throws Exception {
    final ProviderManager manager = new ProviderManager(new ProviderLoader() {

        @Override
        public ServiceProvider load(final ID id) {
            if ("color".equalsIgnoreCase(id.getName())) {
                final ServiceProvider color = new ServiceProvider(Color.class, "Color", "Resource");
                color.getProperties().setProperty("red", "0");
                color.getProperties().setProperty("green", "0");
                color.getProperties().setProperty("blue", "0");
                return color;
            }
            if ("red".equalsIgnoreCase(id.getName())) {
                final ServiceProvider red = new ServiceProvider();
                red.setId("Red");
                red.setParent("Color");
                red.getProperties().setProperty("red", "255");
                return red;
            }
            if ("orange".equalsIgnoreCase(id.getName())) {
                final ServiceProvider orange = new ServiceProvider();
                orange.setId("Orange");
                orange.setParent("Red");
                orange.getProperties().setProperty("green", "200");
                return orange;
            }
            throw new IllegalStateException(id.toString());
        }

        @Override
        public List<ServiceProvider> load(final String namespace) {
            final List<ServiceProvider> list = new ArrayList<>();
            list.add(load(new ID(namespace, "color")));
            list.add(load(new ID(namespace, "red")));
            list.add(load(new ID(namespace, "orange")));
            return list;
        }
    });
    assertEquals(0, manager.getAll().size());
    manager.get("default", "Color");
    assertEquals(1, manager.getAll().size());
    manager.load("default");
    assertEquals(3, manager.getAll().size());
    {
        // Assert Orange
        // Should have inherited from Red
        final ServiceProvider provider = manager.get("dEFAUlT", "orAngE");
        assertNotNull(provider);
        assertEquals(Color.class.getName(), provider.getClassName());
        assertEquals("255", provider.getProperties().getProperty("reD"));
        assertEquals("200", provider.getProperties().get("grEeN"));
        assertEquals("0", provider.getProperties().get("bLue"));
    }
    {
        // Assert Red
        // Should have inherited green and blue values from Color
        final ServiceProvider provider = manager.get("dEFaulT", "REd");
        assertNotNull(provider);
        assertEquals(Color.class.getName(), provider.getClassName());
        assertEquals("255", provider.getProperties().getProperty("rED"));
        assertEquals("0", provider.getProperties().get("grEEN"));
        assertEquals("0", provider.getProperties().get("bLUe"));
    }
    {
        // Assert Color
        // Must be able to retrieve provider and properties in a case-insensitive manner
        final ServiceProvider provider = manager.get("DeFaulT", "CoLoR");
        assertNotNull(provider);
        assertEquals(Color.class.getName(), provider.getClassName());
        assertEquals("0", provider.getProperties().getProperty("rEd"));
        assertEquals("0", provider.getProperties().get("grEEn"));
        assertEquals("0", provider.getProperties().get("blUE"));
    }
    assertEquals(3, manager.getAll().size());
}
Also used : ProviderManager(org.apache.openejb.config.provider.ProviderManager) ServiceProvider(org.apache.openejb.config.sys.ServiceProvider) List(java.util.List) ArrayList(java.util.ArrayList) ID(org.apache.openejb.config.provider.ID) ProviderLoader(org.apache.openejb.config.provider.ProviderLoader)

Example 14 with ServiceProvider

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

the class ConfigurationFactory method getServiceProvider.

private <T extends ServiceInfo> ServiceProvider getServiceProvider(final org.apache.openejb.config.Service service, final Class<? extends T> infoType) throws OpenEJBException {
    final String providerType = getProviderType(service);
    final ServiceProvider provider = resolveServiceProvider(service, infoType);
    if (provider == null) {
        final List<ServiceProvider> providers = ServiceUtils.getServiceProvidersByServiceType(providerType);
        final StringBuilder sb = new StringBuilder();
        final List<String> types = new ArrayList<>();
        for (final ServiceProvider p : providers) {
            for (final String type : p.getTypes()) {
                if (types.contains(type)) {
                    continue;
                }
                types.add(type);
                sb.append(JavaSecurityManagers.getSystemProperty("line.separator"));
                sb.append("  <").append(p.getService());
                sb.append(" id=\"").append(service.getId()).append('"');
                sb.append(" type=\"").append(type).append("\"/>");
            }
        }
        final String noProviderMessage = messages.format("configureService.noProviderForService", providerType, service.getId(), service.getType(), service.getProvider(), sb.toString());
        throw new NoSuchProviderException(noProviderMessage);
    }
    if (!provider.getService().equals(providerType)) {
        throw new OpenEJBException(messages.format("configureService.wrongProviderType", service.getId(), providerType));
    }
    return provider;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ServiceProvider(org.apache.openejb.config.sys.ServiceProvider) ArrayList(java.util.ArrayList)

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