• SEARCH BOX

Combine Jetty and Spring Application Context

Spring's Logo The goal of this post is to use one xml configuration file which is Spring's applicationContext.xml and load it only once for the duration of the application running in an embedded jetty server. Read more...

Tutorial on Android Layout

A thumbnail of adroid. Android is an open source platform for mobile devices. Not a hardware. It is a software stack as defined by Google. Encase your not familiar with the term, software stack is compose of an Operating System(OS), middle-ware and the key applications. Read more...

Hi, my name's Paul Labis.

I'm a developer in Makati City, Philippines. Focused on building online Java/J2EE based applications. I'm interested in freelance or part-time work from home.

Sunday, August 1, 2010

Software Engineer and Trainee

This tutorial is to create simple blogger template, with simple methods. The easiest way to create blogger template is by edit a template. But don't worry, althought this is a simple methods but it can produce a beautifull template, it depend on how you design it. This tutorial is not to create blogger template from beginning but this is to create template by editing a template. Ok lets begin.
Before we begin I suggest you to create a new blog,weI don't wont to destroy your blog.
To follow this tutorial you have to download this template first. downlad template.


We will create a template that contain four main elements, (Bakground, Header, Main and Footer) Like the image :

Saturday, May 29, 2010

Combine Jetty and Spring Application Context XML Configuration

The goal is to use one xml configuration file which is Spring's applicationContext.xml and load it only once for the duration of the application. Using Embedded jetty as my lightweight server to run my Spring MVC application, I must tell my dispatcher servlets to use spring applicationContext.xml which already been loaded as its parent context. That way, dispatcher servlet will be able to  referenced bean configured on springs application context.

To do that, create a class that extends org.mortbay.jetty.Server and implement org.springframework.context.ApplicationContextAware. This is pretty straight forward, we extend jetty server so we can configure its parent context. We implemented spring application context aware so whenever spring successfully loaded applicatContext.xml bean configuration, it gets the current application context. We then configure it as parent context of servlets.

Code on ServerConfigurer.java:
package com.sample.config;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.webapp.WebAppContext;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;

