Developing java web application powered by Amazon S3.

More and more people are tilting towards cloud deployment and defacto go to guys for supporting their cloud app is Amazon AWS.  Apart for providing highly scalable computing capabilities,  AWS also throws out lot of cool services which we can directly leverage while developing our application. One such service is Amazon S3.

What is Amazon S3?

S3 is a highly scalable, durable and virtually limitless storage service for any kind of web-object like files, pictures, videos etc. S3 can store an object of upto 5 terabyte of size.

Buckets and keys – All the objects in S3 are stored within a logical container called Bucket. Bucket acts like a namespace so its name should be unique across all the buckets created across all the amazon accounts. Bucket name has certain restrictions to it. It can be between 3-63 character long and should be lowercase and alphanumeric. There are a bunch of restrictions out there, the complete listing can be found here  http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html. Any object stored in a bucket has a unique key within that bucket. For example if you store an object call mypic.jpg in a bucket called “demobucket” the mypic.jpg becomes a key. The key is sequence of Unicode character whose UTF-8 encoding are at-most 1024 bytes long.

Some examples of keys are

mypic.jpg
thumbnails/mypic.jpg
thumbnails/mypic1.jpg
medium/mypic.jpg
original/mypic.jpg

In nutshell bucket provides a folder like behavior for accessing and storing object. Objects can be accessed via key which are unique within the bucket. There is no limit to number of objects we can store in a bucket. But for each account we can create only 100 buckets.

So how can be use this awesome storage engine in our application? Awazon provides sdk for java which can be directly downloaded from here http://aws.amazon.com/sdkforjava/. In this article we will see how to download this sdk and use it to store photos in S3. Please note that amazon charges for storage space you are using, but for few MBs it is almost 0. So here are the steps.

1. Signup for your amazon account. You will need a credit card for this.

2. Now logon to your console “console.aws.amazon.com”. In your console home you will see lots of amazon services listed. Search for S3 and and click on the link.

3. On the s3 home click on create bucket. It is a button of top left of your screen. Select a unique name and region of your choice. Choose your region carefully as once your bucket is created in a region it will stay there for lifetime. There are nitty gritties here which I don’t want to go into.

4. You can provide different access controls to a bucket like read, write, all to different accounts, ip etc. Again not going into details.

5. Once you are done you need to download your security credentials. For this go to “My Account”/”security credentials”. What we are after is access credentials. You will see that there are 3 types of access credentials one of them is “Access Keys”. We need this to call all the rest webservice like S3. We will create a new set of access keys and store the access key and secret key is a safe location.

6. Now in your webapp download the  sdk for java from here. http://aws.amazon.com/sdkforjava. In lib folder of sdk copy the aws-java-sdk-1.3.33.jar to your webapp lib and make sure that it is in class path.

Maven user can add following dependency in their pom

<dependency>
	<groupId>com.amazonaws</groupId>
	<artifactId>aws-java-sdk</artifactId>
	<version>1.3.33</version>
</dependency>

7. Create a file called “AwsCredentials.properties” store this in WEB-INF/classes of your webapplication. This file will contain two main properties

accessKey =
secretKey =

The values for these properties are the one you got in step 5 of the tutorial

8. Now we can get our hand dirty with the code.  I am pasting the code directly here as it is simple. The AWS s3 client is thread safe and also it used heavy objects like HttpClient and HttpConnection so we will create it as singleton

package com.aranin.adconnect.util.aws;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Niraj Singh
 * Date: 3/13/13
 * Time: 10:52 AM
 * To change this template use File | Settings | File Templates.
 */
public class AWSStorageUtil {
    private BasicAWSCredentials credentials;
    private AmazonS3 s3;
    private String bucketName;
    private static volatile AWSStorageUtil  awsstorageUtil = new  AWSStorageUtil();
    private   AWSStorageUtil(){
        try{
            Properties properties = new Properties();
            properties.load(new FileInputStream("D:/samayik/adkonnection/src/main/resources/AwsCredentials.properties"));
            this.credentials = new   BasicAWSCredentials(properties.getProperty("accessKey"),
                                                         properties.getProperty("secretKey"));
            this.bucketName = "aranin";
            this.s3 = new AmazonS3Client(this.credentials);

            /*
               You can use this in your web app where    AwsCredentials.properties is stored in web-inf/classes
             */
            //AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());

        }catch(Exception e){
            System.out.println("exception while creating awss3client : " + e);
        }
    }

    public static AWSStorageUtil getInstance(){
        return awsstorageUtil;
    }

    public static AmazonS3 getAWSClient(){
         return awsstorageUtil.s3;
    }

    public static AmazonS3 getBucketName(){
         return awsstorageUtil.s3;
    }

    public void upload(File file){
        /**
         * key should be unique. an whatever key you set will be used to in url path to access the pic.
         */
        String key = "ads/" + file.getName();
        s3.putObject(this.bucketName, key, file);
    }

    public List<Bucket> listBuckets(){
        for (Bucket bucket : s3.listBuckets()) {
                System.out.println(" bucket name - " + bucket.getName());
        }

        return s3.listBuckets();
    }

    public void getObjectList(){
        System.out.println("Listing objects");
        ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
                .withBucketName(bucketName)
                .withPrefix("ads"));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(" - " + objectSummary.getKey() + "  " +
                               "(size = " + objectSummary.getSize() + ")");
        }
    }

    public void getFileFromS3(String key){
        System.out.println("Downloading an object");
        S3Object object = s3.getObject(new GetObjectRequest(this.bucketName, key));
        System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());

    }

    public static void main(String args[]){
        File file = new File("D:/pics/cam/IMG_1201.jpg");
        AWSStorageUtil aWSStorageUtil = AWSStorageUtil.getInstance();
        //aWSStorageUtil.upload(file);
        aWSStorageUtil.getObjectList();

    }

}

The objects you upload can be retrieved easily using the key via the url. So lets say you have created the bucket in Singapore region and key is ads/IMG_1201.jpg then you can retrieve the object using url

https://s3-ap-southeast-1.amazonaws.com/<bucketName>/<keyname>

Picture uploaded in this example is

https://s3-ap-southeast-1.amazonaws.com/aranin/ads/IMG_1201.jpg

The guy wearing light blue TShirt is me :-).

