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