Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Introduction to the Green-forest Framework

4.85/5 (4 votes)
31 Jan 2013CPOL2 min read 17K  
An introduction to the Green-forest Framework for creating a web application example.

Green-forest Logo

Introduction

Green-forest Framework is a simple IoC Container with Action-Handler architecture. It's not a competitor for Spring Framework or JEE but it's a powerful addition for these frameworks. You can use Green-forest for a single class implementation or for complex business logic.

The Action-Handler architecture can be presented by this scheme:

Image 2

As you see on the scheme we divide our logic into some classes:

  1. Action

  2. The Action class presents an "atomic business method" and contains input and output data:

    Java
    //Action with String input and Integer output data
    public class SomeAction extends Action<String, Integer>{
     
        public SomeAction(String input) {
            super(input);
        }
    }
  3. Handler

  4. The Handler class presents an implementation of business logic:

    Java
    @Mapping(SomeAction.class)
    public class SomeHandler extends Handler<SomeAction>{
     
        @Inject
        SomeService service;
         
        public void invoke(SomeAction action) throws Exception {
         
            String input = action.getInput();
            Integer result = service.doSomeWork(input);
             
            action.setOutput(result);
        }
    }
  5. Framework

  6. The Framework's Engine ensures functioning of handlers:

    Java
    //create Engine instance
    Engine engine = new Engine();
     
    //register the handler
    engine.putHandler(SomeHandler.class);
     
    //invoke the action
    Integer result = engine.invoke(new SomeAction("some data"));

Motivation

What is the motivation for using the Green-forest framework? The main task is to simplify code: transfer of one big class (some service implementation) to a set of small classes (handlers).

See this example: "Classic" implementation presents one big class in 1000 lines of code:

Java
//"classic" impl of API
public class SomeBigServiceImpl implements SomeService {
    
    public Doc createDoc(...){...}
    
    public Doc createDocWithAttach(...){...}
    
    public Doc getDocById(...){...}
    
    public List getDocsList(...){...}
    
    //and many many other methods on 1000 lines of a code
    
}

Action-Handler implementation presents a set of small classes:

Java
//Action-Handler API
public class CreateDocHandler extends Handler<CreateDoc>{
  //10-20 lines of code
}

public class CreateDocWithAttachHandler extends Handler<CreateDocWithAttach>{
  //10-20 lines of code
}

public class GetDocByIdHandler extends Handler<GetDoc>{
  //10-20 lines of code
}

public class GetDocListHandler extends Handler<GetDocList>{
  //10-20 lines of code
}

Example application

Overview

Let's review a simple example application with Green-forest framework. It presents a web interface for users and uses a database for storage. With this application we can create, read, update, and delete simple objects.

Here is a view of the application's web interface:

Image 3

You can download binaries and sources for this example from the article's download section. After downloading the "web-app-example.war" file, deploy it into your Web Server. This application was tested on a Tomcat 7.0.28 web server.

Explanation of example

First we present the model class for CRUD operations - Doc:

Java
package example.common.model;

public class Doc {
    public long id;
    public String name;
}

After that we create the set of actions - our business API:

src/example/common/action/
    CreateDataBase.java
    CreateDocs.java
    GetDocsCount.java
    GetDocsPage.java
    RenameDoc.java

Every class of this set is an action class:

Java
package example.common.action;

public class RenameDoc extends Action<Doc, Void> {
    
    public RenameDoc(int id, String newName){
        this(new Doc(id, newName));
    }
}

So we can use these actions in our web layer:

Java
package example.web;

public class AppServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
        
        //get data for update
        int id = Util.tryParse(req.getParameter("id"), -1);
        String newName = req.getParameter("name");
        
        //invoke update action
        application.invoke(new RenameDoc(id, newName));
    }

}

And for every action we create its own handler:

Java
package jdbc.storage.handler;

@Mapping(RenameDoc.class)
public class RenameDocHandler extends Handler<RenameDoc>{
    
    @Inject
    Connection c;

    @Override
    public void invoke(RenameDoc action) throws Exception {
        
        Doc input = action.input();
        
        PreparedStatement ps = c.prepareStatement("UPDATE doc SET name=? WHERE id=?");
        ps.setInt(2, input.id);
        ps.setString(1, input.name);
        ps.executeUpdate();
    }
}

That's it! We receive a set of compact simple classes for API (Actions) and for implementation (Handlers).

Summary

Green-forest Framework presents a new perspective on the development of Java applications. It's a free open-source tool which can simplify the code of your application. This library requires minimum time to learn and is suitable for a wide range of tasks. Happy coding with it!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)