KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > jndi > UseContextTag


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.taglibs.jndi;
18
19 import javax.servlet.jsp.*;
20 import javax.servlet.jsp.tagext.*;
21
22 import java.io.*;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import javax.naming.*;
27 /**
28  *
29  * @author Danno Ferrin <shemnon@earthlink.net>
30  * @version $Revision: 1.4 $
31  */

32 public class UseContextTag extends BodyTagSupport {
33  
34     Hashtable JavaDoc env = new Hashtable JavaDoc();
35     //id handled by bodytagsupport
36
private int scope = PageContext.PAGE_SCOPE;
37     String JavaDoc url;
38     
39     /**
40      * Creates new UseDirContextTag
41      */

42     public UseContextTag() {
43         
44     }
45     
46     /**
47      * Setter for property envRef.
48      * <BR>Bad refs will be silently ignored.
49      * @param envRef New value of property envRef.
50      */

51     public void setEnvRef(String JavaDoc envRef) {
52         Object JavaDoc o = pageContext.findAttribute(envRef);
53         if ((o != null) && (o instanceof Hashtable JavaDoc)) {
54             setEnv((Hashtable JavaDoc)o);
55         }
56     }
57     
58     /**
59      * Getter for property env.
60      * @return Value of property env.
61      */

62     public Hashtable JavaDoc getEnv() {
63         return env;
64     }
65
66     /**
67      * Setter for property env.
68      * This copies the Hashtable in and does not mutate the hashtable passed in.
69      * @param env New value of property env.
70      */

71     public void setEnv(Hashtable JavaDoc env) {
72         Enumeration JavaDoc e = env.keys();
73         while (e.hasMoreElements()) {
74             String JavaDoc s = (String JavaDoc)e.nextElement();
75             //XXX only put stuff not there? This could be an intersting
76
//XXX example of ordered attributes in a custom tag then
77
this.env.put(s, env.get(s));
78         }
79     }
80     
81     /**
82      * Getter for property providerUrl.
83      * @return Value of property providerUrl.
84      */

85     public String JavaDoc getProviderUrl() {
86         try {
87             return (String JavaDoc) env.get(Context.PROVIDER_URL);
88         } catch (ClassCastException JavaDoc cce) {
89             return null;
90         }
91     }
92     
93     /**
94      * Setter for property providerUrl.
95      * @param providerUrl New value of property providerUrl.
96      */

97     public void setProviderUrl(String JavaDoc providerUrl) {
98         env.put(Context.PROVIDER_URL, providerUrl);
99     }
100
101     /**
102      * Getter for property url
103      * @return Value of property url.
104      */

105     public String JavaDoc getUrl() {
106         return url;
107     }
108
109     /**
110      * Setter for property url.
111      * @param url New value of property url.
112      */

113     public void setUrl(String JavaDoc url) {
114         this.url = url;
115     }
116
117     /**
118      * Getter for property intialFactory.
119      * @return Value of property intialFactory.
120      */

121     public String JavaDoc getInitialFactory() {
122         try {
123             return (String JavaDoc) env.get(Context.INITIAL_CONTEXT_FACTORY);
124         } catch (ClassCastException JavaDoc cce) {
125             return null;
126         }
127     }
128
129     /**
130      * Setter for property intialFactory.
131      * @param intialFactory New value of property intialFactory.
132      */

133     public void setInitialFactory(String JavaDoc initialFactory) {
134         env.put(Context.INITIAL_CONTEXT_FACTORY, initialFactory);
135     }
136
137     /**
138      * Getter for property authoritative.
139      * @return Value of property authoritative.
140      */

141     public String JavaDoc getAuthoritative() {
142         try {
143             return (String JavaDoc) env.get(Context.AUTHORITATIVE);
144         } catch (ClassCastException JavaDoc cce) {
145             return null;
146         }
147     }
148
149     /**
150      * Setter for property authoritative.
151      * @param authoritative New value of property authoritative.
152      */

153     public void setAuthoritative(String JavaDoc authoritative) {
154         env.put(Context.AUTHORITATIVE, authoritative);
155     }
156
157     /**
158      * Getter for property batchsize.
159      * @return Value of property batchsize, or -1 if not set
160      */

161     public int getBatchsize() {
162         Object JavaDoc o = env.get(Context.BATCHSIZE);
163         if (o instanceof Integer JavaDoc) {
164             return ((Integer JavaDoc)o).intValue();
165         } else if (o != null) {
166             return Integer.parseInt(o.toString());
167         } else {
168             return -1;
169         }
170     }
171
172     /**
173      * Setter for property batchsize.
174      * @param batchsize New value of property batchsize.
175      */

176     public void setBatchsize(int batchsize) {
177         env.put(Context.BATCHSIZE, new Integer JavaDoc(batchsize));
178     }
179
180     /**
181      * Getter for property objectFactories.
182      * @return Value of property objectFactories.
183      */

184     public String JavaDoc getObjectFactories() {
185         try {
186             return (String JavaDoc) env.get(Context.OBJECT_FACTORIES);
187         } catch (ClassCastException JavaDoc cce) {
188             return null;
189         }
190     }
191
192     /**
193      * Setter for property objectFactories.
194      * @param objectFactories New value of property objectFactories.
195      */

196     public void setObjectFactories(String JavaDoc objectFactories) {
197         env.put(Context.OBJECT_FACTORIES, objectFactories);
198     }
199
200     /**
201      * Getter for property stateFactories.
202      * @return Value of property stateFactories.
203      */

204     public String JavaDoc getStateFactories() {
205         try {
206             return (String JavaDoc) env.get(Context.STATE_FACTORIES);
207         } catch (ClassCastException JavaDoc cce) {
208             return null;
209         }
210     }
211
212     /**
213      * Setter for property stateFactories.
214      * @param stateFactories New value of property stateFactories.
215      */

216     public void setStateFactories(String JavaDoc stateFactories) {
217         env.put(Context.STATE_FACTORIES, stateFactories);
218     }
219
220     /**
221      * Getter for property urlPkgPrefixes.
222      * @return Value of property urlPkgPrefixes.
223      */

224     public String JavaDoc getUrlPkgPrefixes() {
225         try {
226             return (String JavaDoc) env.get(Context.URL_PKG_PREFIXES);
227         } catch (ClassCastException JavaDoc cce) {
228             return null;
229         }
230     }
231
232     /**
233      * Setter for property urlPkgPrefixes.
234      * @param urlPkgPrefixes New value of property urlPkgPrefixes.
235      */

236     public void setUrlPkgPrefixes(String JavaDoc urlPkgPrefixes) {
237         env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
238     }
239     
240     /**
241      * Getter for property dnsUrl.
242      * @return Value of property dnsUrl.
243      */

244     public String JavaDoc getDnsUrl() {
245         try {
246             return (String JavaDoc) env.get(Context.DNS_URL);
247         } catch (ClassCastException JavaDoc cce) {
248             return null;
249         }
250     }
251
252     /**
253      * Setter for property dnsUrl
254      * @param dnsUrl New value of property dnsUrl
255      */

256     public void setDnsUrl(String JavaDoc dnsUrl) {
257         env.put(Context.DNS_URL, dnsUrl);
258     }
259     
260     public void supplementFromAttributes(String JavaDoc entry) {
261         if (!env.containsKey(entry)) {
262             Object JavaDoc o = pageContext.findAttribute(entry);
263             if (o != null) {
264                 env.put(entry, o);
265             }
266         }
267     }
268     
269     /**
270      * Setter for property scope.
271      * @param scope New value of property scope.
272      */

273     public void setScope(String JavaDoc scope) {
274         if (scope.equalsIgnoreCase("request")) {
275             this.scope = PageContext.REQUEST_SCOPE;
276         } else if (scope.equalsIgnoreCase("session")) {
277             this.scope = PageContext.SESSION_SCOPE;
278         } else if (scope.equalsIgnoreCase("application")) {
279             this.scope = PageContext.APPLICATION_SCOPE;
280         }
281     }
282     
283     protected void pullInSupplimentalAttributes() {
284         supplementFromAttributes(Context.INITIAL_CONTEXT_FACTORY);
285         supplementFromAttributes(Context.OBJECT_FACTORIES);
286         supplementFromAttributes(Context.STATE_FACTORIES);
287         supplementFromAttributes(Context.URL_PKG_PREFIXES);
288         supplementFromAttributes(Context.PROVIDER_URL);
289         supplementFromAttributes(Context.DNS_URL);
290     }
291     
292     protected Object JavaDoc getObjectToExport() throws JspException
293     {
294         try {
295             Context ctx = new InitialContext(env);
296             if (url != null) {
297                 Object JavaDoc o = ctx.lookup(url);
298                 if (o instanceof Context) {
299                     ctx = (Context) o;
300                 }
301             }
302             return ctx;
303         } catch (NamingException ne) {
304             throw new JspException("JNDI useContext tag could not find url: "+ne.getMessage());
305         }
306     }
307
308     public Class JavaDoc getClassOfExportedObject() {
309         return Context.class;
310     }
311
312     public int doEndTag() throws JspException {
313         Object JavaDoc o = pageContext.getAttribute(getId(), scope);
314         if (o != null) {
315             if (!getClassOfExportedObject().isInstance(o)) {
316                 o = null;
317             }
318         }
319         if (o == null) {
320             pullInSupplimentalAttributes();
321             o = getObjectToExport();
322             pageContext.setAttribute(getId(), o, scope);
323         }
324         return EVAL_PAGE;
325     }
326 }
327
Popular Tags