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  package org.eclipse.jetty.embedded;
21  
22  
23  import java.util.Properties;
24  import org.eclipse.jetty.server.Server;
25  import org.eclipse.jetty.webapp.WebAppContext;
26  
27  /**
28   * ServerWithJNDI
29   *
30   *
31   */
32  public class ServerWithJNDI
33  {
34      public static void main(String[] args) throws Exception
35      {
36  
37          //Create the server
38          Server server = new Server(8080);
39          
40          //Enable parsing of jndi-related parts of web.xml and jetty-env.xml
41          org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
42          classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
43  
44          //Create a WebApp
45          WebAppContext webapp = new WebAppContext();
46          webapp.setContextPath("/");
47          webapp.setWar("../../tests/test-webapps/test-jndi-webapp/target/test-jndi-webapp-9.0.4-SNAPSHOT.war");
48          server.setHandler(webapp);
49  
50          //Register new transaction manager in JNDI
51          //At runtime, the webapp accesses this as java:comp/UserTransaction
52          org.eclipse.jetty.plus.jndi.Transaction transactionMgr = new org.eclipse.jetty.plus.jndi.Transaction(new com.acme.MockUserTransaction());
53  
54          //Define an env entry with Server scope.
55          //At runtime, the webapp accesses this as java:comp/env/woggle  
56          //This is equivalent to putting an env-entry in web.xml: 
57          //<env-entry> 
58          //  <env-entry-name>woggle</env-entry-name> 
59          //  <env-entry-type>java.lang.Integer</env-entry-type> 
60          //  <env-entry-value>4000</env-entry-value> 
61          //</env-entry>
62          org.eclipse.jetty.plus.jndi.EnvEntry woggle = new org.eclipse.jetty.plus.jndi.EnvEntry(server, "woggle", new Integer(4000), false);
63  
64  
65          //Define an env entry with webapp scope.
66          //At runtime, the webapp accesses this as java:comp/env/wiggle
67          //This is equivalent to putting a web.xml entry in web.xml:    
68          //<env-entry>
69          //  <env-entry-name>wiggle</env-entry-name>
70          //  <env-entry-value>100</env-entry-value>
71          //  <env-entry-type>java.lang.Double</env-entry-type>
72          //</env-entry>
73          //Note that the last arg of "true" means that this definition for "wiggle" would override an entry of the
74          //same name in web.xml
75          org.eclipse.jetty.plus.jndi.EnvEntry wiggle = new org.eclipse.jetty.plus.jndi.EnvEntry(webapp, "wiggle", new Double(100), true);
76  
77          //Register a reference to a mail service scoped to the webapp.
78          //This must be linked to the webapp by an entry in web.xml:
79          // <resource-ref>
80          //  <res-ref-name>mail/Session</res-ref-name>
81          //  <res-type>javax.mail.Session</res-type>
82          //  <res-auth>Container</res-auth>
83          // </resource-ref>
84          //At runtime the webapp accesses this as java:comp/env/mail/Session
85          org.eclipse.jetty.jndi.factories.MailSessionReference mailref = new org.eclipse.jetty.jndi.factories.MailSessionReference();
86          mailref.setUser("CHANGE-ME");
87          mailref.setPassword("CHANGE-ME");
88          Properties props = new Properties();
89          props.put("mail.smtp.auth", "false");
90          props.put("mail.smtp.host","CHANGE-ME");
91          props.put("mail.from","CHANGE-ME");
92          props.put("mail.debug", "false");
93          mailref.setProperties(props);
94          org.eclipse.jetty.plus.jndi.Resource xxxmail = new org.eclipse.jetty.plus.jndi.Resource(webapp, "mail/Session", mailref);
95       
96  
97          // Register a  mock DataSource scoped to the webapp
98          //This must be linked to the webapp via an entry in web.xml:
99          //<resource-ref>
100         //  <res-ref-name>jdbc/mydatasource</res-ref-name>
101         //  <res-type>javax.sql.DataSource</res-type>
102         //  <res-auth>Container</res-auth>
103         //</resource-ref>
104         //At runtime the webapp accesses this as java:comp/env/jdbc/mydatasource
105         org.eclipse.jetty.plus.jndi.Resource mydatasource = new org.eclipse.jetty.plus.jndi.Resource(webapp, "jdbc/mydatasource",
106                                                                                                      new com.acme.MockDataSource());
107     
108         server.start();
109         server.join();
110     }
111 }