Monday, August 27, 2007

Launching and debugging Portlets from Eclipse with Jetty and Pluto

Last time I showed how you can combine Jetty, Pluto and JWebUnit (or any other web testing framework for that matter) to easily write integration tests for your portlets. As a short follow up, I'll show you how to use the same setup to create a simple "Launcher" to get your portlets up and running quickly from Eclipse (but it can easily be modified to work in your favorite IDE).

The first thing to do is to add the usual dependencies to your pom.xml file (assuming you're using Maven 2 to build your portlet project. If not, add the dependencies to your classpath manually):

Update: The maven-jetty-pluto-embedded artifact is available in the Maven 2 repositories.



<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1</artifactId>
<version>6.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.bekk.boss</groupId>
<artifactId>maven-jetty-pluto-embedded</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>


(if you want to know what the maven-jetty-pluto-embedded artifact is, read this). Note that if you have any other dependencies that either directly or indirectly has a dependency to a different version of the servlet api that Jetty 6.1.5 is using (like commons-logging), you need to explicitly exclude this dependency, adding the following snippet inside the dependency:


<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>


If you have not already done so, run mvn eclipse:eclipse to add the dependencies to your Eclipse project. Next, create a new class called JettyPlutoLauncher (I put it in my src/test/java folder). We'll add a main method to this class, doing the exact same as the setUp() method of our base integration test described in the previous entry:


import org.apache.pluto.core.PortletServlet;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.webapp.WebAppContext;

public class JettyPlutoLauncher {
public static void main(String[] args) throws Exception {
System.setProperty("org.apache.pluto.embedded.portletId", "portletId");
Server server = new Server(8080);
WebAppContext webapp = new WebAppContext("src/main/webapp", "/contextRoot");
webapp.setDefaultsDescriptor("/WEB-INF/jetty-pluto-web-default.xml");
ServletHolder portletServlet = new ServletHolder(new PortletServlet());
portletServlet.setInitParameter("portlet-name", "portletId");
portletServlet.setInitOrder(1);
webapp.addServlet(portletServlet, "/PlutoInvoker/portletId");
server.addHandler(webapp);
server.start();
}
}


Replace all occurrences of portletId with the actual id of the portlet to run, and contextRoot with the context root you want for your application.

That's it. Now you can simply select your class and do "Run As - Java Application" to run, or "Debug As - Java Application" to do step by step debugging your portlet!

2 comments:

sofwarewiki said...

Hi, Would you be interested to exchange link with my blog at http://software-wikipedia.blogspot.com.
mail me at software.wikipedia@gmail.com if you are interested.

Prabhat Jha said...

I wrote a similar blog at http://sensiblerationalization.blogspot.com/2010/09/how-to-debug-portlet-in-gatein.html