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: <span class=\"fail\">FALSE</span>";
59      private String preDestroyResult = "PreDestroy method called: <span class=\"pass\">NOT YET</span>";
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          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>");
75          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>";
76          postConstructResult = "PostConstruct method called: <span class=\"pass\">PASS</span>";
77      }
78      
79      private void preDestroy()
80      {
81          preDestroyResult = "PreDestroy method called: <span class=\"pass\">PASS</span>";
82      }
83      
84      
85      public void init(ServletConfig config) throws ServletException
86      {
87          super.init(config);
88          try
89          {
90              InitialContext ic = new InitialContext();
91              woggle = (Integer)ic.lookup("java:comp/env/woggle");
92              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>";
93              gargle = (Double)ic.lookup("java:comp/env/gargle");
94              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>";
95              UserTransaction utx = (UserTransaction)ic.lookup("java:comp/UserTransaction");
96              userTransactionResult = "UserTransaction lookup result (java:comp/UserTransaction): "+(utx!=null?"<span class=\"pass\">PASS":"<span class=\"fail\">FAIL")+"</span>";
97              myMailSession = (Session)ic.lookup("java:comp/env/mail/Session");
98              mailSessionResult = "Mail Session lookup result (java:comp/env/mail/Session): "+(myMailSession!=null?"<span class=\"pass\">PASS": "<span class=\"fail\">FAIL")+"</span>";
99          }
100         catch (Exception e)
101         {
102             throw new ServletException(e);
103         }
104     }
105 
106     
107     
108     /* ------------------------------------------------------------ */
109     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
110     {
111         doGet(request, response);
112     }
113 
114     /* ------------------------------------------------------------ */
115     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
116     {   
117         String mailTo = request.getParameter("mailto");
118         String mailFrom = request.getParameter("mailfrom");
119         
120         if (mailTo != null)
121             mailTo = mailTo.trim();
122         
123         if (mailFrom != null)
124             mailFrom = mailFrom.trim();
125         
126         try
127         {
128             response.setContentType("text/html");
129             ServletOutputStream out = response.getOutputStream();
130             out.println("<html>");
131             out.println("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></head>");
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 }