KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.naming.*;
22 import javax.naming.directory.*;
23
24 import java.io.*;
25
26 /**
27  *
28  * @author Danno Ferrin <shemnon@earthlink.net>
29  * @version $Revision: 1.4 $
30  */

31 public class SearchTag extends BodyTagSupport {
32
33     private int scope = PageContext.PAGE_SCOPE;
34     private String JavaDoc attributes;
35     private String JavaDoc attributeSeparator = ",";
36     private SearchControls controls;
37     private DirContext context;
38     private String JavaDoc contextRef;
39     private String JavaDoc filter;
40     private NamingEnumeration nameEnum;
41     private SearchResult currentResult;
42     private String JavaDoc name = "";
43     private Name nameObject;
44
45     /** Creates new SearchTag */
46     public SearchTag() {
47     }
48
49     public void setPageContext(PageContext pc) {
50         controls = new SearchControls();
51         super.setPageContext(pc);
52     }
53
54     /**
55      * Setter for property scope.
56      * @param Scope New value of property scope.
57      */

58     public void setScope(String JavaDoc scope) {
59         this.scope = decodeScope(scope);
60     }
61
62     /**
63      * Getter for property context.
64      * @return Value of property context.
65      */

66     public DirContext getContext() {
67         return context;
68     }
69
70     /**
71      * Setter for property context.
72      * @param context New value of property context.
73      */

74     public void setContext(DirContext context) {
75         this.context = context;
76     }
77
78     /**
79      * Setter for property contextRef.
80      * @param contextRef New value of property contextRef.
81      */

82     public void setContextRef(String JavaDoc contextRef) {
83         this.contextRef = contextRef;
84     }
85
86     /**
87      * Getter for property `.
88      * @return Value of property name.
89      */

90     public String JavaDoc getName() {
91         return name;
92     }
93
94     /**
95      * Setter for property name.
96      * @param name New value of property name.
97      */

98     public void setName(String JavaDoc name) {
99         this.name = name;
100     }
101
102     /**
103      * Getter for property nameObject.
104      * @return Value of property nameObject.
105      */

106     public Name getNameObject() {
107         return nameObject;
108     }
109
110     /**
111      * Setter for property nameObject.
112      * @param nameObject New value of property nameObject.
113      */

114     public void setNameObject(Name nameObject) {
115         this.nameObject = nameObject;
116     }
117
118     /**
119      * Getter for property filter.
120      * @return Value of property filter.
121      */

122     public String JavaDoc getFilter() {
123         return filter;
124     }
125
126     /**
127      * Setter for property filter.
128      * @param filter New value of property filter.
129      */

130     public void setFilter(String JavaDoc filter) {
131         this.filter = filter;
132     }
133
134     /**
135      * Getter for property countLimit.
136      * @return Value of property countLimit.
137      */

138     public long getCountLimit() {
139         return controls.getCountLimit();
140     }
141
142     /**
143      * Setter for property countLimit.
144      * @param countLimit New value of property countLimit.
145      */

146     public void setCountLimit(long countLimit) {
147         controls.setCountLimit(countLimit);
148     }
149
150     /**
151      * Getter for property derefLink.
152      * @return Value of property derefLink.
153      */

154     public boolean getDerefLink() {
155         return controls.getDerefLinkFlag();
156     }
157
158     /**
159      * Setter for property derefLink.
160      * @param derefLink New value of property derefLink.
161      */

162     public void setDerefLink(boolean derefLink) {
163         controls.setDerefLinkFlag(derefLink);
164     }
165
166     /**
167      * Getter for property attributes.
168      * @return Value of property attributes.
169      */

170     public String JavaDoc getAttributes() {
171         return attributes;
172     }
173
174     /**
175      * Setter for property attributes.
176      * @param attributes New value of property attributes.
177      */

178     public void setAttributes(String JavaDoc attributes) {
179         this.attributes = attributes;
180     }
181
182     /**
183      * Getter for property attributeSeparator.
184      * @return Value of property attrbuteSeparator.
185      */

186     public String JavaDoc getAttributeSeparator() {
187         return attributeSeparator;
188     }
189
190     /**
191      * Setter for property attributeSeparator.
192      * @param attrbuteSeparator New value of property attrbuteSeparator.
193      */

194     public void setAttributeSeparator(String JavaDoc attributeSeparator) {
195         this.attributeSeparator = attributeSeparator;
196     }
197
198     /**
199      * Getter for property bindings.
200      * @return Value of property bindings.
201      */

202     public boolean getBindings() {
203         return controls.getReturningObjFlag();
204     }
205
206     /**
207      * Setter for property bindings.
208      * @param bindings New value of property bindings.
209      */

210     public void setBindings(boolean bindings) {
211         controls.setReturningObjFlag(bindings);
212     }
213
214     /**
215      * Getter for property searchScope.
216      * @return Value of property searchScope.
217      */

218     public String JavaDoc getSearchScope() {
219         return decodeSearchScope(controls.getSearchScope());
220     }
221
222     /**
223      * Setter for property searchScope.
224      * @param searchScope New value of property searchScope.
225      */

226     public void setSearchScope(String JavaDoc searchScope) {
227         controls.setSearchScope(decodeSearchScope(searchScope));
228     }
229
230     /**
231      * Getter for property timeLimit.
232      * @return Value of property timeLimit.
233      */

234     public int getTimeLimit() {
235         return controls.getTimeLimit();
236     }
237
238     /**
239      * Setter for property timeLimit.
240      * @param timeLimit New value of property timeLimit.
241      */

242     public void setTimeLimit(int timeLimit) {
243         controls.setTimeLimit(timeLimit);
244     }
245     
246     public int doStartTag() throws JspException {
247         // Initialize invocation variables
248
nameEnum = null;
249
250         if( contextRef != null ) {
251             context = null;
252             Object JavaDoc o = pageContext.findAttribute(contextRef);
253             if (o instanceof DirContext) {
254                 context = (DirContext) o;
255             } else if (o instanceof Context) {
256                 try {
257                     // attempt a blank lookup
258
o = ((Context)o).lookup("");
259                     if (o instanceof DirContext) {
260                         context = (DirContext) o;
261                     }
262                 } catch (NamingException ne) {
263                     // oh well, it failed, toss it out
264
}
265             }
266         }
267         if( context == null ) {
268             throw new JspException("JNDI search tag could not find a context");
269         }
270         String JavaDoc[] attrs = null;
271         if (attributes != null) {
272             if (attributes.length() == 0) {
273                 attrs = new String JavaDoc[0];
274             } else {
275                 int count = 1;
276                 int i=0, j=0, size = attributeSeparator.length();
277                 while ((j=attributes.indexOf(attributeSeparator, i)) != -1) {
278                     count++;
279                     i = j + size;
280                 }
281                 attrs = new String JavaDoc[count];
282                 count = 0;
283                 i = 0;
284                 while ((j=attributes.indexOf(attributeSeparator, i)) != -1) {
285                     attrs[count] = attributes.substring(i, j);
286                     count++;
287                     i = j + size;
288                 }
289                 attrs[count] = attributes.substring(i);
290             }
291         }
292         
293         controls.setReturningAttributes(attrs);
294
295         try {
296             if (nameObject != null) {
297                 nameEnum = context.search(nameObject, filter, controls);
298             } else {
299                 nameEnum = context.search(name, filter, controls);
300             }
301         } catch (NamingException ne) {
302             throw new JspException("JNDI search tag failed: "+ne.getMessage());
303         }
304         
305         if (nameEnum.hasMoreElements()) {
306             return EVAL_BODY_TAG;
307         } else {
308             return SKIP_BODY;
309         }
310     }
311     
312     public void doInitBody() {
313         currentResult = (SearchResult) nameEnum.nextElement();
314         if (getId() != null) {
315             pageContext.setAttribute(getId(),
316                     currentResult, scope);
317         }
318     }
319         
320     public int doAfterBody() {
321         if (! nameEnum.hasMoreElements()) {
322             return SKIP_BODY;
323         } else {
324             currentResult = (SearchResult) nameEnum.nextElement();
325             if (getId() != null) {
326                 pageContext.setAttribute(getId(),
327                         currentResult, scope);
328             }
329             return EVAL_BODY_TAG;
330         }
331     }
332     
333     public int doEndTag() throws JspException {
334         try {
335             if (bodyContent != null) {
336                 bodyContent.writeOut(pageContext.getOut());
337             }
338         } catch (IOException ioe) {
339             throw new JspException(ioe.toString());
340         }
341         try {
342             nameEnum.close();
343         } catch (NamingException ne) {
344             throw new JspException(ne.toString());
345         }
346         return EVAL_PAGE;
347     }
348     
349     public static int decodeSearchScope(String JavaDoc scope) {
350         // a better way would be to pop a hashtable with Integer as value
351
if ("object_scope".equalsIgnoreCase(scope) ||
352             "object".equalsIgnoreCase(scope)) {
353             return SearchControls.OBJECT_SCOPE;
354         } else if ("onelevel_scope".equalsIgnoreCase(scope) ||
355                    "onelevel".equalsIgnoreCase(scope)) {
356             return SearchControls.ONELEVEL_SCOPE;
357         } else if ("subtree_scope".equalsIgnoreCase(scope) ||
358                    "subtree".equalsIgnoreCase(scope)) {
359             return SearchControls.SUBTREE_SCOPE;
360         } else {
361             return -1;
362         }
363     }
364     
365     public static String JavaDoc decodeSearchScope(int scope) {
366         switch (scope) {
367             case SearchControls.OBJECT_SCOPE:
368                 return "OBJECT_SCOPE";
369             case SearchControls.ONELEVEL_SCOPE:
370                 return "ONELEVEL_SCOPE";
371             case SearchControls.SUBTREE_SCOPE:
372                 return "SUBTREE_SCOPE";
373             default:
374                 return null;
375         }
376     }
377
378     public static int decodeScope(String JavaDoc scope) {
379         if (scope.equalsIgnoreCase("request")) {
380             return PageContext.REQUEST_SCOPE;
381         } else if (scope.equalsIgnoreCase("session")) {
382             return PageContext.SESSION_SCOPE;
383         } else if (scope.equalsIgnoreCase("application")) {
384             return PageContext.APPLICATION_SCOPE;
385         } else {
386             return PageContext.PAGE_SCOPE;
387         }
388     }
389 }
390
Popular Tags