zondag 26 oktober 2014

Execution Framework introduction

Introduction:

In the execution service there are many ways to do a trick I have been googling around and found a lot of words but none of them explains well what is what and why.

Solvation:

Most of the executor service are obtained through the Executors class like:
 Executors.newCachedThreadPool(); 
This threadpool provides you with the number of threads that you need. It reuses threads that come available again.
Executors.newFixedThreadPool(10); 
This threadpool only provides you with the number of threads that you have declared 10 in my example.
Executors.newSingleThreadExecutor(); 
This threadpool contains one single thread and all given tasks will be executed in sequence.

Except ofr the SingleThreadExecutor() all of the others deal the same way if it becomes to lists. As soon you expose a list to one of the named threadpools the behaviour is as follows:

Lets say we take a CachedThreadPool or a FixedThreadPool we expose a list of elements to the executorservice initialized with one of the above named pools.  The first element of that is exposed for threading it will be spread over all the available threads. As soon the task is done, the next element of the list will use all of the threads.  This is a good thing if that is what you desire. But what if you want to share all the elements of the list shared over different threads. In other words every element of the list should be worked on at the same moment in parallel.

This is where the only pool comes in that can do that is the ForkJoinPool (since java 7).  This pool is not obtained through the Executors class but is instanciated on its own like :

ForkJoinPool forkJoinPool = new ForkJoinPool(3); The constructor takes a int as argument to initialize the number of threads, 3 in my example. The ForkJoinPool uses the  work-stealing philosophy. This means that if a thread is running out of jobs, it will visit one of the neighbor threads and see if he can help him out.

Conclusion:

If you want to work a single task in different threads the ExecutionService is what you need. If you need different tasks work in paralel the same time, you should use the ForkJoinPool.

If you are using a version of Java lesser then 7, you can try this trick with google guava. I worked at this time with version 18.0 and that worked fine. Here is a piece of example code:

//Java class
ExecutorService executorService = Executors.newCachedThreadPool();
//Guava class
ExecutionList executionList = new ExecutionList();
//code to execute
executionList.add( new myRunnable(), executorService);
executionList.execute();

Have fun!

zaterdag 11 oktober 2014

reading method from stack


Introduction:

One of my colleagues came up with the idea to setup logging through AOP. I always encourage ideas like that. This might set you on the wrong foot. This blog is not about how to deal with AOP. This little part is about how to read the method name from the stack so it can be written to a log.

The difficulty came that certain classes should log the method name with a certain pattern and other method names not. This made it hard to solve with the patterns available in the Loggger settings.

The solution:

The idea sounds simple. I worked it a bit more out to be comply  to the open closed design way. Because what I learned in composite designing is that every object should interfere as less as possible with other objects. In other words, It should be as independent as possible. The only way to achieve this is to use reflection.

There we open another discussion when and where should we use reflection. The fist rule I is simple If there is another solution that is as valid use that one. If there is not another valid option then use reflection. So why reflection in this matter?

There are two ways to solve this:


  1. hardcode every method name also as a constant in the class that needs to log the method name.
  2. Use Reflection to get the name of the stack.   
So this how we solved it:

String methodname = Thread.currentThread().getStackTrace()[1].getMethodName();
Logger.info(methodName);


The .currentThread() is a static method from Thread from which you can get the stack called in that Thread. In my example there was only one thread. 
The 1 refers to the position of the method you desire from the stack. The further back you want the higher this number becomes. The only bummer in this story is that you cannot obtain the names that have been given to the parameters of the required method. The jvm creates new instances of them and name them  like arg0, arg1 etc...

Conclusion:

Knowing what you are doing is very important. It also shows everytime that things in Java are not that hard ones you understand the concepts. This is something you probably not gonne use a lot but when you need it, it is handy.

Have fun!

zaterdag 20 september 2014

Node.js reading json from rest service

Introduction:

The concept of a java script server is in my opinion revolutionary because of the fact that frontend technology also can be separated from the backend as a specialism. But what good is the one without the other. Creating a presentation  is no good without data. But what good is data if we cannot present it. So in this matter  I would show you that it is possible to combine the best of both worlds.


The solution:

In my previous post I described how to install a nodejs server and create an hello world application.
Because connecting to a rest service is a bit more complex we need to do some actions before we can start:

create an folder for example rest-api
cd rest-api

We need the superagent library:
npm install superagent@0.17.0 --save-dev

After this we can start building up the server:
// initializing the http package.
var http = require("http"),
//making the port available through a parameter 
port = process.argv[2] || 8888;
// initializing the superagent package
var superagent = require('superagent');
// creating the http server
http.createServer(function(request, response) {
//retrieving the rest url throught the superagent package
            superagent.get('http://localhost:8080/hello')
                .end(function(e, res){
//writing the res.text response from the rest server to the response which is the browser client response.
                    response.write(res.text, "binary");
                    response.end();
                })
//make the server listen to the port that is declared in my case 8090 or the standard port 8888
}).listen(parseInt(port, 10));
//just a friendly console message.
console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL + C to shutdown");

