Enterprise Java

Embedded Jetty, Vaadin and Weld

When I develop web applications I like to be able to quickly start them from Eclipse without having to rely on all kinds of heavy-weight tomcat or glassfish plugins. So what I often do is just create a simple Java based Jetty launcher I can run directly from Eclipse. This launcher starts up within a couple of seconds so this makes developing a lot more pleasant.

However, sometimes, getting everything setup correctly is a bit hard. So in this article I’ll give you a quick overview of how you can setup Jetty together with Weld for CDI and Vaadin as the web framework.

To get everything setup correctly we’ll need to take the following steps:

  1. Setup the maven pom for the required dependencies
  2. Create a java based Jetty Launcher
  3. Setup web.xml
  4. Add Weld placeholders

Setup the maven pom for the required dependencies

I use the following pom.xml file. You might not need everything if you for instance don’t use custom components. But it should serve as a good reference of what should be in there.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?xml version="1.0" encoding="UTF-8"?>
 <modelVersion>4.0.0</modelVersion>
 <groupId>group.id</groupId>
 <artifactId>artifact.id</artifactId>
 <packaging>war</packaging>
 <version>1.0</version>
 <name>Vaadin Web Application</name>
  
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <vaadin.version>6.7.1</vaadin.version>
  <gwt.version>2.3.0</gwt.version>
  <gwt.plugin.version>2.2.0</gwt.plugin.version>
 </properties>
  
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>gwt-maven-plugin</artifactId>
    <version>${gwt.plugin.version}</version>
    <configuration>
     <webappDirectory>${project.build.directory}/${project.build.finalName}/VAADIN/widgetsets</webappDirectory>
     <extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs>
     <runTarget>cvgenerator-web</runTarget>
     <hostedWebapp>${project.build.directory}/${project.build.finalName}</hostedWebapp>
     <noServer>true</noServer>
     <port>8080</port>
     <compileReport>false</compileReport>
    </configuration>
    <executions>
     <execution>
      <goals>
       <goal>resources</goal>
       <goal>compile</goal>
      </goals>
     </execution>
    </executions>
    <dependencies>
     <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-dev</artifactId>
      <version>${gwt.version}</version>
     </dependency>
     <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-user</artifactId>
      <version>${gwt.version}</version>
     </dependency>
    </dependencies>
   </plugin>
   <plugin>
    <groupId>com.vaadin</groupId>
    <artifactId>vaadin-maven-plugin</artifactId>
    <version>1.0.2</version>
    <executions>
     <execution>
      <configuration>
      </configuration>
      <goals>
       <goal>update-widgetset</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>
  
       <!-- extra repositories for Vaadin extensions -->
 <repositories>
  <repository>
   <id>vaadin-snapshots</id>
   <releases>
    <enabled>false</enabled>
   </releases>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
  </repository>
  <repository>
   <id>vaadin-addons</id>
  </repository>
 </repositories>
  
        <!-- repositories for the plugins -->
 <pluginRepositories>
  <pluginRepository>
   <id>codehaus-snapshots</id>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
   <releases>
    <enabled>false</enabled>
   </releases>
  </pluginRepository>
  <pluginRepository>
   <id>vaadin-snapshots</id>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
   <releases>
    <enabled>false</enabled>
   </releases>
  </pluginRepository>
 </pluginRepositories>
  
       <!-- minimal set of dependencies -->
 <dependencies>
  <dependency>
   <groupId>com.vaadin</groupId>
   <artifactId>vaadin</artifactId>
   <version>${vaadin.version}</version>
  </dependency>
  <dependency>
   <groupId>org.vaadin.addons</groupId>
   <artifactId>stepper</artifactId>
   <version>1.1.0</version>
  </dependency>
                <!-- the jetty version we'll use -->
  <dependency>
   <groupId>org.eclipse.jetty.aggregate</groupId>
   <artifactId>jetty-all-server</artifactId>
   <version>8.0.4.v20111024</version>
   <type>jar</type>
   <scope>compile</scope>
   <exclusions>
    <exclusion>
     <artifactId>mail</artifactId>
     <groupId>javax.mail</groupId>
    </exclusion>
   </exclusions>
  </dependency>
  
  
  
               <!-- vaadin custom field addon -->
  <dependency>
   <groupId>org.vaadin.addons</groupId>
   <artifactId>customfield</artifactId>
   <version>0.9.3</version>
  </dependency>
  
               <!-- with cdi utils plugin you can use Weld -->
  <dependency>
   <groupId>org.vaadin.addons</groupId>
   <artifactId>cdi-utils</artifactId>
   <version>0.8.6</version>
  </dependency>
  
               <!-- we'll use this version of Weld -->
  <dependency>
   <groupId>org.jboss.weld.servlet</groupId>
   <artifactId>weld-servlet</artifactId>
   <version>1.1.5.Final</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  
               <!-- normally following are provided, but not if you run within jetty -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
   <type>jar</type>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.2</version>
   <type>jar</type>
   <scope>provided</scope>
  </dependency>
  
  <dependency>
   <artifactId>el-api</artifactId>
   <groupId>javax.el</groupId>
   <version>2.2</version>
   <scope>provided</scope>
  </dependency>
 </dependencies>
  
