| Subcribe via RSS

Using Jackrabbit to Store Velocity Templates in Spring

November 23rd, 2008 | No Comments | 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 | 1 Comment | 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: ,

Innovate or Die

October 26th, 2008 | No Comments | Posted in evangalism

I had a Eureka! moment about why companies choose open source. They choose it because they must innovate or die. There will always be people who choose open source because it is bleeding edge and I am not talking about those people. I am talking about companies that choose open source because of a compelling business reason.

Innovation comes because of two problems: costs and features.

Most people will quickly site costs as a reason to choose open source. This belittles many of the other reasons to choose open source, but cost is a valid point. Many companies cannot pay for existing software. Just look at some of the “enterprise” type of programs and the costs are astronomical. So what is the company suppose to do, just roll over dead because it can’t buy software. NO! The company can choose open source because the initial licensing fees are now gone. Sure the company will have to support the software themselves with more sweat equity, but small companies are full of sweat equity and little cash. So these companies innovated by taking on their software responsibilities themselves.

The second way to innovate is through new features. Tons of companies differentiate themselves by the features on their websites. Do you think that these fancy features came right out of the box? NO! Many of these companies started with an existing open source project and then added new features to it. Instead of buying a proprietary system that they could not extend, these types of companies chose open source where they have full access to all the code allowing them to add features out the wazoo.

I think it is easy for small companies to fit into the innovate or die category. So selling open source to these types of companies is rather easy. Open source in larger companies is harder. With the budget to buy enterprise class software and a track record of out sourcing major projects, convincing a company to bring development in-house and use open source is a tough sell. I think if the desire to innovate is in the company, it would be easy to make the switch. If that innovation desire does not exist, put your flack jacket on because you have a long hard fight ahead of you.

But there is good news. Board rooms of larger companies are starting to feel the breeze of the open source change. They see how open source has benefited other companies and are starting to ask, “can we use open source?” And that my friend, seems like the beginning of a desire to innovate.

dotCMS First Look

October 22nd, 2008 | No Comments | Posted in java

I’ve recently been looking around for a good java cms. I know that is seems like php has all the fun with Drupal and Wordpress and java is really lagging. I think that is really true to a point. Java websites are not intended for the hobbiest. That is why php based systems have caught on. But java is a great player for larger corporate environments. The established java cms systems are proprietary and costly. There have been a few stabs at creating an open source cms system. Alfresco and Liferay are large and cumbersome and I think try to do to much. OpenCMS is pretty solid, but I wish it would use more common open source projects. Just yesterday I can across dotCMS and so far I am pretty impressed. Here are my first impressions.

What I like about dotCMS

I like that it uses popular open source projects like hibernate, velocity and quartz. These are projects I understand, so I like that I get to reuse that knowledge. The user interface is crisp and looks professional. dotCMS uses the term “Structure” to define bits of content, like blog postings or news items. In these structures, you can dynamically define fields through the admin screen, without having to change the database or a configuration file. That is nice. There is a built-in workflow engine that is good for approving copy. I think the way that workflow is assigned and used could be improved, but they have a good start.

But my favorite part of dotCMS is the integration of lucene. I really love lucene. Most people may think of lucene as just a search engine, but that is just scratching the surface. True, it has an index and you can search, but you can extend that thinking to have lucene act as a meta database. You can tag your content with lots of different meta fields, then use lucene to return results based on complex queries. This is extremely useful once you understand how to use that. And dotCMS uses this mentality throughout their site. They have a velocity macro setup to pull content into a webpage based on a lucene query.

What I don’t like about dotCMS

I never had a great impression of Liferay and dotCMS initially got it’s start as a port of Liferay 3.x. I know that that is an old version of Liferay, but sharing the same heritage carries with it those past feelings. I am worried that basing a website on a portlet type of system may have performance issues. I wish that it would be easier to make clean, seo friendly urls. They currently let you define url redirects, but I don’t want a redirect. I want the final url to be clean. I wish there were some type of module/plugin system. Creating new velocity macros is nice, but what if I want to add a message board or link directory. I haven’t spend enough time to see how that would be integrated.

Questions still lingering

I have really only spent a few hours working with dotCMS, so I’m sure there is plenty of things I haven’t learned yet. But here a few questions I still have.

  1. How does clustering work? Can you have one master server then publish to multiple slave servers?
  2. What is the performance like on a high traffic site?
  3. Can I assign templates to match url patterns, like sitemesh?
  4. How do you adminster multiple sites through the admin screens
Tags: ,

Open Source Enthusiasts are DIY’ers

October 22nd, 2008 | No Comments | Posted in evangalism

I think that a majority of people who use open source do it because they like to DO and solve problems. They are the type of people who would rather drive around a city aimlessly instead of stopping to ask for directions. Solving the problem, at whatever cost, is a major driving force for many open source users.

Since the problem solving desire is a personality trait, it is hard to make a business case to choose open source based on your own desire to solve the problem. Managers want numbers. They want to know that by choosing open source, they will save $X or productivity will increase Y%. Maybe instead, there should be a measurement that with the right developers, choosing open source will increase morale Z%. I know that I am more happy when I work with tools I enjoy.

Increased morale is one of those touchy-feely areas. Just because you have happy employees doesn’t mean you will have a better product. But it might make the whole process more enjoyable if you don’t have cranky developers.

I think choosing open source is most effective when the whole company wants to solve the problem. If parts of the company are ready to pay a vendor mucho dinero to make the problem go away, then there will probably be friction if you choose open source. Take a look at other parts of the business like accounting or marketing. Does the company outsource a lot of work? Would the company rather pay a consultant to do a project instead of investing in more internal resources to get the project done? If you are working with an “outsourcing” minded company, then open source will probably be a hard sell unless you can prove that is saves a ton of money . But when it comes to choosing between two equally priced options, outsourcing or diy’ing, the company will probably choose the option that is more inline with the existing corporate mindset.

An Unintended Open Source Advocate

October 21st, 2008 | No Comments | Posted in evangalism

I never consider myself one to take up the torch for a particular cause. You won’t see me demonstrating infront of a government building or writing letters to my Senator. But lately I have noticed that I am championing the idea of open source to clients.

Over the years, I have used plenty of open source projects while developing websites. At first it was probably because I didn’t want to pay for MS Visual Studio. Then I didn’t want to pay for proprietery components. But I think what it really turned into was an issue of freedom.

With open source, I feel like I am in control of my project. If I want a feature that isn’t standard with a particular product, open source allows me to create that feature. I can pick and choose on the component and framework level. This lets me create the system that I WANT to use, not a system that is full of hacks to make things work the way I want.

There are tons of great projects with solid documentation and online support options. Just think of Apache, Tomcat and MySQL. These are all major players in the internet world and they are all open source. If you get stuck, just search around the internet to find your answer or you can even find places that offer paid support.

So am I an advocate? I guess so. The freedom of choice and extensibility is what I really love about open source.