Conclusion:

The world of javascript is not that different from the world of Java these days. There is this virtual machine, it is getting Object Oriented in a kind of way and you have to know your way in the library wilderness. This makes the gap between the two worlds smaller. There are still differences between the way of working between these kind of frontend technologies and Java as allways the devil is in the details.

Have fun!

vrijdag 5 september 2014

Hello world with node.js

Introduction:

In my previous post I wrote about the clean separation of concerns by using a javascript server like node.js. In the past I worked on a project where they used node.js but personally I never got involved. I wrote the backend part of the system where we used rest services that returned json as an answer for the node.js server (javascript can handle json). 

So curiousity killed the cat but made man kind evolve. So I tried to set this up on my linux machine. Googled around a bit and found the answers to my questions. 

Installation:

actually on linux it is pretty basic if you have git running:
My attempt under debian:


  • git clone --depth 1 git://github.com/joyent/node.git
  • cd node
  • git checkout v0.4.11
  • export JOBS=2
  • mkdir ~/local
  • ./configure --prefix=$HOME/local/node
  • make
  • make install
  • echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
  • echo 'export NODE_PATH=$HOME/local/node:$HOME/local/node/lib/node_modules' >> ~/.profile
  • source ~/.profile

My attempt under ubuntu:
  • sudo apt-get install curl
  • curl -sL https://deb.nodesource.com/setup | sudo bash -
  • sudo apt-get install nodejs
(The path under ubuntu will bie /usr/bin/node)


After you done litterly this (take care of the $PATH ELEMENTS. You need to replace them with the real path where you put the node server). 
after this you can create a test.js in any folder you like containing:
//require is the import in java
 var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
//creating the http server
var server = http.createServer(function (request, response) {
//bare writing to the response like in the old days with Java.
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8090);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8090/");
After you saved the file you have to tell nodejs that he has to start this application:
  1. ./node test.js in your console in the node directory
  2. Start a browser on the desired port 8090 in my case
The result should be hello world on a snowy white page.

have fun!

dinsdag 2 september 2014

separating the frontend from the backend

Introduction

Through the years I seen a couple of application architectures. Some of them had the multi tier design but where  still one war file. I never seen that as a bad design. That was the best available way. The fun part of a way like that was that the architect had to defend the borders between the layers. That is where the weak point in this architecture lies. It is easy for a developer, for what ever reason, to cross the borders and start mixing the responsibilities of the different tiers. This is the first step of downgrading an well setup application to a kind of chaos architecture.

As allways in time ideas improve. A couple of years ago we suddenly had SOA. The backend logic was pushed back into REST or SOAP services with a separate project that was the frontend. The downside of this architecture was, that the html code was still infected with tags from java frameworks. Fore example
JSF had is own XHTML and even the newer like Wicket have there own tags. The most Frontend guys dont like that. They know html and they dont want to know about all the java frameworks.

The other downside of weaving the java frameworks into html is: that if the compagny decides that in some cases it should not be Java but something like PHP or Ruby etc... they still endup with a java frontend framework.

Now we enter a time where the possibilities to separate the frontend from the backend in a sharper and smarter way. It is possible to run a frontend tier completely on a javascript based server like node.js. Here is a list of js solutions.

The architecture:


Behind teh node js server part you can build as many java rest services as needed. Now we have a complete separation from the frontend and the backend. On the node.js server it is also possible to declare small html objects like textboxes or buttons etc...

Conclusion:

There are allready compagnies working on a application architecture like this or have in place. It requires to see the frontend as separate specialism and for that you need people that know how to deal with a frontend architecture like this. As a backend developer you have to let te responsibility for the frontend go and concentrate on what you can do actually the best backend development.

Have fun!


maandag 11 augustus 2014

From String to calculation

Introduction:

After the marshalling drill I have been thinking about how can we do small changes in xml without marshalling. The only other option I could come up with is xpath queries combined with element value changes. For primitive type changes this is a good idea but what if someone needs some complex calculations with a value from the xml file. Even this can be done.

The solution:

What I could think of was some kind of calculation template in a String something


//The numbers to calculate.
        int number1 = 40;
        int number2 = 2;

//Creating the javascript engine
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");

//The calculation template can be more complex
        String calcuationTemplate = "%d * %d";
        calcuationTemplate = String.format(calcuationTemplate, number1,number2);
        try {
//The actual calculation
            System.out.println(engine.eval(calcuationTemplate));
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }

The conclusion:

After some googling and trying I found that yet again this is pretty simple. With this I can do as much math I like in a very simple way. The only thing I still have to take care of is a difference between the numbers that have been given and the numbers that are retrieved from the xml file.

have fun!

vrijdag 8 augustus 2014

replace diacritic characters

Introduction:

We had a request to remove all the diacritic  from a String. As usual Java has a nice solution for this.

Solution:


String str = "ÚÝâ";

String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD);
Pattern pattern = Pattern .compile("\\p{InCombiningDiacriticalMarks}+");

