Monday, August 09, 2010

Spicing your Code

In the road trying to develop in Java, i found that many things are repetitively useless.
Thus is how i thought that there must be a way for doing the same things, but in a simpler and faster way. And i was correct, with the introducing of JDK6, many features really became useful such as: annotations, generics and so on, this combined with power of the interfaces and abstract classes brought us all these necessary features to achieve such purpose.
Below i introduce two very interesting APIs that represent that i mean.

The first one is very interesting, because allows developers to reduce in the source code writing, avoiding waste of time implementing such that lines of code:

http://projectlombok.org/

For example: the most common case is to implement Settes/Getters, this can be easily implemented as follows:

01 import lombok.AccessLevel;
02 import lombok.Getter;
03 import lombok.Setter;
04
05 public class GetterSetterExample {
06 @Getter @Setter private int age = 10;
07 @Setter(AccessLevel.PROTECTED) private String name;
08
09 @Override public String toString() {
10 return String.format("%s (age: %d)", getName(), getAge());
11 }
12 }

An entire support is provided for Netbeans IDE.


The second one simplifies the usage of XML and Database Connections.

http://cjb.sourceforge.net/

Thus, an example of a connection looks like follows:


 39         JdbcConnection con = fvm.getDataStore().buildJdbcConnection();
40 //let's get the company ID to get the schema to which belogs
41 PreparedStatement ps = con.prepareStatement("select any_fied from any_table where id = ?");
42 ps.setString(1, request.getId());
43 QueryListener ql = new QueryListener() {
44 public String queryResult(ResultSet rs, JdbcConnection conn) throws SQLException {
45 if (rs.next()) {
46 return rs.getString(1);
47 }
48 return null;
49 }
50 };
51 String id = con.executeQuery(ql, ps);

This API Call your close() methods to connections, statements, etc safely with no hassle. Also allows to manually close
any connection object.
Provides a interesting XML schema to embed connection configurations and read it from any XML document.
Or just check out the following example:

 37         JdbcConnectionReader r = new JdbcConnectionReader();
38 r.parseDocument(getClass().getResourceAsStream("ComplexConn.xml"));
39 JdbcConnection con = r.buildJdbcConnection();

Where ComplexConn.xml resource looks like:
 1 <?xml version="1.0" encoding="UTF-8"?>
2 <jms:ServerDataContext
3 xmlns:mb="http://www.dacorpsoft.com/mapbeans"
4 xmlns:jms="http://www.dacorpsoft.com/jmapserver"
5 xmlns:jcore="http://cjb.sourceforge.net"
6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 xsi:schemaLocation="http://www.dacorpsoft.com/jmapserver JMapServer2.0.xsd">
8 <jms:NameSpaceCatalog>
9 ....
28 </jms:NameSpaceCatalog>
29 <jms:DacorpDataSource>
30 <jcore:JdbcConnection jcore:alias="JNDI">
31 <jcore:Param>
32 <jcore:Key>jdniReferenceName</jcore:Key>
33 <jcore:Value>jdbc/geobase</jcore:Value>
34 </jcore:Param>
35 </jcore:JdbcConnection>
36 </jms:DacorpDataSource>
37 </jms:ServerDataContext>
38
where the Tag JdbcConnection contains information about a connection JNDI named geobase.

Try it and give'em a chance, both have good documentation to start to use them on your projects, may be they can save your time.