Search in sources :

Example 1 with NamingObjectFactory

use of com.sun.enterprise.naming.spi.NamingObjectFactory in project Payara by payara.

the class ComponentEnvManagerImpl method getCompEnvBinding.

private CompEnvBinding getCompEnvBinding(final ResourceEnvReferenceDescriptor next) {
    final String name = descriptorToLogicalJndiName(next);
    Object value = null;
    if (next.isEJBContext()) {
        value = new EjbContextProxy(next.getRefType());
    } else if (next.isValidator()) {
        value = new ValidatorProxy(_logger);
    } else if (next.isValidatorFactory()) {
        value = new ValidatorFactoryProxy(_logger);
    } else if (next.isCDIBeanManager()) {
        value = namingUtils.createLazyNamingObjectFactory(name, "java:comp/BeanManager", false);
    } else if (next.isManagedBean()) {
        ManagedBeanDescriptor managedBeanDesc = next.getManagedBeanDescriptor();
        if (processEnv.getProcessType().isServer()) {
            value = namingUtils.createLazyNamingObjectFactory(name, next.getJndiName(), false);
        } else {
            value = namingUtils.createLazyNamingObjectFactory(name, managedBeanDesc.getAppJndiName(), false);
        }
    } else {
        // lookup in the InitialContext
        value = new NamingObjectFactory() {

            // It might be mapped to a managed bean, so turn off caching to ensure that a
            // new instance is created each time.
            NamingObjectFactory delegate = namingUtils.createLazyNamingObjectFactory(name, next.getJndiName(), false);

            public boolean isCreateResultCacheable() {
                return false;
            }

            public Object create(Context ic) throws NamingException {
                return delegate.create(ic);
            }
        };
    }
    return new CompEnvBinding(name, value);
}
Also used : Context(javax.naming.Context) NamingObjectFactory(com.sun.enterprise.naming.spi.NamingObjectFactory) NamingException(javax.naming.NamingException)

Example 2 with NamingObjectFactory

use of com.sun.enterprise.naming.spi.NamingObjectFactory in project Payara by payara.

the class ComponentEnvManagerImpl method addResourceReferences.