System.out.println(pattern.matcher(nfdNormalizedString).replaceAll(""));

result: UYa

The Normalizer class according to the javadoc:

 This class provides the method normalize which transforms Unicode
 text into an equivalent composed or decomposed form, allowing for easier
 sorting and searching of text.


Normalizer.Form.NFD
This is an Enum that tells the Normalizer class what kind of normalisation you want there are:

     /**
         * Canonical decomposition.
         */
        NFD,

        /**
         * Canonical decomposition, followed by canonical composition.
         */
        NFC,

        /**
         * Compatibility decomposition.
         */
        NFKD,

        /**
         * Compatibility decomposition, followed by canonical composition.
         */
        NFKC

The NFD variant decomposes the Ú in U and ' this makes it possible to do a regular expression like 
Pattern pattern = Pattern .compile("\\p{InCombiningDiacriticalMarks}+");

At last but not least the question we had was: what in heavens name is \\p{InCombiningDiacriticalMarks}
This answer we found at stackoverflow:

\p{InCombiningDiacriticalMarks} is a Unicode block property. In JDK7, you will be able to write it using the two-part notation \p{Block=CombiningDiacriticalMarks}, which may be clearer to the reader. It is documented here in UAX#44: “The Unicode Character Database”.

Conclusion:

The code we build today is just a tip of the iceberg. The functionality that the Normalizer can bring is much more. I hope in the future I can do some other stuff with this.

Have fun! 

dinsdag 5 augustus 2014

open closed Marshalling with Jaxb



Introduction

One of the things we had to tackle this week was some marshalling issue with a zillion small xml files. The feast of legacy. It was a very depressing thought to create separate marshalling objects for each of them. That Idea popped faster out of my head then in. After some thinking we came up with a component that could do all of the xml files

The solution

The idea I came up with is very simple it is base on Jaxb marshalling which is quit powerfull in the way that the code you need to write is very small. Also the speed of marshalling and unmarshalling is quit fast.

To get this to work you need the following dependencies:

<dependencies>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.14.4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
   //This one you only need when you want to use xpath queries
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.0</version>
    </dependency>
</dependencies>


The one and only class for marshalling and unmarshalling:

package nl.lostlemon.Marshal;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

//The Type is to make it possible to instanciate this class with different marshalling beans.
public class Marshalling<T> {

    // Notice that T is the return typed bean. The xm is an inputStream. Jaxb can deal with that. It improves       performance 
    public T unMarshall(final InputStream xmlStream, final Class clazz) {
        T value = null;
        try {
//create an unmarshaller
            Unmarshaller unmarshal = JAXBContext.newInstance(clazz).createUnmarshaller();
//read the xml input stream into Jaxb
            XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(xmlStream);
//marshall the xml file into a JavaBean
            value = (T)unmarshal.unmarshal(reader, clazz).getValue();
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }

        return value;
    }
//Notice that the parameter is also The T of type.
    public ByteArrayOutputStream marshal(final T type) {
//Create the return type for this method.
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
//Create a marshaller
            Marshaller marshaller = JAXBContext.newInstance(type.getClass()).createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//Create the xml in the form of an outputstream to boost performance
            marshaller.marshal(type, outputStream);
        } catch (PropertyException e1) {
            e1.printStackTrace();
        } catch (JAXBException e1) {
            e1.printStackTrace();
        }
        return  outputStream;
    }
}

Running the following tests:

//This test uses the BookBean with an xml containing a single book.
    @Test
    public void testUnmarshallingXpath() {
        InputStream inputStream = this.getClass().getResourceAsStream("testSingle.xml");
        Marshalling<SingleBookBean> marshalling = new Marshalling<SingleBookBean>();
        SingleBookBean bookBean = marshalling.unMarshall(inputStream,SingleBookBean.class);
        Assert.assertTrue(bookBean.getTitle() != null);
    }

// The BookBean the @Getter and @Setter is from Lombok.
// Take care of the 
@XmlRootElement(name = "book")
@XmlAccessorType(XmlAccessType.FIELD) These are the identifiers to tell JaxB what to do. The XmlRootElement refers to the xml rootelement in the xml file.

@Getter
@Setter
@XmlRootElement(name = "book")
@XmlAccessorType(XmlAccessType.FIELD)
public class BookBean {
  // These are the exact same names as the xml Elements
    private String author;
    private String title;
}

//The single xml file:
<book>
    <title>witches abroad</title>
    <author>Terry Pratchet</author>
</book>