</project>

Create the java launcher

With this pom we have all the dependencies we need to run Jetty, Vaadin and Weld together. Lets look at the Jetty Launcher.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import javax.naming.InitialContext;
import javax.naming.Reference;
  
import org.eclipse.jetty.plus.jndi.Resource;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
  
/**
 * Simple jetty launcher, which launches the webapplication from the local
 * resources and reuses the projects classpath.
 *
 * @author jos
 */
public class Launcher {
  
 /** run under root context */
 private static String contextPath = "/";
 /** location where resources should be provided from for VAADIN resources */
 private static String resourceBase = "src/main/webapp";
 /** port to listen on */
 private static int httpPort = 8081;
  
    private static String[] __dftConfigurationClasses =
    {
        "org.eclipse.jetty.webapp.WebInfConfiguration",
        "org.eclipse.jetty.webapp.WebXmlConfiguration",
        "org.eclipse.jetty.webapp.MetaInfConfiguration",
        "org.eclipse.jetty.webapp.FragmentConfiguration",       
        "org.eclipse.jetty.plus.webapp.EnvConfiguration",
        "org.eclipse.jetty.webapp.JettyWebXmlConfiguration"
    } ;
  
 /**
  * Start the server, and keep waiting.
  */
 public static void main(String[] args) throws Exception {
  
  System.setProperty("java.naming.factory.url","org.eclipse.jetty.jndi");
  System.setProperty("java.naming.factory.initial","org.eclipse.jetty.jndi.InitialContextFactory");
  
  InitialContext ctx = new InitialContext();
  ctx.createSubcontext("java:comp");
  
  Server server = new Server(httpPort);
  WebAppContext webapp = new WebAppContext();
  webapp.setConfigurationClasses(__dftConfigurationClasses);
  
  webapp.setDescriptor("src/main/webapp/WEB-INF/web.xml");
  webapp.setContextPath(contextPath);
  webapp.setResourceBase(resourceBase);
  webapp.setClassLoader(Thread.currentThread().getContextClassLoader());
  
  server.setHandler(webapp);
  server.start();
  
  new Resource("BeanManager", new Reference("javax.enterprise.inject.spi.BeanMnanager",
    "org.jboss.weld.resources.ManagerObjectFactory", null));
  
  server.join();
 }
}

This code will start a Jetty server that uses the web.xml from the project to launch the Vaadin web-app. Take note that we explicitly use the
setConfigurationClasses
operation. This is needed to make sure we have a JNDI context we can use to register the Weld beanmanager in.

Setup the web.xml

Next we’ll look at the web.xml. The one I use in this example is shown next:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
 id="WebApp_ID" version="2.5">
 <display-name>Vaadin Web Application</display-name>
 <context-param>
  <description>Vaadin production mode</description>
  <param-name>productionMode</param-name>
  <param-value>false</param-value>
 </context-param>
 <servlet>
  <servlet-name>example</servlet-name>
  <servlet-class>ServletSpecifiedByTheCDIVaadinPlugin</servlet-class>
  <init-param>
   <description>Vaadin application class to start</description>
   <param-name>application</param-name>
   <param-value>VaadinApplicationClassName</param-value>
  </init-param>
  <init-param>
   <param-name>widgetset</param-name>
   <param-value>customwidgetsetnameifyouuseit</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>example</servlet-name>
  <url-pattern>/example/*</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
 <listener>
  <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
 </listener>
 <resource-env-ref>
  <description>Object factory for the CDI Bean Manager</description>
  <resource-env-ref-name>BeanManager</resource-env-ref-name>
  <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
 </resource-env-ref>
</web-app>

At the bottom of the web.xml you can see the resource-env we define for Weld and the required listener to make sure Weld is started and our beans are injected. You can also see that we specified a different servlet name instead of the normal Vaadin servlet. For the details on this see the CDI plugin page: https://vaadin.com/directory#addon/cdi-utils

The main steps are (taken from that page):

  1. Add empty beans.xml -file (CDI marker file) to your project under WEB-INF dir
  2. Add cdiutils*.jar to your project under WEB-INF/lib
  3. Create your Application class by extending AbstractCdiApplication
  4. Extend AbstractCdiApplicationServlet and annotate it with @WebServlet(urlPatterns = “/*”)
  5. Deploy to JavaEE/Web profile -compatible container (CDI apps can also be run on servlet containers etc. but some further configuration is required)

Add weld placeholder

At this point we have all the dependencies, we created a launcher that can be directly used from Eclipse, and we made sure Weld is loaded on startup. We’ve also configured the CDI plugin for Vaadin. At this point we’re almost done. We only need to add empty beans.xml files in the location we want to be included by the beans discovery of Weld.

1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
</beans>

I had to add these to the
src/main/java/META-INF
library and to the
WEB-INF
directory for Weld to pickup all the annotated beans. And that’s it. You can now start the launcher and you should see all kind of Weld and Vaadin logging appearing.

Reference: Embedded Jetty, Vaadin and Weld from our JCG partner Jos Dirksen  at the Smart Java blog.

Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button