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
20 package org.apache.myfaces.custom.scope;
21
22 import javax.faces.component.UIParameter;
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.ValueBinding;
25
26 /**
27 * Scope control
28 * which does basically the same as
29 * savestate but bypasses the serialization
30 * and utilizes the session directly
31 *
32 * @JSFComponent
33 * name = "s:scope"
34 * tagClass = "org.apache.myfaces.custom.scope.ScopeTag"
35 *
36 * @author Werner Punz werpu@gmx.at
37 * @version $Revision$ $Date$
38 */
39
40 public class UIScope extends UIParameter
41 {
42
43 public static final String SCOPE_CONTAINER_KEY = "ScopeContainer";
44 public static final String COMPONENT_TYPE = "org.apache.myfaces.Scope";
45 public static final String COMPONENT_FAMILY = "javax.faces.Parameter";
46
47 ScopeHolder holder = (ScopeHolder) ScopeUtils
48 .getManagedBean(UIScope.SCOPE_CONTAINER_KEY);
49
50 public String getFamily()
51 {
52 return COMPONENT_FAMILY;
53 }
54
55 /**
56 * save state saves the scope value binding into the holder map
57 */
58 public Object saveState(FacesContext context)
59 {
60 Object values[] = new Object[1];
61 values[0] = super.saveState(context);
62
63 ValueBinding vb = getValueBinding("value");
64 holder.saveScopeEntry(this, context, vb);
65 return ((Object) (values));
66 }
67
68 /**
69 * in the restores state phase we get the binding and replace the one from
70 * the system with the one from the scope map
71 */
72 public void restoreState(FacesContext context, Object state)
73 {
74 Object values[] = (Object[]) state;
75 super.restoreState(context, values[0]);
76
77 /*
78 * fetch the old scoped object and bind it to the value binding if it
79 * exists, otherwise there will be no binding
80 */
81 ValueBinding vb = getValueBinding("value");
82 Object oldVal = holder.restoreScopeEntry(vb.getExpressionString());
83 if (oldVal == null)
84 return;
85 vb.setValue(context, oldVal);
86 setValueBinding("value", vb);
87
88 }
89
90 /**
91 * reset scope helper which allows to remove the scope from the system
92 * within the backend context
93 *
94 * @param context
95 */
96 public void resetScope(FacesContext context)
97 {
98 ValueBinding vb = getValueBinding("value");
99 vb.setValue(context, "");
100 setValueBinding("value", vb);
101 holder.resetScope(vb.getExpressionString());
102 }
103 }