//This test is using the books bean with an xml containing multiple books.
    @Test
    public void testUnmarshallingList() {
        long start = Calendar.getInstance().getTimeInMillis();
        InputStream inputStream = this.getClass().getResourceAsStream("test.xml");
        Marshalling<Books> marshalling = new Marshalling();
        Books books = marshalling.unMarshall(inputStream,Books.class);
        //Assert.assertTrue(books.getBookBeans().size() == 3);
        long stop = Calendar.getInstance().getTimeInMillis();
        long elapsedTime = (stop-start);
        System.out.println("number of elements " + books.getBookBeans().size() + " elapsed time " + elapsedTime + " ms");

    }

// The Books bean based on the same principle as the BookBean.
@XmlRootElement(name = "catalog")
@XmlAccessorType(XmlAccessType.FIELD)
public class Books {

    @Getter
    @Setter
    @XmlElement(name = "book", type = BookBean.class)
    private List<BookBean> bookBeans;
}

//the multiple books xml file:
<catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>An in-depth look at creating applications
            with XML.</description>
    </book>
    <book id="bk102">
        <author>Ralls, Kim</author>
        <title>Midnight Rain</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-12-16</publish_date>
        <description>A former architect battles corporate zombies,
            an evil sorceress, and her own childhood to become queen
            of the world.</description>
    </book>
    <book id="bk103">
        <author>Corets, Eva</author>
        <title>Maeve Ascendant</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-11-17</publish_date>
        <description>After the collapse of a nanotechnology
            society in England, the young survivors lay the
            foundation for a new society.</description>

    </book>
</catalog>

//The marshalling test with the books bean:

    @Test
    public void testMarshallingList() {
        Books books = new Books();
        List<BookBean> bookBeans = new ArrayList<BookBean>();
        for(int i=0;i<100000;i++) {
            BookBean bookBean = new BookBean();
            bookBean.setAuthor("J.R.R Tolkien");
            bookBean.setTitle("The hobbit");
            bookBeans.add(bookBean);
        }
        books.setBookBeans(bookBeans);
        long start = Calendar.getInstance().getTimeInMillis();
        Marshalling<Books> marshalling = new Marshalling<Books>();
}

// The marshalling test with the BookBean.
   @Test
    public void testMarshalling() {
        BookBean bookBean = new BookBean();
        bookBean.setAuthor("Terry Pratchet");
        bookBean.setTitle("Witches abroad");
        Marshalling<BookBean> marshalling = new Marshalling<BookBean>();
        String xml = marshalling.marshal(bookBean).toString();
    }

Conclusion: 

Using Java typing in combination with a usefull library in the open closed principal, saves you a lot of code writing and maintainance. I had real pleassure to write in such a small piece of code so much power. 

Have fun!

dinsdag 24 juni 2014

Email Validation

Introduction

I was strugling with the apache commons email validator. This litle baby was not working according the specs they gave me. After a while I gave up on that and descided against my own rules to build it myself. What I mean with against my own rules is: Dont build if it is out there in a proper library. But in this case I had an exception. 

The best way to solve this issue was to use a regular expression. Not my favorite but mighty handy when you need them. 

Solution:

The scary part:

For those who have no experience with R.E. this might look like cursing in a comic :).

"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]+)$";

Lets analyze this:First lets have a look at the structure and later on we spill some information about the details.
The first thing you might notice in the structure are blocks like these [_A-Za-z0-9-\\+]+the A-Z, a-z or 0-9 this means that it contains an character in capitals (A-Z), or in non-capitals(a-z)or digits from 0-9. if the notation is like:[_A-Za-z0-9] It will only search for one character each.if you add a + it means iterate till you can find no more.if you add {2} or {2,5} instead of a + it means only 2 characters or from 2 to 5 characters.
In the case of the email validator you also need one .(dot) and one @ as you can see they are not followed by the annotations I described to iterate for more. 
Some of the details:
^ the carrot sign before the brackets means "Starts with" So in this case It can be a multiple underscores, characters or numbers.
The ^ inside the brackets means not like [^A-Z] everything but capitals.
The * is the same as +  occurs 1 or more times (iteration).
The $ checks if the end of line is following. 

Conclusion:

It still looks like cursing in a comic or something that gives you an headache. But it is the way forward if you want to check a string on patterns. This whole exercise is useless without execution. So you still need to use the Pattern and Matcher class (Examples all over the internet).
Or use the String.matches method. Will do two.

Have fun! 

maandag 9 juni 2014

scraping html with selenium server

Introduction:

In this case I  tried to create a server that is capable of getting the values from an html file. The first part what is needed I describe here.

The first part is of course to get the html from the url. This part you can do with Selenium Server, this is an java library which integrates selenium into the virtual machine. This is actual not the hardest part. The hardest part I am working currently namely to get crappy html a bit xml compliant.

The example:

public class Scraper {
    private static  final String BROWSER = "*firefox";
    private static  final String SERVER = "localhost";
    private static  final int PORT = 4444;
    private static final String SLASHES = "//";
    private static final String SLASH = "/";

