KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > ContextTag


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 package org.mmbase.bridge.jsp.taglib;
11 import java.io.IOException JavaDoc;
12
13 import javax.servlet.jsp.*;
14
15 import org.mmbase.bridge.*;
16 import org.mmbase.bridge.jsp.taglib.util.*;
17 import org.mmbase.util.logging.*;
18
19
20
21
22 /**
23  * <p>
24  * A ContextTag is like parentheses, and as such can act as
25  * a 'namespace' (if it has an id) or as 'scope' (if it doesn't).
26  * </p>
27  * <p>
28  * The context can be seen as a container for variables and their values.
29  * </p><p>
30  * (ContextReferrer) Tags living under a context tag can 'register'
31  * themselves in the context (by use of the 'id' attribute') and in
32  * that way become a variable. Other tags can refer to such variables.
33  * </p><p>
34  * A ContextTag is a ContextReferrer itself too, and therefore it is
35  * possible to 'nest' contextes. And perhaps we will also make it
36  * possible to treat contextes as variables and e.g. pass them to
37  * another page as a whole.
38  * </p><p>
39  * It is also possible to put something into the Context by hand. For
40  * that you can use the `ImportTag'.
41  * </p><p>
42  * Writing out the value of a variable can be done with the `Write' Tag.
43  * </p>
44  *
45  * @author Michiel Meeuwissen
46  * @version $Id: ContextTag.java,v 1.87 2006/07/17 15:38:47 johannes Exp $
47  * @see ImportTag
48  * @see WriteTag
49  */

