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  package org.eclipse.jetty.embedded;
20  
21  import java.io.File;
22  
23  import org.eclipse.jetty.plus.jndi.EnvEntry;
24  import org.eclipse.jetty.plus.jndi.Resource;
25  import org.eclipse.jetty.plus.jndi.Transaction;
26  import org.eclipse.jetty.security.HashLoginService;
27  import org.eclipse.jetty.server.Server;
28  import org.eclipse.jetty.webapp.Configuration;
29  import org.eclipse.jetty.webapp.WebAppContext;
30  
31  /**
32   * ServerWithAnnotations
33   */
34  public class ServerWithAnnotations
35  {
36      public static final void main( String args[] ) throws Exception
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          Configuration.ClassList classlist = Configuration.ClassList
43                  .setServerDefault(server);
44          classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",
45                  "org.eclipse.jetty.plus.webapp.EnvConfiguration",
46                  "org.eclipse.jetty.plus.webapp.PlusConfiguration");
47          classlist.addBefore(
48                  "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
49                  "org.eclipse.jetty.annotations.AnnotationConfiguration");
50  
51          // Create a WebApp
52          WebAppContext webapp = new WebAppContext();
53          webapp.setContextPath("/");
54          File warFile = new File(
55                  "../../jetty-distribution/target/distribution/demo-base/webapps/test.war");
56          webapp.setWar(warFile.getAbsolutePath());
57          webapp.setAttribute(
58                  "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
59                  ".*/javax.servlet-[^/]*\\.jar$|.*/servlet-api-[^/]*\\.jar$");
60          server.setHandler(webapp);
61  
62          // Register new transaction manager in JNDI
63          // At runtime, the webapp accesses this as java:comp/UserTransaction
64          new Transaction(new com.acme.MockUserTransaction());
65  
66          // Define an env entry with webapp scope.
67          new EnvEntry(webapp, "maxAmount", new Double(100), true);
68  
69          // Register a mock DataSource scoped to the webapp
70          new Resource(webapp, "jdbc/mydatasource", new com.acme.MockDataSource());
71  
72          // Configure a LoginService
73          HashLoginService loginService = new HashLoginService();
74          loginService.setName("Test Realm");
75          loginService.setConfig("src/test/resources/realm.properties");
76          server.addBean(loginService);
77  
78          server.start();
79          server.join();
80      }
81  
82  }