    private DefaultSelenium selenium;
    private String url;
    public Scraper(String url) {
        this.url = url;
    }

    public void startScraper() {
        //This is to separate the server from the page where you want to start
        int startPosition = url.indexOf(SLASHES)  + SLASHES.length();
        //this turns http://google.nl/search into google.nl/search
        String tmpUrl = url.substring(startPosition);
        int endPosition = tmpUrl.indexOf(SLASH);
        //this turns http://google.nl/search into google.nl
        String baseUrl = url.substring(0, endPosition + startPosition);
       //this turns http://google.nl/search into /search
        String pageUrl = url.substring(endPosition + startPosition, url.length());
        //instanciate selenium with a port, your browser and the baseUrl
        selenium = new DefaultSelenium(SERVER, PORT, BROWSER, baseUrl);
        try {
        //instanciate the selenium server 
            SeleniumServer server = new SeleniumServer();
            server.start();
            selenium.start();
            //open the page you like search in this example
            selenium.open(pageUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   //This method is the first part for xml compliancy.
   //In my case the data I want is allways in the body.
    public String createHTmlOutPutStream () {
        String htmlSource = selenium.getHtmlSource();
        int start = htmlSource.indexOf("<body");
        int stop = htmlSource.indexOf("</body>") + 7 ;
        htmlSource = htmlSource.substring(start,stop);
     
        return htmlSource;
    }

  // This method fills every textfield if you know the name
    public void populateTextField(String fieldName, String text) {
        selenium.type(fieldName, text);
   }
 
   // This method clicks the button for you
    public void clickButton(String buttonName) {
        selenium.click(buttonName);
    }

    // This method runs through every option in a dropdown list.
    public void chooseDropDownAll(String dropDownName) {
        String[] options =  selenium.getSelectOptions("dropDownName");
        for (String option : options) {
            selenium.select("dropDownName", option);
        }
    }
    // This method only picks one option in the dropdown list.
    public void chooseDropDownSpecific(String dropDownName, String optionName) {
            selenium.select("dropDownName", optionName);
        }

}

Conclusion:

With those two libraries:
       <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>2.42.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>2.42.0</version>

 you will be capable of doing this trick. This only a small set of possibilities to discover.
You should try it, the next thing what I want to do is to write a test that does the login and the first search for me on the project I am working on right now. This because I like programming and not logging in :).


Have fun!

zondag 4 mei 2014

Jpa another way of persisting

Introduction:

To deal with persistence in Java is getting less complicated by the tools that are being offered to us. The complication lies in the fact what to choose and what kind of strategy should you choose. In my previous blog I opened the doors to JPA which i denied to exist for a couple of years. "Just another Hibernate" was my defense. Looking at it from a historical point of view, My thoughts where based on some points. Hibernate was there together with toplnk and EJB to deal with ORM before JPA. These three tools where the bases for the reference called JPA. At last I opened that small and started being interested in JPA. I have to admit it is getting to me. I really like the way the things have been setup. A lot of people that tried to explain to me that JPA gave more freedom, have been shot by me as evangelists. The fun part of the story is, that not there preaching but a project that was using JPA brought me the light.

The example:

There are a couple of ways to deal with persistence in JPA. The most easy and fun one are repositories. I have them on my list to be study. But for now there is SQL, HQL (hibernate) and JQL (jpa). Sql gives you the straight forward way to acces an database something select * from customers or update customers set name = 'john doe' where id = 1 should look familiair. In JQL this looks pretty much the same but you are not talking to the database but to the objects that are representing the database in the application called entities.

So we have a simple Java pojo for customers with a customer name (I like it simple :)):
Lets use Lombok for fun:


public class Customer {

@Id
@GeneratedValue
@Getter
@Setter
private long id;
//lombok annotations.
@Getter
@Setter
private String name;

}

//Now we make it persistable:
@Entity
//the table to point to the name is not mandotory if the class has the same name as the database table.
@Table(name="customer")
public class Customer {

@Id
@GeneratedValue
@Getter
@Setter
private long id;
//lombok annotations.
@Getter
@Setter
@Column //you could also use the name attribute if the column name in the table difffers from the class //member name.
private String name;

}

if JPA is configured correctly this class is coupled to the table now by the ORM of JPA.

But now my customers are hooked up with the products they bought from me That is going to give me an N to N relation ship. 1 customer can buy multiple products and 1 product could been bought by multiple customers. An N to N relation ship can only achieved through a coupling table in the database.  It would be really annoying if the represenation of your class model should be one on one with the database. This means that you would need to model all your coupling tables which yo are not going to need to present your data. 

One of the things we can do is Model it with @named queries in JPA That would look something like this:


@Entity
//the table to point to the name is not mandotory if the class has the same name as the database table.
@Table(name="customer")
//The JPA named query
@NamedQuery(name = "selectPrdFromCustomer" , query = "select c.products from Customers c where c.id = ?1 ") })
public class Customer {

private long id;
//lombok annotations.
@Getter
@Setter
@Column //you could also use the name attribute if the column name in the table difffers from the class //member name.
private String name;

//lombok annotations.
@Getter
@Setter
//ManyToMany is the equilevant for N to N cascading all means get all the data you can on all the levels allowed. 
@ManyToMany(cascade = CascadeType.ALL)
//The CUSTOMER_PRODUCT table is the coupling table. The join collumn is the collumn where the id from this class should couple with. the inverseJoinColum is the id of the products where it should couple to.
@JoinTable(name = "CUSTOMER_PRODUCT", joinColumns = @JoinColumn(name = "product_id"), inverseJoinColumns = @JoinColumn(name = "id"))
Private List<Products> products

}

