KickJava   Java API By Example, From Geeks To Geeks.

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


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

30 public class ListTag extends BodyTagSupport {
31
32     private Context context;
33     private String JavaDoc contextRef;
34     private String JavaDoc nameId;
35     private String JavaDoc classId;
36     private String JavaDoc objId;
37     private int nameScope = PageContext.PAGE_SCOPE;
38     private int classScope = PageContext.PAGE_SCOPE;
39     private int objScope = PageContext.PAGE_SCOPE;
40
41     private String JavaDoc name;
42     private Name nameObject;
43     private boolean bindings;
44     
45     private NamingEnumeration nameEnum;
46     private NameClassPair currentListing;
47     
48     /** Creates new ListTag */
49     public ListTag() {
50     }
51
52     /** Getter for property context.
53      * @return Value of property context.
54      */

55     public Context getContext() {
56         return context;
57     }
58
59     /**
60      * Setter for property context.
61      * @param context New value of property context.
62      */

63     public void setContext(Context context) {
64         this.context = context;
65     }
66
67     /**
68      * Setter for property contextRef.
69      * @param contextRef New value of property contextRef.
70      */

71     public void setContextRef(String JavaDoc contextRef) {
72         this.contextRef = contextRef;
73     }
74
75     /**
76      * Getter for property nameId.
77      * @return Value of property nameId.
78      */

79     public String JavaDoc getNameId() {
80         return nameId;
81     }
82
83     /**
84      * Setter for property nameId.
85      * @param nameId New value of property nameId.
86      */

87     public void setNameId(String JavaDoc nameId) {
88         this.nameId = nameId;
89     }
90
91     /**
92      * Setter for property nameScope.
93      * @param nameScope New value of property nameScope.
94      */

95     public void setNameScope(String JavaDoc nameScope) {
96         this.nameScope = decodeScope(nameScope);
97     }
98
99     /**
100      * Getter for property classId.
101      * @return Value of property classId.
102      */

103     public String JavaDoc getClassId() {
104         return classId;
105     }
106
107     /**
108      * Setter for property classId.
109      * @param classId New value of property classId.
110      */

111     public void setClassId(String JavaDoc classId) {
112         this.classId = classId;
113     }
114
115     /**
116      * Setter for property classScope.
117      * @param classScope New value of property classScope.
118      */

119     public void setClassScope(String JavaDoc classScope) {
120         this.classScope = decodeScope(classScope);
121     }
122
123     /**
124      * Getter for property objId.
125      * @return Value of property objId.
126      */

127     public String JavaDoc getObjId() {
128         return objId;
129     }
130
131     /**
132      * Setter for property objId.
133      * @param objId New value of property objId.
134      */

135     public void setObjId(String JavaDoc objId) {
136         this.objId = objId;
137     }
138
139     /** Setter for property objScope.
140      * @param objScope New value of property objScope.
141      */

142     public void setObjScope(String JavaDoc objScope) {
143         this.objScope = decodeScope(objScope);
144     }
145
146     /**
147      * Getter for property name.
148      * @return Value of property name.
149      */

150     public String JavaDoc getName() {
151         return name;
152     }
153
154     /**
155      * Setter for property name.
156      * @param name New value of property name.
157      */

158     public void setName(String JavaDoc name) {
159         this.name = name;
160     }
161
162     /**
163      * Getter for property nameObject.
164      * @return Value of property nameObject.
165      */

166     public Name getNameObject() {
167         return nameObject;
168     }
169
170     /**
171      * Setter for property nameObject.
172      * @param nameObject New value of property nameObject.
173      */

174     public void setNameObject(Name nameObject) {
175         this.nameObject = nameObject;
176     }
177
178     /**
179      * Getter for property bindings.
180      * @return Value of property bindings.
181      */

182     public boolean getBindings() {
183         return bindings;
184     }
185
186     /**
187      * Setter for property bindings.
188      * @param bindings New value of property bindings.
189      */

190     public void setBindings(boolean bindings) {
191         this.bindings = bindings;
192     }
193     
194     public int doStartTag() throws JspException {
195         if( contextRef != null ) {
196             context = null;
197             Object JavaDoc o = pageContext.findAttribute(contextRef);
198             if (o instanceof Context) {
199                 context = (Context)o;
200             }
201         }
202         if (context == null) {
203             throw new JspException("Context is not set for list itteration");
204         }
205         try {
206             if (nameObject != null) {
207                 if (bindings) {
208                     nameEnum = context.listBindings(nameObject);
209                 } else {
210                     nameEnum = context.list(nameObject);
211                 }
212             } else {
213                 if (bindings) {
214                     nameEnum = context.listBindings(name);
215                 } else {
216                     nameEnum = context.list(name);
217                 }
218             }
219             
220             if (nameEnum.hasMoreElements()) {
221                 return EVAL_BODY_TAG;
222             } else {
223                 return SKIP_BODY;
224             }
225         } catch (NamingException ne) {
226             throw new JspTagException("JNDI list tag could not list bindings: "+ne.getMessage());
227         }
228     }
229     
230     public void doInitBody() {
231         currentListing = (NameClassPair) nameEnum.nextElement();
232         if (nameId != null) {
233             pageContext.setAttribute(nameId,
234                     currentListing.getName(), nameScope);
235         }
236         if (classId != null) {
237             pageContext.setAttribute(classId,
238                     currentListing.getClassName(), classScope);
239         }
240         if (bindings && (objId != null)) {
241         if (currentListing instanceof Binding) {
242                 pageContext.setAttribute(objId,
243                     ((Binding)currentListing).getObject(), objScope);
244         } else {
245                 pageContext.setAttribute(objId, "Uh Oh, no Binding!", objScope);
246             }
247         }
248     }
249     
250     public int doAfterBody() {
251         if (nameEnum.hasMoreElements()) {
252             currentListing = (NameClassPair) nameEnum.nextElement();
253             if (nameId != null) {
254                 pageContext.setAttribute(nameId,
255                         currentListing.getName(), nameScope);
256             }
257             if (classId != null) {
258                 pageContext.setAttribute(classId,
259                         currentListing.getClassName(), classScope);
260             }
261             if (bindings && (objId != null)) {
262                 if (currentListing instanceof Binding) {
263                     pageContext.setAttribute(objId,
264                         ((Binding)currentListing).getObject(), objScope);
265             } else {
266                     pageContext.setAttribute(objId, "Uh Oh, no Binding!", objScope);
267                 }
268             }
269             return EVAL_BODY_TAG;
270         } else {
271             return SKIP_BODY;
272         }
273     }
274     
275     public int doEndTag() throws JspException {
276         try {
277             if (bodyContent != null) {
278                 bodyContent.writeOut(pageContext.getOut());
279             }
280         } catch (IOException ioe) {
281             throw new JspException(ioe.toString());
282         }
283         if (nameId != null && nameScope != PageContext.REQUEST_SCOPE) {
284             pageContext.removeAttribute(nameId, nameScope);
285         }
286         if (classId != null && classScope != PageContext.REQUEST_SCOPE) {
287             pageContext.removeAttribute(classId, classScope);
288         }
289         if (bindings && (objId != null) && objScope != PageContext.REQUEST_SCOPE) {
290             pageContext.removeAttribute(objId, objScope);
291         }
292         try {
293              nameEnum.close();
294         } catch (NamingException ne) {
295             // we got what we wanted, it should be safe to ignore this
296
}
297         return EVAL_PAGE;
298     }
299     
300     static int decodeScope(String JavaDoc scope) {
301         if (scope.equalsIgnoreCase("page")) {
302             return PageContext.PAGE_SCOPE;
303         } else if (scope.equalsIgnoreCase("request")) {
304             return PageContext.REQUEST_SCOPE;
305         } else if (scope.equalsIgnoreCase("session")) {
306             return PageContext.SESSION_SCOPE;
307         } else if (scope.equalsIgnoreCase("application")) {
308             return PageContext.APPLICATION_SCOPE;
309         } else {
310             return -1;
311         }
312     }
313 }
314
Popular Tags