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  import org.eclipse.jetty.security.HashLoginService;
23  import org.eclipse.jetty.server.Server;
24  import org.eclipse.jetty.webapp.WebAppContext;
25  
26  /**
27   * ServerWithAnnotations
28   *
29   *
30   */
31  public class ServerWithAnnotations
32  {
33      public static final void main(String args[]) throws Exception
34      {
35          //Create the server
36          Server server = new Server(8080);
37  
38          //Enable parsing of jndi-related parts of web.xml and jetty-env.xml
39          org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
40          classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
41          classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
42          
43          //Create a WebApp
44          WebAppContext webapp = new WebAppContext();
45          webapp.setContextPath("/");
46          webapp.setWar("../../tests/test-webapps/test-servlet-spec/test-spec-webapp/target/test-spec-webapp-9.0.4-SNAPSHOT.war");
47          server.setHandler(webapp);
48  
49          //Register new transaction manager in JNDI
50          //At runtime, the webapp accesses this as java:comp/UserTransaction
51          org.eclipse.jetty.plus.jndi.Transaction transactionMgr = new org.eclipse.jetty.plus.jndi.Transaction(new com.acme.MockUserTransaction());
52  
53          //Define an env entry with webapp scope.
54          org.eclipse.jetty.plus.jndi.EnvEntry maxAmount = new org.eclipse.jetty.plus.jndi.EnvEntry (webapp, "maxAmount", new Double(100), true);
55          
56          
57          // Register a  mock DataSource scoped to the webapp    
58          org.eclipse.jetty.plus.jndi.Resource mydatasource = new org.eclipse.jetty.plus.jndi.Resource(webapp, "jdbc/mydatasource", new com.acme.MockDataSource());
59      
60          // Configure a LoginService
61          HashLoginService loginService = new HashLoginService();
62          loginService.setName("Test Realm");
63          loginService.setConfig("src/test/resources/realm.properties");
64          server.addBean(loginService);
65          
66          
67          server.start();
68          server.join();
69      }
70  
71  }