At this point you can that a normal sql query has been handled on Entity level. The interesting part is that this sql query is separated on two different places. The named query is taking responsibility for the selecting part while the ManyToMany and the JoinTable is taking the responsibility for the coupling part.

As last you can create an PersistenceController or manager and that would need to do something like this:

Public PersistenceController {
    @PersistenceContext
    private EntityManager em;
@Setter
@Getter
private long customerId;

public List<String getProductNames() {

public List<String> product name = em.createNamedQuery("selectPrdFromCustomer").setParameter(1, customerId).getSingleResult();
//The getSingleResult is only applicable if you really want to have one result instead of an list but then the type of the method should be also String and not al list.

}
}

Conclusion:

Ones you know how to deal with persistence like this it is easy. But to learn it can give you a bad hadache. It is worth the effort That is for sure. Just try the example that has been given and have FUN!


zaterdag 26 april 2014

peristence future arrived


Introduction:

Traditionally Java and databases have been on friends.  Starting with the java persistence libraries where soon enough we could use prepared statements to secure the way we queried the database. After that the world of Java  was shocked with the birth of ORM toolings. These babies really decoupled the whole data tier from the rest and saved a lot of work. Implementations of  JPA and Eclipse (Top) link helped the word easy up the way of persistence.   That the future is allways one step away is common knowledge these days. But in some cases the future allready catched up with me before I realize this. m
In the case of JPA repositories that happened. I figured this technique out the last couple of weeks and I am already in love with it. They pushed things a little bit further then my imagination would go. It is not Star trek yet but still quite impressive.

The concept:

The Idea behind this to diminish the boiler plate coding further. The benefits of this I can bring down to two points:


  1. After configuring this right which is a bit more work, the coding comes down to allmost nothing.
  2. Because of the fact that the persistence part is mostly configuration, the repositories them selves can (not necessarily) function as a complete database tier.  
The configuration is a combination between JPA and spring in my example. There might be other implementations out there where I don't have knowledge of.

The configuration:


The configuration I set up is a mix of spring and maven:

Maven pom  part:

    <properties>
        <spring.version>4.0.3.RELEASE</spring.version>
    </properties>
  <dependencies>
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>
        <!-- Spring Data JPA -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.5.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.4.Final</version>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.0-801.jdbc4</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.5.Final</version>
//could not load the JPA from here so I did it separate.
            <exclusions>
                <exclusion>
                    <groupId>javax.transaction</groupId>
                    <artifactId>jta</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
//The jpa libary that is needed for hibernate.
        <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>

the persistence xml which needs to be placed in src/main/resources/META-INF.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="dataSource" transaction-type="RESOURCE_LOCAL">
        <description><YOUR DESCRIPTION></description>

        <class>com.shops.data.info.CustomerInfo</class>

        <properties>
            <property name="javax.persistence.jdbc.driver"   value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url"      value="jdbc:postgresql://localhost:5432/shops" />
            <property name="javax.persistence.jdbc.user"     value="<USERNAME>" />
            <property name="javax.persistence.jdbc.password" value="<PASSWORD>" />

//optional parameters.
            <property name="hibernate.show_sql"     value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>
    </persistence-unit>
</persistence>

The spring context file:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <jpa:repositories base-package="THE REPOSITORY PACKAGE"/>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5432/<YOURDATABASE>"/>
        <property name="username" value="<USERNAME>"/>
        <property name="password" value="PASSWORD"/>
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true"/>
        <property name="generateDdl" value="true"/>
        <property name="database" value="POSTGRESQL"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <!-- spring based scanning for entity classes-->
        <property name="packagesToScan" value="<THE D.O. PACKAGE>"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>
</beans>

The repository part:

You can have multiple repositories. How to deal with the number of repositories depends completely on the fact how you want to deal delete with your data.

Here is al single example:

import com.my.web.shop.dataobjects.CustomerInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
@Transactional
interface CustomerRepository extends JpaRepository<CustomerInfo, Long > {

    public List<CustomerInfo> findByEmailadressAndPassword(String email, String password);
    public List<CustomerInfo> findByEmailadress(String email);
}

