1 16 17 package org.apache.tester; 18 19 20 import java.io.*; 21 import javax.naming.Binding ; 22 import javax.naming.Context ; 23 import javax.naming.InitialContext ; 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NamingException ; 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 42 43 public class Jndi01 extends HttpServlet { 44 45 public void init() throws ServletException { 46 47 Context ctx = null; 49 try { 50 ctx = new InitialContext (); 51 ctx.lookup("java:/comp"); 52 log("initialized successfully in init()"); 53 } catch (NamingException e) { 54 e.printStackTrace(); 55 log("Cannot create context in init()", e); 56 throw new ServletException(e); 57 } 58 59 61 try { 62 SessionBean sb = new SessionBean(); 63 log("OK Accessing SessionBean"); 64 } catch (Throwable t) { 65 log("FAIL Accessing SessionBean", t); 66 } 67 68 try { 69 SharedSessionBean sb = new SharedSessionBean(); 70 log("OK Accessing SharedSessionBean"); 71 } catch (Throwable t) { 72 log("FAIL Accessing SharedSessionBean", t); 73 } 74 75 try { 76 UnpSharedSessionBean sb = new UnpSharedSessionBean(); 77 log("OK Accessing UnpSharedSessionBean"); 78 } catch (Throwable t) { 79 log("FAIL Accessing UnpSharedSessionBean", t); 80 } 81 82 try { 83 UnsharedSessionBean sb = new UnsharedSessionBean(); 84 log("OK Accessing UnsharedSessionBean"); 85 } catch (Throwable t) { 86 log("FAIL Accessing UnsharedSessionBean", t); 87 } 88 89 } 90 91 public void destroy() { 92 Context ctx = null; 93 try { 94 ctx = new InitialContext (); 95 ctx.lookup("java:/comp"); 96 log("initialized successfully in destroy()"); 97 } catch (NamingException 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 response.setContentType("text/plain"); 108 PrintWriter writer = response.getWriter(); 109 StringBuffer sb = new StringBuffer (); 110 boolean ok = true; 111 Object value = null; 112 113 Context initContext = null; 115 try { 116 initContext = new InitialContext (); 117 } catch (NamingException e) { 118 log("Create initContext", e); 119 sb.append(" Cannot create initContext."); 120 ok = false; 121 } 122 123 Context envContext = null; 125 try { 126 if (ok) { 127 value = initContext.lookup("java:comp/env"); 128 envContext = (Context ) value; 129 if (envContext == null) { 130 sb.append(" Missing envContext."); 131 ok = false; 132 } 133 } 134 } catch (ClassCastException e) { 135 sb.append(" envContext class is "); 136 sb.append(value.getClass().getName()); 137 sb.append("."); 138 ok = false; 139 } catch (NamingException e) { 140 log("Create envContext", e); 141 sb.append(" Cannot create envContext."); 142 ok = false; 143 } 144 145 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 e) { 155 log("Add binding", e); 156 } 157 158 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 ) && 166 "Changed Value".equals((String ) value)) 167 sb.append(" Allowed lookup() of changed entry."); 168 } 169 } catch (Throwable e) { 170 log("Change binding", e); 171 } 172 173 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 e) { 183 log("Delete binding", e); 184 } 185 186 if (sb.length() < 1) 188 writer.println("Jndi01 PASSED"); 189 else { 190 writer.print("Jndi01 FAILED -"); 191 writer.println(sb); 192 } 193 194 while (true) { 196 String message = StaticLogger.read(); 197 if (message == null) 198 break; 199 writer.println(message); 200 } 201 StaticLogger.reset(); 202 203 } 204 205 } 206 | Popular Tags |