This is all I have to say, you guys are smart enough to extrapolate from here so I will leave it here. If you have questions feel free to drop a comment here or you can write me a mail at singh.niraj@aranin.com and I will love to answer it. (Although I am not an expert).

Bye and Take Care

Niraj

 

Print Friendly, PDF & Email
Posted in Amazon Web Services, Cloud Computing, Java, tutorial | Tagged , | 10 Comments

Developing SOAP Web service using Apache CXF

In last post I walked through the steps for developing a simple RESTFull service using apache CXF.  In this post I will be talking about developing SOAP web service using CXF.

Before moving forward let us understand few of the concepts/elements which makes up a SOAP web service

SOAP or Simple Object Access Protocol 

SOAP is a protocol for exchanging XML-based messages over the network using application protocols like http, smtp, etc as carrier. SOAP message comprises of a SOAP envelope. The envelope can be broken into a header and a body. Header contains context related definitions like security while the body contains actual application data. A typical SOAP message looks like

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <m:GetStockPrice xmlns:m="http://www.example.org/stock">
      <m:StockName>IBM</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>

WSDL or Web Services Description Language

WSDL is a standard based XML Language which is used to describe a web service. A WSDL completely describes what public interface an web service exposes, what parameter it expects, structure of output it returns, location of web service. A WSDL defines a web service as collection of communication end points that are capable of exchanging messages. These communication end points are called ports. Port are composed of two parts.

  1. Contains the public interface exposed by the web service. The interface contains all the methods, parameter needed to invoke them and response structure returned by them.
  2. The second part binds the public interface to network protocol like http. The binding comprises of information like location of the public interface and message format for the service.

SOAP communication styles

There exists two types of communication styles

  1. Document
  2. RPC

The communication style used by SOAP web service is defined in its WSDL.

In the Document style the application data which is part of soap body is sent as XML document. This document can be validated completely by a xml schema which is also part of WSDL. As XML can contain structure as per wish of service developer hence the responsibility of marshaling and unmarshaling xml payload lies at end of provider and consumer code.

In RPC style as the name suggests the consumer invokes the methods of service as if he were invoking a local method. To facilitate this the RPC message consists of list of public interface methods that a consumer can invoke. These methods are listed by names as xml elements.  The method parameters needed by these method forms sub elements of  the method element. The responisibility of marshaling/unmarshaling lies with the web service framework. The framework contains its own marshaling/unmarshaling libraries. RPC style results in tightly coupled code between application code and the web service framework, hence norm is create document style services.

With Key concepts in place let see an example of how to write a soap web service using Apache CXF.

Getting the source code for this tutorial

I have committed the source files for this tutorial in SVN.

You can download the web app from

http://subversion.assembla.com/svn/weblog4j/Weblog4jDemo/trunk

You can download the client from

http://subversion.assembla.com/svn/weblog4j/DemoClient/trunk

Note: Both are ItelliJ maven projects so you can directly import them to your intelliJ IDE or copy over the files manually to other IDEs

Create a struts2 skeleton application to contain your service.

You can use any MVC framework but I prefer struts2 for my own reasons. You can see an example of how to create an empty struts2 application in eclipse using maven here.

Add CXF dependencies 

In your project POM add following dependencies to download CXF jars

<properties>
       <cxf.version>2.5.0</cxf.version>
</properties>
<dependencies>
       <!-- apache cxf -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>

        <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-frontend-jaxrs</artifactId>
          <version>${cxf.version}</version>
       </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
</dependencies>

For example let us create a simple book shelf web service. For simplicity let us assume following use case.

  1. Insert a book in book self
  2. Retrieve a book from book shelf by title.

Developing the service

This can be done in two ways Code first and contract first. We will be using the code first approach.

Creating a Service Endpoint Interface (SEI)

Let us create a SEI interface called BookShelfService

package com.aranin.weblog4j.services;

import com.aranin.weblog4j.vo.BookVO;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface BookShelfService {

    @WebMethod
    public  String insertBook(BookVO bookVO);
    @WebMethod
    public  BookVO getBook(String title);
}

If you look at the above SEI you can tell that it is a normal java interface with exception of two annotation

@WebService – This is an annotation JAXWS library. It turns a normal POJO into a webservice. In our case the annotation is placed right above the interface definition and it notifies that BookShelfService is not a normal interface rather an webservice interface or SEI. There are other attributes to this annotation that can completely define the webservice but we will not be using it right now.

@WebMethod – This annotation is optional and is mainly used to provide a name attribute to the public method in wsdl.

Implementing the service. 

Now we have our SEI so let us implement the methods in the interface in our BookShelfServiceImpl

package com.aranin.weblog4j.services;

import com.aranin.weblog4j.hashdb.HashDB;
import com.aranin.weblog4j.vo.BookVO;

import javax.jws.WebService;

@WebService(endpointInterface = "com.aranin.weblog4j.services.BookShelfService",
		serviceName="bookShelfService")
public class BookShelfServiceImpl implements BookShelfService {
    public String insertBook(BookVO bookVO) {
        HashDB.insertBook(bookVO);
        return "Book with name : " + bookVO.getBookName() + " is now available on the shelf";  //To change body of implemented methods use File | Settings | File Templates.
    }

    public BookVO getBook(String title) {

        return HashDB.getBook(title);  //To change body of implemented methods use File | Settings | File Templates.
    }
}

This class is a simple POJO implementing the SEI. Only notable thing here is the @WebService annotation. If you look at it closely we have provided the fully qualified class name of the SEI it implements and name of the webservice.

Data Binding class (BookVO)

package com.aranin.weblog4j.vo;

import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

@XmlRootElement(name = "Book")
public class BookVO implements Serializable {

    private long bookId;
    private String bookName;
    private String author;

    public long getBookId() {
        return bookId;
    }

