View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
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  /**
20   * 
21   */
22  package com.acme;
23  
24  import java.io.IOException;
25  import java.lang.reflect.Method;
26  
27  import javax.mail.Session;
28  import javax.naming.InitialContext;
29  import javax.servlet.ServletConfig;
30  import javax.servlet.ServletException;
31  import javax.servlet.ServletOutputStream;
32  import javax.servlet.http.HttpServlet;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import javax.sql.DataSource;
36  import javax.transaction.UserTransaction;
37  
38  /**
39   * JNDITest
40   * 
41   * Use JNDI from within Jetty.
42   * 
43   * Also, use servlet spec 2.5 resource injection and lifecycle callbacks from within the web.xml
44   * to set up some of the JNDI resources.
45   *
46   */
47  public class JNDITest extends HttpServlet 
48  {   
49      private DataSource myDS;
50    
51      private Session myMailSession;
52      private Double wiggle;
53      private Integer woggle;
54      private Double gargle;
55      
56      private String resourceNameMappingInjectionResult;
57      private String envEntryOverrideResult;
58      private String postConstructResult = "PostConstruct method called: FALSE";
59      private String preDestroyResult = "PreDestroy method called: NOT YET";
60      private String envEntryGlobalScopeResult;
61      private String envEntryWebAppScopeResult;
62      private String userTransactionResult;
63      private String mailSessionResult;
64      
65      
66      public void setMyDatasource(DataSource ds)
67      {
68          myDS=ds;
69      }
70   
71      
72      private void postConstruct ()
73      {
74          String tmp = (myDS == null?"":myDS.toString());
75          resourceNameMappingInjectionResult= "Injection of resource to locally mapped name (java:comp/env/mydatasource as java:comp/env/mydatasource1): "+String.valueOf(myDS);
76          envEntryOverrideResult = "Override of EnvEntry in jetty-env.xml (java:comp/env/wiggle): "+(wiggle==55.0?"PASS":"FAIL(expected 55.0, got "+wiggle+")");
77          postConstructResult = "PostConstruct method called: PASS";
78      }
79      
80      private void preDestroy()
81      {
82          preDestroyResult = "PreDestroy method called: PASS";
83      }
84      
85      
86      public void init(ServletConfig config) throws ServletException
87      {
88          super.init(config);
89          try
90          {
91              InitialContext ic = new InitialContext();
92              woggle = (Integer)ic.lookup("java:comp/env/woggle");
93              envEntryGlobalScopeResult = "EnvEntry defined in context xml lookup result (java:comp/env/woggle): "+(woggle==4000?"PASS":"FAIL(expected 4000, got "+woggle+")");
94              gargle = (Double)ic.lookup("java:comp/env/gargle");
95              envEntryWebAppScopeResult = "EnvEntry defined in jetty-env.xml lookup result (java:comp/env/gargle): "+(gargle==100.0?"PASS":"FAIL(expected 100, got "+gargle+")");
96              UserTransaction utx = (UserTransaction)ic.lookup("java:comp/UserTransaction");
97              userTransactionResult = "UserTransaction lookup result (java:comp/UserTransaction): "+(utx!=null?"PASS":"FAIL");
98              myMailSession = (Session)ic.lookup("java:comp/env/mail/Session");
99              mailSessionResult = "Mail Session lookup result (java:comp/env/mail/Session): "+(myMailSession!=null?"PASS": "FAIL");
100         }
101         catch (Exception e)
102         {
103             throw new ServletException(e);
104         }
105     }
106 
107     
108     
109     /* ------------------------------------------------------------ */
110     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
111     {
112         doGet(request, response);
113     }
114 
115     /* ------------------------------------------------------------ */
116     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
117     {   
118         String mailTo = request.getParameter("mailto");
119         String mailFrom = request.getParameter("mailfrom");
120         
121         if (mailTo != null)
122             mailTo = mailTo.trim();
123         
124         if (mailFrom != null)
125             mailFrom = mailFrom.trim();
126         
127         try
128         {
129             response.setContentType("text/html");
130             ServletOutputStream out = response.getOutputStream();
131             out.println("<html>");
132             out.println("<h1>Jetty JNDI Tests</h1>");
133             out.println("<body>");
134             
135             out.println("<h2>Injection and JNDI Lookup Results</h2>");
136             out.println("<p>"+resourceNameMappingInjectionResult+"</p>");
137             out.println("<p>"+envEntryOverrideResult+"</p>");
138             out.println("<p>"+postConstructResult+"</p>");
139             out.println("<p>"+preDestroyResult+"</p>");
140             out.println("<p>"+envEntryGlobalScopeResult+"</p>");
141             out.println("<p>"+envEntryWebAppScopeResult+"</p>");
142             out.println("<p>"+userTransactionResult+"</p>");
143             out.println("<p>"+mailSessionResult+"</p>");
144         
145 
146             out.println("</body>");            
147             out.println("</html>");
148             out.flush();
149         }
150         catch (Exception e)
151         {
152             throw new ServletException(e);
153         }
154     }
155     
156   
157     
158     public void destroy ()
159     {
160     }
161 }