Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Harald Wellmann.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  * implied.
  *
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.ops4j.pax.cdi.openwebbeans.impl;
 
 import static org.ops4j.pax.swissbox.core.ContextClassLoaderUtils.doWithClassLoader;
 
 
 
org.ops4j.pax.cdi.spi.CdiContainer implementation wrapping an Apache OpenWebBeans container, represented by a org.apache.webbeans.config.WebBeansContext.

Author(s):
Harald Wellmann
 
 public class OpenWebBeansCdiContainer extends AbstractCdiContainer {
 
     private static Logger log = LoggerFactory.getLogger(OpenWebBeansCdiContainer.class);

    
OpenWebBeans container lifecycle.
 
     private ContainerLifecycle lifecycle;

    
Helper for accessing Instance and Event of CDI container.
 
     private InstanceManager instanceManager;
 
     private WebBeansContext context;

    
Construct a CDI container for the given extended bundle.

Parameters:
ownBundle bundle containing this class
extendedBundle bundle to be extended with CDI container
extensionBundles CDI extension bundles to be loaded by OpenWebBeans
 
     public OpenWebBeansCdiContainer(CdiContainerType containerTypeBundle ownBundle,
         Bundle extendedBundleCollection<BundleextensionBundles) {
         super(containerTypeextendedBundleextensionBundles, Collections.singletonList(ownBundle));
         .debug("creating OpenWebBeans CDI container for bundle {}"extendedBundle);
     }

    
Creates and starts a WebBeansContext for the given bundle using an appropriate class loader as TCCL.

Parameters:
bundle
Returns:
 
     private WebBeansContext createWebBeansContext(Bundle bundlefinal Object environment) {
         buildContextClassLoader();
         try {
            return doWithClassLoader(getContextClassLoader(), new Callable<WebBeansContext>() {
                @Override
                public WebBeansContext call() throws Exception {
                    WebBeansContext webBeansContext = WebBeansContext.currentInstance();
                     = webBeansContext.getService(ContainerLifecycle.class);
                    .startApplication(environment);
                    startContexts(webBeansContext);
                    return webBeansContext;
                }
            });
        }
        // CHECKSTYLE:SKIP
        catch (Exception exc) {
            throw Exceptions.unchecked(exc);
        }
    }

    
Starts all CDI contexts.

Parameters:
webBeansContext
    private void startContexts(WebBeansContext webBeansContext) {
        ContextsService contextService = .getContextService();
        contextService.startContext(RequestScoped.classnull);
        contextService.startContext(ConversationScoped.classnull);
        contextService.startContext(SessionScoped.classnull);
        contextService.startContext(ApplicationScoped.classnull);
        contextService.startContext(Singleton.classnull);
    }

    
Stops all CDI contexts.
    private void stopContexts() {
        ContextsService contextService = .getContextService();
        contextService.endContext(RequestScoped.classnull);
        contextService.endContext(ConversationScoped.classnull);
        contextService.endContext(SessionScoped.classnull);
        contextService.endContext(ApplicationScoped.classnull);
        contextService.endContext(Singleton.classnull);
    }
    @Override
    protected void doStart(Object environment) {
         = createWebBeansContext(getBundle(), environment);
        if (.isDebugEnabled()) {
            for (Bean<?> bean : .getBeanManagerImpl().getBeans()) {
                .debug("  {}"bean);
            }
        }
    }
    @Override
    protected void doStop() {
        try {
            doWithClassLoader(getContextClassLoader(), new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    if ( != null) {
                        stopContexts();
                        .stopApplication(getContextClassLoader());
                    }
                    return null;
                }
            });
        }
        // CHECKSTYLE:SKIP
        catch (Exception exc) {
            throw Exceptions.unchecked(exc);
        }
    }
    @Override
    public Event<ObjectgetEvent() {
        return getInstanceManager().getEvent();
    }
    @Override
    public BeanManager getBeanManager() {
        return .getBeanManager();
    }
    @Override
    public Instance<ObjectgetInstance() {
        return getInstanceManager().getInstance();
    }
    private InstanceManager getInstanceManager() {
        if ( == null) {
            BeanManager beanManager = getBeanManager();
             = new InstanceManager();
            AnnotatedType<InstanceManagerannotatedType = beanManager
                .createAnnotatedType(InstanceManager.class);
            InjectionTarget<InstanceManagertarget = beanManager
                .createInjectionTarget(annotatedType);
            CreationalContext<InstanceManagercc = beanManager.createCreationalContext(null);
            target.inject(cc);
        }
        return ;
    }
    @Override
    public <T> T unwrap(Class<T> wrappedClass) {
        if (wrappedClass.isAssignableFrom(WebBeansContext.class)) {
            return wrappedClass.cast();
        }
        if (wrappedClass.isAssignableFrom(ContainerLifecycle.class)) {
            return wrappedClass.cast();
        }
        return null;
    }
    @Override
    public void startContext(Class<? extends Annotationscope) {
        ContextsService contextsService = .getService(ContextsService.class);
        contextsService.startContext(scopenull);
    }
    @Override
    public void stopContext(Class<? extends Annotationscope) {
        ContextsService contextsService = .getService(ContextsService.class);
        contextsService.endContext(scopenull);
    }
New to GrepCode? Check out our FAQ X