KickJava   Java API By Example, From Geeks To Geeks.

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


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 LookupTag extends TagSupport {
31
32     private Context context;
33     private String JavaDoc contextRef;
34     private String JavaDoc name = "";
35     private int scope = PageContext.PAGE_SCOPE;
36     private String JavaDoc type = "java.lang.Object";
37     private Name nameObject;
38     
39     
40     /** Creates new LookupTag */
41     public LookupTag() {
42     }
43
44     /** Getter for property context.
45      * @return Value of property context.
46      */

47     public Context getContext() {
48         return context;
49     }
50
51     /**
52      * Setter for property context.
53      * @param context New value of property context.
54      */

55     public void setContext(Context context) {
56         this.context = context;
57     }
58
59     /**
60      * Setter for property contextRef.
61      * @param contextRef New value of property contextRef.
62      */

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

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

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

87     public void setType(String JavaDoc type) {
88         this.type = type;
89     }
90
91     /**
92      * Getter for property name.
93      * @return Value of property name.
94      */

95     public String JavaDoc getName() {
96         return name;
97     }
98
99     /**
100      * Setter for property name.
101      * @param name New value of property name.
102      */

103     public void setName(String JavaDoc name) {
104         this.name = name;
105     }
106
107     /**
108      * Getter for property nameObject.
109      * @return Value of property nameObject.
110      */

111     public Name getNameObject() {
112         return nameObject;
113     }
114
115     /**
116      * Setter for property nameObject.
117      * @param nameObject New value of property nameObject.
118      */

119     public void setNameObject(Name nameObject) {
120         this.nameObject = nameObject;
121     }
122
123     public int doStartTag() throws JspException {
124         return SKIP_BODY;
125     }
126     
127     public int doEndTag() throws JspException {
128         if( contextRef != null ) {
129             context = null;
130             Object JavaDoc o = pageContext.findAttribute(contextRef);
131             if (o instanceof Context) {
132                 context = (Context)o;
133             }
134         }
135         if (context == null) {
136             throw new JspException("context not set in a lookup invocation");
137         }
138         Object JavaDoc o;
139         try {
140             if (nameObject != null) {
141                 o = context.lookup(nameObject);
142             } else {
143                 o = context.lookup(name);
144             }
145             if (Class.forName(type).isInstance(o)) {
146                 pageContext.setAttribute(getId(), o, scope);
147             }
148         } catch (NamingException ne) {
149             throw new JspException(ne.toString());
150         } catch (ClassNotFoundException JavaDoc cnfe) {
151             // no need to handle classNotFound,since the enclosing page
152
// compiles the variable with the same class and it wouldn't
153
// compile since it wasn't found.
154
}
155                 
156         return EVAL_PAGE;
157     }
158     
159     static int decodeScope(String JavaDoc scope) {
160         if (scope.equalsIgnoreCase("request")) {
161             return PageContext.REQUEST_SCOPE;
162         } else if (scope.equalsIgnoreCase("session")) {
163             return PageContext.SESSION_SCOPE;
164         } else if (scope.equalsIgnoreCase("application")) {
165             return PageContext.APPLICATION_SCOPE;
166         } else {
167             return PageContext.PAGE_SCOPE;
168         }
169     }
170 }
171
Popular Tags