Search in sources :

Example 1 with ProviderLoader

use of org.apache.openejb.config.provider.ProviderLoader in project tomee by apache.

the class ProviderManagerTest method testInheritedAttributes.

public void testInheritedAttributes() 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.setClassName(Color.class.getName());
                color.setFactoryName("fooFactory");
                color.setId("Color");
                color.setService("Resource");
                color.setConstructor("one, two, three");
                color.setDescription("the description");
                color.setDisplayName("the display name");
                color.getProperties().setProperty("red", "0");
                color.getProperties().setProperty("green", "0");
                color.getProperties().setProperty("blue", "0");
                color.getTypes().add(Color.class.getName());
                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;
        }
    });
    {
        // Assert Orange
        // Should have inherited from Red
        final ServiceProvider provider = manager.get("dEFAUlT", "orAngE");
        assertNotNull(provider);
        assertEquals(Color.class.getName(), provider.getClassName());
        assertEquals("Resource", provider.getService());
        assertEquals("one, two, three", provider.getConstructor());
        assertEquals("the description", provider.getDescription());
        assertEquals("the display name", provider.getDisplayName());
        assertEquals("fooFactory", provider.getFactoryName());
        assertEquals("255", provider.getProperties().getProperty("reD"));
        assertEquals("200", provider.getProperties().get("grEeN"));
        assertEquals("0", provider.getProperties().get("bLue"));
        assertEquals(1, provider.getTypes().size());
    }
    {
        // 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("Resource", provider.getService());
        assertEquals("one, two, three", provider.getConstructor());
        assertEquals("the description", provider.getDescription());
        assertEquals("the display name", provider.getDisplayName());
        assertEquals("fooFactory", provider.getFactoryName());
        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("Resource", provider.getService());
        assertEquals("one, two, three", provider.getConstructor());
        assertEquals("the description", provider.getDescription());
        assertEquals("the display name", provider.getDisplayName());
        assertEquals("fooFactory", provider.getFactoryName());
        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 2 with ProviderLoader

use of org.apache.openejb.config.provider.ProviderLoader in project tomee by apache.

the class ProviderManagerTest method testLoader.

public void testLoader() 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) {
            return null;
        }
    });
    assertEquals(0, 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 3 with ProviderLoader

use of org.apache.openejb.config.provider.ProviderLoader 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 4 with ProviderLoader

use of org.apache.openejb.config.provider.ProviderLoader 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 5 with ProviderLoader

use of org.apache.openejb.config.provider.ProviderLoader in project tomee by apache.

the class ServiceProviderInheritanceTest method test.

public void test() throws Exception {
    SystemInstance.get().setComponent(ProviderManager.class, new ProviderManager(new ProviderLoader() {

        final ProviderLoader loader = new ServiceJarXmlLoader();

        @Override
        public ServiceProvider load(final ID id) {
            {
                // try the regular loader
                final ServiceProvider provider = loader.load(id);
                if (provider != null)
                    return provider;
            }
            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");
                color.getTypes().add(Color.class.getName());
                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 = loader.load(namespace);
            list.add(load(new ID(namespace, "color")));
            list.add(load(new ID(namespace, "red")));
            list.add(load(new ID(namespace, "orange")));
            return list;
        }
    }));
    final ConfigurationFactory factory = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createSecurityService(factory.configureService(SecurityServiceInfo.class));
    assembler.createTransactionManager(factory.configureService(TransactionServiceInfo.class));
    {
        final Resource orange = new Resource("Orange", Color.class.getName(), "Orange");
        final ResourceInfo resourceInfo = factory.configureService(orange, ResourceInfo.class);
        assertEquals(Color.class.getName(), resourceInfo.className);
        assertEquals("Orange", resourceInfo.id);
        assertEquals(3, resourceInfo.properties.size());
        assertEquals("255", resourceInfo.properties.get("red"));
        assertEquals("200", resourceInfo.properties.get("green"));
        assertEquals("0", resourceInfo.properties.get("blue"));
        assembler.createResource(resourceInfo);
    }
    {
        final EjbJar ejbJar = new EjbJar();
        ejbJar.addEnterpriseBean(new SingletonBean(MyBean.class));
        final AppContext application = assembler.createApplication(factory.configureApplication(new EjbModule(ejbJar)));
        final MyBean myBean = (MyBean) application.getBeanContexts().get(0).getBusinessLocalBeanHome().create();
        final Color color = myBean.getColor();
        assertNotNull(color);
        assertEquals(255, color.getRed());
        assertEquals(200, color.getGreen());
        assertEquals(0, color.getBlue());
    }
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) AppContext(org.apache.openejb.AppContext) EjbModule(org.apache.openejb.config.EjbModule) ProviderLoader(org.apache.openejb.config.provider.ProviderLoader) SingletonBean(org.apache.openejb.jee.SingletonBean) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) ProviderManager(org.apache.openejb.config.provider.ProviderManager) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) ID(org.apache.openejb.config.provider.ID) Assembler(org.apache.openejb.assembler.classic.Assembler) ServiceJarXmlLoader(org.apache.openejb.config.provider.ServiceJarXmlLoader) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) EjbJar(org.apache.openejb.jee.EjbJar)

Aggregations

ID (org.apache.openejb.config.provider.ID)5 ProviderLoader (org.apache.openejb.config.provider.ProviderLoader)5 ProviderManager (org.apache.openejb.config.provider.ProviderManager)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ServiceProvider (org.apache.openejb.config.sys.ServiceProvider)4 AppContext (org.apache.openejb.AppContext)1 Assembler (org.apache.openejb.assembler.classic.Assembler)1 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)1 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)1 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)1 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)1 EjbModule (org.apache.openejb.config.EjbModule)1 ProviderCircularReferenceException (org.apache.openejb.config.provider.ProviderCircularReferenceException)1 ServiceJarXmlLoader (org.apache.openejb.config.provider.ServiceJarXmlLoader)1 EjbJar (org.apache.openejb.jee.EjbJar)1 SingletonBean (org.apache.openejb.jee.SingletonBean)1