50
51 public class ContextTag extends ContextReferrerTag implements ContextProvider {
52     private static final Logger log = Logging.getLoggerInstance(ContextTag.class);
53
54     public static final String JavaDoc CONTEXTTAG_KEY = "org.mmbase.taglib.context";
55     public static final String JavaDoc CONTAINER_KEY_PREFIX = "org.mmbase.taglib.contextcontainer.";
56     public static final String JavaDoc DEFAULTENCODING_KEY = "org.mmbase.taglib.defaultencoding";
57     public static final String JavaDoc ISELIGNORED_PARAM = "mmbase.taglib.isELIgnored";
58
59     private ContextProvider parent = null;
60     private boolean searchedParent = false;
61     private static int latestNumber = 0;
62
63     private int number;
64
65     private CloudContext cloudContext;
66
67     private Attribute referid = Attribute.NULL;
68     private Attribute scope = Attribute.NULL;
69
70     public void setReferid(String JavaDoc r) throws JspTagException {
71         referid = getAttribute(r);
72     }
73
74     public void setScope(String JavaDoc s) throws JspTagException {
75         scope = getAttribute(s);
76     }
77
78     private int getScope() throws JspTagException {
79         String JavaDoc ss = scope.getString(this).toLowerCase();
80         if ("".equals(ss)) {
81             return PageContext.PAGE_SCOPE;
82         } else if ("request".equals(ss)) {
83             return PageContext.REQUEST_SCOPE;
84         } else if ("session".equals(ss)) {
85             return PageContext.SESSION_SCOPE;
86         } else if ("page".equals(ss)) {
87             return PageContext.PAGE_SCOPE;
88         } else if ("application".equals(ss)) {
89             return PageContext.APPLICATION_SCOPE;
90         } else {
91             throw new JspTagException("Unknown scope '" + ss + "'");
92         }
93     }
94
95     /**
96      * This context can also serve as a 'cloudcontext'.
97      * That means that the cloud context commmunicates its cloudcontext to the context.
98      */

99     public void setCloudContext(CloudContext cc) {
100         cloudContext = cc;
101         String JavaDoc def = (String JavaDoc) pageContext.getAttribute(DEFAULTENCODING_KEY);
102         if (def == null && cloudContext != null) {
103             pageContext.setAttribute(DEFAULTENCODING_KEY, cloudContext.getDefaultCharacterEncoding());
104         }
105     }
106
107
108     public void setPageContext(PageContext pc) {
109         super.setPageContext(pc); // This will call fillVars for the 'page' Context.
110
log.debug("setting page context");
111     }
112
113     /**
114      * @param c Parent context-container, if <code>null</code> then a container writing to page context will be instantiated.
115      */

116     ContextContainer createContainer(ContextContainer c) { //throws JspTagException {
117
number = latestNumber++;
118         ContextContainer container = null;
119         if (c == null && (!"true".equals(pageContext.getServletContext().getInitParameter(ISELIGNORED_PARAM)))) {
120             container = new PageContextContainer(pageContext);
121         } else {
122             container = new StandaloneContextContainer(pageContext, getId(), c);
123         }
124         pageContext.setAttribute(CONTAINER_KEY_PREFIX + number, container, PageContext.PAGE_SCOPE);
125         return container;
126     }
127
128     public ContextContainer getContextContainer() {
129         return (ContextContainer)pageContext.getAttribute(CONTAINER_KEY_PREFIX + number);
130     }
131
132     public int doStartTag() throws JspTagException {
133         log.debug("Start tag of ContextTag");
134         parent = null;
135         searchedParent = false;
136
137         ContextContainer container;
138         int s = getScope();
139         if (referid != Attribute.NULL || (s != PageContext.PAGE_SCOPE && getId() != null)) {
140             Object JavaDoc o;
141             if (s == PageContext.PAGE_SCOPE) {
142                 o = getObject(referid.getString(this));
143             } else {
144                 String JavaDoc id = referid.getString(this);
145                 if (id.equals("")) {
146                     id = getId();
147                     if (id == null) throw new JspTagException("Must use id or referid attributes when using 'scope' attibute of context tag");
148                 }
149                 o = pageContext.getAttribute(id, s);
150             }
151             if (o == null || "".equals(o)) { // that means, lets ignore it.
152
container = createContainer(getContextProvider().getContextContainer());
153             } else {
154                 if (! (o instanceof ContextContainer)) {
155                     throw new JspTagException("Found context var '" + o + "' is not of type Context but of '" + o.getClass().getName());
156                 }
157                 container = (ContextContainer) o;
158                 container.setParent(pageContext, getContextProvider().getContextContainer());
159                 pageContext.setAttribute(CONTAINER_KEY_PREFIX + number, container, PageContext.PAGE_SCOPE);
160             }
161         } else {
162             container = createContainer(getContextProvider().getContextContainer());
163         }
164
165
166         if (s != PageContext.PAGE_SCOPE) {
167             String JavaDoc id = getId();
168             if (id == null) {
169                 id = referid.getString(this);
170             }
171             pageContext.setAttribute(id, container, s);
172         }
173         setCloudContext(getContextTag().cloudContext);
174         if (getId() != null) {
175             if (log.isDebugEnabled()) {
176                 log.debug("registering container " + container + " " + getId() + " with context " + getContextProvider().getContextContainer().getId());
177             }
178             getContextProvider().getContextContainer().register(getId(), container, referid == Attribute.NULL);
179         }
180         log.debug("out");
181         // return EVAL_BODY_INCLUDE; does not work in orion 1.6, tomcat < 4.1.19
182
return EVAL_BODY;
183     }
184
185     /**
186      * Precisely like 'register', only it wants a Node.
187      *
188      * @param key the key (id) of the node to register
189      * @param node the node to put in the hashmap
190      * @deprecated Use getContextProvider().getContextContainer().registerNode
191      */

192     public void registerNode(String JavaDoc key, Node node) throws JspTagException {
193         getContextContainer().registerNode(key, node);
194     }
195
196
197     /**
198      * Searches a key in request, postparameters, session, parent
199      * context and registers it in this one.
200      *
201      * Returns null if it could not be found.
202      * @deprecated Use getContextProvider().getContextContainer().findAndRegister
203      */

204     public Object JavaDoc findAndRegister(int from, String JavaDoc referid, String JavaDoc newid) throws JspTagException {
205         return getContextContainer().findAndRegister(pageContext, from, referid, newid, true);
206     }
207
208     /**
209      * @deprecated Use getContextProvider().getContextContainer().findAndRegister
210      */

211     protected Object JavaDoc findAndRegister(int from, String JavaDoc referid, String JavaDoc newid, boolean check) throws JspTagException {
212         return getContextContainer().findAndRegister(pageContext, from, referid, newid, check);
213     }
214
215     /**
216      * @deprecated Use getContextProvider().getContextContainer().findAndRegister
217      */

218     public Object JavaDoc findAndRegister(String JavaDoc externid, String JavaDoc newid) throws JspTagException {
219         return getContextContainer().findAndRegister(pageContext, externid, newid);
220     }
221
222
223     /**
224      * @deprecated Use getContextProvider().getContextContainer().register
225      */

226     public void register(String JavaDoc newid, Object JavaDoc n, boolean check) throws JspTagException {
227         getContextContainer().register(newid, n, check);
228     }
229
230     /**
231      * Register an Object with a key in the context. If the Context is
232      * a session context, then it will be put in the session, otherwise in the hashmap.
233      * @deprecated Use getContextProvider().getContextContainer().register
234      */

235     public void register(String JavaDoc newid, Object JavaDoc n) throws JspTagException {
236         getContextContainer().register(newid, n);
237     }
238
239
240     /**
241      * @deprecated Use getContextProvider().getContextContainer().unRegister
242      */

243     public void unRegister(String JavaDoc key) throws JspTagException {
244         getContextContainer().unRegister(key);
245     }
246
247     /**
248      * Registers an variable again. This can be used to change the type of a variable, e.g.
249      *
250      * @since MMBase-1.6
251      * @deprecated Use getContextProvider().getContextContainer().reregister
252      */

253     public void reregister(String JavaDoc id, Object JavaDoc n) throws JspTagException {
254         getContextContainer().reregister(id, n);
255     }
256
257
258     /**
259      * 'present' means 'not null'. 'null' means 'registered, but not present'.
260      * Not registered is not present, of course.
261      * @deprecated Use getContextProvider().getContextContainer().isPresent
262      */

263
264     public boolean isPresent(String JavaDoc key) throws JspTagException {
265         return getContextContainer().isPresent(key);
266     }
267
268     /**
269      * @deprecated Use getContextProvider().getContextContainer().isRegistered
270      */

271     public boolean isRegistered(String JavaDoc key) throws JspTagException {
272         return getContextContainer().isRegistered(key);
273     }
274     /**
275      * @deprecated Use getContextProvider().getContextContainer().isRegisteredSomewhere
276      */

277     private boolean isRegisteredSomewhere(String JavaDoc key) throws JspTagException {
278         return getContextContainer().containsKey(key, true); // do check parent.
279
}
280
281     /**
282      * @deprecated Use getContextProvider().getContextContainer().findAndRegister
283      */

284     public Object JavaDoc findAndRegister(String JavaDoc id) throws JspTagException {
285         return getContextContainer().findAndRegister(pageContext, id);
286     }
287     /**
288      * @deprecated Use getContextProvider().getContextContainer().findAndRegisterString
289      */

290     public String JavaDoc findAndRegisterString(String JavaDoc id) throws JspTagException {
291         return getContextContainer().findAndRegisterString(pageContext, id);
292     }
293
294     /**
295      * @deprecated Use getContextProvider().getContextContainer().getObject
296      */

297
298     public Object JavaDoc getContainerObject(String JavaDoc key) throws JspTagException {
299         return getContextContainer().getObject(key);
300
301     }
302
303     /**
304      * hmm.. This kind of stuf must move to ImportTag, I think.
305      */

306
307     public byte[] getBytes(String JavaDoc key) throws JspTagException {
308         return MultiPart.getMultipartRequest(pageContext).getBytes(key);
309
310     }
311
312     public org.apache.commons.fileupload.FileItem getFileItem(String JavaDoc key) throws JspTagException {
313         return MultiPart.getMultipartRequest(pageContext).getFileItem(key);
314
315     }
316
317     public int doAfterBody() throws JspTagException {
318         if (log.isDebugEnabled()) {
319             log.debug("after body of context " + getId());
320         }
321         getContextContainer().release(pageContext, getContextProvider().getContextContainer()); // remove the vars from 'page-context' again if necessary.
322
// just to serve lousy app-server which do not support EVAL_BODY_INCLUDE
323
if (EVAL_BODY == EVAL_BODY_BUFFERED) {
324             try {
325                 if (bodyContent != null) {
326                     bodyContent.writeOut(bodyContent.getEnclosingWriter());
327                 }
328             } catch (IOException JavaDoc ioe){
329                 throw new TaglibException(ioe);
330             }
331         }
332         return SKIP_BODY;
333     }
334
335     public int doEndTag() throws JspTagException {
336         parent = null;
337         cloudContext = null;
338         return super.doEndTag();
339     }
340
341     public void doFinally() {
342         parent = null;
343         cloudContext = null;
344         super.doFinally();
345     }
346
347     public String JavaDoc toString() {
348         return getClass().getName() + " with id " + getId();
349     }
350 }
351
352
353
Popular Tags