Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.conversation » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.conversation;
   15   
   16   import java.util.Iterator;
   17   import java.util.Map;
   18   import java.util.Set;
   19   import java.util.concurrent.ConcurrentHashMap;
   20   
   21   import javax.enterprise.context.Conversation;
   22   import javax.enterprise.inject.spi.Bean;
   23   
   24   import org.apache.webbeans.annotation.CurrentLiteral;
   25   import org.apache.webbeans.config.WebBeansFinder;
   26   import org.apache.webbeans.container.BeanManagerImpl;
   27   import org.apache.webbeans.context.ConversationContext;
   28   import org.apache.webbeans.util.Asserts;
   29   
   30   public class ConversationManager
   31   {
   32       private Map<Conversation, ConversationContext> conversations = null;
   33   
   34       public ConversationManager()
   35       {
   36           if(conversations == null)
   37           {
   38               conversations = new ConcurrentHashMap<Conversation, ConversationContext>();   
   39           }
   40       }
   41   
   42       public static ConversationManager getInstance()
   43       {
   44           ConversationManager manager = (ConversationManager) WebBeansFinder.getSingletonInstance(WebBeansFinder.SINGLETON_CONVERSATION_MANAGER);
   45           
   46           return manager;
   47       }
   48   
   49       public void addConversationContext(Conversation conversation, ConversationContext context)
   50       {
   51           conversations.put(conversation, context);
   52       }
   53   
   54       public ConversationContext removeConversation(Conversation conversation)
   55       {
   56           Asserts.assertNotNull(conversation, "conversation can not be null");
   57   
   58           return conversations.remove(conversation);
   59       }
   60   
   61       public ConversationContext getConversationContext(Conversation conversation)
   62       {
   63           Asserts.assertNotNull(conversation, "conversation can not be null");
   64   
   65           return conversations.get(conversation);
   66       }
   67   
   68       public Conversation getConversation(String conversationId)
   69       {
   70           Asserts.assertNotNull(conversationId, "conversationId parameter can not be null");
   71   
   72           ConversationImpl conv = null;
   73           Set<Conversation> set = conversations.keySet();
   74           Iterator<Conversation> it = set.iterator();
   75   
   76           while (it.hasNext())
   77           {
   78               conv = (ConversationImpl) it.next();
   79               if (conv.getId().equals(conversationId))
   80               {
   81                   return conv;
   82               }
   83           }
   84   
   85           return null;
   86   
   87       }
   88   
   89       public void destroyConversationContextWithSessionId(String sessionId)
   90       {
   91           Asserts.assertNotNull(sessionId, "sessionId parameter can not be null");
   92   
   93           ConversationImpl conv = null;
   94           Set<Conversation> set = conversations.keySet();
   95           Iterator<Conversation> it = set.iterator();
   96   
   97           while (it.hasNext())
   98           {
   99               conv = (ConversationImpl) it.next();
  100               if (conv.getSessionId().equals(sessionId))
  101               {
  102                   it.remove();
  103               }
  104           }
  105       }
  106   
  107       public Conversation createNewConversation()
  108       {
  109           Conversation conversation = getCurrentConversation();
  110   
  111           return conversation;
  112   
  113       }
  114   
  115       @SuppressWarnings("unchecked")
  116       public Conversation getCurrentConversation()
  117       {
  118   
  119           Bean<Conversation> bean = (Bean<Conversation>)BeanManagerImpl.getManager().resolveByType(Conversation.class, new CurrentLiteral()).iterator().next();
  120           Conversation conversation = BeanManagerImpl.getManager().getInstance(bean);
  121   
  122           return conversation;
  123       }
  124   
  125       public void destroyWithRespectToTimout()
  126       {
  127           ConversationImpl conv = null;
  128           Set<Conversation> set = conversations.keySet();
  129           Iterator<Conversation> it = set.iterator();
  130   
  131           while (it.hasNext())
  132           {
  133               conv = (ConversationImpl) it.next();
  134               long timeout = conv.getTimeout();
  135   
  136               if (timeout != 0L)
  137               {
  138                   if ((System.currentTimeMillis() - conv.getActiveTime()) > timeout)
  139                   {
  140                       it.remove();
  141                   }
  142               }
  143           }
  144       }
  145   
  146       public void destroyAllConversations()
  147       {
  148           if (conversations != null)
  149           {
  150               conversations.clear();
  151           }
  152       }
  153   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.conversation » [javadoc | source]