| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package com.acme; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import javax.mail.Session; |
| 23 |
import javax.naming.InitialContext; |
| 24 |
import javax.servlet.ServletConfig; |
| 25 |
import javax.servlet.ServletException; |
| 26 |
import javax.servlet.ServletOutputStream; |
| 27 |
import javax.servlet.http.HttpServlet; |
| 28 |
import javax.servlet.http.HttpServletRequest; |
| 29 |
import javax.servlet.http.HttpServletResponse; |
| 30 |
import javax.sql.DataSource; |
| 31 |
import javax.transaction.UserTransaction; |
| 32 |
|
| 33 |
/** |
| 34 |
* JNDITest |
| 35 |
* |
| 36 |
* Use JNDI from within Jetty. |
| 37 |
* |
| 38 |
* Also, use servlet spec 2.5 resource injection and lifecycle callbacks from within the web.xml |
| 39 |
* to set up some of the JNDI resources. |
| 40 |
* |
| 41 |
*/ |
| 42 |
public class JNDITest extends HttpServlet |
| 43 |
{ |
| 44 |
private DataSource myDS; |
| 45 |
|
| 46 |
private Session myMailSession; |
| 47 |
private Double wiggle; |
| 48 |
private Integer woggle; |
| 49 |
private Double gargle; |
| 50 |
private String svr; |
| 51 |
|
| 52 |
private String resourceNameMappingInjectionResult; |
| 53 |
private String envEntryOverrideResult; |
| 54 |
private String postConstructResult = "PostConstruct method called: <span class=\"fail\">FALSE</span>"; |
| 55 |
private String preDestroyResult = "PreDestroy method called: <span class=\"pass\">NOT YET</span>"; |
| 56 |
private String envEntryGlobalScopeResult; |
| 57 |
private String envEntryWebAppScopeResult; |
| 58 |
private String userTransactionResult; |
| 59 |
private String mailSessionResult; |
| 60 |
private String svrResult; |
| 61 |
|
| 62 |
public void setMyDatasource(DataSource ds) |
| 63 |
{ |
| 64 |
myDS = ds; |
| 65 |
} |
| 66 |
|
| 67 |
private void postConstruct() |
| 68 |
{ |
| 69 |
resourceNameMappingInjectionResult = "Injection of resource to locally mapped name (java:comp/env/mydatasource as java:comp/env/mydatasource1): " + (myDS != null ? "<span class=\"pass\">PASS</span>" : "<span class=\"fail\">FAIL</span>"); |
| 70 |
envEntryOverrideResult = "Override of EnvEntry in jetty-env.xml (java:comp/env/wiggle): " + (wiggle == 55.0 ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL(expected 55.0, got " + wiggle + ")") + "</span>"; |
| 71 |
postConstructResult = "PostConstruct method called: <span class=\"pass\">PASS</span>"; |
| 72 |
} |
| 73 |
|
| 74 |
private void preDestroy() |
| 75 |
{ |
| 76 |
preDestroyResult = "PreDestroy method called: <span class=\"pass\">PASS</span>"; |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void init(ServletConfig config) throws ServletException |
| 81 |
{ |
| 82 |
super.init(config); |
| 83 |
try |
| 84 |
{ |
| 85 |
InitialContext ic = new InitialContext(); |
| 86 |
woggle = (Integer)ic.lookup("java:comp/env/woggle"); |
| 87 |
envEntryGlobalScopeResult = "EnvEntry defined in context xml lookup result (java:comp/env/woggle): " + (woggle == 4000 ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL(expected 4000, got " + woggle + ")") + "</span>"; |
| 88 |
gargle = (Double)ic.lookup("java:comp/env/gargle"); |
| 89 |
svr = (String)ic.lookup("java:comp/env/svr"); |
| 90 |
svrResult = "Ref to Server in jetty-env.xml result: " + (svr != null ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span>"; |
| 91 |
|
| 92 |
envEntryWebAppScopeResult = "EnvEntry defined in jetty-env.xml lookup result (java:comp/env/gargle): " + (gargle == 100.0 ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL(expected 100, got " + gargle + ")") + "</span>"; |
| 93 |
UserTransaction utx = (UserTransaction)ic.lookup("java:comp/UserTransaction"); |
| 94 |
userTransactionResult = "UserTransaction lookup result (java:comp/UserTransaction): " + (utx != null ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span>"; |
| 95 |
myMailSession = (Session)ic.lookup("java:comp/env/mail/Session"); |
| 96 |
mailSessionResult = "Mail Session lookup result (java:comp/env/mail/Session): " + (myMailSession != null ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span>"; |
| 97 |
} |
| 98 |
catch (Exception e) |
| 99 |
{ |
| 100 |
throw new ServletException(e); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| 106 |
{ |
| 107 |
doGet(request, response); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| 112 |
{ |
| 113 |
String mailTo = request.getParameter("mailto"); |
| 114 |
String mailFrom = request.getParameter("mailfrom"); |
| 115 |
|
| 116 |
if (mailTo != null) |
| 117 |
mailTo = mailTo.trim(); |
| 118 |
|
| 119 |
if (mailFrom != null) |
| 120 |
mailFrom = mailFrom.trim(); |
| 121 |
|
| 122 |
try |
| 123 |
{ |
| 124 |
response.setContentType("text/html"); |
| 125 |
ServletOutputStream out = response.getOutputStream(); |
| 126 |
out.println("<html>"); |
| 127 |
out.println("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></head>"); |
| 128 |
out.println("<h1>Jetty JNDI Tests</h1>"); |
| 129 |
out.println("<body>"); |
| 130 |
|
| 131 |
out.println("<h2>Injection and JNDI Lookup Results</h2>"); |
| 132 |
out.println("<p>" + resourceNameMappingInjectionResult + "</p>"); |
| 133 |
out.println("<p>" + envEntryOverrideResult + "</p>"); |
| 134 |
out.println("<p>" + postConstructResult + "</p>"); |
| 135 |
out.println("<p>" + preDestroyResult + "</p>"); |
| 136 |
out.println("<p>" + envEntryGlobalScopeResult + "</p>"); |
| 137 |
out.println("<p>" + envEntryWebAppScopeResult + "</p>"); |
| 138 |
out.println("<p>" + svrResult + "</p>"); |
| 139 |
out.println("<p>" + userTransactionResult + "</p>"); |
| 140 |
out.println("<p>" + mailSessionResult + "</p>"); |
| 141 |
|
| 142 |
out.println("</body>"); |
| 143 |
out.println("</html>"); |
| 144 |
out.flush(); |
| 145 |
} |
| 146 |
catch (Exception e) |
| 147 |
{ |
| 148 |
throw new ServletException(e); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void destroy() |
| 154 |
{ |
| 155 |
} |
| 156 |
} |