In this litle piece of code you find some astonishing things first of all after creating this interface, I did not create an implementation. Creating your own repository is not necessary in the most cases. As a matter of fact if you create an Repository on your own, you be at the point where we would start writing this extra boiler plate code we done for years. 

Under water this interface is hooked up with the CrudRepository of Spring. This baby gives you allmost everything you need. All the basic database actions as select, update, delete and insert are there.    
The second part you might find different is the naming of the methods. This is actually one of the most amazing parts. In the name of the method you declare the fields which you want to use in your sql statement. The parameters represent the values. So the first method would build under water: where emailAdress = "a@b.nl" and password = "myPassword.". If you have a big query the name of your method would become very long. Luckily there is another way to deal with queries. The @Query annotation helps to solve this problem. It could look something like this:


//The D.O. for the myShop customers.  
@Entity
@Table(name = "customers")
public class CustomerInfo implements Serializable{

    @Id
    @GeneratedValue
  private long id;
    @Column
    private String emailadress;
    @Column
    private String password;


@Id
public Long getId() {
return id;
}

public String getPassword() {
return password;
}

public void setId(Long id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}


    public String getEmailadress() {
        return emailadress;
    }

    public void setEmailadress(String emailadress) {
        this.emailadress = emailadress;
    }
}

// The D.O that couples that couples the customer to  the test data. And contains the test data.
@Entity
@Table(name = "test")
public class Test implements Serializable {
    @Id
    @GeneratedValue
    private long id;
    @Column
    private String label;
    @Column(name = "customer_id")
    private long customersId;

    @OneToMany()
    @JoinColumn(name="id")
    private List<CustomerInfo> customerInfo;

   public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public long getCustomersId() {
        return customersId;
    }

    public void setCustomersId(long customersId) {
        this.customersId = customersId;
    }
}

//The test data repository 
import com.shops.data.info.Test;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by compurat on 4/19/14.
 */
@Repository
@Transactional
interface TestRepository extends JpaRepository<Test, Long > {

    @Query("SELECT t.label FROM Test t  left join t.customerInfo c  where t.id=(?1)")
    public String findById(long id);

}

As you can see the findById method is not acting on the method name and the parameter value but will act on the @Query that we put on top of it.  The last part of the query works in a simular way as we know with PreparedStatement. The questionmark tells the compiler this a placeholder an d the number tells which parameter in line it needs to pickup. In this case we only have one parameter. Not enough space for a mistake :).

There is another piece that might have caught your eye. There is a join in this query. It took me a while to figure out how that worked. The secret lies  in  the simple fact that you have to divide the join over two places. 

  1.     @OneToMany()
        @JoinColumn(name="id")
        private List<CustomerInfo> customerInfo; defines this part of the join. It tells: listen there is a join coming up. In this case it is a OneToMany but it can also be @ManyToMany or @OneToOne. The @JoinColumn tells to connect to a certainfield in the CustomerInfo class. 
  2. The  left join t.customerInfo c  in the @Query part tells to actually execute the join. As you might notice the ON part from sql is not translated here. That is one of the parts That still puzzles me.How is it known to couple to? For the rest As allways as you understand the concept You find out it was not as hard as you expected first. 
Conclusion:

The concept itself and the amazement it gave me leveled up the steepness for me to understand it. But now I am happy I gave it time and picked up the things I allready know now. The concept to me is still a bit futuristic. But the fact it is here and I love to use it. The power of this is amazing. 

Have fun!

dinsdag 25 maart 2014

Test with Wicket and Spring

Introduction:

I started working with Wicket and as a Java developer I like the concept. I think when there are really front end developers working, it is harder to defend the fact that you want to use wicket. Wicket brings it own html notations. Most frontenders are not keen on that. For working with Wicket there is a very good starting point: the wicket free guide. The interesting part starts when you are about to use DI and Wicket. Setting that up is not to hard The web.xml should look like this:

<web-app>
  <display-name>MyApp</display-name>
  <filter>
  <filter-name>MyAppFilter</filter-name>
  <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
  <init-param>
  <param-name>applicationClassName</param-name>
<param-value>com.my.app.MyApp</param-value>
  </init-param>
  </filter>
  <filter-mapping>
    <filter-name>MyAppFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

For maven just get the latest version of wicket and spring and you be up and running in no time.

The problem:

The thing i ran into was the part where I wanted to start testing with Wicket using some DI to deal with some Classes which otherwise would end up in a very complex design. So the thing I cooked up after reading a lot of examples on the internet that didn't work.

The spring test config in src/test/resources:

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  <bean id="classa" class="com.myapp.ClassA">
</bean>
  <bean id="classb" class="com.myapp.ClassB">
</bean>
 <bean id="wicketApplication" class="com.myapp.MyWicketApp">
</bean>
</beans>

