Search in sources :

Example 6 with ProviderManager

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

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

ProviderManager (org.apache.openejb.config.provider.ProviderManager)7 ID (org.apache.openejb.config.provider.ID)5 ProviderLoader (org.apache.openejb.config.provider.ProviderLoader)5 ServiceProvider (org.apache.openejb.config.sys.ServiceProvider)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ServiceJarXmlLoader (org.apache.openejb.config.provider.ServiceJarXmlLoader)3 File (java.io.File)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 Pattern (java.util.regex.Pattern)1 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