KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > SessionAttributeModule


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
17 package org.apache.cocoon.components.modules.input;
18
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22 import org.apache.cocoon.environment.ObjectModelHelper;
23 import org.apache.cocoon.environment.Request;
24 import org.apache.cocoon.environment.Session;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.SortedSet JavaDoc;
32 import java.util.TreeSet JavaDoc;
33
34 /**
35  * SessionAttributeModule accesses session attributes. If the
36  * attribute name contains an askerisk "*" this is considered a
37  * wildcard and all attributes that would match this wildcard are
38  * considered to be part of an array of that name for
39  * getAttributeValues. Only one "*" is allowed.
40  *
41  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
42  * @version CVS $Id: SessionAttributeModule.java 30932 2004-07-29 17:35:38Z vgritsenko $
43  */

44 public class SessionAttributeModule extends AbstractInputModule implements ThreadSafe {
45
46     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
47         throws ConfigurationException {
48
49         String JavaDoc pname = (String JavaDoc) this.settings.get("parameter", name);
50         if ( modeConf != null ) {
51             pname = modeConf.getAttribute( "parameter", pname );
52             // preferred
53
pname = modeConf.getChild("parameter").getValue(pname);
54         }
55         return ObjectModelHelper.getRequest(objectModel).getSession().getAttribute( pname );
56     }
57
58
59     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel )
60         throws ConfigurationException {
61
62         return new IteratorHelper(ObjectModelHelper.getRequest(objectModel).getSession().getAttributeNames());
63     }
64
65
66     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
67         throws ConfigurationException {
68
69         Request request = ObjectModelHelper.getRequest(objectModel);
70         String JavaDoc wildcard = (String JavaDoc) this.settings.get("parameter", name);
71         if ( modeConf != null ) {
72             wildcard = modeConf.getAttribute( "parameter", wildcard );
73             // preferred
74
wildcard = modeConf.getChild("parameter").getValue(wildcard);
75         }
76         int wildcardIndex = wildcard.indexOf( "*" );
77         if ( wildcardIndex != -1 ) {
78             // "*" contained in attribute name => combine all
79
// attributes' values that match prefix, suffix
80

81             // split the attribute's name so that the "*" could be
82
// determined by looking at the attributes' names that
83
// start with the prefix and end with the suffix
84
//
85
String JavaDoc prefix = wildcard.substring( 0, wildcardIndex );
86             String JavaDoc suffix;
87             if ( wildcard.length() >= wildcardIndex + 1 ) {
88                 suffix = wildcard.substring( wildcardIndex + 1 );
89             } else {
90                 suffix = "";
91             }
92             SortedSet JavaDoc names = new TreeSet JavaDoc();
93             Session session = request.getSession();
94             Enumeration JavaDoc allNames = session.getAttributeNames();
95
96             while (allNames.hasMoreElements()) {
97                 String JavaDoc pname = (String JavaDoc) allNames.nextElement();
98                 if ( pname.startsWith( prefix ) && pname.endsWith( suffix ) ) {
99                     names.add(pname);
100                 }
101             }
102
103             List JavaDoc values = new LinkedList JavaDoc();
104             Iterator JavaDoc j = names.iterator();
105             while (j.hasNext()){
106                 String JavaDoc pname = (String JavaDoc) j.next();
107                 values.add( session.getAttribute(pname) );
108             }
109
110             return values.toArray();
111
112         } else {
113             // no "*" in attribute name => just return all values of
114
// this one attribute. Make sure, it's an array.
115

116             Object JavaDoc value = request.getSession().getAttribute( wildcard );
117             if ( value != null && !value.getClass().isArray() ) {
118                 Object JavaDoc[] values = new Object JavaDoc[1];
119                 values[0] = value;
120                 return values;
121             } else {
122                 return (Object JavaDoc[]) value;
123             }
124
125         }
126
127     }
128 }
129
Popular Tags