| Subcribe via RSS

Using Jackrabbit to Store Velocity Templates in Spring

November 23rd, 2008 | 1 Comment | Posted in java

I love Velocity. It is simple and quick to pickup. Plus, it plays nicely into MVC design patterns. On a recent project, I wanted to use velocity templates as my views in Spring MVC. Setting that up is pretty straight forward. You just need to add a few entries to your ***-servlet.xml

<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/" />
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".vm" />
<property name="exposeSpringMacroHelpers" value="false" />
</bean>

The velocityConfig bean initializes the velocity engine and sets any velocity specific properties. The viewResolver bean tells spring to use velocity as the view layer instead of the default jsp.

So it’s that simple to get velocity working in Spring. On my current project, I am using Apache Jackrabbit to store content. I started thinking, why not store my velocity templates in Jackrabbit also? It may be a bit of a philosophical reason on where to store your templates, but for this project I wanted to limit the amount of server access needed by the site administrators. So by storing the templates in Jackrabbit, or a database, I can allow the templates to be modified through web forms. Again, this decision is more philosophical and not really the intent of this posting.

Back to the main topic, how to store your velocity templates in Jackrabbit and then have Spring use those templates. This is actually really simple to do. First, you need to get your Spring application setup to handle Jackrabbit. I did this by following the instructions to setup the springmodules-jcr module of Spring Modules. Once you setup springmodules-jcr, you will most likely have a spring bean that will interact with Jackrabbit. For this example, I will call that bean “jcrService”.

We are going to use the ResourceLoader feature of velocity. The ResourceLoader was built to do exactly what we are doing. It allows you to override where and how your templates files are stored. Some existing ResourceLoaders include DataSourceResourceLoader, JarResourceLoader and URLResourceLoader. We need to make a JcrResourceLoader.

Here is the code for my JcrResourceLoader:

import java.io.InputStream;
import javax.jcr.Node;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;

public class JcrResourceLoader extends ResourceLoader {

private JcrService jcrService;

public JcrService getJcrService() {
return jcrService;
}

public void setJcrService(JcrService jcrService) {
this.jcrService = jcrService;
}

@Override
public InputStream getResourceStream(String name)
throws ResourceNotFoundException {

try {
InputStream ins = null;
Node node = jcrService.getNode(name);
Node content = node.getNode("jcr:content");
if (content.hasProperty("jcr:data")) {
ins = content.getProperty("jcr:data").getStream();
}
return ins;

}
catch (Exception e) {
log.error("could not load template for path: " + name);
return null;
}
}

}

Now we need to tell our velocity configuration about this resource loader. In our ***-servlet.xml file, we need to create a bean for our resource loader and to change our velocity config parameters.

<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/" />
<property name="velocityPropertiesMap">
<map>
<entry key="resource.loader" value="jcr" />
<entry key="jcr.resource.loader.instance" value-ref="jcrResourceLoader" />
</map>
</property>

</bean>

<bean id="jcrResourceLoader" class="JcrResourceLoader" >
<property name="jcrService" ref="jcrService"/>
</bean>

And that’s it. Now when your spring controller goes tries to load the velocity template, it will use the JcrResourceLoader to lookup and load the velocity template. This code is just a first prototype and will need to be cleaned up for error checking and performance.

Tags: , ,

Why I WANT to use Apache Jackrabbit

November 13th, 2008 | No Comments | Posted in java

There are times when I come across a new technology and just start to get giddy because it seems so cool. I want to use it right then and there. But then the rational part of my brain kicks in and makes me start to evaluate the situation and determine if this new spiffy technology is even right for my current project. Sometimes it’s a fit, sometimes it’s not.

For my latest project, a new CMS, I have decided to try out Apache Jackrabbit. The title of this post mentions why I WANT to use Jackrabbit. I’ve just started the new CMS, so I haven’t found all the warts and gotcha points of Jackrabbit, but I will try to tell you what excites me about Jackrabbit.

Versioning
Jackrabbit has built-in versioning history. This is great. If I want to keep a history of changes, this is ready for me to use.

Clustering
The CMS I am building is for a high traffic site with a load balanced configuration. Jackrabbit is suppose to be able to cluster. The way I plan to do this is have a staging server as the master. Once all changes have been made, the staging server will push the changes out to the live web servers. I am pretty sure this is possible and if so, this should work nicely.

