Click here to Skip to main content
Click here to Skip to main content

Introduction to the Green-forest Framework

, 31 Jan 2013
Rate this:
Please Sign up or sign in to vote.
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:

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:

    //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:

    @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:

    //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:

//"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:

//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:

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:

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:

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:

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:

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)

About the Author

evgeny.dolganov
Software Developer (Senior)
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberFred Flams1-Feb-13 1:54 
Good article, clear and concise.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 31 Jan 2013
Article Copyright 2013 by evgeny.dolganov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid