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