KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > SessionPropagatorAction


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.cocoon.acting;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.avalon.framework.thread.ThreadSafe;
28
29 import org.apache.cocoon.environment.ObjectModelHelper;
30 import org.apache.cocoon.environment.Redirector;
31 import org.apache.cocoon.environment.Request;
32 import org.apache.cocoon.environment.Session;
33 import org.apache.cocoon.environment.SourceResolver;
34
35 /**
36  * This is the action used to propagate parameters into session. It
37  * simply propagates given expression to the session. If session does not
38  * exist, action fails. Additionaly it will make all propagated values
39  * available via returned Map.
40  *
41  * <pre>
42  * &lt;map:act type="session-propagator"&gt;
43  * &lt;paramater name="example" value="{example}"&gt;
44  * &lt;paramater name="example1" value="xxx"&gt;
45  * &lt;/map:act&gt;
46  * </pre>
47  *
48  * @author <a HREF="mailto:Martin.Man@seznam.cz">Martin Man</a>
49  * @version CVS $Id: SessionPropagatorAction.java 54548 2004-10-11 10:32:37Z cziegeler $
50  */

51 public class SessionPropagatorAction extends AbstractConfigurableAction implements ThreadSafe {
52
53     /**
54      * A private helper holding default parameter entries.
55      *
56      */

57     private static class Entry {
58         public String JavaDoc key = null;
59         public String JavaDoc value = null;
60
61         public Entry(String JavaDoc key, String JavaDoc value) {
62             this.key = key;
63             this.value = value;
64         }
65     }
66     
67     private List JavaDoc defaults;
68
69     public void configure(Configuration conf) throws ConfigurationException {
70         super.configure(conf);
71         Configuration[] dflts = conf.getChildren();
72         if (dflts != null) {
73             this.defaults = new ArrayList JavaDoc(dflts.length);
74             for (int i = 0; i < dflts.length; i++) {
75                 this.defaults.add(
76                     new Entry(
77                         dflts[i].getName(),
78                         dflts[i].getValue()));
79             }
80         } else {
81             this.defaults = new ArrayList JavaDoc(0);
82         }
83     }
84
85     /**
86      * Main invocation routine.
87      */

88     public Map JavaDoc act (Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src,
89                     Parameters parameters) throws Exception JavaDoc {
90         Request req = ObjectModelHelper.getRequest(objectModel);
91         HashMap JavaDoc actionMap = new HashMap JavaDoc ();
92
93         /* check session validity */
94         Session session = req.getSession (false);
95         if (session == null) {
96             if (getLogger().isDebugEnabled()) {
97                 getLogger().debug("No session object");
98             }
99             return null;
100         }
101
102         try {
103             String JavaDoc[] names = parameters.getNames();
104
105             // parameters
106
for (int i = 0; i < names.length; i++) {
107                 String JavaDoc sessionParamName = names[i];
108                 String JavaDoc value = parameters.getParameter(sessionParamName);
109                 if (getLogger().isDebugEnabled()) {
110                     getLogger().debug("Propagating value "
111                                       + value
112                                       + " to session attribute "
113                                       + sessionParamName);
114                 }
115                 session.setAttribute(sessionParamName, value);
116                 actionMap.put(sessionParamName, value);
117             }
118
119             // defaults, that are not overridden
120
for (int i = 0; i < defaults.size(); i++) {
121                 final Entry entry = (Entry)defaults.get(i);
122                 if (!actionMap.containsKey(entry.key)) {
123                     if (getLogger().isDebugEnabled()) {
124                         getLogger().debug("Propagating value "
125                                           + entry.value
126                                           + " to session attribute "
127                                           + entry.key);
128                     }
129                     session.setAttribute(entry.key, entry.value);
130                     actionMap.put(entry.key, entry.value);
131                 }
132             }
133             if (getLogger().isDebugEnabled()) {
134                 getLogger().debug("All params propagated " + "to session");
135             }
136             return Collections.unmodifiableMap(actionMap);
137         } catch (Exception JavaDoc e) {
138             getLogger().warn("exception: ", e);
139         }
140         return null;
141     }
142 }
143
144
Popular Tags