Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2010, Red Hat Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.as.weld.arquillian;
 
 import java.util.Map;
 import java.util.Set;
 
 
 import  org.jboss.weld.Container;
Sets up the session, request and conversation contexts for a weld deployment

Author(s):
Stuart Douglas
 
 public class WeldContextSetup implements SetupAction {
 
     private static class ContextMapThreadLocal extends ThreadLocal<Map<StringObject>> {
         @Override
         protected Map<StringObjectinitialValue() {
             return new ConcurrentHashMap<StringObject>();
         }
     }
 
     private static final String STANDARD_BEAN_MANAGER_JNDI_NAME = "java:comp/BeanManager";
 
     private final ThreadLocal<Map<StringObject>> sessionContexts = new ContextMapThreadLocal();
     private final ThreadLocal<Map<StringObject>> requestContexts = new ContextMapThreadLocal();
     private final ThreadLocal<BoundRequestboundRequests = new ThreadLocal<BoundRequest>();
 
     @SuppressWarnings("unchecked")
     public void setup(Map<StringObjectproperties) {
         try {
             final BeanManager manager = (BeanManagernew InitialContext().lookup();
 
             if (manager != null && Container.available()) {
 
                 final Bean<BoundSessionContextsessionContextBean = (Bean<BoundSessionContext>) manager.resolve(manager
                         .getBeans(BoundSessionContext.class.));
                 CreationalContext<?> ctx = manager.createCreationalContext(sessionContextBean);
                 final BoundSessionContext sessionContext = (BoundSessionContextmanager.getReference(sessionContextBean,
                         BoundSessionContext.classctx);
                 sessionContext.associate(.get());
                 sessionContext.activate();
 
                 final Bean<BoundRequestContextrequestContextBean = (Bean<BoundRequestContext>) manager.resolve(manager
                         .getBeans(BoundRequestContext.class.));
                 ctx = manager.createCreationalContext(requestContextBean);
                 final BoundRequestContext requestContext = (BoundRequestContextmanager.getReference(requestContextBean,
                         BoundRequestContext.classctx);
                 requestContext.associate(.get());
                 requestContext.activate();
 
                 final Bean<BoundConversationContextconversationContextBean = (Bean<BoundConversationContext>) manager
                         .resolve(manager.getBeans(BoundConversationContext.class.));
                 ctx = manager.createCreationalContext(conversationContextBean);
                 final BoundConversationContext conversationContext = (BoundConversationContextmanager.getReference(
                         conversationContextBeanBoundConversationContext.classctx);
                 BoundRequest request = new MutableBoundRequest(.get(), .get());
                 .set(request);
                 conversationContext.associate(request);
                 conversationContext.activate();
             }
        } catch (NamingException e) {
        }
    }
    @SuppressWarnings("unchecked")
    public void teardown(Map<StringObjectproperties) {
        try {
            final BeanManager manager = (BeanManagernew InitialContext().lookup("java:comp/BeanManager");
            if (manager != null && Container.available()) {
                final Bean<BoundSessionContextsessionContextBean = (Bean<BoundSessionContext>) manager.resolve(manager.getBeans(
                        BoundSessionContext.class.));
                CreationalContext<?> ctx = manager.createCreationalContext(sessionContextBean);
                final BoundSessionContext sessionContext = (BoundSessionContextmanager.getReference(sessionContextBean,
                        BoundSessionContext.classctx);
                sessionContext.invalidate();
                sessionContext.deactivate();
                sessionContext.dissociate(.get());
                final Bean<BoundRequestContextrequestContextBean = (Bean<BoundRequestContext>) manager.resolve(manager.getBeans(
                        BoundRequestContext.class.));
                ctx = manager.createCreationalContext(requestContextBean);
                final BoundRequestContext requestContext = (BoundRequestContextmanager.getReference(requestContextBean,
                        BoundRequestContext.classctx);
                requestContext.invalidate();
                requestContext.deactivate();
                requestContext.dissociate(.get());
                final Bean<BoundConversationContextconversationContextBean = (Bean<BoundConversationContext>) manager
                        .resolve(manager.getBeans(BoundConversationContext.class.));
                ctx = manager.createCreationalContext(conversationContextBean);
                final BoundConversationContext conversationContext = (BoundConversationContextmanager.getReference(
                        conversationContextBeanBoundConversationContext.classctx);
                conversationContext.invalidate();
                conversationContext.deactivate();
                conversationContext.dissociate(.get());
            }
        } catch (NamingException e) {
        } finally {
            .remove();
            .remove();
            .remove();
        }
    }
    @Override
    public Set<ServiceNamedependencies() {
        return Collections.emptySet();
    }
    @Override
    public int priority() {
        return 100;
    }
New to GrepCode? Check out our FAQ X