private void addResourceReferences(ScopeType scope, Iterator resRefItr, Collection<JNDIBinding> jndiBindings) {
    while (resRefItr.hasNext()) {
        ResourceReferenceDescriptor resourceRef = (ResourceReferenceDescriptor) resRefItr.next();
        if (!dependencyAppliesToScope(resourceRef, scope)) {
            continue;
        }
        resourceRef.checkType();
        String name = descriptorToLogicalJndiName(resourceRef);
        Object value = null;
        String physicalJndiName = resourceRef.getJndiName();
        // or another jndi-name that can be looked up
        if (resourceRef.isURLResource()) {
            if (physicalJndiName.startsWith(JAVA_GLOBAL_PREFIX) || physicalJndiName.startsWith(JAVA_APP_PREFIX) || physicalJndiName.startsWith(JAVA_MODULE_PREFIX) || physicalJndiName.startsWith(JAVA_COMP_PREFIX)) {
                // for jndi-name or lookup-name like "java:module/env/url/testUrl"
                value = namingUtils.createLazyNamingObjectFactory(name, physicalJndiName, false);
            } else {
                try {
                    // for jndi-name like "http://localhost:8080/index.html"
                    Object obj = new java.net.URL(physicalJndiName);
                    NamingObjectFactory factory = namingUtils.createSimpleNamingObjectFactory(name, obj);
                    value = namingUtils.createCloningNamingObjectFactory(name, factory);
                } catch (MalformedURLException e) {
                    // for jndi-name or lookup-name like "url/testUrl"
                    value = namingUtils.createLazyNamingObjectFactory(name, physicalJndiName, false);
                }
            }
        } else if (resourceRef.isORB()) {
            // TODO handle non-default ORBs
            value = namingUtils.createLazyNamingObjectFactory(name, physicalJndiName, false);
        } else if (resourceRef.isWebServiceContext()) {
            WebServiceReferenceManager wsRefMgr = locator.getService(WebServiceReferenceManager.class);
            if (wsRefMgr != null) {
                value = wsRefMgr.getWSContextObject();
            } else {
                _logger.log(Level.SEVERE, "Cannot find the following class to proceed with @Resource WebServiceContext" + wsRefMgr + "Please confirm if webservices module is installed ");
            }
        } else if (resourceRef.isJDBCResource() || resourceRef.isJMSConnectionFactory() || resourceRef.isMailResource() || resourceRef.isResourceConnectionFactory()) {
            value = namingUtils.createLazyInitializationNamingObjectFactory(name, physicalJndiName, false);
        } else {
            value = namingUtils.createLazyNamingObjectFactory(name, physicalJndiName, false);
        }
        jndiBindings.add(new CompEnvBinding(name, value));
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) NamingObjectFactory(com.sun.enterprise.naming.spi.NamingObjectFactory) WebServiceReferenceManager(com.sun.enterprise.container.common.spi.WebServiceReferenceManager)

Example 3 with NamingObjectFactory

use of com.sun.enterprise.naming.spi.NamingObjectFactory in project Payara by payara.

the class AppTest method testCachingNamingObjectFactory.

@Test
public void testCachingNamingObjectFactory() {
    GlassfishNamingManagerImpl nm = null;
    try {
        InvocationManager im = new InvocationManagerImpl();
        InitialContext ic = newInitialContext();
        nm = new GlassfishNamingManagerImpl(ic);
        nm.publishObject("foo", "Hello: foo", false);
        System.out.println("**lookup() ==> " + ic.lookup("foo"));
        NamingObjectFactory factory = new NamingObjectFactory() {

            private int counter = 1;

            public boolean isCreateResultCacheable() {
                return true;
            }

            public Object create(Context ic) {
                return ("FACTORY_Created: " + counter++);
            }

            public String toString() {
                return "Huh? ";
            }
        };
        nm.publishObject("bar", factory, false);
        System.out.println("**lookup() ==> " + ic.lookup("bar"));
        System.out.println("**lookup() ==> " + ic.lookup("bar"));
        assert (true);
    } catch (Exception ex) {
        ex.printStackTrace();
        assert (false);
    } finally {
        try {
            nm.unpublishObject("foo");
            nm.unpublishObject("bar");
        } catch (Exception ex) {
        // ignore
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) InvocationManagerImpl(org.glassfish.api.invocation.InvocationManagerImpl) InvocationManager(org.glassfish.api.invocation.InvocationManager) NamingObjectFactory(com.sun.enterprise.naming.spi.NamingObjectFactory) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) InvocationException(org.glassfish.api.invocation.InvocationException)

Example 4 with NamingObjectFactory

use of com.sun.enterprise.naming.spi.NamingObjectFactory in project Payara by payara.

the class AppTest method testNonCachingNamingObjectFactory.

@Test
public void testNonCachingNamingObjectFactory() {
    GlassfishNamingManagerImpl nm = null;
    InvocationManager im = new InvocationManagerImpl();
    ComponentInvocation inv = null;
    try {
        InitialContext ic = newInitialContext();
        triggerLoadingNamedProxies(ic);
        nm = new GlassfishNamingManagerImpl(ic);
        nm.setInvocationManager(im);
        List<Binding> bindings = new ArrayList<Binding>();
        NamingObjectFactory intFactory = new NamingObjectFactory() {

            private int counter = 1;

            public boolean isCreateResultCacheable() {
                return false;
            }

            public Object create(Context ic) {
                return new Integer(++counter);
            }

            public String toString() {
                return "Huh? ";
            }
        };
        bindings.add(new Binding("conf/area", intFactory));
        bindings.add(new Binding("conf/location", "Santa Clara"));
        nm.bindToComponentNamespace("app1", "mod1", "comp1", false, bindings);
        inv = new ComponentInvocation("comp1", ComponentInvocation.ComponentInvocationType.EJB_INVOCATION, null, null, null);
        im.preInvoke(inv);
        System.out.println("**lookup(java:comp/env/conf/area) ==> " + ic.lookup("java:comp/env/conf/area"));
        System.out.println("**lookup(java:comp/env/conf/location) ==> " + ic.lookup("java:comp/env/conf/location"));
        NamingObjectFactory floatFactory = new NamingObjectFactory() {

            private int counter = 1;

            public boolean isCreateResultCacheable() {
                return false;
            }

            public Object create(Context ic) {
                return Float.valueOf(("7" + (++counter)) + "." + 2323);
            }

            public String toString() {
                return "Huh? ";
            }
        };
        List<Binding> bindings2 = new ArrayList<Binding>();
        bindings2.add(new Binding("conf/area", floatFactory));
        bindings2.add(new Binding("conf/location", "Santa Clara[14]"));
        nm.bindToComponentNamespace("app1", "mod1", "comp2", false, bindings2);
        inv = new ComponentInvocation("comp2", ComponentInvocation.ComponentInvocationType.EJB_INVOCATION, null, null, null);
        im.preInvoke(inv);
        System.out.println("**lookup(java:comp/env/conf/area) ==> " + ic.lookup("java:comp/env/conf/area"));
        System.out.println("**lookup(java:comp/env/conf/location) ==> " + ic.lookup("java:comp/env/conf/location"));
        assert (true);
    } catch (InvocationException inEx) {
        inEx.printStackTrace();
        assert (false);
    } catch (Exception ex) {
        ex.printStackTrace();
        assert (false);
    } finally {
        try {
            im.postInvoke(inv);
            nm.unbindComponentObjects("comp1");
        } catch (InvocationException inEx) {
        } catch (Exception ex) {
        }
    }
}
Also used : JNDIBinding(org.glassfish.api.naming.JNDIBinding) InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation) InvocationManager(org.glassfish.api.invocation.InvocationManager) ArrayList(java.util.ArrayList) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) InvocationException(org.glassfish.api.invocation.InvocationException) InvocationManagerImpl(org.glassfish.api.invocation.InvocationManagerImpl) InvocationException(org.glassfish.api.invocation.InvocationException) NamingObjectFactory(com.sun.enterprise.naming.spi.NamingObjectFactory)

Aggregations

NamingObjectFactory (com.sun.enterprise.naming.spi.NamingObjectFactory)4 Context (javax.naming.Context)3 NamingException (javax.naming.NamingException)3 InitialContext (javax.naming.InitialContext)2 InvocationException (org.glassfish.api.invocation.InvocationException)2 InvocationManager (org.glassfish.api.invocation.InvocationManager)2 InvocationManagerImpl (org.glassfish.api.invocation.InvocationManagerImpl)2 WebServiceReferenceManager (com.sun.enterprise.container.common.spi.WebServiceReferenceManager)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 ComponentInvocation (org.glassfish.api.invocation.ComponentInvocation)1 JNDIBinding (org.glassfish.api.naming.JNDIBinding)1