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