View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.struts.flow.core;
17  
18  import org.mozilla.javascript.Undefined;
19  import org.mozilla.javascript.Wrapper;
20  
21  import java.util.Map;
22  
23  /***
24   * Provides the interface between the flow controller layer and the 
25   * view layer. A view can obtain the context object sent by a flow
26   * script and the current web continuation, if any.
27   */
28  public class FlowHelper {
29  
30      // Constants defining keys in the object model used to store the various objects.
31      // These constants are private so that access to these objects only go through the
32      // accessors provided below.
33      //
34      // These objects are stored in the object model rather than as request attributes,
35      // as object model is cloned for subrequests (see EnvironmentWrapper), whereas
36      // request attributes are shared between the "real" request and all of its
37      // child requests.
38  
39      /***
40       * Request attribute name used to store flow context.
41       */
42      private static final String CONTEXT_OBJECT = "cocoon.flow.context";
43  
44      /***
45       * Request attribute name used to store flow continuation.
46       */
47      private static final String CONTINUATION_OBJECT = "cocoon.flow.continuation";
48  
49      /***
50       * Get the flow context object associated with the current request
51       *
52       * @param objectModel The Cocoon Environment's object model
53       * @return The context object 
54       */
55      public final static Object getContextObject(Map objectModel) {
56          return objectModel.get(CONTEXT_OBJECT);
57      }
58  
59      /***
60       * Get the web continuation associated with the current request
61       *
62       * @param objectModel The Cocoon Environment's object model
63       * @return The web continuation
64       */
65      public final static WebContinuation getWebContinuation(Map objectModel) {
66          return (WebContinuation)objectModel.get(CONTINUATION_OBJECT);
67      }
68  
69      /***
70       * Set the web continuation associated with the current request
71       *
72       * @param objectModel The Cocoon Environment's object model
73       * @param kont The web continuation
74       */
75      public final static void setWebContinuation(Map objectModel,
76                                            WebContinuation kont) {
77          objectModel.put(CONTINUATION_OBJECT, kont);
78      }
79  
80      /***
81       * Set the flow context object associated with the current request
82       *
83       * @param objectModel The Cocoon Environment's object model
84       * @param obj The context object 
85       */
86      public final static void setContextObject(Map objectModel, Object obj) {
87          objectModel.put(CONTEXT_OBJECT, obj);
88      }
89      
90      /***
91       * Unwrap a Rhino object (getting the raw java object) and convert undefined to null
92       */
93      public static Object unwrap(Object obj) {
94          if (obj instanceof Wrapper) {
95              obj = ((Wrapper)obj).unwrap();
96          } else if (obj == Undefined.instance) {
97              obj = null;
98          }
99          return obj;
100     }
101 }