Contents

Introduction

Let's play! Imagine playing an action game like God of War(0) with the difficulty level set to "Nightmare." Now, can you imagine beating the final boss "Zeus" easily? Maybe if the game had a bug or if were cheat you could imagine winning, but the process would be slow-going! How can we battle him without passing all the missions?

This is impossible; in order to face Zeus on Nightmare difficulty we need to pass all the missions from the first to the last. And in this article, we will do just: We play the first mission of the J2EE level, but we will play it on Nightmare difficulty.

This article describes how to create a basic modular Java Enterprise Application, by using these Open Source frameworks: an OSGi implementation, Spring, Zk, EhCache, EclipseLink, and some others. I will keep things as simple as I possibly can. But! where is this "nightmare" and this dramatic story? You will be able to answer this question by yourself later.

Ah! I forget to tell you, the Zk Framework doesn't have support for OSGi or EhCache =). No worries, you will taste them. Another thing, just because I said "Zk," and "EclipseLink," doesn't mean I favor one framework over another because this article doesn't favor anything. It is a challenge, a challenge to put certain frameworks in action, nothing more.

You might be saying, "This sound interesting, but I'm a newbie. What is 'OSGi' and 'EhCache' and ...?" Do a little research on them first and come back. There is a full section here just for making a clear image of all these things in your mind.

In your Mind

If you really do the research on OSGi, or you know it already, or you have simply heard by OSGi(1), you can go off this small description:

"OSGi, euh, it is just a framework that allows us to create modular applications, so we can add/ remove plugins easily, and applications stay working without any problems. Experts call these plugins bundles, and it is similar to Eclipse"

With this interesting framework we can be more organized in our development, but don't forget Eclipse is our "Kitchen." But what about the rest of our ingredients like Zk(2)?:

"hmm... Zk! is a web framework for Java, similar to Adobe Flex. However, Zk don't use Flash or MoonLight but generates HTML. Wait, wait, it is like ASP.Net with Ajax Toolkit."

Yeah, cool, Drag and Drop, Effects, no Postback, etc..., very nice RIA. Next is Spring and Spring DM(4,5):

"Spring and Spring DM, just all in one (or in two?)"

Firstly this is a slogan not a description. Secondly, yes; it has many modules for maybe everything needed to develop a Java applications (desktop or web). Last but not least is EclipseLink and EHCache (6,7):

"EclipseLink is an ORM framework for persistence, for inserting, updating, etc... and it is powerful that classical JDBC. EhCache is what his name implies, used for caching things (objects) in memory or hard disk."

Additionally, EclipseLink supports OSGi by default, unlike EhCache which, ifwe battle with it, can support OSGi. There are other secrets about these frameworks but we will discover them later.

Now the big picture in your mind is like ... TA-DA:

No worries at all, this just game we assemble it here, for we can finally have something like this or like:

It is wonderful to hear (read) "Great! You have finished the first mission of J2EE Level: Nightmare!" This also means we can now dirty our hands in Kitchen (Environment) setup, after closing this visual novel.

Environment Setup

Do you remember how we mentioned in precedent chapters the word "Eclipse?" Now it is time to set it up using easy steps for everyone so we can be familiarized with this new world (ops kitchen).

Don't download them integrated by default, use "UPDATE SITE INSTALLATION" so we can have all these tools in one environment. Or if you prefer to discover these toools do whatever you want — just don't lose your way.

Finally we have our environment which looks like this:

We can see Zk things and in top left "Spring Elements."

Now to create our platform for OSGi development in Eclipse, we go to Menu Window > Preferences > and Target Platform. After that we create new target called "J2EE Nightmare" and we add all JARs that support OSGi, this means JARs that we have properties in there MANIFEST files for can be visible in OSGi world, for example:

You notice something? No? Scroll up and re-read "I forget to tell you, the Zk Framework doesn't have support for OSGi" and now we see the MINEFIST file for Zk contains OSGi declarations! What does this mean? Yup, I have added it for you, just download all JARs and add them. To differentiate these JARs I have added the prefix "adrabi-osgi-" if you wish change them by your own.

Testing Time! We want to create a new OSGi run configuration and we UNCHECK three bundles:

com.springsource.junit

org.springframework.test

org.springframework.osgi.test

These are the testing bundles and also, your homework :D

Also, we check "Clear the configuration area before launching" in Settings. Now push the button "Run" after 1 second and you will see a very nice red warning:

[BEGIN-CODE]
log4j:WARN No appenders could be found for logger (org.springframework.osgi.web.tomcat.internal.Activator).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[END-CODE]

If you come come back in a hurry to find out what you have missed just calm down because this is normal warning which we will fix before starting.

Before Starting

We start by creating a fragment for Log4j (fragment is a part of bundle).

Logging Fragment

We give to project a name and choose "an OSGi framework." After that (do you remember?) "fragment is a part of bundle," we need a host bundle! It is log4j "com.springframework.apache.log4j" (click in browser button and type "com" for you can list them):

In this version we remove ".qualifier." Then we create a file named log4j.properties in the root of this project with this simple configuration:

Core Component

We keep the core very simple. It is just for configuring basic services that will be used in all applications:

We configure data source service and transactions.

Also, cache service.

We create now a bundle not a fragment, called in Eclipse "Plug-in Project" (remember to choose the target platform as "an OSGi framework"), give our project a name, and finally we create two directories in the META-INF folder called "config" and "spring,"

This configuration for using HSQL(8) as a database with "pooling" by using the DBCP(9) Component, note it has another secret and lets us continue our work in service component.

Service Component

We type a delicious code for crafting our component service, transactions, cache services, and also an interface for our services and implementation. We're also going to need some entities! We will do so quickly in the next section "Bored Story."

