KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Jndi01


1 /*
2  * Copyright 1999, 2000, 2001 ,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.tester;
18
19
20 import java.io.*;
21 import javax.naming.Binding JavaDoc;
22 import javax.naming.Context JavaDoc;
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingEnumeration JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28 import org.apache.tester.SessionBean;
29 import org.apache.tester.shared.SharedSessionBean;
30 import org.apache.tester.unpshared.UnpSharedSessionBean;
31 import org.apache.tester.unshared.UnsharedSessionBean;
32
33
34 /**
35  * Negative test for ensuring that the naming context provided by the servlet
36  * container is immutable. No attempt to add, modify, or delete any binding
37  * should succeed.
38  *
39  * @author Craig R. McClanahan
40  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
41  */

42
43 public class Jndi01 extends HttpServlet {
44
45     public void init() throws ServletException {
46
47         // Access the naming context from init()
48
Context JavaDoc ctx = null;
49         try {
50             ctx = new InitialContext JavaDoc();
51             ctx.lookup("java:/comp");
52             log("initialized successfully in init()");
53         } catch (NamingException JavaDoc e) {
54             e.printStackTrace();
55             log("Cannot create context in init()", e);
56             throw new ServletException(e);
57         }
58
59         // Access some application beans from init()
60

61         try {
62             SessionBean sb = new SessionBean();
63             log("OK Accessing SessionBean");
64         } catch (Throwable JavaDoc t) {
65             log("FAIL Accessing SessionBean", t);
66         }
67
68         try {
69             SharedSessionBean sb = new SharedSessionBean();
70             log("OK Accessing SharedSessionBean");
71         } catch (Throwable JavaDoc t) {
72             log("FAIL Accessing SharedSessionBean", t);
73         }
74
75         try {
76             UnpSharedSessionBean sb = new UnpSharedSessionBean();
77             log("OK Accessing UnpSharedSessionBean");
78         } catch (Throwable JavaDoc t) {
79             log("FAIL Accessing UnpSharedSessionBean", t);
80         }
81
82         try {
83             UnsharedSessionBean sb = new UnsharedSessionBean();
84             log("OK Accessing UnsharedSessionBean");
85         } catch (Throwable JavaDoc t) {
86             log("FAIL Accessing UnsharedSessionBean", t);
87         }
88
89     }
90
91     public void destroy() {
92         Context JavaDoc ctx = null;
93         try {
94             ctx = new InitialContext JavaDoc();
95             ctx.lookup("java:/comp");
96             log("initialized successfully in destroy()");
97         } catch (NamingException JavaDoc e) {
98             e.printStackTrace();
99             log("Cannot create context in destroy()", e);
100         }
101     }
102
103     public void doGet(HttpServletRequest request, HttpServletResponse response)
104         throws IOException, ServletException {
105
106         // Prepare to render our output
107
response.setContentType("text/plain");
108         PrintWriter writer = response.getWriter();
109         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
110         boolean ok = true;
111         Object JavaDoc value = null;
112
113         // Look up the initial context provided by our servlet container
114
Context JavaDoc initContext = null;
115         try {
116             initContext = new InitialContext JavaDoc();
117         } catch (NamingException JavaDoc e) {
118             log("Create initContext", e);
119             sb.append(" Cannot create initContext.");
120             ok = false;
121         }
122
123         // Look up the environment context provided to our web application
124
Context JavaDoc envContext = null;
125         try {
126             if (ok) {
127                 value = initContext.lookup("java:comp/env");
128                 envContext = (Context JavaDoc) value;
129                 if (envContext == null) {
130                     sb.append(" Missing envContext.");
131                     ok = false;
132                 }
133             }
134         } catch (ClassCastException JavaDoc e) {
135             sb.append(" envContext class is ");
136             sb.append(value.getClass().getName());
137             sb.append(".");
138             ok = false;
139         } catch (NamingException JavaDoc e) {
140             log("Create envContext", e);
141             sb.append(" Cannot create envContext.");
142             ok = false;
143         }
144
145         // Attempt to add a new binding to our environment context
146
try {
147             if (ok) {
148                 envContext.bind("newEntry", "New Value");
149                 sb.append(" Allowed bind().");
150                 value = envContext.lookup("newEntry");
151                 if (value != null)
152                     sb.append(" Allowed lookup() of added entry.");
153             }
154         } catch (Throwable JavaDoc e) {
155             log("Add binding", e);
156         }
157
158         // Attempt to change the value of an existing binding
159
try {
160             if (ok) {
161                 envContext.rebind("stringEntry", "Changed Value");
162                 sb.append(" Allowed rebind().");
163                 value = envContext.lookup("stringEntry");
164                 if ((value != null) &&
165                     (value instanceof String JavaDoc) &&
166                     "Changed Value".equals((String JavaDoc) value))
167                     sb.append(" Allowed lookup() of changed entry.");
168             }
169         } catch (Throwable JavaDoc e) {
170             log("Change binding", e);
171         }
172
173         // Attempt to delete an existing binding
174
try {
175             if (ok) {
176                 envContext.unbind("byteEntry");
177                 sb.append(" Allowed unbind().");
178                 value = envContext.lookup("byteEntry");
179                 if (value == null)
180                     sb.append(" Allowed unbind of deleted entry.");
181             }
182         } catch (Throwable JavaDoc e) {
183             log("Delete binding", e);
184         }
185
186         // Report our ultimate success or failure
187
if (sb.length() < 1)
188             writer.println("Jndi01 PASSED");
189         else {
190             writer.print("Jndi01 FAILED -");
191             writer.println(sb);
192         }
193
194         // Add wrapper messages as required
195
while (true) {
196             String JavaDoc message = StaticLogger.read();
197             if (message == null)
198                 break;
199             writer.println(message);
200         }
201         StaticLogger.reset();
202
203     }
204
205 }
206
Popular Tags