Thursday, July 31, 2014

Spring MVC, OSGI bundles and forwarding requests

I have a post in this blog with example of using OSGI servlet bridge and embedded OSGI framework. Now it's time to extend it.
Let's play with OSGI combining it with Spring MVC application.



With stackoverflow you can find some links to samples and explanations about Sprng MVC and OSGI
http://stackoverflow.com/questions/12832697/looking-for-an-osgi-with-spring-specifically-spring-mvc-tutorial
http://stackoverflow.com/questions/12331722/osgi-spring-mvc-bundle-nightmare-java-lang-classnotfoundexception-org-springf

In this article I'd like to show how does spring MVC application may use OSGI servlet-bundles.
1. My spring MVC application is going to be the main application with a fundamental business logic and it's going to be a bridge for bundles
2. I'd like to have a special bundle with a logic that is common for osgi bundles installed. Some kind of super bundle with general functionality.
3. Other bundles have it's logic implemented inside and also could interact with general bundle and main spring application forwarding its queries.

Step-by-step

Thursday, July 17, 2014

Simple jUnit which compares two files

I have a piece of code that generates some file an I need to cover this code with unit-tests. With a sample file I may check my code whether the file was generated correctly. Maven creates src/test/resources directory automatically, there is no better place to put the sample file.
If the file has a path /src/test/resources/tables/sample.xml we can read them in a following way

URL url = this.getClass().getResource("/tables/sample.xml");
File sampleFile = new File(url.getFile());

Don't need to compare files line-by-line, just use commons-io util

FileUtils.contentEquals(file1, file2);

Finally, three lines of code below can be applied for unit tests (and not only for tests) to check for files equality:

private boolean isFileDataEqual(File target) throws IOException {
 URL url = this.getClass().getResource("/tables/sample.xml");
 File sampleFile = new File(url.getFile());
 return FileUtils.contentEquals(target, sampleFile);
}

Monday, July 7, 2014

OSGI servlet bridge sample


Servlet bridge is a mechanism for extending your servlet functionality adding other servlets. In this case your main servlet is not affected within some changes however the number of processed request is increased by newly added servlets (modules).
We don't want to have some sophisticated way to register/unregister new modules, don't want to be forced to rebuild the whole application when the new servlet is registered. OSGI is a keyword which helps us to avoid of this issues. In the scope of Servlet Bridge feature OSGI is a powerful mechanism which can help you to extend the functionality of your java servlet application. It can be achieved by using HttpService implementation provided by OSGI standards. Image source: http://www.jayway.com/2007/05/01/osgi-not-just-a-four-letter-word/
(image from: http://www.jayway.com/2007/05/01/osgi-not-just-a-four-letter-word/)
This post is a good way to start with theoretical basics of OSGI, let's try to implement the OSGI servlet bridge.

Tuesday, July 1, 2014

Get data table metadata using MyBatis

Don't try to use myBatis type handlers to get the metadata of some database tables.
If the datatable doesn't contain any data the type handler won't work and you will not get any result.
There is a link about how to do it with TypeHandlers but don't forget that your datatable shoudn't be empty:
http://www.coderanch.com/t/415314/ORM/databases/retrieve-metadata-ibatis
http://www.thespringriver.com/simple-example-of-mybatis-java-maven-implementation-8-customized-type-handler/ (This is about how to use TypeHandler for the latest MyBatis version [3.2.7])

The better solution is using the data from INFORMATION_SCHEMA, for example you can use some query like:

<select id="selectMeta" parameterType="String" resultType="DataColumnForm">
 SELECT TABLE_CATALOG, TABLE_NAME, COLUMN_NAME, TYPE_NAME, DATA_TYPE, COLUMN_DEFAULT
  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=#{tbl_name}  
</select>