Package javax.naming

Examples of javax.naming.Context


        List<RARDeployable> rars = earDeployable.getRARDeployables();
        if (rars.size() > 0) {
            if (this.rarService == null) {
                logger.warn("There are RAR files in the EAR ''{0}'' but the resource service is not available", earDeployable);
            } else {
                Context ctx = new ContextImpl(earURL.toExternalForm());
                try {
                    ctx.rebind("earUrl", earURL);
                } catch (NamingException e) {
                    throw new DeployerException("Cannot add the EAR URL parameter '" + earURL + "'", e);
                }
                List<URL> urls = new ArrayList<URL>();
                for (RARDeployable rarDeployable : rars) {
                    try {
                        urls.add(rarDeployable.getArchive().getURL());
                    } catch (ArchiveException e) {
                        throw new DeployerException("Cannot get the URL for the archive '" + rarDeployable.getArchive() + "'",
                                e);
                    }
                }
                try {
                    ctx.rebind("urls", urls.toArray(new URL[urls.size()]));
                } catch (NamingException e) {
                    throw new DeployerException("Cannot add the urls parameter '" + urls + "'", e);
                }
                try {
                    ctx.rebind("earClassLoader", earClassLoader);
                } catch (NamingException e) {
                    throw new DeployerException("Cannot add the earClassLoader parameter '" + earClassLoader + "'", e);
                }
                try {
                    ctx.rebind("altDDs", new URL[urls.size()]);
                } catch (NamingException e) {
                    throw new DeployerException("Cannot add the altDDs parameter.'", e);
                }

                try {
View Full Code Here


                    }
                }


                // Deploy EJB 2.1 on JOnAS service
                Context ctx = new ContextImpl(earURL.toExternalForm());
                try {
                    ctx.rebind("earUrl", earURL);
                    ctx.rebind("earRootUrl", earURL);
                } catch (NamingException e) {
                    throw new DeployerException("Cannot add the EAR URL parameter '" + earURL + "'", e);
                }

                try {
                    ctx.rebind("jarURLs", urls.toArray(new URL[urls.size()]));
                } catch (NamingException e) {
                    throw new DeployerException("Cannot add the urls parameter '" + urls + "'", e);
                }
                try {
                    ctx.rebind("earClassLoader", earClassLoader);
                } catch (NamingException e) {
                    throw new DeployerException("Cannot add the earClassLoader parameter '" + earClassLoader + "'", e);
                }

                // Bind the EJB classloader
                try {
                    ctx.rebind("ejbClassLoader", ejbClassLoader);
                } catch (NamingException e) {
                    throw new DeployerException("Cannot add the ejbClassLoader parameter '" + ejbClassLoader + "'", e);
                }

                // Role names
                try {
                    ctx.rebind("roleNames", new String[0]);
                } catch (NamingException e) {
                    throw new DeployerException("Cannot add the altDDs parameter.'", e);
                }

                try {
View Full Code Here

     */
    public void handle(final IEvent event) {

        JavaContextNamingEvent cne = (JavaContextNamingEvent) event;
        Factory<?, ?> easyBeansFactory = cne.getFactory();
        Context javaCtx = cne.getJavaContext();
        Context compCtx = null;
        try {
            compCtx = (Context) javaCtx.lookup("comp");
        } catch (NamingException e) {
            throwException(cne, new IllegalStateException("Cannot get java:comp object", e));
        }

        // Unbind UserTransaction context if it is not a BMT bean
        // as specified in chapter 16.12 of EJB 3 spec.
        if (easyBeansFactory.getBeanInfo().getTransactionManagementType() == CONTAINER) {
            logger.debug("Bean is container managed so remove availability of java:comp/UserTransaction object");
            try {
                compCtx.unbind("UserTransaction");
            } catch (NamingException e) {
                throwException(cne, new IllegalStateException("Cannot remove java:comp/UserTransaction object", e));
            }
        }

        // bind EJBContext
        EasyBeansEJBContext<?> context = null;
        if (easyBeansFactory instanceof StatelessSessionFactory) {
            context = new EasyBeansSessionContext<StatelessSessionFactory>((StatelessSessionFactory) easyBeansFactory);
        } else if (easyBeansFactory instanceof StatefulSessionFactory) {
            context = new EasyBeansSessionContext<StatefulSessionFactory>((StatefulSessionFactory) easyBeansFactory);
        } else if (easyBeansFactory instanceof SingletonSessionFactory) {
                context = new EasyBeansSessionContext<SingletonSessionFactory>((SingletonSessionFactory) easyBeansFactory);
        } else if (easyBeansFactory instanceof MDBFactory) {
            context = new EasyBeansMessageDrivenContext((MDBFactory) easyBeansFactory);
        } else {
            throwException(cne, new IllegalStateException("Unable to detect factory type '" + easyBeansFactory + "'"));
        }


        try {
            compCtx.bind("EJBContext", context);
        } catch (NamingException e) {
            throwException(cne, new IllegalStateException("Cannot bind EJBContext", e));
        }

        // bind TimerService
        try {
            compCtx.bind("TimerService", context.getTimerService());
        } catch (NamingException e) {
            throwException(cne, new IllegalStateException("Cannot bind TimerService", e));
        }
    }
View Full Code Here

            // Sets writable mode
            ContextAccessController.setSecurityToken(getName(), this.container);
            ContextAccessController.setWritable(getName(), this.container);

            // Gets the java:comp/env context
            Context context = getNamingContext();
            Context envCtx;
            try {
                envCtx = (Context) context.lookup("comp/env");
            } catch (NamingException e) {
                throw new IllegalStateException("Unable to get comp/env subcontext", e);
            }

            // Needs to bind object

            // Persistence context
            EZBPersistenceUnitManager persistenceUnitManager = this.injectionHolder.getPersistenceUnitManager();
            for (IENCBinding<IJavaxPersistenceContext> binding : this.encBindingHolder.getPersistenceContextBindings()) {
                String encName = binding.getName();
                String unitName = binding.getValue().getUnitName();
                PersistenceContextType type = binding.getValue().getType();
                EntityManager em = persistenceUnitManager.getEntityManager(unitName, type);

                try {
                    createSubcontexts(envCtx, encName);
                    envCtx.rebind(encName, em);
                    if (this.logger.isDebugEnabled()) {
                        this.logger.debug("Adding persistence-context 'java:comp/env/" + encName + "'");
                    }
                } catch (NamingException e) {
                    this.logger.error("Unable to bind persistence-context in ENC", e);
                }
            }

            // Persistence unit
            for (IENCBinding<IJavaxPersistenceUnit> binding : this.encBindingHolder.getPersistenceUnitBindings()) {
                String encName = binding.getName();
                String unitName = binding.getValue().getUnitName();
                EntityManagerFactory emf = persistenceUnitManager.getEntityManagerFactory(unitName);

                try {
                    createSubcontexts(envCtx, encName);
                    envCtx.rebind(encName, emf);
                    if (this.logger.isDebugEnabled()) {
                        this.logger.debug("Adding persistence-unit 'java:comp/env/" + encName + "'");
                    }
                } catch (NamingException e) {
                    this.logger.error("Unable to bind persistence-unit in ENC", e);
                }
            }

            // EJBs
            EZBJNDIResolver jndiResolver = this.injectionHolder.getJNDIResolver();
            if (jndiResolver != null) {
                for (IENCBinding<IJEjbEJB> binding : this.encBindingHolder.getEJBBindings()) {
                    String encName = binding.getName();
                    IJEjbEJB jEjbEJB = binding.getValue();
                    String interfaceName = jEjbEJB.getBeanInterface();
                    String beanName = jEjbEJB.getBeanName();

                    String jndiName;
                    try {
                        jndiName = jndiResolver.getEJBJNDIUniqueName(interfaceName, beanName);

                        // Bind a link to this JNDI name
                        try {
                            createSubcontexts(envCtx, encName);
                            envCtx.rebind(encName, new LinkRef(jndiName));
                            if (this.logger.isDebugEnabled()) {
                                this.logger.debug("Adding ejb 'java:comp/env/" + encName + "' from JNDIName '" + jndiName
                                        + "'.");
                            }
                        } catch (NamingException e) {
View Full Code Here

    public void handle(final IEvent event) {

        JavaContextNamingEvent javaContextNamingEvent = (JavaContextNamingEvent) event;
        String beanName = javaContextNamingEvent.getFactory().getBeanInfo().getName();

        Context javaContext = javaContextNamingEvent.getJavaContext();
        Context javaCompEnvCtx = null;
        try {
            javaCompEnvCtx = (Context) javaContext.lookup("comp/env");
        } catch (NamingException e) {
            throwException(javaContextNamingEvent, new IllegalStateException("Cannot lookup java:comp/env element.", e));
        }

        Context javaModuleCtx = null;
        try {
            javaModuleCtx = (Context) javaContext.lookup("module");
        } catch (NamingException e) {
            throwException(javaContextNamingEvent, new IllegalStateException("Cannot lookup java:module element.", e));
        }

        Context javaAppCtx = null;
        try {
            javaAppCtx = (Context) javaContext.lookup("app");
        } catch (NamingException e) {
            throwException(javaContextNamingEvent, new IllegalStateException("Cannot lookup java:app element.", e));
        }

        Context javaGlobalCtx = null;
        try {
            javaGlobalCtx = (Context) javaContext.lookup("app");
        } catch (NamingException e) {
            throwException(javaContextNamingEvent, new IllegalStateException("Cannot lookup java:app element.", e));
        }

        for (IEnvEntry envEntry : javaContextNamingEvent.getBeanMetadata().getEnvEntryCollection()) {
            String name = envEntry.getName();
            Object value = getEnvEntryValue(envEntry, javaContextNamingEvent);
            // if null, no entry in java:comp/env as specified chapter 15.4.1
            if (value != null) {

                Context bindContext = javaCompEnvCtx;

                // if name starts with "java:" locates the correct namespace
                if (name != null && name.startsWith("java:")) {

                    // check authorized context
                    if (name.startsWith("java:module/")) {
                        bindContext = javaModuleCtx;
                        name = name.substring("java:module/".length());
                    } else if (name.startsWith("java:app/")) {
                        bindContext = javaAppCtx;
                        name = name.substring("java:app/".length());
                    } else if (name.startsWith("java:global/")) {
                        bindContext = javaGlobalCtx;
                        name = name.substring("java:global/".length());
                    } else if (name.startsWith("java:comp/")) {
                        bindContext = javaCompEnvCtx;
                        name = name.substring("java:comp/".length());
                    } else {
                        throwException(javaContextNamingEvent, new IllegalStateException("Invalid Env Entry name '" + name
                                + "' for bean '" + beanName + "'."));
                    }

                }

                try {
                    bindContext.rebind(name, value);
                } catch (NamingException e) {
                    throwException(javaContextNamingEvent, new IllegalStateException("Cannot bind element '" + name
                            + "' for bean '" + beanName + "'.", e));
                }
            }
View Full Code Here

        // Create a new environment
        ContextImpl ctx = new ContextImpl(namespace, true, null);

        // Create subContext
        Context compCtx = (Context) ctx.lookup("comp");

        // Bind java:comp/UserTransaction
        if (this.userTransaction == null) {
            try {
                this.userTransaction = (UserTransaction) this.ictx.lookup("javax.transaction.UserTransaction");
            } catch (NamingException e) {
                logger.error("Cannot lookup UserTransaction.", e);
            }
        }
        if (this.userTransaction != null) {
            compCtx.rebind("UserTransaction", this.userTransaction);
        }

        // bind ORB
        try {
            compCtx.rebind("ORB", ORBInitHelper.getORB());
        } catch (NamingException e) {
            logger.error("Cannot bind ORB", e);
        }

        return ctx;
View Full Code Here

     * @return Context the component context.
     * @throws NamingException When operation is not allowed
     */
    public Context getComponentContext() throws NamingException {

        Context ctx = null;

        // Check if there is a context to the local thread
        // For ejbs
        ctx = threadContext.get();
        if (ctx != null) {
View Full Code Here

     * After, resetComponentContext should be called to reset the context.
     * @param ctx the context to associate to the current thread.
     * @return Context the context of the thread
     */
    public Context setComponentContext(final Context ctx) {
        Context ret = threadContext.get();
        threadContext.set(ctx);
        return ret;
    }
View Full Code Here

     * Main method.
     * @param args the arguments (not required)
     * @throws Exception if exception is found.
     */
    public static void main(final String[] args) throws Exception {
        Context initialContext = getInitialContext();

        TimerBeanRemote timerBean = (TimerBeanRemote) initialContext.lookup("timerBean");

        System.out.println("Calling init method that will fire a new timer...");
        timerBean.init();
    }
View Full Code Here

     * @throws Exception needs for signature of interceptor.
     */
    @Override
    public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception {
        // Get the subcontext (comp)
        Context oldContext = (Context) setComponentContextMethod.invoke(null, invocationContext
                .getFactory().getJavaContext().lookup("comp"));
        try {
            return invocationContext.proceed();
        } finally {
            resetComponentContextMethod.invoke(null, oldContext);
View Full Code Here

TOP

Related Classes of javax.naming.Context

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.