KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > common > ScriptablePageInput


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.script.common;
19
20 // java imports
21

22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.servlet.ServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 // internal imports
31
import org.apache.beehive.netui.pageflow.PageFlowUtils;
32 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
33 import org.apache.beehive.netui.util.logging.Logger;
34
35 // external imports
36

37 /**
38  * Provide a {@link java.util.Map} object that exposes a set of page inputs to
39  * expression language clients. Access to these page inputs is based on the
40  * name of the page input.
41  * <p/>
42  * Access is optimized for read in that the "get" method is fast. The entrySet()
43  * method is only used if needed, which is generally to toString the Map.
44  */

45 public class ScriptablePageInput
46     extends AbstractScriptableMap {
47
48     private static final Logger _logger = Logger.getInstance(ScriptablePageInput.class);
49
50     private HttpServletRequest JavaDoc _request = null;
51     private Set JavaDoc _entrySet = null;
52
53     public ScriptablePageInput(ServletRequest JavaDoc request) {
54         assert request instanceof HttpServletRequest JavaDoc;
55
56         _request = (HttpServletRequest JavaDoc)request;
57     }
58
59     public Object JavaDoc get(Object JavaDoc name) {
60         if(_logger.isDebugEnabled()) _logger.debug("page input get: " + name);
61
62         assert name instanceof String JavaDoc;
63
64         return PageFlowUtils.getActionOutput((String JavaDoc)name, _request);
65     }
66
67     /**
68      * Create a {@link java.util.Set} of page input entries. This implementation
69      * assumes that the page input set does not change, which is acceptable for
70      * JSP clients as the page inputs have been specified when the JSP starts
71      * to render.
72      *
73      * @return Set
74      */

75     public Set JavaDoc entrySet() {
76         if(_entrySet == null) {
77             Map JavaDoc piMap = InternalUtils.getPageInputMap(_request);
78             ArrayList JavaDoc list = new ArrayList JavaDoc();
79             if(piMap != null) {
80                 Iterator JavaDoc iterator = piMap.keySet().iterator();
81                 while(iterator.hasNext()) {
82                     Object JavaDoc name = iterator.next();
83                     Object JavaDoc value = piMap.get(name);
84                     list.add(new Entry(name, value));
85                 }
86             }
87
88             _entrySet = new EntrySet((Entry[])list.toArray(new Entry[]{}));
89         }
90
91         return _entrySet;
92     }
93
94     public boolean equals(Object JavaDoc obj) {
95         if(obj == null || !(obj instanceof ScriptablePageInput))
96             return false;
97         return super.equals(obj);
98     }
99
100     public boolean containsKey(Object JavaDoc key) {
101         Map JavaDoc piMap = InternalUtils.getPageInputMap(_request);
102         return (piMap != null ? piMap.containsKey(key) : false);
103     }
104 }
105
Popular Tags