View Javadoc

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