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 java.io.IOException;
25  
26  import javax.faces.context.FacesContext;
27  import javax.faces.el.ValueBinding;
28  import javax.faces.component.UIComponent;
29  
30  /**
31   * puts a bean into the conversation context
32   * 
33   * add a bean under context control
34   *
35   * @JSFComponent
36   *   name = "s:conversation"
37   *   tagClass = "org.apache.myfaces.custom.conversation.ConversationTag"
38   *   
39   * @author imario@apache.org
40   */
41  public class UIConversation extends AbstractConversationComponent
42  {
43      private final static Log log = LogFactory.getLog(UIConversation.class);
44  
45      public static final String COMPONENT_TYPE = "org.apache.myfaces.Conversation";
46  
47      public void encodeBegin(FacesContext context) throws IOException
48      {
49          super.encodeBegin(context);
50  
51          UIComponent cmp = getParent();
52          if (cmp instanceof UIStartConversation)
53          {
54              // start conversation should to the work
55              return;
56          }
57  
58          if (getName() == null)
59          {
60              throw new IllegalArgumentException("conversation name (attribute name=) required if used outside of startConversation tag");
61          }
62  
63          elevateBean(context, getName(), getBeanBinding());
64      }
65  
66      ValueBinding getBeanBinding()
67      {
68          return getValueBinding("value");
69      }
70      
71      public static void elevateBean(FacesContext context, String conversationName, ValueBinding valueBinding)
72      {
73          Conversation conversation = ConversationManager.getInstance().getConversation(conversationName);
74          if (conversation == null)
75          {
76              log.debug("no conversation named '" + conversationName + "' running - can't elevate bean '" + valueBinding.getExpressionString());
77              return;
78          }
79          ConversationManager.getInstance().getConversationBeanElevator().elevateBean(context, conversation, valueBinding);
80      }
81  }