View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.conversation;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import javax.faces.context.FacesContext;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  /**
30   * handle conversation related stuff like beans
31   * @author imario@apache.org
32   */
33  public class Conversation
34  {
35      private final static Log log = LogFactory.getLog(Conversation.class);
36  
37      private final String name;
38      private final boolean persistence;
39  
40      private PersistenceManager persistenceManager;
41  
42      // private final Map beans = new TreeMap(new ValueBindingKey());
43      private final Map beans = new TreeMap();
44  
45      protected Conversation(String name, boolean persistence)
46      {
47          this.name = name;
48          this.persistence = persistence;
49  
50          if (log.isDebugEnabled())
51          {
52              log.debug("start conversation:" + name + "(persistence=" + persistence + ")");
53          }
54      }
55  
56      /**
57       * Add the given valueBinding to the context map. <br/>
58       * This will also resolve the value of the binding.
59       */
60      public void putBean(FacesContext context, String name, Object value)
61      {
62          if (name.indexOf('.') > -1)
63          {
64              throw new IllegalArgumentException("you cant put a property under conversation control. name: " + name);
65          }
66  
67          if (beans.containsKey(name))
68          {
69              // already there
70              return;
71          }
72          if (log.isDebugEnabled())
73          {
74              log.debug("put bean to conversation:" + name + "(bean=" + name + ")");
75          }
76          beans.put(name, value);
77      }
78  
79      /**
80       * the conversation name
81       */
82      public String getName()
83      {
84          return name;
85      }
86  
87      /**
88       * end this conversation <br />
89       * <ul>
90       * <li>inform all beans implementing the {@link ConversationListener} about the conversation end</li>
91       * <li>free all beans</li>
92       * </ul>
93       */
94      public void endConversation(boolean regularEnd)
95      {
96          if (log.isDebugEnabled())
97          {
98              log.debug("end conversation:" + name);
99          }
100 
101         Iterator iterBeans = beans.values().iterator();
102         while (iterBeans.hasNext())
103         {
104             Object bean = iterBeans.next();
105             if (bean instanceof ConversationListener)
106             {
107                 ((ConversationListener) bean).conversationEnded();
108             }
109         }
110         beans.clear();
111 
112         if (isPersistence())
113         {
114             if (regularEnd)
115             {
116                 getPersistenceManager().commit();
117             }
118             else
119             {
120                 getPersistenceManager().rollback();
121             }
122 
123             getPersistenceManager().purge();
124         }
125     }
126 
127     /**
128      * Iterate all beans associated to this context
129      *
130      * @return Iterator of {@link Map.Entry} elements
131     public Iterator iterateBeanEntries()
132     {
133         return beans.entrySet().iterator();
134     }
135      */
136 
137     public boolean hasBean(String name)
138     {
139         return beans.containsKey(name);
140     }
141 
142     public Object getBean(String name)
143     {
144         return beans.get(name);
145     }
146 
147     public Object removeBean(String name)
148     {
149         return beans.remove(name);
150     }
151 
152     /**
153      * returns true if this conversation hold the persistence manager (aka EntityManager)
154      */
155     public boolean isPersistence()
156     {
157         return persistence || persistenceManager != null;
158     }
159 
160     public PersistenceManager getPersistenceManager()
161     {
162         if (persistenceManager == null)
163         {
164             persistenceManager = ConversationManager.getInstance().createPersistenceManager();
165         }
166 
167         return persistenceManager;
168     }
169 
170     /**
171      * returns true if we hold the given instance
172      */
173     public boolean hasBean(Object instance)
174     {
175         return beans.containsValue(instance);
176     }
177 }