Momentum
For ages relational databases have been the standard. But more recently, people are finding that those types of databases might not be the best solution for all situations. That is why JCR 170 was created as a content repository spec for java. JCR 170 is meant to store all types of content (text and binary) not just simple text. So you can use it to store files, snippets and even images. JCR 170 has been adopted by projects like Alfresco and Magnolia. I feel that the momentum is gaining in this direction and soon most larger projects will start to take the content repository approach.

So there you have it. I’ve just begun my Jackrabbit life. I’m crossing my fingers but I feel confident that Jackrabbit will be a major help to me in my CMS project.

Evaluating Open Source Options

November 11th, 2008 | 2 Comments | Posted in evangalism, java

The great thing about open source is that there are usually a ton of open source projects to help you solve a problem. The bad thing about open source is that there are usually a ton of open source projects to help you solve a problem. Part of a good open source developer is the mentality to test and evaluate different projects and then choose the best option for your current problem.

I do a lot of development in java. For every type of framework or library you would need, there are a ton of java options to go through. In this post I will outline my thought process on how I evaluate which java product to use in development.

The first thing I do is find what options are available. This is just a bunch of googling and reading message boards to see what projects names are most popular. I also use Open Source Software in Java. This is a great website that categorizes and summarizes the major java open source projects. Once I am familiar with the major names, I go to the project website and start to work through the introductory documentation and “Hello World” examples. At this stage, I physically don’t write the “Hello World” code, instead I just read over the documentation and get a grasp of things work. I know many people will start doing prototypes and such to evaluate options, but I just prefer to do more reading.

So now that I know my options and roughly how they work, how do I choose? There is no set formula, but here are some of the criteria that I use:

Documentation
This goes along with my research phase. Projects with good documentation are much easier to work with. If the project leaders have not written a lot of documentation, then they will most likely not be very forthcoming with support requests. Sometimes programmers put the documentation inside the code itself and solely rely on things like javadocs. This is nice that they wrote something, but digging through javadocs can be a pain because it is often hard to piece together how the classes interact.

Community Activity
The project community is another great place to get support. I look for projects that have active communities with participants outside the sponsoring company. If the community is just full of employees from the sponsoring company, I worry that the project has not been picked up by a lot of people. But a vibrant community is a great thing because people outside the company start to take ownership of the project.

Project Lifetime & Release Cycle
Look at how long the project has been around and how often it releases code. A project that has been around a few years with regular releases is probably solid. A project that hasn’t released in five years is probably dead. I don’t mind using really new projects, you just have to be careful with them. For new projects, I put a more emphasis on the project momentum.

Project Momentum
Project momentum is probably the most important factor for me. I want to use an open source project that will be active throughout the life cycle of my development project. Otherwise you start to have the situation of supporting legacy code that no-one is familiar with. Momentum is tricky and you have to take into account the overall open source atmosphere. What trends is the industry following? What types of technologies are now hot?

A good example of this is Struts. Struts became the standard for java MVC programming a long time ago. There are a ton of projects and programmers that use Struts. But Struts is now on the way out. People have figured out ways to design frameworks that extend the functionality of struts. Would I choose Struts for a new project? No. I feel that a safer bet would be to use a newer, more up and coming framework like Spring MVC.

So evaluting your open source options is not a fixed science and involves some fuzzy math. In the end, choose a library that you are comfortable with and you think will have a longevity in the industry.

Java CMS Dream

November 1st, 2008 | 1 Comment | Posted in java

Where oh where is the java cms of my dreams? For years people have pondered that same question and there is still no answer.

This is what I want: full templating engine, database based on hibernate, deployable as a war and a plugin system similar to Drupal or Wordpress.

There are a few existing systems that have a few of these, but not all. The big stumbling block is the plugin system. But there may be hope now.

The past few days I’ve been learning about OSGi and it looks it will fit the bill. Now I either have to write a cms with OSGi myself or wait for someone else to do it. I predict that within a year, fall 2009, there will be a cms that uses OSGi for plugins. Atleast that is what I am crossing my fingers for.

Tags: ,