    public void setBookId(long bucketId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

The only thing to note here is the @XmlRootElement annotation. This annotation is part of JAXB library. CXF uses JAXB as default data binding component. As BookVO needs to be transported as XML during the webservice calls hence it needs to marshalled/unmarshalled by the JAXB engine in the CXF installation. Using @XmlRootElement annotation we help JAXB in mapping the BookVO class to an xml with its name attribute as root element of the xml.

Spring Based Server Bean

What makes CXF a first rate choice as webservice framework is that it publishes its service endpoints via a spring based configuration file. Lets create a  the configuration file and register our service in it. We will name the file as beans.xml and save it in WEB-INF folder of our application

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxrs
    http://cxf.apache.org/schemas/jaxrs.xsd
	http://cxf.apache.org/jaxws
	http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />	

<jaxws:endpoint
       id="bookShelfService"
       implementor="com.aranin.weblog4j.services.BookShelfServiceImpl"
       address="/bookshelfservice" />

</beans>

Now to load the beans.xml we simply add following in web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/beans.xml,/WEB-INF/applicationContext.xml</param-value>
</context-param>

Finally we need to wire spring and CXF through web.xml.

<servlet>
       <servlet-name>CXFServlet</servlet-name>
       <display-name>CXF Servlet</display-name>
	<servlet-class>
		org.apache.cxf.transport.servlet.CXFServlet
	</servlet-class>
	<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>CXFServlet</servlet-name>
	<url-pattern>/*</url-pattern>
</servlet-mapping>

Note: I have not included loading of Spring ContextLoaderListner. If you create a struts2 application via maven using struts2 starter archetype then spring is downloaded and registered by maven project itself.

Now your webservice is ready. Compile and deploy the application in any servlet container. If everything is good then you can see your wsld on following location

http://localhost:8080/weblog4jdemo/bookshelfservice?wsdl

Create your client

There are many tools which can be used to generate client code using the wsdl. To save you further trouble we will utilize CXF’s own front end apis. So let us look at the steps.

  1. Create a simple maven project using IDE of your choice. I am using IntelliJ currently and it is awesome. Lets say name of the project is DemoClient.
  2. Add the CXF dependencies as shown in create skeleton application section.
  3. Since we know what the SEIs and public method and binding objects are. We will create them in the client side to save us trouble. In case there are many such classes we can use tools like wsdl2java etc to generate our code.
  4. Create a stub SEI in exact same package structure as the parent SEI.
  5. Create BookVO in same package structure as the parent BookVO.
  6. The above classes should be exactly same as you have created in the parent application.
  7. We need not create the SEI implementation at client end.
  8. Now we will create a client using JaxWsProxyFactoryBean. This class is a factory which works with the SEI proxies to invoke web service methods. Here is the class.
       

package com.aranin.weblog4j.client;

import com.aranin.weblog4j.services.BookShelfService;
import com.aranin.weblog4j.vo.BookVO;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class DemoClient {
    public static void main(String[] args){
        String serviceUrl = "http://localhost:8080/weblog4jdemo/bookshelfservice";
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(BookShelfService.class);
		factory.setAddress(serviceUrl);
		BookShelfService bookService = (BookShelfService) factory.create();

        //insert book
        BookVO bookVO = new BookVO();
        bookVO.setAuthor("Issac Asimov");
        bookVO.setBookName("Foundation and Earth");

        String result = bookService.insertBook(bookVO);

        System.out.println("result : " + result);

        bookVO = new BookVO();
        bookVO.setAuthor("Issac Asimov");
        bookVO.setBookName("Foundation and Empire");

        result = bookService.insertBook(bookVO);

        System.out.println("result : " + result);

        bookVO = new BookVO();
        bookVO.setAuthor("Arthur C Clarke");
        bookVO.setBookName("Rama Revealed");

        result = bookService.insertBook(bookVO);

        System.out.println("result : " + result);

        //retrieve book

        bookVO = bookService.getBook("Foundation and Earth");

        System.out.println("book name : " + bookVO.getBookName());
        System.out.println("book author : " + bookVO.getAuthor());

    }
}

Here is the output of above calls

INFO: Creating Service {http://services.weblog4j.aranin.com/}BookShelfServiceService from class com.aranin.weblog4j.services.BookShelfService
result : Book with name : Foundation and Earth is now available on the shelf
result : Book with name : Foundation and Empire is now available on the shelf
result : Book with name : Rama Revealed is now available on the shelf
book name : Foundation and Earth
book author : Issac Asimov

Process finished with exit code 0

There are tons of other stuff you can explore in Apache CXF like Creating dynamic clients, interceptors, leveraging other transport protocol, webservice over https etc. But I intend this post as getting started tutorial.

Phew this is a long post again. I need to improve my writing skills to shorten the length. But still I hope that you enjoyed it and found it useful. I intend to write about a javascript client for webservices in my next post. Until then goodbye and happy coding.

Print Friendly, PDF & Email
Posted in Apache CXF, web service | Tagged , | 12 Comments

Developing RESTful Services using Apache CXF

Introduction

As you already know there are two ways of developing a web service

  1. Simple Object Access Protocol (SOAP)
  2. Representational State Transfer (REST)

Before jumping on how to create a REST based web service using Apache CXF we shall see what is REST. REST is not a technology and certainly is not a standard of some kind. It is merely an architectural style that chalks down how to write a web service in a certain way. This style was defined by a certain Roy Fielding (ring bells? yep you guessed right, he is one of the architects of HTTP) in 2000. Principal protagonist of a REST architecture is a Resource which can be uniquely identified by an Uniform Resource Identifier or URI. State of a resource at any given point of time is represented by a document and is called Representation of resource. The client can update the state of resource by transferring the representation along with the request. The new representation is now returned to client along with the response.  The representation contains the information in formats like html, xml, JSON etc that is accepted by the resource. The resource which adheres to rules of REST architecture is called a RESTfull resource and web service that adheres to this rule are called RESTfull web service.

Create a project to contain your web service

I generally do my web development in struts2+spring using maven Strut2 starter archetype to create my web project. To use CXF in my project I add following dependencies to my POM

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>${cxf.version}</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>${cxf.version}</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http-jetty</artifactId>
	<version>${cxf.version}</version>
</dependency>

The non maven users can find the details of dependencies to be added in following link

http://cxf.apache.org/docs/using-cxf-with-maven.html

The CXF can be directly downloaded from here

http://cxf.apache.org/download.html

How to Create a CXF RESTfull web-service?

Suppose you want to create a RESTfull web service using CXF for managing books in your personal bookshelf.  You would typically want to perform following operations on the bookshelf

  1. Add a book
  2. Update book information
  3. Delete a book from the shelf
  4. Get a book
  5. Get book list
  6. Get book list by author name

Following steps are needed create such a service

  1. Create BookVO, BookList (value object) for passing as representation in request and response.
  2. Bind the objects with request and response.
  3. Create the service implementation class to accept request and generate response.
  4. Registering your webservice with CXF container.
  5. Deploy the service in a web container.
  6. Create clients to invoke methods on the service.

Getting the source code for this tutorial

I have committed the source files for this tutorial in SVN.

You can download the web app from

http://subversion.assembla.com/svn/weblog4j/Weblog4jDemo/trunk

You can download the client from

http://subversion.assembla.com/svn/weblog4j/DemoClient/trunk

Note: Both are ItelliJ maven projects so you can directly import them to your intelliJ IDE or copy over the files manually to other IDE


Create BookVO (value object) for passing as representation in request and response.

BookVO Class

package com.aranin.weblog4j.vo;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.Serializable;

@XmlRootElement(name="Book")
public class BookVO implements Serializable{

private long bookId;
private String bookName;
private String author;

public long getBookId() {
return bookId;
}

public void setBookId(long bucketId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}
}

BookList Class

package com.aranin.weblog4j.vo;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name="BookList")
public class BookList {
private List<BookVO> bookList;

public List<BookVO> getBookList() {
if(bookList == null){
bookList = new ArrayList<BookVO>();
}
return bookList;
}

public void setBookList(List<BookVO> bookList) {
this.bookList = bookList;
}
}

Bind the Data object i.e. BookVO with request and response

To bind the BookVO with the request or response it needs to serialized into either XML or JSON streams. The serialization needs to be done using one of the data binding components. CXF uses JAXB for default data binding component. JaXB uses @XmlRootElement annotation to map the data object to the xml. You can see the use of XmlRootElement annotation in code above.

Create the service implementation class to accept request and generate response

Let us see how a CXF RestFull webservice looks. We will create a BookService class which will perform add,update,delete and get operations on BookSelf.

BookService Class

package com.aranin.weblog4j.services.rest;


import com.aranin.weblog4j.hashdb.HashDB;
import com.aranin.weblog4j.vo.BookVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by IntelliJ IDEA.
 * User: Niraj Singh
 * Date: 3/13/13
 * Time: 3:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class BookService {

protected final Logger log = LoggerFactory.getLogger(BookService.class);


    @POST
    @Path("/getbook/{name}")
    @Produces({"application/xml","application/json"})
    @Consumes({"application/xml","application/json","application/x-www-form-urlencoded"})
    public Response getBucket(@PathParam("name") String name) {
        log.debug("name : " + name);
        BookVO bookVO = null;
        try {
            bookVO = HashDB.getBook(URLDecoder.decode(name, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        if(bookVO == null){
            return Response.status(Response.Status.BAD_REQUEST).build();
        }else{
            return Response.ok(bookVO).build();
        }
    }

    @POST
    @Path("/addbook")
    @Produces({"application/xml","application/json"})
    @Consumes({"application/xml","application/json","application/x-www-form-urlencoded"})
    public Response addBook(@FormParam("name") String bookName,
                            @FormParam("author") String author) {
        log.debug("inside addBook");
        BookVO bookVO = new BookVO();
        bookVO.setBookName(bookName);
        bookVO.setAuthor(author);
        HashDB.insertBook(bookVO);
        if(HashDB.getBook(bookName) == null){
            return Response.status(Response.Status.BAD_REQUEST).build();
        }else{
            return Response.ok(bookVO).build();
        }

    }
}

You can see two methods in the BookService class getBook and addBook. They are service methods for getting and adding a book. Rest of the methods for update delete etc can be written in same way. Now lets see what the various annotations and method call means.

@POST – This indicates that service receives only POST request.

@Path – This is the path of webservice. So the webservice can be invoked using following Url <base_url>/bookservice/getbook/{name} for fetching, <base_url>/bookservice/addbook for adding.

@Produces – Indicates the MIME type of response generated. In our case it is both application/xml and application/json.

@Consumes – Indicates the MIME type of request which this service can consume.

Registering your webservice with CXF container.

One cool thing with CXF is that it uses a spring based configuration for registering its webservice endpoints so let us create a beans.xml in WEB-INF and configure the CXF in web.xml. For this first we need to wire the beans.xml to get loaded by spring container.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml,/WEB-INF/applicationContext.xml</param-value>
</context-param>

Secondly load the register the CXFServlet in web.xml.

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Now open your bean.xml and register your bookservice end point.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxrs
    http://cxf.apache.org/schemas/jaxrs.xsd
	http://cxf.apache.org/jaxws
	http://cxf.apache.org/schemas/jaxws.xsd">


	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	


	<jaxws:endpoint
	  id="bookShelfService"
	  implementor="com.aranin.weblog4j.services.BookShelfServiceImpl"
	  address="/bookshelfservice" />


    <bean id="bookserviceclass" class="com.aranin.weblog4j.services.rest.BookService"/>


    <jaxrs:server id="bookservice" address="/bookservice">
        <jaxrs:serviceBeans>
        <ref bean="bookserviceclass" />
        </jaxrs:serviceBeans>
    </jaxrs:server>



</beans>

Now your webservice is ready. Build your web application and deploy it in any servlet container.

Creating Client for your webservice

Clients can be created in many ways, I have used apache Http Components to write my client. The libraries can be found in http://hc.apache.org/httpclient-3.x/.

Maven user can pull the Http components jar using following

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.3</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.1.3</version>
<scope>compile</scope>
</dependency>

Now to invoke the webservice I have created a util class called DemoRestClient.

package com.aranin.weblog4j.client;


import com.aranin.weblog4j.vo.BookVO;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import java.net.URLEncoder;

/**
 * Created by IntelliJ IDEA.
 * User: Niraj Singh
 * Date: 3/13/13
 * Time: 4:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class DemoRestClient {
    public static void main(String[] args){
        DemoRestClient restClient = new DemoRestClient();
        try {
            //restClient.addBook("Naked Sun", "Issac Asimov");
            restClient.getBook("Naked Sun");
        } catch (Exception e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public BookVO getBook(String bookName) throws Exception {

        String output = null;
        try{
            String url = "http://localhost:8080/weblog4jdemo/bookservice/getbook/";

            url = url + URLEncoder.encode(bookName, "UTF-8");

            HttpClient client = new HttpClient();
            PostMethod mPost = new PostMethod(url);
            client.executeMethod( mPost );
            Header mtHeader = new Header();
            mtHeader.setName("content-type");
            mtHeader.setValue("application/x-www-form-urlencoded");
            mtHeader.setName("accept");
            mtHeader.setValue("application/xml");
            mPost.addRequestHeader(mtHeader);
            client.executeMethod(mPost);
            output = mPost.getResponseBodyAsString( );
            mPost.releaseConnection( );
            System.out.println("out : " + output);
        }catch(Exception e){
            throw new Exception("Exception in retriving group page info : " + e);
        }
        return null;
    }

    public void addBook(String bookName, String author) throws Exception {

        String output = null;
        try{
            String url = "http://localhost:8080/weblog4jdemo/bookservice/addbook";
            HttpClient client = new HttpClient();
            PostMethod mPost = new PostMethod(url);
            mPost.addParameter("name", "Naked Sun");
            mPost.addParameter("author", "Issac Asimov");
            Header mtHeader = new Header();
            mtHeader.setName("content-type");
            mtHeader.setValue("application/x-www-form-urlencoded");
            mtHeader.setName("accept");
            mtHeader.setValue("application/xml");
            //mtHeader.setValue("application/json");
            mPost.addRequestHeader(mtHeader);
            client.executeMethod(mPost);
            output = mPost.getResponseBodyAsString( );
            mPost.releaseConnection( );
            System.out.println("output : " + output);
        }catch(Exception e){
        throw new Exception("Exception in adding bucket : " + e);
        }

    }

}

Run this client to see the output of your webservice. Right now it will sending xmloutput as the response accept header is “application/xml”. You can change it to application/json to get an json output.

Thats all folks. This is a very basic introduction to developing RestFull web service using apache CXF, there is a lot more to explore. Happy exploring, till then good bye. Please drop some comments as and if you read this to keep me inspired 🙂

 

Print Friendly, PDF & Email
Posted in Apache CXF, REST, web service | Tagged , , , | 8 Comments

UML – Package Diagram

So the story continues, Ever Whining Rational Rose Corporation were not happy with just drawing classes. They wanted to wrap them in packages. So Three Amigos concocted what we now call Package Diagram.

So what is a package diagram?

A Package is a container which contains groups of related elements. These elements can be any of the UML constructs like classes, components , use cases, etcetera.

A Package Diagram is an UML structural Diagrams that helps us in showing and modelling the dependencies between various packages in a system.

Although packages can be used to group anything but they are mostly used to group classes and components and sometimes use cases. From UML 2 superstructure Package  definition goes as follows.

A package is a namespace for its members, and may contain other packages. Only packageable elements can be owned members of a package. By virtue of being a namespace, a package can import either individual members of other packages, or all the members of other packages. In addition a package can be merged with other packages.

Within a system a package name is unique, but for elements the rules change a bit. Elements of same types within a package should have unique names but two elements of different type can have same name in the same package.

A typical element in a package is owned by that package. The package can import elements of other packages by importing the package.

Graphically a package is depicted like a folder with package name written in center of the package.

Visibility of package elements

An element of package can be marked as public, private and protected.

  • public (+) – public elements can be accessed by any elements inside or outside the package.
  • private (-)- Only the elements within the same package can access a private element.
  • protected (#)- Protected elements can be used by elements within the same package or any package that extends the parent package.

A package can contain other packages and the nested packages can be marked as public private or protected. Elements of  a package can be represented using text, rectangular content notation or composition style notation. For example consider a package called CourseManager. CourseManager has public elements like Course, Tutor, Students and a private element called StudentCourseLink. Then the package can be represented graphically in following three ways.

Textual Representation

Rectangular Content Notation

Composition Style Notation

Package Dependencies – As we already know that is a software system we have multiple components which interact with each other to provide services to client. Same concept holds for packages. A real live systems will be composed of many packages and they will interact with each other. In other words the packages have dependencies on each other. The UML defines two kinds of dependencies on the packages.

  1. Access – Signifies that a package uses or accesses public element of another package. It is depicted by drawing a dotted arrow stereotyped with <<access>>. The direction of arrow is from the package that uses another package to package which is used.
  2. Import – Signifies that a package has imported the public elements of another package into its own namespace. The imported package namespace remains intact.
Both the access and import sounds almost similar. The difference is that in case of access. the source package access selected public elements of the target package however in case of import, all the public elements of target package is imported. Both the dependencies are depicted by an open ended broken arrow pointing towards target 
The diagram below will explain how to show dependency between packages. 

Package Stereotypes – There are many predefined UML stereotypes, of these 5 are applicable to package diagrams. They are facade, framework, stub, subsystem and system. Let us see what they mean.

  1. facade – As the name suggests a Facade package is a public representation of another larger package. Facade package does not contain any element of its own rather it contains references to elements of other larger packages. To make it clear you can visualize facade design pattern where working of a larger complex component/s is hidden from the client by use of a public facade interface.
  2. Framework – When creating a framework we use a lot of application level patterns to implement a various subsystems within the framework. Classes and interfaces used to implement the subsystems can be packaged in a Framework package.
  3. Stub – Stub Package is usually used to package interfaces and abstract classes that defines a web service.
  4. Subsystem – A system is composed of many subsystems and each subsystem in turn will contain many component. A subsystem package contains classes and interfaces that are used to fully implement a subsystem.
  5. System – A system package contains all the classes, interfaces, components and subsystems that creates the system

Package Tagged Values – Packages support tag values. You can tag a package with information such as version, author name, description, major version etc

Package Merge – Apart from import there is one more important dependency between packages, i.e., Package Merge. Package merge indicates a relationship between two packages where all the public contents of target package is to be combined with those of source package. Merge is done if multiple packages have elements of same name which also represent a similar concept.  Merge is very similar to generalization (inheritance) in sense that all the matching elements are combined to give a base definition to the matching element then the base definition is extended incrementally in different merged packages giving the merged element a specific purpose in each of the packages. The independent elements of merged packages retain their original characteristic.

Here are some exerpts from UML 2 superstructure document

A package merge is a directed relationship between two packages that indicates that the contents of the two packages are to be combined. It is very similar to Generalization in the sense that the source element conceptually adds the characteristics of the target element to its own characteristics resulting in an element that combines the characteristics of both.

This mechanism should be used when elements defined in different packages have the same name and are intended to represent the same concept. Most often it is used to provide different definitions of a given concept for different purposes, starting from a common base definition. A given base concept is extended in increments, with each increment defined in a separate merged package. By selecting which increments to merge, it is possible to obtain a custom definition of a concept for a specific end. Package merge is particularly useful in meta-modeling and is extensively used in the definition of the UML metamodel.

Conceptually, a package merge can be viewed as an operation that takes the contents of two packages and produces a new package that combines the contents of the packages involved in the merge. In terms of model semantics, there is no difference between a model with explicit package merges, and a model in which all the merges have been performed.

Package merge is shown as dotted line with open arrowhead pointing from receiving package to target package. In addition keyword <<merge>> is put near the the dotted line.

Small Example – Consider a video processing engine. It has a VideoQueue which is turn contains video objects to be processed. There is a Videopocessing thread picks the video  from queue and uses a VideoProcessor to process the video. The VideoProcessor is an interface implemented by various kinds of video processor. A have tried to create a simple package diagram to capture the the elements in this design.

Package Diagram for a Video processing engine. 

I mostly use package diagram to capture the high level entities of a system and then finally use it as starting point to model my database. Some times I also use it to capture the solution architecture of my application. I will share the solution architecture diagram I created for one of my projects. Would love to see the comments of reader and check if makes sense to rest of folks or only to me.

Note – some of the info about the system has been removed due to non disclosure agreement.

Package Diagram depicting high level solution Architecture (Beautiful ain’t it. :)).

Phew! that was quite something. Writing a blog post is very difficult and I am sure I have missed many things. But I know that my fans (err I meant dear readers 🙂 ) will find few missing points or mistake and they will notify me about it. But for now I feel bit tired and want to go out for a coffee.

I my next post on UML I will be producing an awesomely beautiful essay on Component Diagram. I will surely remember to out a link out here for easy navigation. Till then bye bye and take care  🙂 .

 

 

 

Print Friendly, PDF & Email
Posted in Package Diagram | Tagged , | 1 Comment

UML – Class Diagrams

Long time ago far far away in galaxy there was a fussy company called Rational Software Corporation. They always used to cry about existing object oriented modelling systems. With all fussing and crying and some more fussing and crying they somehow manage to frustrate the GOD. Now since GOD cannot directly intervene in our day to day business (not sure read some where) so he came up with a plan. He came into dreams of Three Amigos and gave them Idea about theUML. Inspired by the dream Three Amigos contacted Rational and preached them the gospel of UML and delivered the GOD of all the sufferings. Here is an exert from that gospel.

Since I am always honest I will tell you again that I copied bit and pieces from other places and wrote this one. But I feel it is good one so happy reading and in advance I thank you  for writing appreciative comments.

A UML Class diagram is a static structural diagram that describes structure of a system by showing system classes, their attributes, operations and the relationships  they have with other  classes in system.

In a Class Diagram each class is represented by a box with three component.

  • First compartment – Name of the class
  • Second compartment – Properties/attributes of the class.
  • Third compartment – Methods or operation of the class.
            

Attributes – Attributes of classes are the properties representing features of the class. For example a car class can have attribute like color, make, model etc. In UML the attributes are present in the second compartment of the class box. Attribute can be classified as inline or static. Inline attributes are specific to instance of the class, while the static attribute belongs to the class. Attributes are typically represented as
[visibility]/[attributeName]:[attributeType] [multiplicity=defaultValue] {properties and constraints}
For example consider a library with many members, all the members are ordered by their names and they are unique.
-mMembers:Member[o..*]{ordered,unique}
The static attributes are represented by underlining it.
Visibility – Attribute as well as operations visibility can be expressed using the following

  • + public member
  • – private member
  • # protected member
  • ~ package
  • / derived
  • underline – static
Properties – Apart from ordered and unique their are many other properties which an attribute can have. To list them
  1. ordered – used when multiplicity is greater than 1. It means that attributes should be ordered sequentially using some rule.
  2. unique – This property is also used with attributes with multiplicity greater than 1. It means that all the values in the collection are unique.
  3. readOnly – The attribute value cannot change once the value has been set (final variables in java).
  4. union – This is mostly used for attributes derived from other attributes.
  5. redefine <attribute_name>- Means that attribute is alias for some other attribute.
  6. subset<attribute_name> – This means that attribute is a subset of all the valid values of attribute_name.
  7. composite  – This property is used to signify that attribute is a part class which composes the main class.  
Class Mutliplicity – Multiplicity of a class can be defined by placing a number on upper right corner of top most compartment. It signifies the number of allowable instances of class in the system. For example multiplicity 1 indicates a singleton class
                               
Class Relationships – The different classes in a system share a distinct and different kind of relationships. These relationships are denoted by bunch of line and different start and end points. These relationships can be categorized into following
 
  • Dependency
  • Association
  • Aggregation
  • Composition
  • Realization
Dependency– Dependency is the weakest relation that two class can have. It is indicative of a loose coupling between two classes. It signifies a “uses a” kind of relationship. For exmaple a FileReader uses a FileUtils class to get the absolute path of a file. Dependency is indicated by a dashed line followed by an arrow pointing to the target class.
 dependency
Association – Association is a stronger relation than dependency. It indicates a “has a” type of relation. Association is represented by a drawing a solid line between two classes. Association relation may persist longer than a single method call.  For example A class has many student. 
Self Association Some times a class can be associated with itself. Consider a team of employee. It is possible that team can have only one manager.
Multiplicity in Association – Some times Two classes can have multiple associations and each association has different multiplicity. For example consider a team of football players. A player can belong to only one team and each team has 10 players and exactly one captain.
Constrained Association – A class can have many similar elements and relationship between the elements can be expressed using constraints. Constraints can be shown using a dotted line between the two element and nature of constraint is expressed using a text enclosed within curly braces. For example consider a airline reservation system. This system can book a ticket which can be a business class ticket or economy class ticket.
 
 
 
There are many other constraints that are defined by UML’s own Object Constraint Language. There is an awesome pdf which you can find here http://www.omg.org/spec/OCL/2.2/. This PDF will give all the information you need regarding the OCL syntax.
 
Named Association – And finally we can have a named association. The association can be named using short meaningful phrases which describes the association. UML advises us to put an triangle at end of the phrase which tells the reader about the direction in which name should be read. The following example will explain it all.
 
 
Aggregation – Aggregation is a relationship that is stronger than dependency and association. Aggregation signifies a “owns a” relationship i.e. Class A owns a Class B. It is denoted by a solid line with diamond at end of owner and a arrow at the end of class that is owned. As is case of association, an aggregation can be directional but in most situation it is navigable only from container to contained. For example a Department owns an employee
 
 
An employee can be transferred to other department but at any given period he is owned by the department or department aggregates a bunch of employees. As is case of association can be refined using constraints, multiplicity, tagged values and names.
 
Composition – Composition is the strongest relationship that two classes can have. It signifies “is made up of” kind of relationship and is denoted by a dotted line with a solid diamond at end of composed and arrow at end of the composer. Composition is a special kind of aggregation the only difference it that the relationship exists for lifetime of objects. So if a Class A is made up of Class B and Class A is destroyed then Class B will also get destroyed. Class B simply cannot exist without Class A. A typical example of composition is a Connection Pool. A connection pool is made up of many connections and once connection pool is shut down the connection objects are destroyed. 
 
Generalization – Generalization is used to show inheritance. It is typically read as “is a” kind of relationship. So is Class B is subclass of Class A then this relationship can be read as Class B is a Class A. Generalization is denoted by a solid line with a closed arrow pointing towards the super class. Example below should be sufficient to explain the diagram.  
 
 
Realization – And Last but not the least we have Realization. Realization indicates that a Class implements or realizes an Interface. In other words realization ties an interface with its implementation. It is indicated by a dotted line with a closed arrow pointing towards the interface.
 
 
Association Class – Sometime it happens that while associating two classes it becomes necessary to bring in a third class in picture as it contains a valuable information about the association. To explain this let me take up an example of a bank. A bank has many customers and each of the customers has an account is one of the bank’s branch. So association between a bank and customer is tied up using a third entity called branch. The branch class can be termed as association class.
 
To denote a association class a dotted line is drawn between association line of primary classes to the association class.
 
Association Qualifiers –  Often a one to many associations are related via a unique value in the target class. For books in library may be related to its members via a memberId.  This unique key relation can be depicted by something known as association qualifier. Qualifiers are represented by a small rectangle attached to the bigger class with name of the unique key written in it.
Abstract Operation – In abstract classes and interfaces the operations are abstract. An abstract operation is written in italics. In example below convertToFlv() is an abstract method hence it is written in italics.
 
Phew! that was quite something. Writing a blog post is very difficult and I am sure I have missed many things. But I know that my fans (err I meant dear readers :)) will find few missing points or mistake and they will notify me about it. But for now I feel bit tired and want to go out for a coffee. 
 
I my next post on UML I will be producing an awesomely beautiful essay on Package Diagram. I will surely remember to out a link out here for easy navigation. Till then bye bye and take care :):).
Print Friendly, PDF & Email
Posted in Class Diagram, UML | Tagged , | 1 Comment

Multitenant Database using Hibernate Shards

In my previous post “How I Introduced Multitenant Architecture In my Web Application” I discussed database isolation as one of main requirements in a Multitenant SaaS application. So what are the reasons for data isolation? They are obvious and to lay them down.

  1. Security – The user does not want to mingle their data with their competitors.
  2. Scalability – There are many users, for example billion users in my webtop application :). So to deal with gazzilion bytes of data in a table and also to keep database sanity intact you need to chop down the table rows in smaller chunks.
  3. Combination of other two – Most of the users don’t mind their data coexisting with other users however there still are some fussy customers. 

If you feel I left out points then please leave a comment or two. I will update my post.

Whatever be your reason once you decide to separate user data data you have to think about the ways by which you can achieve this. Once again I will lay down the approaches (Ok. I have again stolen the points from google, big deal.)

  1. Separate database instance – Each tenant has his own database instance.
  2. Separate schema but same database instance – Each tenant has his own schema or sets of table in a single database instance.
  3. Separate Rows – The database instance is same same however rows of tenant data is partitioned using some kind of discriminator (tenant_id for example).

Each approach has their own pros and cons. Kindly check the table compiled in postSaaS – Multi-Tenant Database Design Options. It compares the various approaches under different user requirements.

If you had to make your decision then you could easily choose The separate database instances for each tenant approach, but you know that it could prove costly in long run and also add to maintenance and administrative headache. So what to do now? Don’t worry there is a middle path which will help you counter all these. Can you tell me? No? Not to worry I am here for the rescue. Here is my fourth Multitenant database approach. 

  1. Sharding – http://en.wikipedia.org/wiki/Sharding

Sharding is a mechanism by which data of a table is horizontally partitioned across multiple database instances. Each of the independent database partition is called a Shard. Concept of Sharding is different than the Horizontal Partitioning. In horizontal partitioning the data in a single table in a single database instance is partitioned across rows. Meanwhile in sharding the horizontal partitioning is achieved across multiple database servers. The shards are not aware of each other and work independently with each other. They can be present in any database server in any data-center anywhere in the world. So now look at advantages of using this approach.

  1. The total number of rows in each table are lesser now. Hence the size of index hence the performance of queries improve.
  2. Due to sharding not only the problematic table is spread across multiple database other table are also replicated across multiple database instances hence overall load on a database is reduced hence improving performance. In other words we have a database cluster each serving its own bunch of clients and each of which can be scaled in it own way.
  3. We can also get one shard for each tenant using this approach.

Ok, since we have the best approach (I know nosql cassandra etc lovers will be gunning for my head, but let me warn you I am very strong, I got super powers) now let us put our heads down and try to figure out how to implement sharding in your own cool application. But first let me pen down the approach once more

Multitenant database approach by horizontally partitioning data across multiple database instance each having identical schema.

 I had some portion my webtop already written and I had used Hibernate as my ORM tool. It took a long time to get my database layer designed in a way I liked so I decided to keep it. Hence  I decided to give a try to hibernate shards. Hibernate shards is not in active development however it is still a very robust piece of code. I thought it was better to use it rather than writing a utility right from scratch. I did a proof of concept by splitting data across three shards. Each installed in three separate database servers in my office, I really loved what I saw so I will share it with my fans ;).

Just FYI – Hibernate 4 will be released in next few months with full Multitenant support embedded with its core API. https://hibernate.onjira.com/browse/HHH-5697 . I am happy as I am sure that I can easily re-factor my shards code using latest API.

Print Friendly, PDF & Email
Posted in Cloud Computing, HIbernate, Hibernate Shards, Multitenancy, SaaS | Tagged , , , , , | 20 Comments

Creating a new struts2 project in Eclipse with Struts2-archetype-starter using m2eclipse

Last year I created my own struts2 project with help of m2eclipse. It was very simple. I just had to create a maven project and select “Struts 2 Starter” archetype from the catalog. It created a simple struts2 project for integrating it with spring, sitemesh etc. So when I had to start on a new struts2 project I was happy I updated my m2eclipse (it had been a long time after all) and tried to follow the same steps. However I could not find my old struts2 starter archtype, and so the time of frustration started.

After many hours of searching I could not get my project set up. I tried adding a new archtype using values from http://struts.apache.org/2.2.3/docs/struts-2-maven-archetypes.html , but it was no avail I kept getting some exception. Then I finally realized that the struts2 archetype repository does not contain the archetype files anymore. You can try finding the archetypes in http://people.apache.org/builds/struts/<version>/m2-staging-repository/

Once the realization dawned I started looking for new repository and finally found it on this page

http://mavenhub.com/mvn/central/org.apache.struts/struts2-archetypes/2.2.3

The struts2-arhetype-starter for 2.2.3 can be found in http://repo1.maven.org/maven2

Now finally the last bit that comprises of steps to create the struts2 project with m2eclipse

  1. Open your eclipse and click on File → New → Project
  2. Assuming you have m2eclipse installed select Maven → Maven Project in the new Project wizard.
  3. Click on Next twice and you will arrive on Select An Archetype screen.                                                              
  4. Click on Add Archetype button and supply following value                                                                     Archetype Group Id – org.apache.struts
    Archetype Artifact Id – struts2-archetype-starter
    Archetype Version –  2.2.3
    Repository Url – http://repo1.maven.org/maven2                                                                                        
  5. Click on OK.
  6. Now in Catalog Drop down select “All Catalog”. In filter type “org.apache.struts”. The newly added “struts2-archtype-starter” will show up in pane below. Select it and click Next                         
  7. On next screen specify groupid, artifactid, version and package for the application you wish to create.                                                                                                                                                                                            
  8. Click on finish and enjoy :).

 

Print Friendly, PDF & Email
Posted in eclipse, Java, m2eclipse, maven, mvc framework, Struts2, tutorial | Tagged , , , | 14 Comments

How I Introduced Multitenant Architecture In my Web Application

I am all in one kind of guy, I have always wanted to create a software which had little (or more :)) bit of many things. So I started building a webtop solution of my own. As you can quickly see I get to work on a great webapp in which I can integrate a variety of mini applications (All In One). As it happens with any project, even the fun ones, you get to sit down and draw all the beautiful diagrams capturing bits and pieces of the system. During this process I soon realized that I can turn my webtop into a money mongering machine by selling it to many users (definitely more than a billion users would log into my application and make me rich, after all who can resist it :)). So immediately my initial design took a wider berth as it has to include the good ol Multitenant SaaS architecture into consideration.

For people who are into stingy definitions here are couple

Software as a service (SaaS, typically pronounced [sæs]), sometimes referred to as “on-demand software,” is a software delivery model in which software and its associated data are hosted centrally (typically in the (Internet Cloud) and are typically accessed by users using a thin client, normally using a web browser over the Internet.

Multitenancy refers to a principle in software where a single instance of the software runs on a server, serving multiple client organizations (tenants).

Ok, you all correctly guessed I stole the above definitions from wikipedia. But give me a break I am not a genius, I cant create definitions on my own so give it a rest. In lieu of above definition let us ponder a bit on what multitenant Saas Application has to offer.  Since many users share same application instance hence there is a

  1. Increased resource utilization  
  2. Decreased cost in Delivering the service.
  3. Easily manageable
  4. Decrease in hardware cost which can be installed on-demand.

Design considerations for a Multitenant SaaS application.

Designing the Multitenant Webtop was very fun and satisfying and off-course not so easy. There are tons of architecture, methods and what not on web and the available on the net, also the existing books are not easy to decipher. After reading much I arrived at following design aspects I had to take care in my webtop.

  1. On Demand Scalibility – Since many users access a single instance of application hence the application should be easily and higly scalable. And on top of that SaaS provides should have a facility to scale easily when demand on system grows.

  2. Data isolation – For many industry ciritical SaaS application, like payroll SaaS for instance, The data security is of critical importance hence there is a need of isolating data between multiple tenants.

  3. High Degree of configurability – Each tenant has their own set of taste for look and feel, also there are set of configuration data, like smtp for instance, that are solely relevant to user. Hence it is pertinent for any SaaS application to go an extra length to provide user configurable version of application.

  4. Security – It is quite obvious that any tenant would not like other users to be accessing and messing up with their data. Hence a security becomes unarguably one of the most important architectural component of a SaaS application.

    If multitenancy was a person then we would look something like this.

    multitenant man

    Figure 0:  Multitenant Man

It is easy to write in one paragraph all this on-demand scalability, security blah, blah. But how do we go in about achieving it.  I will try and lay down some general rules on how to achieve this but not in this post. For that you have to wait for my future posts (can’t put everything in same bucket). For time being I will just tell you about technologies I will be using for my multitenant webtop application. I am a java guy hence I am biased towards using java technologies.

  1. Web Layer – Struts 2,  Tuckey’s UrlFilter,  jquery pluggin, sitemesh
  2. Business layer – Spring
  3. Database layer – Hibernate and Hibernate shards
  4. Security – Spring Security.
Over next few posts I will be outlining how the above technologies were mixed and used to make my webtop multitenant. I will also be commenting on general topics on how to achieve scalability  etc. So for time being bye bye 🙂 

Print Friendly, PDF & Email
Posted in Cloud Computing, Multitenancy | Tagged , , , | Comments Off on How I Introduced Multitenant Architecture In my Web Application

Welcome to weblog4j

Hi,

Welcome to weblog4j. We have hosted this blog to pour our Java thoughts and ideas into text. We will try to make the posts simple, informative and to the point. Feel free to read, comment, criticize , and show some love (if it any good).

Regards

Weblog4j Team

PS: I hope no one reads this 🙂

Print Friendly, PDF & Email
Posted in Welcome | Tagged | 3 Comments