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.springframework.beans.factory.config.Scope;
22  import org.springframework.beans.factory.ObjectFactory;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import javax.faces.context.FacesContext;
27  
28  /**
29   * adapts the conversation scope to a new spring scope
30   */
31  public class SpringConversationScope implements Scope
32  {
33      private final static Log log = LogFactory.getLog(SpringConversationScope.class);
34  
35      public String getConversationId()
36      {
37          FacesContext facesContext = FacesContext.getCurrentInstance();
38          ConversationManager manager = ConversationManager.getInstance(facesContext);
39          if (manager.hasConversationContext())
40          {
41              return Long.toString(manager.getConversationContextId().longValue(), 10);
42          }
43  
44          return null;
45      }
46  
47      /**
48       * get the conversation bean.<br />
49       * this will start a new conversation with the same name in case a conversation
50       * is not existent yet.
51       */
52      public Object get(String name, ObjectFactory objectFactory)
53      {
54          name = buildBeanName(name);
55  
56          FacesContext facesContext = FacesContext.getCurrentInstance();
57  
58          ConversationManager manager = ConversationManager.getInstance(facesContext);
59  
60          Object value = null;
61          boolean created = false;
62  
63          // check if we have a conversation
64          if (!manager.hasConversation(name))
65          {
66              // ... no ... create the object to ...
67              value = objectFactory.getObject();
68              created = true;
69  
70              // ... determine if it should deal with persistence.
71              boolean isPersistent = (value instanceof PersistentConversation);
72  
73              // start the conversation
74              manager.startConversation(name, isPersistent);
75          }
76  
77          // get the conversation
78          Conversation conversation = manager.getConversation(name);
79          if (!conversation.hasBean(name))
80          {
81              // create the bean (if not already done)
82              if (!created)
83              {
84                  value = objectFactory.getObject();
85              }
86  
87              conversation.putBean(facesContext, name, value);
88          }
89  
90          // get the bean
91          return conversation.getBean(name);
92      }
93  
94      /**
95       * strip off any spring namespace.
96       * TODO: Is this valid?
97       */
98      protected String buildBeanName(String name)
99      {
100         if (name == null)
101         {
102             return null;
103         }
104 
105         int pos = name.lastIndexOf('.');
106         if (pos < 0)
107         {
108             return name;
109         }
110 
111         return name.substring(pos+1);
112     }
113 
114     /**
115      * remove the bean from the conversation.
116      * TODO: when will this be called .. and should we close the conversation then?
117      */
118     public Object remove(String name)
119     {
120         FacesContext facesContext = FacesContext.getCurrentInstance();
121 
122         ConversationManager manager = ConversationManager.getInstance(facesContext);
123         if (!manager.hasConversation(name))
124         {
125             return null;
126         }
127 
128         return manager.getConversation(name).removeBean(name);
129     }
130 
131     public void registerDestructionCallback(String name, Runnable runnable)
132     {
133         if (log.isWarnEnabled())
134         {
135             log.warn("SpringConversationScope: registerDestructionCallback not yet supported. Name=" + name);
136         }
137     }
138 }