public class ServerConfigurer
    extends Server
    implements ApplicationContextAware
{
    private String _webAppDir = null;
    private String _contextPath = null;
    private ServletHandler _servletHandler = null;
    private static ApplicationContext _applicationContext = null;

    public String getContextPath() {
        return _contextPath;
    }
    public ServletHandler getServletHandler() {
        return _servletHandler;
    }
    public String getWebAppDir() {
        return _webAppDir;
    }
    /**
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException
    {
        _applicationContext = applicationContext;
    }
    public void setContextPath(String contextPath) {
        _contextPath = contextPath;
    }
    public void setServletHandler(ServletHandler servletHandler) {
        _servletHandler = servletHandler;
    }
    public void setWebAppDir(String webAppDir) {
        _webAppDir = webAppDir;
    }

    @Override
    protected void doStart()
        throws Exception
    {
        final WebAppContext webAppContext = new WebAppContext(getServer(), _webAppDir, _contextPath);
        final GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
        webApplicationContext.setServletContext(webAppContext.getServletContext());
        webApplicationContext.setParent(_applicationContext);
 
webAppContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
        webApplicationContext.refresh();
        webAppContext.setServletHandler(_servletHandler);
        addHandler(webAppContext);
        super.doStart();

    }
}

Text on bold note: Those are the pieces of code that basically are the most important in the server configurer. The setApplicationContext() method is the one that configures that gets the application context loaded by spring. The method doStart() is the one that does the story telling to web app that a current context has been loaded and it is the context should be used. 

Code on applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >


    <!-- more beans configured -->
    <bean name="webServer" class="point to your server configurer java file" init-method="start">   
        <property name="connectors">
            <list>
                <bean class="org.mortbay.jetty.nio.SelectChannelConnector">
                    <property name="host" value="${jetty.host}"/>
                    <property name="port" value="${jetty.port}"/>
                </bean>
            </list>
        </property>

        <property name="webAppDir" value="${jetty.webApp.dir}"/>
        <property name="contextPath" value="${jetty.context.Path}"/>
       
        <property name="servletHandler">
            <bean class="org.mortbay.jetty.servlet.ServletHandler">
                <property name="servlets">
                    <list>
                        <bean class="org.mortbay.jetty.servlet.ServletHolder">
                            <property name="name" value="dispatcher" />
                            <property name="servlet">
                                <bean class="org.springframework.web.servlet.DispatcherServlet" />
                            </property>
                        </bean>
                    </list>
                </property>
                <property name="servletMappings">
                    <list>
                        <bean class="org.mortbay.jetty.servlet.ServletMapping">
                            <property name="servletName" value="dispatcher" />
                            <property name="pathSpec" value="*.htm" />
                        </bean>
                    </list>
                </property>
            </bean>
        </property>

        <property name="handlers">
            <list>
                <!-- log handler -->
                <bean class="org.mortbay.jetty.handler.RequestLogHandler">
                    <property name="requestLog">
                        <bean class="org.mortbay.jetty.NCSARequestLog">
                            <property name="append" value="true"/>
                            <property name="filename" value="${http.log.dir}/access.log.yyyy_mm_dd"/>
                            <property name="extended" value="true"/>
                            <property name="retainDays" value="999"/>
                            <property name="filenameDateFormat" value="yyyy-MM-dd"/>
                        </bean>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
               
</beans>

That xml file comprise server configuration of your jetty server pointing dispatcher servlet use  Spring's application context. Your next step is run your application the way you want.
 example:
        _applicationContext = new ClassPathXmlApplicationContext(_resourceLocations);
        _applicationContext.start();


I also did another article related to Spring MVC/IOC-DI and Embedded Jetty utilizing applicationContext .xml of Spring. You might be interested running and configuring jetty through java code and not from applicationContext.xml.

Hope this helps! For suggestion, please feel free to leave your comments.

Use Arguments on Property File

The goal on this tutorial is to be able to format a value on a properties file given its parameter or arguments. It is best describe as passing value as a parameter and inserting those values on the string retrieved from the properties file.

To better understand what I'm doing, read and understand my example code below.

Property File Sample
message.welcome= Welcome {0} to {1}!
message.thank= Thank you for you visit {0}.
Java Code using  ResourceBundle and MessageFormat
ResourceBundle resBundle = new ResourceBundle("path to property file");
String welcomeText=resBundle.getString("message.welcome");
MessageFormat msgFormatter = new MessageFormat(welcomeText);

// Create the dynamic args you want to replace
Object[] messageArguments = {"Techie boy","Philippines"}; 

String finalText= msgFormatter.format(messageArguments);

The finalText will result to "Welcome Techie boy to Philippines!"

Wednesday, May 26, 2010

Embedded Jetty in Spring MVC, IoC/DI


I was trying to configure embedded jetty in a spring application that implements MVC(Model View Controller). I want to utilize single spring applicationContext xml file for IoC(Inversion of Control) and DI(Dependency Injection) on dispatcher servlets.

The goal is to be able to use or reference beans configured/located on already been loaded spring application context. Read and understand my resolution below.

This is what my main method look like:
LOG.info("Application starting");
_applicationContext = new ClassPathXmlApplicationContext(_resourceLocations);

_webServer = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setHost("localhost");
connector.setPort(8080);
_webServer.addConnector(connector);

WebAppContext webAppContext = new WebAppContext(_webServer, "path to web app folder", "/");

GenericWebApplicationContext webApplicationContext = new GenericWebApplicationContext();
webApplicationContext.setServletContext(webAppContext.getServletContext());
webApplicationContext.setParent(_applicationContext);

webAppContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
webApplicationContext.refresh();

ServletHandler servletHandler = new ServletHandler();

ServletHolder servletHolder = new ServletHolder(new DispatcherServlet());
servletHolder.setName("dispatcher");
servletHandler.addServlet(servletHolder);

ServletMapping servletMapping = new ServletMapping();
servletMapping.setServletName(servletHolder.getName());
servletMapping.setPathSpec("*.htm");
servletHandler.addServletMapping(servletMapping);

webAppContext.setServletHandler(servletHandler);
_webServer.addHandler(webAppContext);

RequestLogHandler logHandler = new RequestLogHandler();
NCSARequestLog ncsaLog = new NCSARequestLog();
ncsaLog.setExtended(true);
ncsaLog.setFilename("logs/jetty-yyyy_mm_dd.log");
logHandler.setRequestLog(ncsaLog);
_webServer.addHandler(logHandler);

LOG.info("Starting main application context");
_applicationContext.start();

LOG.info("Starting Jetty Server");

try {
_webServer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

LOG.info("Application started");
return null;

 The idea is that DispatcherServlet's context is child context of  GenericWebApplicationContext which in turn is a child of ClassPathXmlApplicationContext. Explicitly invoking setParent() gives you the access to all beans from the web application context.

Hope this helps. Feel free to leave your comment or suggestions.

Saturday, May 22, 2010

SVN error on Eclipse: org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir

I met an svn eclipse error while attempting to committing to svn repository. I was thinking I might aswell share how I resolve this problem to everyone.
SVN error on Eclipse: org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir
org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir
svn: Working copy ‘D:\htdocs_svn\sites\all\modules’ locked
org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir
svn: Working copy ‘D:\htdocs_svn\sites\all\modules’ locked
To be able to get rid of it, I did the following:
1. Right click on project folder
2. Goto Team and click Cleanup
3. Try to commit again

In my understanding, it happens when there is a pending session  which already locked the working directory copy. So, to get rid of it.. You'll need to remove the old session by deleting them through svn cleanup.

Good luck!

I'm still breathing

It's been a while since my last post! I had been very busy lately with my work... I miss writing articles. After weeks of hardcore work, I've learn a lot about spring and jetty and I would say, its worth working hard.

I have this task of setting up a project for development. Its a web application for testing. I've learned embedding jetty in a spring application implementing MVC(Model View Controller) design pattern. Other than that, it builds character. Patience really was important.

Anyway, not good sharing my story. Anyway, I can't wait to share my learnings. Later I'll write about those things I've learned..Just the basics..

Saturday, April 24, 2010

Asus PL30JT Technology Business Notebook

Asus showed a notebook of new businesses, PL30JT at CeBIT 2010, which comes with a stylish design. The new Asus notebook includes Turbo33 technology company claims to improve performance by 33%.

Asus PL30JT Business Notebook

The PL30JT is fueled by Intel Core i5 processor and up to 4 GB of RAM. It is equipped with two graphics cards, the 1GB NVIDIA GeForce 310M dedicated graphics and integrated Intel GMA HD technology with Optimus. The professional notebook comes with a 13.3-inch 1366 × 768 LCD display and support for WiFi. It weighs only 1.7 kilograms, with an 8-cell battery. [via itechnews]

This post Asus PL30JT Technology Business Notebook brough to by Netbook Review. Visit Electronics Review for information about gadget and technology
 

Author

Followers

Techie Projects

Open Source