Package org.apache.openejb

Examples of org.apache.openejb.AppContext


        assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
        assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));


        AppContext circleApp = createApp("circle", config, assembler);
        AppContext squareApp = createApp("square", config, assembler);

        {
            BeanContext bean = squareApp.getBeanContexts().get(0);

            Context context = bean.getJndiContext();

            assertTrue(context.lookup("global/square") instanceof Context);
            assertTrue(context.lookup("global/square/Bean") instanceof Bean);
View Full Code Here


    private DeploymentIndex deploymentIndex;

    @Before
    public void setUp() throws SystemException {
        method = Method.class.getMethods()[0];
        beanContext = new BeanContext("aDeploymentId", null, new ModuleContext("", null, "", new AppContext("", SystemInstance.get(), null, null, null, false), null), DeploymentIndexTest.class, null, null, null, null, null, null, null, null, null, null, false);
        deploymentIndex = new DeploymentIndex(new BeanContext[] {beanContext, beanContext});
    }
View Full Code Here

    protected void setUp() throws Exception {
        requestHandler = new BasicClusterableRequestHandler();
        request = (ClusterableRequest) mock(ClusterableRequest.class);
        response = (ClusterableResponse) mock(ClusterableResponse.class);
        clusteredContainer = (ClusteredRPCContainer) mock(ClusteredRPCContainer.class);
        beanContext = new BeanContext("aDeploymentId", null, new ModuleContext("", null, "", new AppContext("", SystemInstance.get(), null, null, null, false), null), BasicClusterableRequestHandlerTest.class, null, null, null, null, null, null, null, null, null, null, false);
    }
View Full Code Here

        }
    }

    private BeanContext createDeployment(Class<?> testClass) {
        try {
            return new BeanContext(null, new IvmContext(), new ModuleContext("", null, "", new AppContext("", SystemInstance.get(), testClass.getClassLoader(), new IvmContext(), new IvmContext(), false), new IvmContext()), testClass, null, null, null, null, null, null, null, null, null, BeanType.MANAGED, false);
        } catch (SystemException e) {
            throw new IllegalStateException(e);
        }
    }
View Full Code Here

        // deploy the applications
        Assembler assembler = getAssembler();
        for (AppInfo appInfo : appInfos) {
            try {
                AppContext appContext = assembler.createApplication(appInfo, assembler.createAppClassLoader(appInfo), true);
        List<BeanContext> deployments = appContext.getBeanContexts();
                if (export) {
                    for (BeanContext deployment : deployments) {
                        JndiNameStrategy strategy = createStrategy(appInfo, deployments, deployment);
                        Map<String, EJB> bindings = getEjbBindings(strategy, deployment);
                        for (Entry<String, EJB> entry : bindings.entrySet()) {
View Full Code Here

            return factory.getApplication();
        }

        if (wrappedApplication == null) {

            final AppContext appContext = webBeansContext.getService(AppContext.class);

            if (appContext != null && !appContext.isCdiEnabled()) {

                wrappedApplication = factory.getApplication();

            } else {
View Full Code Here

            File generatedJar = cmpJarBuilder.getJarFile();
            if (generatedJar != null) {
                classLoader = ClassLoaderUtil.createClassLoader(appInfo.path, new URL []{generatedJar.toURI().toURL()}, classLoader);
            }

            final AppContext appContext = new AppContext(appInfo.appId, SystemInstance.get(), classLoader, globalJndiContext, appJndiContext, appInfo.standaloneModule);
            appContext.getInjections().addAll(injections);
            appContext.getBindings().putAll(globalBindings);
            appContext.getBindings().putAll(appBindings);

            containerSystem.addAppContext(appContext);

            final Context containerSystemContext = containerSystem.getJNDIContext();
           
            if (!SystemInstance.get().hasProperty("openejb.geronimo")) {
                // Bean Validation
                // ValidatorFactory needs to be put in the map sent to the entity manager factory
                // so it has to be constructed before
                Map<String, ValidatorFactory> validatorFactories = new HashMap<String, ValidatorFactory>();
                for (ClientInfo clientInfo : appInfo.clients) {
                    validatorFactories.put(clientInfo.uniqueId, ValidatorBuilder.buildFactory(classLoader, clientInfo.validationInfo));
                }
                for (ConnectorInfo connectorInfo : appInfo.connectors) {
                    validatorFactories.put(connectorInfo.uniqueId, ValidatorBuilder.buildFactory(classLoader, connectorInfo.validationInfo));
                }
                for (EjbJarInfo ejbJarInfo : appInfo.ejbJars) {
                    validatorFactories.put(ejbJarInfo.uniqueId, ValidatorBuilder.buildFactory(classLoader, ejbJarInfo.validationInfo));
                }
                for (WebAppInfo webAppInfo : appInfo.webApps) {
                    validatorFactories.put(webAppInfo.uniqueId, ValidatorBuilder.buildFactory(classLoader, webAppInfo.validationInfo));
                }
                moduleIds.addAll(validatorFactories.keySet());

                // validators bindings
                for (Entry<String, ValidatorFactory> validatorFactory : validatorFactories.entrySet()) {
                    String id = validatorFactory.getKey();
                    ValidatorFactory factory = validatorFactory.getValue();
                    try {
                        containerSystemContext.bind(VALIDATOR_FACTORY_NAMING_CONTEXT + id, factory);
                        containerSystemContext.bind(VALIDATOR_NAMING_CONTEXT + id, factory.usingContext().getValidator());
                    } catch (NameAlreadyBoundException e) {
                        throw new OpenEJBException("ValidatorFactory already exists for module " + id, e);
                    } catch (Exception e) {
                        throw new OpenEJBException(e);
                    }
                }
            }
           
            // JPA - Persistence Units MUST be processed first since they will add ClassFileTransformers
            // to the class loader which must be added before any classes are loaded
            Map<String, String> units = new HashMap<String, String>();
            PersistenceBuilder persistenceBuilder = new PersistenceBuilder(persistenceClassLoaderHandler);
            for (PersistenceUnitInfo info : appInfo.persistenceUnits) {
                try {
                    EntityManagerFactory factory = persistenceBuilder.createEntityManagerFactory(info, classLoader);
                    containerSystem.getJNDIContext().bind(PERSISTENCE_UNIT_NAMING_CONTEXT + info.id, factory);
                    units.put(info.name, PERSISTENCE_UNIT_NAMING_CONTEXT + info.id);
                } catch (NameAlreadyBoundException e) {
                    throw new OpenEJBException("PersistenceUnit already deployed: " + info.persistenceUnitRootUrl);
                } catch (Exception e) {
                    throw new OpenEJBException(e);
                }
            }

            // Connectors
            for (ConnectorInfo connector : appInfo.connectors) {
                ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
                Thread.currentThread().setContextClassLoader(classLoader);
                try {
                    // todo add undeployment code for these
                    if (connector.resourceAdapter != null) {
                        createResource(connector.resourceAdapter);
                    }
                    for (ResourceInfo outbound : connector.outbound) {
                        createResource(outbound);
                    }
                    for (MdbContainerInfo inbound : connector.inbound) {
                        createContainer(inbound);
                    }
                    for (ResourceInfo adminObject : connector.adminObject) {
                        createResource(adminObject);
                    }
                } finally {
                    Thread.currentThread().setContextClassLoader(oldClassLoader);
                }
            }

            List<BeanContext> allDeployments = new ArrayList<BeanContext>();

            // EJB
            EjbJarBuilder ejbJarBuilder = new EjbJarBuilder(props, appContext);
            for (EjbJarInfo ejbJar : appInfo.ejbJars) {
                HashMap<String, BeanContext> deployments = ejbJarBuilder.build(ejbJar, injections);

                JaccPermissionsBuilder jaccPermissionsBuilder = new JaccPermissionsBuilder();
                PolicyContext policyContext = jaccPermissionsBuilder.build(ejbJar, deployments);
                jaccPermissionsBuilder.install(policyContext);

                TransactionPolicyFactory transactionPolicyFactory = createTransactionPolicyFactory(ejbJar, classLoader);
                for (BeanContext beanContext : deployments.values()) {

                    beanContext.setTransactionPolicyFactory(transactionPolicyFactory);
                }

                MethodTransactionBuilder methodTransactionBuilder = new MethodTransactionBuilder();
                methodTransactionBuilder.build(deployments, ejbJar.methodTransactions);

                MethodConcurrencyBuilder methodConcurrencyBuilder = new MethodConcurrencyBuilder();
                methodConcurrencyBuilder.build(deployments, ejbJar.methodConcurrency);

                for (BeanContext beanContext : deployments.values()) {
                    containerSystem.addDeployment(beanContext);
                }

                //bind ejbs into global jndi
                jndiBuilder.build(ejbJar, deployments);

                // setup timers/asynchronous methods - must be after transaction attributes are set
                for (BeanContext beanContext : deployments.values()) {
                    if (beanContext.getComponentType() != BeanType.STATEFUL) {
                        Method ejbTimeout = beanContext.getEjbTimeout();
                        boolean timerServiceRequired = false;
                        if (ejbTimeout != null) {
                            // If user set the tx attribute to RequiresNew change it to Required so a new transaction is not started
                            if (beanContext.getTransactionType(ejbTimeout) == TransactionType.RequiresNew) {
                                beanContext.setMethodTransactionAttribute(ejbTimeout, TransactionType.Required);
                            }
                            timerServiceRequired = true;
                        }
                        for (Iterator<Map.Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext();) {
                            Map.Entry<Method, MethodContext> entry = it.next();
                            MethodContext methodContext = entry.getValue();
                            if (methodContext.getSchedules().size() > 0) {
                                timerServiceRequired = true;
                                Method method = entry.getKey();
                                //TODO Need ?
                                if (beanContext.getTransactionType(method) == TransactionType.RequiresNew) {
                                    beanContext.setMethodTransactionAttribute(method, TransactionType.Required);
                                }
                            }
                        }
                        if (timerServiceRequired) {
                            // Create the timer
                            EjbTimerServiceImpl timerService = new EjbTimerServiceImpl(beanContext);
                            //Load auto-start timers
                            TimerStore timerStore = timerService.getTimerStore();
                            for (Iterator<Map.Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext();) {
                                Map.Entry<Method, MethodContext> entry = it.next();
                                MethodContext methodContext = entry.getValue();
                                for(ScheduleData scheduleData : methodContext.getSchedules()) {
                                    timerStore.createCalendarTimer(timerService, (String) beanContext.getDeploymentID(), null, entry.getKey(), scheduleData.getExpression(), scheduleData.getConfig());
                                }
                            }
                            beanContext.setEjbTimerService(timerService);
                        } else {
                            beanContext.setEjbTimerService(new NullEjbTimerServiceImpl());
                        }
                    }
                    //set asynchronous methods transaction
                    //TODO ???
                    for (Iterator<Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext();) {
                        Entry<Method, MethodContext> entry = it.next();
                        if (entry.getValue().isAsynchronous() && beanContext.getTransactionType(entry.getKey()) == TransactionType.RequiresNew) {
                            beanContext.setMethodTransactionAttribute(entry.getKey(), TransactionType.Required);
                        }
                    }
                }
                // process application exceptions
                for (ApplicationExceptionInfo exceptionInfo : ejbJar.applicationException) {
                    try {
                        Class exceptionClass = classLoader.loadClass(exceptionInfo.exceptionClass);
                        for (BeanContext beanContext : deployments.values()) {
                            beanContext.addApplicationException(exceptionClass, exceptionInfo.rollback, exceptionInfo.inherited);
                        }
                    } catch (ClassNotFoundException e) {
                        logger.error("createApplication.invalidClass", e, exceptionInfo.exceptionClass, e.getMessage());
                    }
                }

                allDeployments.addAll(deployments.values());
            }

            allDeployments = sort(allDeployments);

            appContext.getBeanContexts().addAll(allDeployments);

            new CdiBuilder().build(appInfo, appContext, allDeployments);

            ensureWebBeansContext(appContext);

            appJndiContext.bind("app/BeanManager", appContext.getBeanManager());
            appContext.getBindings().put("app/BeanManager", appContext.getBeanManager());

            // now that everything is configured, deploy to the container
            if (start) {
                // deploy
                for (BeanContext deployment : allDeployments) {
View Full Code Here

        deployedApplications.remove(appInfo.path);
        logger.info("destroyApplication.start", appInfo.path);

        fireBeforeApplicationDestroyed(appInfo);

        final AppContext appContext = containerSystem.getAppContext(appInfo.appId);

        EjbResolver globalResolver = new EjbResolver(null, EjbResolver.Scope.GLOBAL);
        for (AppInfo info : deployedApplications.values()) {
            globalResolver.addAll(info.ejbJars);
        }
        SystemInstance.get().setComponent(EjbResolver.class, globalResolver);


        Context globalContext = containerSystem.getJNDIContext();
        UndeployException undeployException = new UndeployException(messages.format("destroyApplication.failed", appInfo.path));

        WebAppBuilder webAppBuilder = SystemInstance.get().getComponent(WebAppBuilder.class);
        if (webAppBuilder != null) {
            try {
                webAppBuilder.undeployWebApps(appInfo);
            } catch (Exception e) {
                undeployException.getCauses().add(new Exception("App: " + appInfo.path + ": " + e.getMessage(), e));
            }
        }

        // get all of the ejb deployments
        List<BeanContext> deployments = new ArrayList<BeanContext>();
        for (EjbJarInfo ejbJarInfo : appInfo.ejbJars) {
            for (EnterpriseBeanInfo beanInfo : ejbJarInfo.enterpriseBeans) {
                String deploymentId = beanInfo.ejbDeploymentId;
                BeanContext beanContext = containerSystem.getBeanContext(deploymentId);
                if (beanContext == null) {
                    undeployException.getCauses().add(new Exception("deployment not found: " + deploymentId));
                } else {
                    deployments.add(beanContext);
                }
            }
        }

        // Just as with startup we need to get things in an
        // order that respects the singleton @DependsOn information
        // Theoreticlly if a Singleton depends on something in its
        // @PostConstruct, it can depend on it in its @PreDestroy.
        // Therefore we want to make sure that if A dependsOn B,
        // that we destroy A first then B so that B will still be
        // usable in the @PreDestroy method of A.

        // Sort them into the original starting order
        deployments = sort(deployments);
        // reverse that to get the stopping order
        Collections.reverse(deployments);

        // stop
        for (BeanContext deployment : deployments) {
            String deploymentID = deployment.getDeploymentID() + "";
            try {
                Container container = deployment.getContainer();
                container.stop(deployment);
            } catch (Throwable t) {
                undeployException.getCauses().add(new Exception("bean: " + deploymentID + ": " + t.getMessage(), t));
            }
        }

        // undeploy
        for (BeanContext bean : deployments) {
            String deploymentID = bean.getDeploymentID() + "";
            try {
                Container container = bean.getContainer();
                container.undeploy(bean);
                bean.setContainer(null);
            } catch (Throwable t) {
                undeployException.getCauses().add(new Exception("bean: " + deploymentID + ": " + t.getMessage(), t));
            } finally {
                bean.setDestroyed(true);
            }
        }

        // get the client ids
        List<String> clientIds = new ArrayList<String>();
        for (ClientInfo clientInfo : appInfo.clients) {
            clientIds.add(clientInfo.moduleId);
            for (String className : clientInfo.localClients) {
                clientIds.add(className);
            }
            for (String className : clientInfo.remoteClients) {
                clientIds.add(className);
            }
        }

        if (appContext != null) for (WebContext webContext : appContext.getWebContexts()) {
            containerSystem.removeWebContext(webContext);
        }

    // Clear out naming for all components first
        for (BeanContext deployment : deployments) {
View Full Code Here

        BeanContext beanContext = newContext.getBeanContext();
        if (beanContext == null) return;
        ModuleContext moduleContext = beanContext.getModuleContext();
        //TODO its not clear what the scope for one of these context should be: ejb, module, or app
        //For now, go with the attachment of the BeanManager to AppContext
        AppContext appContext = moduleContext.getAppContext();
        WebBeansContext owbContext = appContext.getWebBeansContext();
        if (owbContext == null) {
//            throw new IllegalStateException("WebBeansContext not initialized in appContext " + appContext);
            return;
        }
        Object oldOWBContext = singletonService.contextEntered(owbContext);
View Full Code Here



    @Override
    public void initialize(StartupObject startupObject) {
        AppContext appContext = startupObject.getAppContext();

        appContext.setCdiEnabled(hasBeans(startupObject.getAppInfo()));

        //initialize owb context, cf geronimo's OpenWebBeansGBean
        Properties properties = new Properties();
        Map<Class<?>, Object> services = new HashMap<Class<?>, Object>();
        properties.setProperty(OpenWebBeansConfiguration.APPLICATION_IS_JSP, "true");
        properties.setProperty(OpenWebBeansConfiguration.USE_EJB_DISCOVERY, "true");
        //from CDI builder
        properties.setProperty(OpenWebBeansConfiguration.INTERCEPTOR_FORCE_NO_CHECKED_EXCEPTIONS, "false");
        properties.setProperty(SecurityService.class.getName(), ManagedSecurityService.class.getName());
        properties.setProperty(OpenWebBeansConfiguration.CONVERSATION_PERIODIC_DELAY, "1800000");
        properties.setProperty(OpenWebBeansConfiguration.APPLICATION_SUPPORTS_CONVERSATION, "true");
        properties.setProperty(OpenWebBeansConfiguration.IGNORED_INTERFACES, "org.apache.aries.proxy.weaving.WovenProxy");

        services.put(AppContext.class, appContext);
        services.put(TransactionService.class, new OpenEJBTransactionService());
        services.put(ELAdaptor.class,(ELAdaptor) new CustomELAdapter(appContext));
        services.put(ContextsService.class, new CdiAppContextsService(true));
        services.put(ResourceInjectionService.class, new CdiResourceInjectionService());
        services.put(ScannerService.class, new CdiScanner());

        optional(services, ConversationService.class, "org.apache.webbeans.jsf.DefaultConversationService");

        WebBeansContext webBeansContext = new WebBeansContext(services, properties);
        appContext.set(WebBeansContext.class, webBeansContext);
        Object old = contextEntered(webBeansContext);
        try {
            setConfiguration(webBeansContext.getOpenWebBeansConfiguration());
            try {
                webBeansContext.getService(ContainerLifecycle.class).startApplication(startupObject);
View Full Code Here

TOP

Related Classes of org.apache.openejb.AppContext

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.