KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > util > BasicBacking


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.jsp.taglib.util;
12
13 import javax.servlet.jsp.PageContext JavaDoc;
14 import java.util.*;
15 import org.mmbase.util.Casting;
16 import org.mmbase.util.transformers.CharTransformer;
17 import org.mmbase.bridge.jsp.taglib.ContextTag;
18 import org.mmbase.bridge.jsp.taglib.ContentTag;
19 import org.mmbase.bridge.jsp.taglib.WriterHelper;
20 import org.mmbase.util.logging.*;
21
22
23 /**
24  * A basic implementation for the backing of a ContextContainter. It uses a HashMap, but is also
25  * writes every entry to the temporary to the page-context, to make them available to JSP2's
26  * expression language, unless the 'ELIgnored' parameter of the MMBase taglib is true, or no
27  * pageContext is given in the constructor.
28
29  * @author Michiel Meeuwissen
30  * @since MMBase-1.8
31  * @version $Id: BasicBacking.java,v 1.7.2.2 2006/11/22 14:52:10 michiel Exp $
32  */

33
34 public class BasicBacking extends AbstractMap implements Backing {
35     private static final Logger log = Logging.getLoggerInstance(BasicBacking.class);
36
37     private static final String JavaDoc PAGECONTEXT_KEY = "org.mmbase.taglib.basicbacking$";
38
39     private static int uniqueNumbers = 0;
40     private static final int SCOPE = PageContext.PAGE_SCOPE;
41
42     private int uniqueNumber = ++uniqueNumbers;
43
44     protected Map originalPageContextValues;
45     private final Map b = new HashMap(); // the actual backing.
46

47     private final boolean isELIgnored;
48     private PageContext JavaDoc pageContext;
49
50     /**
51      * @param pc The page-context to which variables must be reflected or <code>null</code> if this must not happen.
52      */

53     public BasicBacking(PageContext JavaDoc pc, boolean ignoreEL) {
54         pageContext = pc;
55         isELIgnored = ignoreEL || "true".equals(pageContext.getServletContext().getInitParameter(ContextTag.ISELIGNORED_PARAM));
56         if (! isELIgnored) {
57             originalPageContextValues = new HashMap();
58             pageContext.setAttribute(PAGECONTEXT_KEY + uniqueNumber, originalPageContextValues);
59         } else {
60             originalPageContextValues = null;
61         }
62     }
63
64     public void pushPageContext(PageContext JavaDoc pc) {
65         PageContext JavaDoc origPageContext = pageContext;
66         pageContext = pc;
67         if (isELIgnored) return; // never mind
68
log.debug("Pushing page-context for backing " + uniqueNumber + " for " + pc);
69         originalPageContextValues = (Map) pageContext.getAttribute(PAGECONTEXT_KEY + uniqueNumber);
70         if (originalPageContextValues == null) {
71             originalPageContextValues = new HashMap();
72             pageContext.setAttribute(PAGECONTEXT_KEY + uniqueNumber, originalPageContextValues);
73         }
74         if (! origPageContext.equals(pageContext)) {
75             Iterator i = b.entrySet().iterator();
76             while (i.hasNext()) {
77                 Map.Entry entry = (Map.Entry) i.next();
78                 mirrorPut(entry.getKey(), entry.getValue());
79             }
80         }
81     }
82     public void pullPageContext(PageContext JavaDoc pc) {
83         pageContext = pc;
84         if (isELIgnored) return;
85         originalPageContextValues = (Map) pageContext.getAttribute(PAGECONTEXT_KEY + uniqueNumber);
86         if (originalPageContextValues == null) {
87             log.warn("OIE " + Logging.stackTrace(10));
88         }
89     }
90     public PageContext JavaDoc getPageContext() {
91         return pageContext;
92     }
93
94     public void setJspVar(PageContext JavaDoc pc, String JavaDoc jspvar, int vartype, Object JavaDoc value) {
95         if (jspvar == null) return;
96         if (value == null) return;
97         // When it doesn't, it goes ok. (at least I think that this is the difference between orion and tomcat)
98
if (vartype == WriterHelper.TYPE_STRING) {
99             // string is final, the wrapped version cannot be string..
100
Object JavaDoc v = Casting.unWrap(value);
101             if (v == null) return;
102             pc.setAttribute(jspvar, v);
103         } else {
104             pc.setAttribute(jspvar, value);
105         }
106
107     }
108
109     public Set entrySet() {
110         return new AbstractSet() {
111                 public int size() {
112                     return b.size();
113                 }
114                 public Iterator iterator() {
115                     return new Iterator() {
116                             Iterator i = b.entrySet().iterator();
117                             Map.Entry last = null;
118                             public boolean hasNext() {
119                                 return i.hasNext();
120                             }
121                             public Object JavaDoc next() {
122                                 last = (Map.Entry) i.next();
123                                 return last;
124                             }
125                             public void remove() {
126                                 i.remove();
127                                 if (! isELIgnored) {
128                                     String JavaDoc key = (String JavaDoc) last.getKey();
129                                     if (! originalPageContextValues.containsKey(key)) {
130                                         originalPageContextValues.put(key, pageContext.getAttribute(key, SCOPE));
131                                     }
132                                     pageContext.removeAttribute(key);
133                                 }
134                             }
135                         };
136                 }
137             };
138     }
139
140     protected void mirrorPut(Object JavaDoc key, Object JavaDoc value) {
141         if (isELIgnored) return;
142         if (! originalPageContextValues.containsKey(key)) {
143             // log.debug("Storing pageContext key " + key);
144
originalPageContextValues.put((String JavaDoc) key, pageContext.getAttribute((String JavaDoc) key, SCOPE));
145         }
146         
147         if (value != null) {
148             pageContext.setAttribute((String JavaDoc) key, Casting.wrap(value, (CharTransformer) pageContext.findAttribute(ContentTag.ESCAPER_KEY)), SCOPE);
149         } else {
150             pageContext.removeAttribute((String JavaDoc) key, SCOPE);
151         }
152     }
153     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
154         mirrorPut(key, value);
155         return b.put(key, value);
156     }
157
158     // overriden for efficiency only (the implementation of AbstractMap does not seem very efficient)
159
public Object JavaDoc get(Object JavaDoc key) {
160         return b.get(key);
161     }
162
163     public Object JavaDoc getOriginal(Object JavaDoc key) {
164         return b.get(key);
165     }
166     public boolean containsOwnKey(Object JavaDoc key) {
167         return b.containsKey(key);
168     }
169
170         
171     void release() {
172         if (originalPageContextValues != null) {
173             //log.debug("Restoring pageContext with " + originalPageContextValues);
174
// restore the pageContext
175
Iterator i = originalPageContextValues.entrySet().iterator();
176             while (i.hasNext()) {
177                 Map.Entry e = (Map.Entry) i.next();
178                 if (e.getValue() == null) {
179                     pageContext.removeAttribute((String JavaDoc) e.getKey(), SCOPE);
180                 } else {
181                     pageContext.setAttribute((String JavaDoc) e.getKey(), e.getValue(), SCOPE);
182                 }
183             }
184             originalPageContextValues.clear();
185         }
186     }
187
188     public String JavaDoc toString() {
189         return "BASIC BACKING " + super.toString();
190     }
191         
192         
193 }
194
Popular Tags