This is all, just notice "@Size,@Pattern" for validation. Next, we create a fragment for core component called "jln-persistence-1.0.0," which contains two directories 1.0.0 in "persistence," in META-INF folder. Remember this line in the core-data-cfg.xml file:

The rest is just Spring configuration files and MANIFEST files too. Remember NEVER forget to look in the MANIFEST file for ALL these bundles and fragments because they contain importation/exportation for many, many, many packages and bundles.

But we are not sure this service work OK! To be sure that service will work and connect to a database etc... we need to do some homework for testing.

Homework: Testing

For preparing testing environment we need to create a user library in menu Window > Preferences > Java Tab > Build Path Tab > User Libraries, we give it a name and we add all JARs.

At last! We've created a Java project. "Java Project" is not Plug-in nor Fragment, but a classical Java project and we add to it our user library (don't forget to add projects entities and todo component to this project in tab "Projects").

We create the integration test extends class AbstractConfigurableBundleCreatorTests and we override two methods:

Resource[]:getTestFrameworkBundles for setup our custom bundles.

And String:getRootPath to override the default class path.

Now we forge some private stuff, for getting services from OSGi. Remember? The services that we have registered by using Spring OSGi look in spring configuration, core, and todo components!

Other stuff you discover in its source and now we create a test for three basic objects: bundleContext, todoService and cacheService. If these three objects don't work, we've done all this for nothing =)

Before you run this test, we need to export all our bundles/fragments:

This is the final screen-shot for all tests. You need to change the path for the JARs folder on your own, the path in the screen-shot is in Linux and you change it by c:/... or if you are in Windows, the total tests in this test for integration is 10 with tests for the cache included too.

Another thing to note is that this works only with JUnit 3. In other words AbstractConfigurableBundleCreatorTests == Green if and only if JUnit.version == 3.

Web Core Component

Web core component is a bundle. We need to create a Plug-in project, give it a name configure it in three parts: "MVC," "Security," and "URL rewriting." But web bundles have different structures than other bundles, and we will show this in the MVC configuration.

Bored Story Again: MVC

The first thing we do now is adding this line "Web-ContextPath: /" in the MANIFEST file for this bundle. What does this line mean? By adding this line, the URL of our web application be like this: “http://127.0.0.1:8080”, we don't need extra path in the URL like "http://127.0.0.1:8080/jln-blabla/".

What is MVC? Rather "How does our web structure look?" Like this:

We have created a new folder called WEB-INF. In it two other folders components and spring, in web.xml we add this configuration for loading the spring configuration for the web component by Spring Dynamic Modules:

Another thing! Zk doesn't have support for validation in JSR-303 by default, and for this reason we need to import our validation service to use it here. Do you remember it? Remember validation fragment. We import it by referencing it in Spring OSGi in spring configuration file web-core-osgi-cfg.xml:

Also not all we create an abstract bean with "prototype" scope for can be base of all bean controllers. Why prototype? By using prototype, Spring container creates new instances for this object in each call. Why do we need this? Just imagine if we have one instance of this object this means all the web pages access the same object, same reference, and same instance! This causes many problems. To avoid this, Zk stops two instances to the page that uses "singleton" bean as the controller.

We finish MVC configuration by creating a base controller for all controllers in this application that have a basic system to use validation JSR-303. For validation we use the "Default" group by default:

The code of Gentle Page stay simple. There are no WOW layouts, just look at the first line and burn it in your mind. This line is used for using spring beans in ZUL files and in other words in ZkScript or Expression Language. Are you excited to test the home page? Okay, we do just use a small URL rewriting after the security setup and we test our home page.

Hacking Setup (Ops Security)

We setup a very basic security by text-plain password in one file “web-hacking-cfg.xml”:

User is "admin" and the password is "*" (one star, with this password which will never be hacked :D), you can see login.zul page, it is basic and similar to code of demonstration in Zk wiki. Let's jump to rewriting.

URL Rewriting

For what used this URL rewriting? Euh, many things but here we use it to hide real URL path and files extensions, example:

Now the big moment comes for you to forge our complete Todo CRUD in a Web Fragment.

Web Fragment

Now I need to repeat how to create a fragment. We will make one for a web core with same structure without configuration files web.xml and urlrewriting.xml, and we create all our ZUL pages in the folder /jln/admin/todo so it can be accessed only by users that have "ROLE_ADMIN." If you forget it scroll up and re-read about this in security configuration.

For others you see them in the source and spring configuration. I think this is the end of our first mission in J2EE Level: Nightmare. We have cheated and kicked all the formal words and definitions. See you later in summary.

Summary

What are you waiting for? I will get fired for leaking these industry secrets! (just kidding xD) The first thing that probably popped in your mind right now is that we are passed all time in configurations

This is true, but the nightmare here is not the configuration but when we set it up, everyhing may become easy.

Spring is framework for DI has many modules as Spring Dynamic Module;

Spring Dynamic Module is part of this hell JARs called Spring, based in OSGi;

OSGi is framework for creating Modular Java Application for Desktop and Web like Zk;

Zk is a Web framework for Java use ZUL files, that we hide it by URLrewrite;

URLrewrite is an API, used to hide ZUL extensions and rewrite URLs, secured and non-secured by Spring Security;

Spring-Security used for securing URLs by “in-memory”, also can use JDBC to get users from database like HSQL;

HSQL is “in memory database” we access to it by EclipseLink;

EclipseLink is an ORM persistence framework, we cache objects retrieved by EhCache

EHCache is an API for caching objects retrieved in memory by EclipseLink via DBCP.

DBCP is an API for using pools with Apache Tomcat;

And finally, the VIP is Apache tomcat as web server;

I hope you are enjoyed by this first mission, and thank you for reading this article, correcting my errors, critiquing, or suggesting something new or better, etc ... :D.