Thursday, January 12, 2012

Integrating Apache Roller to applications


Apache Roller - Blogger: (http://roller.apache.org)

  • Version: Roller 4.0.1 is the "best available" version of Roller
  • The Java development kit: specifically the Sun Java 2 SE 1.5 JDK. The computer on which you install Roller should be configured to run with the Java SE 5.
  • Application Server - Jboss-5.1.0.GA: a servlet container that supports the Servlet 2.4 API
  • Database Server:  the Roller community tends to use and is best able to answer questions about MySQL 5.X
  • Installation Guide :

Blogger dependency jars list(apart from dependency jars) :

  • roller-business.jar
  • roller-core.jar
  • roller-planet-business.jar
  • guice-1.0.jar
  • jdom.jar
  • lucene-1.4.3.jar
  • ognl-2.6.11.jar
  • openjpa-0.9.7-incubating.jar
  • oro-2.0.8.jar
  • serp-1.11.0.jar
  • velocity-1.5.jar
Context Loader Listener

package youproject.config;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.business.startup.StartupException;
import org.apache.roller.weblogger.business.startup.WebloggerStartup;
import org.apache.roller.weblogger.config.WebloggerConfig;
import org.apache.roller.weblogger.util.cache.CacheManager;
import org.apache.velocity.runtime.RuntimeSingleton;
import org.springframework.beans.factory.access.BootstrapException;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;

/**
 * @author gangadharjn
 *
 */
public class ProjectContextLoaderListener extends ContextLoaderListener {

    private static Log log = LogFactory.getLog(
ProjectContextLoaderListener .class);

    private static ServletContext servletContext = null;

    public
ProjectContextLoaderListener () {
        super();
    }

    /**
     * Get the ServletContext.
     *
     * @return ServletContext
     */
    public static ServletContext getServletContext() {
        return servletContext;
    }

    /**
     * Responds to app-init event and triggers startup procedures.
     */
    @SuppressWarnings("static-access")
    public void contextInitialized(ServletContextEvent sce) {
        // First, initialize everything that requires no database

        // Keep a reverence to ServletContext object
        this.servletContext = sce.getServletContext();

        // Call Spring's context ContextLoaderListener to initialize all the
        // context files specified in web.xml. This is necessary because
        // listeners don't initialize in the order specified in 2.3 containers
        super.contextInitialized(sce);

        // get the *real* path to <context>/resources
        String ctxPath = servletContext.getRealPath("/");
        if (!ctxPath.endsWith(File.separator))
            ctxPath += File.separator + "resources";
        else
            ctxPath += "resources";

        // try setting the uploads path to <context>/resources
        // NOTE: this should go away at some point
        // we leave it here for now to allow users to keep writing
        // uploads into their webapp context, but this is a bad idea
        //
        // also, the WebloggerConfig.setUploadsDir() method is smart
        // enough to disregard this call unless the uploads.path
        // is set to ${webapp.context}
        WebloggerConfig.setUploadsDir(ctxPath);

        // try setting the themes path to <context>/themes
        // NOTE: this should go away at some point
        // we leave it here for now to allow users to keep using
        // themes in their webapp context, but this is a bad idea
        //
        // also, the WebloggerConfig.setThemesDir() method is smart
        // enough to disregard this call unless the themes.dir
        // is set to ${webapp.context}
        WebloggerConfig.setThemesDir(servletContext.getRealPath("/themes"));

        // Now prepare the core services of the app so we can bootstrap
        try {
            WebloggerStartup.prepare();
        } catch (StartupException ex) {
            log
                    .fatal(
                            "Weblogger startup failed during app preparation",
                            ex);
            return;
        }

        // if preparation failed or is incomplete then we are done,
        // otherwise try to bootstrap the business tier
        if (!WebloggerStartup.isPrepared()) {
            StringBuffer buf = new StringBuffer();
            buf
                    .append("\n--------------------------------------------------------------");
            buf
                    .append("\Weblogger startup INCOMPLETE, user interaction required");
            buf
                    .append("\n--------------------------------------------------------------");
            log.info(buf.toString());
        } else {
            try {
                // trigger bootstrapping process
                WebloggerFactory.bootstrap();

                // trigger initialization process
                WebloggerFactory.getWeblogger().initialize();
            } catch (BootstrapException ex) {
                ex.printStackTrace();
                log.fatal("Weblogger bootstrap failed", ex);
            } catch (Exception ex) {
                ex.printStackTrace();
                log.fatal("Weblogger initialization failed", ex);
            }
        }

        WebApplicationContext currentContext = super.getContextLoader()
                .getCurrentWebApplicationContext();
    }

    /**
     * Responds to app-destroy event and triggers shutdown sequence.
     */
    public void contextDestroyed(ServletContextEvent sce) {
        WebloggerFactory.getWeblogger().shutdown();
        // do we need a more generic mechanism for presentation layer shutdown?
        CacheManager.shutdown();
    }
}

Integrating Blogger with Dev Setup

·         Instead of using the Blogger as a separate application, it’s been used as a library which will be added to java/web class path. The initialization process of the Blogger takes place once at the start of the web application. During the initialization process all the contexts related to Blogger such as JPA, Guice etc will be made available to application context.
·         The spring controller layer of application is made to interact with the loosely coupled JPA layer of the Blogger which provides enough control while rendering the UI. The blogger JPA layer can be customized to suite the application requirements.
·         Created new Custom context loader class(yourproject.ProjectContextLoaderListener) for Spring which will first load the Spring context and then the Blogger Guice context.
The entry in web.xml to specify the custom context loader is
<listener>
          <listener-class>yourproject.ProjectContextLoaderListener </listener-class>
    </listener>
·         For the custom context loader to work properly, the application needs to have ‘roller-custom.properties’ and ‘JPAEMF.properties’ in the classpath.

No comments: