June 10, 2010

Using Vaadin with embedded Grizzly

Grizzly is a framework for building fast and incredibly scalable server applications. It takes advantage of the new Java IO API (NIO) and offers an extended framework for web applications, which includes an embeddable Servlet container.

Vaadin is a Rich Internet Application technology built on top of the Google Web Toolkit. Vaadin Applications run within Servlet Sessions. As such they are very easily deployed in just about every Servlet Container. So, it should be possible to deploy it in an embedded Grizzly Servlet Container.

And that is exactly what I did. I used Grizzly 1.9.18 and Vaadin 6.3.3.

The source code below shows how it's done:


GrizzlyWebServer grizzly = new GrizzlyWebServer();
ServletAdapter sa; 
String servletClassName = "com.vaadin.terminal.gwt.server.ApplicationServlet";
try {
    Servlet s = (Servlet)Class.forName(servletClassName).newInstance();
    sa = new ServletAdapter();
    sa.setServletInstance(s);
} catch (...) {
}
sa.setRootFolder("WebContent");
sa.addContextParameter("productionMode", "false");
sa.setContextPath("/MyApp");
sa.setProperty(ServletAdapter.LOAD_ON_STARTUP, "1");
sa.addInitParameter("application", "my.domain.MyApplication");
grizzly.addGrizzlyAdapter(sa, new String[]{"/MyApp","/VAADIN"});

I have marked 4 important lines in the code:
  1. the line where the root folder is set;
  2. the line where the context path is set;
  3. the line where your Vaadin application is deployed;
  4. the line where the ServletAdapter instance that contains your Vaadin application, is deployed within Grizzly.
The root folder must be set to the folder that contains the Vaadin Web library. Vaadin also requires you to specify a servlet mapping that maps * (all files) in the servlet's context folder to your application (when configured with web.xml you would need to add a servlet-mapping element). Grizzly does not support servlet mappings yet. You can only map all files ("*") in the context folder of the servlet to a servlet instance. Fortunately, that is all we need. That's why the context folder of the Vaadin Servlet containing your Vaadin application is explicitly set to folder "MyApp".
The third line I marked instructs the Vaadin Servlet to load your Vaadin application class. I guess I don;t need to explain why that is important.
Finally, on the last line, the Vaadin Servlet is deployed on the embedded Grizzly Server. It is essential that you add the string "/VAADIN" to map the path /VAADIN (which is used the Ajax application that the java code of your Vaadin Application is rendered to by the Google Web Toolkit) to the Vaadin widgets and themes that are inside the Vaadin Web library.

Vaadin runs great inside Grizzly. It allows for building fast and rich user interfaces that run in a browser in embedded environments (such as the dashboard of a car, for example). You could build embedded systems that can be controlled through a standard web interface.

As for the image at the top: I combined the logo of Grizzly with the Vaadin Logo, which I rotated 90 degrees so I could fit it on the poor bear's face. It does make the bear look like an owl, doesn't it? That bear suddenly looks a lot smarter. I hope the designers of the logo's don't mind my playful joke here.

I am using Grizzly and Vaadin to build an HTTP proxy server that runs on your desktop. This proxy server can be used to filter web content, but also to monitor and analyze HTTP traffic sent from and received by your desktop. Vaadin provides a wealth of rich UI widgets (trees, grids, charts, you name it) I can use for that purpose. And Grizzly provides an easy and very fast way for me to embed a thin web application layer on my proxy server. I have named my little pet project "Yapser" (Yet Another Proxy SERver) and made it open source from the start. My project is hosted on Google Code: http://code.google.com/p/yapser/. If you feel like participating, you're more than welcome of course!

1 comment:

Unknown said...

did you try with Grizzly Deployer ?

that should allow you to use a .war instead of using a GrizzlyAdapter.