ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Nukes: the Open Source Java CMS
Pages: 1, 2

Nukes' Architecture Unveiled

Use each technology for what it does the best

We chose JMX to manage Nukes components to provide hot deployment and component decoupling. This means a component can be removed from the system while it is running with little or no consequence. A webpage is made of several tiles, and each "tile" is a component/block/module package. We also wanted to support completely dynamic web sites, so you can add new modules to a running instance without service disruption. The jboss-system module built on top of core JMX brings us more features than plain vanilla JMX, like service dependencies. Therefore, each component is an MBean service hooked in JBoss. The original PostNuke components have a lifecycle, but they do not handle dependencies at all. Nukes does, leveraging a typical J2EE specification with some JBoss flavor.



In our port we split data and made a clean separation between management model and the business model. Usually business data need to be scalable and highly configurable, so they are often made of entity EJB components. Our infrastructure leverages the local interfaces introduced since EJB 2.0 because we stay in the same VM all the time. We colocate the web server and the component containers in the same space, avoiding excessive serialization. We do not use data patterns such as DAO or DTO, preferring to use JBoss in its default local mode.

Because of the JMX architecture decision, the hardcore management data is exposed as MBeans attributes. We saw how to change the site name and the site slogan, but you can configure Nukes further this way. Each MBean attribute is persisted in the relational database as well, avoiding the loss of configuration when you restart the server.

Nukes Component Development

It is easy to create a Nukes component from scratch. We are going to create a small module.

Module invocation

Since a module is more or less a set of operations, we need a standard way to invoke them. We chose to keep the original way that PostNuke authors decided to specify invocations in URL syntax. It looks deceptively standard. Each action on a module is triggered when you call a URL that looks like this:

http://localhost:8080/index.html?module=module_name&op=operation_name&...

That URL triggers the operation operation_name on the module module_name. The two parameters, module and op, tell Nukes which operation to invoke on what module. The other parameters can be fetched by the module by introspection when it is invoked. Since a module is a JMX MBean, each managed operation that follows the right pattern may be invoked in this manner. This spells out an invocation that the JMX server can apply on a target MBean. Behind the scenes, we translate a URL invocation in a JMX invocation in memory. It is fast and follows the same naming patterns.

public void operation(org.jboss.Nukes.html.Page page)

If you type the URL with the value of the op parameter as operation, Nukes will call that method on your module object.

Module Creation

PostNuke's module model is almost identical to the servlet model. You code your page behavior in pure Java. We prefer this to the JSP Model 2 architecture. Let's start with the TemplateModule class declaration:

// a module extends the class org.jboss.Nukes.module.ModuleSupport
public class TemplateModule extends ModuleSupport
{
   // ...
}

In order to have the Nukes support, your module must extend the ModuleSupport class. This has multiple effects:

  • Your class will become a module per contract
  • You will get access to the Nukes core API
  • Nukes can interact with your module and use it for HTML generation purpose

The Nukes API

The major object which you will have to deal with is the Page object. Let's see how to use it:

/**
 * This method is called with the following url:
 * http://localhost:8080/Nukes/index.html?module=template
 */
public void main(Page page)
{
    page.print(&quot;<div align=\"center\">&quot;);
    page.print("Welcome to the template module");
    page.print(&quot;</div>&quot;);

    // print a form that ask for a name
    page.print(&quot;<table align=\"center\">&quot;);

    // call Nukes main entry point
    page.print(&quot;<form action=\"index.html\" method=\"GET\">&quot;);

    // with module = template
    page.print(&quot;<input type=\"hidden\" name=\"module\" 
        value=\"template\"/>&quot;);

    // and op = action
    page.print(&quot;<input type=\"hidden\" name=\"op\" 
        value=\"action\"/>&quot;);

    // name = XXX
    page.print(&quot;<tr><td>Type your name: 
        </td><td><input type=\"text\" 
               name=\"name\" value=\"\"/></td></tr>&quot;);
    page.print(&quot;<tr><td colspan=\"2\"><input 
        type=\"submit\"/></td></tr>&quot;);
    page.print(&quot;</form>&quot;);
    page.print(&quot;</table>&quot;);
    page.print(&quot;</div>&quot;);
}

As you can see the method main is called with the following url:

http://localhost:8080/Nukes/index.html?module=template&op=main

The Page object is provided as a parameter by the core and is used by this method to render the HTML content. In this Nukes operation, the HTML rendered form carries an URL that will activate the method action of the module Template.

Let's see the action method declared by the Template module:

/** 
 * This method is called with the following url: 
 * http://localhost:8080/Nukes/index.html?module=template&op=action 
 */ 
public void action(Page page) 
{ 
    // get the parameter 
    String name = page.getParameter("name"); 

    // if no name has been provided, just render the main page again 
    if (name == null || name.length() == 0) 
    { 
       main(page); 
       return; 
    } 

    page.print(&quot;<div align=\"center\">&quot;); 
    page.print("Welcome to the template module " + name); 
    if ( getApi().userLoggedIn() ) 
    { 
       page.print("you are logged in"); 
    } 
    page.print(&quot;</div>&quot;); 
}

This method uses Page as a context object because it stores the HTTP parameters like the HttpRequest object in the servlet API. You can use the Page API as you do in the servlet API. Another object that is used by this method is the Api object returned by the getApi() method. It provides services a component may need. In this case it is used to know whether a user is logged in or not and to display a message.

Module packaging

The final step in our module creation is deployment. We have to tell JBoss that the class we just wrote is a Nukes module. The jboss-service.xml file declares a Nukes module using the class we just examined.

<?xml version="1.0" encoding="UTF-8"?>
<server>
   <mbean
      code="org.jboss.Nukes.core.modules.user.blocks.LoginBlock"
      name="Nukes.blocks:name=login">
      <depends>Nukes.modules:name=core</depends>
      <attribute name="DisplayName">Template module</attribute>
      <attribute name="Description">The Template module</attribute>
   </mbean>
</server>

Then we package the class in a jar archive and we add a file jboss-service.xml in the META-INF directory of that archive. Packaging the file is done with the command jar -cvf Nukes-template.sar *. The special extension .sar tells JBoss that the packaged file is a service archive.

Template module package
Figure 7 — the module packaged for deployment

Finally we copy that file into the JBoss deploy directory, where it is automatically deployed. The module can be used without restarting Nukes. Deployment is totally dynamic and is done at runtime.

Template module deployment
Figure 8 — the template module is deployed

Final words

What did we learn from our experience? Software that exists is a good place to start even if it is in another language and we are Java snobs. The second lesson was that PHP/PostNuke isn't really designed to handle very high loads. We decided to port to J2EE and leverage many of the existing APIs (JMX and EJB) to provide a straight port of PHP technology that became "enterprise level" almost immediately. That was the real lesson for us, a proof that all the work we do on system level Java pays off in spades at the application level.

Today, the product is maturing. We've ported the majority of the original PostNuke modules and enhanced some of them along the way. Nukes is production ready — it's in production. It has powered the JBoss.org web site for three months without major problems. Its advanced architecture makes it very scalable and easy to work with. We welcome you to come and help us with the porting of modules so we can finally have a decent CMS/Portal base in Open Source Java.

Marc Fleury , Ph.D., is CEO of JBossGroup, LLC. and founder of the JBoss open-source project.


Return to ONJava.com