KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 import java.util.Locale JavaDoc;
13
14 import org.mmbase.bridge.jsp.taglib.util.Attribute;
15 import org.mmbase.bridge.Cloud;
16 import org.mmbase.bridge.Node;
17 import org.mmbase.bridge.CloudContext;
18 import org.mmbase.bridge.ContextProvider;
19
20 import org.mmbase.util.functions.Parameter;
21 import org.mmbase.util.functions.Parameters;
22
23
24 import javax.servlet.jsp.JspTagException JavaDoc;
25
26 import org.mmbase.util.logging.Logger;
27 import org.mmbase.util.logging.Logging;
28
29
30 /**
31  * Tags which are meant to live as a child of the CloudTag, could extend this
32  * class.
33  *
34  * @author Michiel Meeuwissen
35  * @version $Id: CloudReferrerTag.java,v 1.29 2006/03/09 13:24:31 nklasens Exp $
36  */

37
38 public abstract class CloudReferrerTag extends ContextReferrerTag {
39
40     private static final Logger log = Logging.getLoggerInstance(CloudReferrerTag.class);
41
42
43     private static CloudContext cloudContext;
44
45
46     private Attribute cloudId = Attribute.NULL;
47     // the id of the cloud to which we refer
48
// not yet supported by CloudTag
49

50
51     /**
52      * If there are more clouds to choose from, you can have a 'cloud'
53      * attribute in your tag, in wich you can indicate the id of the
54      * cloud you mean.
55      */

56     public void setCloud(String JavaDoc c) throws JspTagException JavaDoc {
57         cloudId = getAttribute(c);
58     }
59
60
61     /**
62     * This method tries to find an ancestor object of type CloudProvider.
63     *
64     * @return the CloudTag if found, else an exception.
65     */

66
67     protected CloudProvider findCloudProvider() throws JspTagException JavaDoc {
68         return (CloudProvider) findParentTag(CloudProvider.class, (String JavaDoc) cloudId.getValue(this));
69     }
70
71     /**
72      * This method tries to find an ancestor object of type CloudProvider.
73      *
74      * @return the CloudProvider or null.
75      *
76     */

77     public CloudProvider findCloudProvider(boolean throwexception) throws JspTagException JavaDoc {
78         return (CloudProvider) findParentTag(CloudProvider.class, (String JavaDoc) cloudId.getValue(this), throwexception);
79     }
80
81
82     /**
83      * Find the CloudProvider and return its cloud variable in one
84      * step. And the result of findCloudProvider is stored, so
85      * invoking this function more often is better then invoking
86      * findCloudProvider every time.
87      *
88      * @return a Cloud
89      */

90     public Cloud getCloudVar() throws JspTagException JavaDoc {
91         return findCloudProvider().getCloudVar();
92     }
93
94
95
96     /**
97     * @return the cloud context
98     */

99     protected CloudContext getCloudContext(){
100         if (cloudContext == null){
101             cloudContext = ContextProvider.getDefaultCloudContext();
102         }
103         return cloudContext;
104     }
105
106     protected Node getNode(String JavaDoc key) throws JspTagException JavaDoc {
107         Node n = getNodeOrNull(key);
108         if (n == null) getCloudVar().getNode((String JavaDoc) getObject(key)); // cause exception
109
return n;
110     }
111     /**
112      * Gets a node from the context.
113      */

114
115     protected Node getNodeOrNull(String JavaDoc key) throws JspTagException JavaDoc {
116         Object JavaDoc n = getObject(key);
117         if (n instanceof Node) {
118             log.debug("found a Node in Context");
119             return (Node) n;
120         } else if ((n instanceof String JavaDoc) || (n instanceof Number JavaDoc)) {
121             log.debug("found a Node Number in Context");
122             if (! getCloudVar().hasNode(n.toString())) return null;
123             return getCloudVar().getNode(n.toString());
124         } else {
125             throw new JspTagException JavaDoc("Element " + referid + " from context " + contextId + " cannot be converted to node (because it is a " + n.getClass().getName() + " now)");
126         }
127     }
128
129
130     protected void fillStandardParameters(Parameters p) throws JspTagException JavaDoc {
131         super.fillStandardParameters(p);
132         CloudProvider prov = findCloudProvider(false);
133         if (prov != null) {
134             Cloud cloud = prov.getCloudVar();
135             if (cloud != null) {
136                 p.setIfDefined(Parameter.CLOUD, cloud);
137                 p.setIfDefined(Parameter.USER, cloud.getUser());
138             }
139         }
140     }
141
142     /**
143      * @since MMBase-1.8
144      */

145     public Locale JavaDoc getLocale() throws JspTagException JavaDoc {
146         LocaleTag localeTag = (LocaleTag)findParentTag(LocaleTag.class, null, false);
147         if (localeTag != null) {
148             Locale JavaDoc locale = localeTag.getLocale();
149             if (locale != null) {
150                 return locale;
151             }
152         }
153         return getCloudVar().getLocale();
154     }
155
156 }
157
Popular Tags