The Junit test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:myApp-test-context.xml" })
public class LoginTest {
private WicketTester wicketTester;

@Inject
        // this stub is inheriting from an abstract class which is enhiriting from the wicket
          WebApplication otherwise I cannot DI with the same type. The real app is also inheriting from
           that abstract class
private AmyApp myAppStub;

@Before
public void init() {
wicketTester = new WicketTester(myAppStub);
}
}

The mock Application:

public class MyAppStub extends AmyApp {
private final ApplicationContextMock applicationContextMock = new ApplicationContextMock();
       //injecting the classes from the test context.
@Inject
private ClassA classa;
@Inject
private ClassB classb;

protected ApplicationContextMock getApplicationContextMock() {
return applicationContextMock;
}

@Override
public Class<? extends Page> getHomePage() {
// TODO Auto-generated method stub
return null;
}

@Override
public void init() {
                //Adding the injected classes to the context mock so they will be used during testing.
applicationContextMock.putBean("classa",
classa);
applicationContextMock.putBean("classb",
classb);
                //adding the applicationmock to spring
getComponentInstantiationListeners().add(
new SpringComponentInjector(this, applicationContextMock));
}

}

Conclusion:

After I figured out how this baby works it is not that hard anymore to test with wicket. All you have to do is figure out the right pieces of the puzzle. In all the other examples the same pieces where missing.

I hope This helps.

Have Fun!

dinsdag 18 maart 2014

CDI (context dependency injection JSR 299) and Junit Testing.

Introduction:

DI is unthinkable these days. Having different variations on the same concept in different classes and of course a   separate context for testing. Although for testing mocking instead of stubbing is also a  very valid option. I have been playing around with Spring and with Google guice but I never came around to play with CDI. I have been playing with it today And I must say it is quiet charming. I really loved the simple and direct way of annotating stuff and no traces of xml files (only if you want too).

The part I want to describe is the part to use for testing. It reminds me a lot of how that works with spring apart from the fact that I did not had to point to a context.xml file.

The example:

Because the jsr 299 is a reference, the main parties allready have build there implementation for it :

(The versions are at this point in time (march 2014) the latest.

  • JBoss it would be: 

<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>2.2.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
  • Apache
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>1.2.2</version>
</dependency>

To write a piece of code that chooses the depedency for its needs CDI is great in being simple:

The interface that makes the difference:

public interface IHelloWorld {

void sayHello();

}

The class that does my realtime serious business kind of logic:
import javax.enterprise.inject.Alternative;
//This annotation tells the CDI container that it is one of the many implementations.
@Alternative
public class HelloWorld implements IHelloWorld {

public void sayHello() {
System.out.println("Hello world");
}
}

The class that does my very serious alternative kind of business logic or test stubbing.
import javax.enterprise.inject.Alternative;
//This annotation tells the CDI container that it is one of the many implementations.
@Alternative
public class HelloAlternative implements IHelloWorld {

public void sayHello() {
System.out.println("hello alternative world");
}


The class that is complete unaware of the fact that he does not speak to the same injected all the time. Love to keep this one in the dark about that.

import javax.inject.Inject;

public class LoginBean {

        //This is the actual injection of the desired class.
@Inject
private IHelloWorld helloWorld;

public void print() {
helloWorld.sayHello();
}
}

The first test that uses my very serious business logic saying: "Hello world "

import javax.inject.Inject;

import org.jglue.cdiunit.ActivatedAlternatives;
import org.jglue.cdiunit.CdiRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

//The annotation that hooks this test up with te CDI container
@RunWith(CdiRunner.class)
// The annotation that tells which implementation of the IHelloWorld Interf to use.
@ActivatedAlternatives(HelloWorld.class)
public class HelloWorldTest {
         //Injects the bean that has a DI itself.
@Inject
LoginBean loginBean;

@Test
public void test() {
loginBean.print();
//outcome : "Hello world"        


}

import javax.inject.Inject;

import org.jglue.cdiunit.ActivatedAlternatives;
import org.jglue.cdiunit.CdiRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

//The annotation that hooks this test up with te CDI container
@RunWith(CdiRunner.class)
// The annotation that tells which implementation of the IHelloWorld Interf to use.
@ActivatedAlternatives(HelloWorld.class)
public class HelloAlternativeTest {

@Inject
LoginBean loginBean;

@Test
public void test() {
loginBean.print();
//outcome: hello alternative world
}

At last but not least the full Apache maven tech stack:


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.jglue.cdi-unit</groupId>
<artifactId>cdi-unit</artifactId>
<version>2.1.0</version>
<scope></scope>
</dependency>

And the full JBoss maven tech stack:

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
</dependency>
<dependency>
<groupId>org.jglue.cdi-unit</groupId>
<artifactId>cdi-unit</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>2.2.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>

Conclusion:

If you are using the Apache stack and you want to move to the JBoss stack or visa versa, that does not mather. They bot comply to the JSR 299. There is no need for code changing. To me personal this way of DI is more intuitive and very powerfull by the annotatons it is using. Give it a try and let me know what you think of it.

Have fun!