1 //======================================================================== 2 //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd. 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 //http://www.apache.org/licenses/LICENSE-2.0 8 //Unless required by applicable law or agreed to in writing, software 9 //distributed under the License is distributed on an "AS IS" BASIS, 10 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 //See the License for the specific language governing permissions and 12 //limitations under the License. 13 //======================================================================== 14 15 /** 16 * 17 */ 18 package com.acme; 19 20 import java.io.IOException; 21 22 import javax.naming.InitialContext; 23 import javax.servlet.ServletConfig; 24 import javax.servlet.ServletException; 25 import javax.servlet.ServletOutputStream; 26 import javax.servlet.http.HttpServlet; 27 import javax.servlet.http.HttpServletRequest; 28 import javax.servlet.http.HttpServletResponse; 29 import javax.sql.DataSource; 30 import javax.transaction.UserTransaction; 31 import javax.annotation.Resource; 32 import javax.annotation.PostConstruct; 33 import javax.annotation.PreDestroy; 34 import javax.annotation.security.RunAs; 35 36 /** 37 * AnnotationTest 38 * 39 * Use Annotations from within Jetty. 40 * 41 * Also, use servlet spec 2.5 resource injection and lifecycle callbacks from within the web.xml 42 * to set up some of the JNDI resources. 43 * 44 */ 45 46 @RunAs("special") 47 public class AnnotationTest extends HttpServlet 48 { 49 private String postConstructResult = ""; 50 private String dsResult = ""; 51 private String envResult = ""; 52 private String envLookupResult = ""; 53 private String envResult2 =""; 54 private String envLookupResult2 = ""; 55 private String envResult3 = ""; 56 private String envLookupResult3 = ""; 57 private String dsLookupResult = ""; 58 private String txResult = ""; 59 private String txLookupResult = ""; 60 private DataSource myDS; 61 62 @Resource(mappedName="UserTransaction") 63 private UserTransaction myUserTransaction; 64 65 66 @Resource(mappedName="maxAmount") 67 private Double maxAmount; 68 69 @Resource(name="someAmount") 70 private Double minAmount; 71 72 @Resource 73 private Double avgAmount; 74 75 76 @Resource(mappedName="jdbc/mydatasource") setMyDatasource(DataSource ds)77 public void setMyDatasource(DataSource ds) 78 { 79 myDS=ds; 80 } 81 82 83 @PostConstruct myPostConstructMethod()84 private void myPostConstructMethod () 85 { 86 postConstructResult = "Called"; 87 try 88 { 89 dsResult = (myDS==null?"FAIL":"myDS="+myDS.toString()); 90 } 91 catch (Exception e) 92 { 93 dsResult = "FAIL: "+e; 94 } 95 96 97 envResult = (maxAmount==null?"FAIL":"maxAmount="+maxAmount.toString()); 98 99 try 100 { 101 InitialContext ic = new InitialContext(); 102 envLookupResult = "java:comp/env/com.acme.AnnotationTest/maxAmount="+ic.lookup("java:comp/env/com.acme.AnnotationTest/maxAmount"); 103 } 104 catch (Exception e) 105 { 106 envLookupResult = "FAIL: "+e; 107 } 108 109 envResult2 = (minAmount==null?"FAIL":"minAmount="+minAmount.toString()); 110 try 111 { 112 InitialContext ic = new InitialContext(); 113 envLookupResult2 = "java:comp/env/someAmount="+ic.lookup("java:comp/env/someAmount"); 114 } 115 catch (Exception e) 116 { 117 envLookupResult2 = "FAIL: "+e; 118 } 119 envResult3 = (minAmount==null?"FAIL":"avgAmount="+avgAmount.toString()); 120 try 121 { 122 InitialContext ic = new InitialContext(); 123 envLookupResult3 = "java:comp/env/com.acme.AnnotationTest/avgAmount="+ic.lookup("java:comp/env/com.acme.AnnotationTest/avgAmount"); 124 } 125 catch (Exception e) 126 { 127 envLookupResult3 = "FAIL: "+e; 128 } 129 130 131 132 try 133 { 134 InitialContext ic = new InitialContext(); 135 dsLookupResult = "java:comp/env/com.acme.AnnotationTest/myDatasource="+ic.lookup("java:comp/env/com.acme.AnnotationTest/myDatasource"); 136 } 137 catch (Exception e) 138 { 139 dsLookupResult = "FAIL: "+e; 140 } 141 142 txResult = (myUserTransaction==null?"FAIL":"myUserTransaction="+myUserTransaction); 143 try 144 { 145 InitialContext ic = new InitialContext(); 146 txLookupResult = "java:comp/env/com.acme.AnnotationTest/myUserTransaction="+ic.lookup("java:comp/env/com.acme.AnnotationTest/myUserTransaction"); 147 } 148 catch (Exception e) 149 { 150 txLookupResult = "FAIL: "+e; 151 } 152 } 153 154 @PreDestroy myPreDestroyMethod()155 private void myPreDestroyMethod() 156 { 157 System.err.println("PreDestroy called"); 158 } 159 init(ServletConfig config)160 public void init(ServletConfig config) throws ServletException 161 { 162 super.init(config); 163 } 164 165 166 167 /* ------------------------------------------------------------ */ doPost(HttpServletRequest request, HttpServletResponse response)168 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 169 { 170 doGet(request, response); 171 } 172 173 /* ------------------------------------------------------------ */ doGet(HttpServletRequest request, HttpServletResponse response)174 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 175 { 176 try 177 { 178 response.setContentType("text/html"); 179 ServletOutputStream out = response.getOutputStream(); 180 out.println("<html>"); 181 out.println("<h1>Jetty6 Annotation Results</h1>"); 182 out.println("<body>"); 183 184 out.println("<h2>@PostConstruct Callback</h2>"); 185 out.println("<pre>"); 186 out.println("@PostConstruct"); 187 out.println("private void myPostConstructMethod ()"); 188 out.println("{}"); 189 out.println("</pre>"); 190 out.println("<br/><b>Result: "+postConstructResult+"</b>"); 191 192 193 out.println("<h2>@Resource Injection for DataSource</h2>"); 194 out.println("<pre>"); 195 out.println("@Resource(mappedName=\"jdbc/mydatasource\");"); 196 out.println("public void setMyDatasource(DataSource ds)"); 197 out.println("{"); 198 out.println("myDS=ds;"); 199 out.println("}"); 200 out.println("</pre>"); 201 out.println("<br/><b>Result: "+dsResult+"</b>"); 202 out.println("<br/><b>JNDI Lookup Result: "+dsLookupResult+"</b>"); 203 204 205 out.println("<h2>@Resource Injection for env-entry </h2>"); 206 out.println("<pre>"); 207 out.println("@Resource(mappedName=\"maxAmount\")"); 208 out.println("private Double maxAmount;"); 209 out.println("@Resource(name=\"minAmount\")"); 210 out.println("private Double minAmount;"); 211 out.println("</pre>"); 212 out.println("<br/><b>Result: "+envResult+": "+(maxAmount.compareTo(new Double(55))==0?" PASS":" FAIL")+"</b>"); 213 out.println("<br/><b>JNDI Lookup Result: "+envLookupResult+"</b>"); 214 out.println("<br/><b>Result: "+envResult2+": "+(minAmount.compareTo(new Double("0.99"))==0?" PASS":" FAIL")+"</b>"); 215 out.println("<br/><b>JNDI Lookup Result: "+envLookupResult2+"</b>"); 216 out.println("<br/><b>Result: "+envResult3+": "+(avgAmount.compareTo(new Double("1.25"))==0?" PASS":" FAIL")+"</b>"); 217 out.println("<br/><b>JNDI Lookup Result: "+envLookupResult3+"</b>"); 218 219 out.println("<h2>@Resource Injection for UserTransaction </h2>"); 220 out.println("<pre>"); 221 out.println("@Resource(mappedName=\"UserTransaction\")"); 222 out.println("private UserTransaction myUserTransaction;"); 223 out.println("</pre>"); 224 out.println("<br/><b>Result: "+txResult+"</b>"); 225 out.println("<br/><b>JNDI Lookup Result: "+txLookupResult+"</b>"); 226 227 out.println("<h2>@RunAs</h2>"); 228 boolean result = request.isUserInRole("special"); 229 out.println("<br/><b>Result: isUserInRole(\"special\")="+result+":"+(result==true?" PASS":" FAIL")+"</b>"); 230 result = request.isUserInRole("other"); 231 out.println("<br/><b>Result: isUserInRole(\"other\")="+result+":"+ (result==false?" PASS":" FAIL")+"</b>"); 232 233 234 out.println("</body>"); 235 out.println("</html>"); 236 out.flush(); 237 } 238 catch (Exception e) 239 { 240 throw new ServletException(e); 241 } 242 } 243 244 245 246 247 } 248