Wednesday, December 29, 2010

Sending SOAP messages through HTTPS using SAAJ

Introduction
A month ago a task assigned to me. It was calling the API of a network management device by sending SOAP messages through HTTPS. It wasn't so easy and straight forward to me.

SAAJ
I've used SAAJ for calling web services. It is well known for Java developers.

Steps
The process contains below steps:
1. Export the HTTPS Certificate into a file
2. Import the HTTPS Certificate into Java HTTPS certificates
3. Creating a SOAP connection
4. Creating a SOAP message
5. Populating the message
6. Trust to HTTPS certificates
7. Sending the message
8. Retrieving the reply

Get the Certificate
Assume WSDL address is:
https://192.168.10.1:8991/wsdl/balance.wsdl

Read this to find out how to get the certificate for access to the upper WSDL address.
Here I saved the certificate in "amirssw.cer" file.
Java HTTPS certificates stored in
$JAVA_HOME/jre/lib/security/cacerts

To import your new certificate into "cacerts" run below command:
sudo keytool -import -file amirssw.cer -keystore cacerts

The password is "changeit".

SOAP message
In my sample SOAP message stored in a file: "/home/amir/projects/mine/p8/message/helloWorld.msg".
You can find a sample SOAP message here.

Java Code
This is a simple code that shows clearly how to send a SOAP message through HTTPS. The only different between sending the message on HTTP over HTTPS is the URL and doTrustToCertificates() method call just before sending the message.


package com.neverhood.webssl;

import javax.net.ssl.*;
import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.Vector;

public class Client {

public static void main(String[] args) {
talk("https://192.168.10.1:8991/balance/soap", "/home/amir/projects/mine/p8/message/helloWorld.msg");
}

private static void talk(String urlval, String msgPath) {
try {
// Create message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();

// Object for message parts
SOAPPart sp = msg.getSOAPPart();
StreamSource prepMsg = new StreamSource(new FileInputStream(msgPath));
sp.setContent(prepMsg);

// Save message
msg.saveChanges();

// View input
System.out.println("\n Soap request:\n");
msg.writeTo(System.out);
System.out.println();

// Trust to certificates
doTrustToCertificates();

//SOAPMessage rp = conn.call(msg, urlval);
SOAPMessage rp = sendMessage(msg, urlval);

// View the output
System.out.println("\nXML response\n");

// Create transformer
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();

// Get reply content
Source sc = rp.getSOAPPart().getContent();

// Set output transformation
StreamResult result = new StreamResult(System.out);
tf.transform(sc, result);
System.out.println();

// now GET the Response and PARSE IT !
Vector list = new Vector();
SOAPBody soapBody = rp.getSOAPBody();
Iterator iterator1 = soapBody.getChildElements();
while (iterator1.hasNext()) {
SOAPBodyElement ThisBodyElement = (SOAPBodyElement) iterator1.next();
Iterator it2 = ThisBodyElement.getChildElements();
while (it2.hasNext()) {
SOAPElement child2 = (SOAPElement) it2.next();
Iterator it3 = child2.getChildElements();
while (it3.hasNext()) {
SOAPElement child3 = (SOAPElement) it3.next();
String value = child3.getValue();
list.addElement(value);
}
}
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.elementAt(i));
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}

}

static public SOAPMessage sendMessage(SOAPMessage message, String endPoint) throws MalformedURLException, SOAPException {
SOAPMessage result = null;
if (endPoint != null && message != null) {
URL url = new URL(endPoint);
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection connection = null;
long time = System.currentTimeMillis();
try {
connection = scf.createConnection(); //point-to-point connection
result = connection.call(message, url);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SOAPException soape) {
System.out.print("Can't close SOAPConnection:" + soape);
}
}
}
}
return result;
}

static public void doTrustToCertificates() throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}

public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
return;
}
}
};

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '" + session.getPeerHost() + "'.");
}
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}


Done.

Saturday, September 25, 2010

Port Forwarding, The way I use SSH

"Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two networked devices." Wikipedia.
That is just an abstract definition. I use SSH to do much more. Here are just some simple commands that I used to run for such a different reasons:

Whenever I need to pass stupid filtering of stupid government

ssh -D 8888 root@100.101.102.103

"100.101.102.103" is just the host that you want to connect it. I assumed that 100.101.102.103 is a SSH enabled machine.
This command simply forwards the remote machine's HTTP port to my local machine. So I just need to use a socks4 proxy like this : 127.0.0.1:8888.
So whatever you do using your browser will forward to the remote machine. The channel between your local machine and the remote one is pretty secured. However, the remote machine should be trusted.


Whenever I need to connect a remote MS-SQLServer on a windows machine using my Linux machine
Assume a service such as SQLServer which runs on a machine that is hosted in a remote LAN with [192.168.0.100] IP address. Assume you just can connect to another machine that is hosted in the same LAN using:
ssh root@100.101.102.103

Assume again the MS-SQLServer uses the port number 1433.
I need to connect to the ssh server machine using ssh root@100.101.102.103 moreover I need to forward 192.168.0.100:1433 to my local machine. This is the simple solution:
ssh -L 1433:192.168.0.100:1433 root@100.101.102.103 -N

The pattern is "ssh -L myPort:host:hostPort". -N is useful for just forwarding ports and nothing else.

The connections could be same as below map:


my machine ssh server MS-SQLServer
127.0.0.1 <===> 100.101.102.103 <===> 192.168.0.100

Note:
-Install OpenSSH if you need to provide SSH over Windows servers.
-To access ports under 1024 you should have root privilege.

Saturday, September 18, 2010

MuleSoft's Tcat, an Enterprise view to the Tomcat

Apache Tomcat is the most convenient and reliable tool that I've ever seen. Here are Tomcat's power points that I've founded during my experiences:

-Tomcat works cross platforms. Just by copying from a machine to another one it serves applications. For example from a Linux to a Windows just a copy is enough.

-Tomcat is fast to deploy and easy to use. In most cases you just need to copy the WAR and forget about later configurations.

-Tomcat doesn't need as much resources as others.

While Tomcat doesn't claim to provide hosting services for the enterprise level of business we developers used to overused it.
It is long time that market is expecting such as plugin or solution that give Tomcat the capabilities which developer and administrators need.

Now MuleSoft made it. I just downloaded and try TCat server of Mulesoft. The amasing thing is that it remained a Tomcat. The major different is that they developed and deployed an application that is hosted within the TCat. It called "Console". TCat's console gives some missed features such as "Profile". I used to have different Tomcats on my machine depends on the projects. I work on Tele-communication and monitoring solutions using SNMP, TR-069 and JMX. In In another team I just develop web-based business applications. So I have to separate Tomcats regarding to different natures of companies and purposes. Now TCat profiles solved this problem. Also TCat has provided a repository that helps you to keep and manage your WARs. TCat has an embedded JRE! Also it is possible to use your pre-installed JRE. Anyway this gives TCat more enterprise shape!
I prefer to use an enterprise Apache Tomcat rather an IBM Webspher. :)

Monday, September 13, 2010

When Tomcat looks different cross Linux flavors

Some days ago at the time I was working on different Linux flavors to setup a uniform solution, I have notified that there are many differences in Tomcat installed files and configuration even when you have same Tomcat version. The mission was setting up a centralized monitoring system that makes user enable to track what is happening within Tomcat and its applications on different Linux machines such as CentOS, FreeBSD and Ubuntu. I was developed the solution in my notebook that uses Ubuntu 9.04, Sun Java 1.6 and Tomcat 6.0. Also I qualified the solution on an Ubuntu server edition. The tests passed perfectly so I tried to migrate it to FreeBSD and CentOS. The system administrator was worry about any major modification on his servers configuration that both were pretty busy. On both FreeBSD and CentOS I had different OS, Java and Tomcat. FreeBSD 7.2 had its own Java runtime and JDK that called Diablo. It used a Tomcat version 5.5. The CentOs machine had an OpenJDK Java edition. Also It uses Tomcat 5.5.
Differences between Sun JDK, OpenJDK and Diablo didn't make any trouble in using JMX. The biggest incompatibility was different installations of the same version of Tomcat that was installed in different ways by CentOS and FreeBSD package managers!
The administrators has been installed a same version of Tomcat using package managers. It seems each package manager installs a customized version of Tomcat that is more respects to the OS concerns rather than development. In CentOS I didn't find "catalina.sh" that is a well known startup routine.
Any way JMX listener didn't work and I guessed that could be caused by different Java editions. Fortunately the administrator rejected my suggestion for using Sun Java on both machine. So I tried other ways.
In latest try I just copied my Tomcat to FreeBSD and CentOS both and it worked like a charm!
Different flavors use their own package managers that install just customized versions of the packages. While any Java developer needs to produce more compatible solutions that are more usable with minor differences cross platforms. Compatibility and scalability are the most advantages of Java over other solutions. This is why we use Java. Every Java developer prefer to keep compatibility over customization.
As a suggestion just use a more secured copy of your own Tomcat over all target servers rather than muted and morphed versions that installed by package managers.
Here is a step by step tutorial of what I have done....

Saturday, July 3, 2010

"In Iran a large scale software project implementation is just like somehow to completing a puzzle that you don't have all pieces. The map is out of date. Moreover you have to use some pieces of other different puzzles to cover some areas of your puzzle with them. Some how there are missing pieces that you have to develop them. Finely you have to integrate them all."
I've just posted an article about Software Development in Iran.

Saturday, June 12, 2010

Object Orientation in Javascript

This could wired for whom used to program using Javascript that they can create objects. I didn't know till some while ago. Here is an example.

Saturday, June 5, 2010

An Opportunity for Investment

I just wrote an article for datispars.com. "How to be a successful investor in software development field" is an article that talks about software development opportunities such as outsourcing. The article tries to explain easy ways of develop better applications using better approaches, regardless of business restrictions and technologies.

Saturday, May 8, 2010

Using A More Specific Programming Language

Whichever programming language you are using in a software application development for, be sure to choose the proper language that exactly fits requirements. This needs to be familiar a bit more with some other technologies and programming languages such as domain specific languages.
Migrating from a multi purpose programming language such as Java to a domain specific programming language such as Groovy and Grails is just like choosing a more specific wrench rather than a general purpose. A handy man who knows just hammer then will recognize every problem as a nail. The same as this story may occur in software development projects too. The developer which uses only Java or .Net as a general purpose technology may loose a lot of resources to make an adoption between the solution and the problem rather than the one who uses a domain specific programming language such as Scala and Groovy.
I think being familiar with other tools, frameworks and technologies give us the opportunity to choose the better solution in many situations. In the case I experienced learning curve of being familiar with Groovy and Grails was pretty shorter than starting a Java project even based on basic configuration that I have provided before. The concept of “convention over configuration” boosts software projects. You don't need to build everything from scratch. The framework prepares a customized and standard basic configuration that is pretty well formed. A Groovy domain class itself describes the business more clearly and more abstracted rather than Java POJOs. Moreover built in naming convention that respected by Grails makes a terminology between developers.
All together the developers that uses DSLs are able to develop relevantly with a smaller knowledge base and terminology. In addition learning of Groovy and Grails is based on samples that called “Learning by Example” and Learning by example is a smooth way that we learned it when we were child.

Friday, April 23, 2010

Using Clear Source Java Codes In A Groovy/Grails Application

Groovy/Grails boosts the development process in many ways. The convention over configuration approach helps the developer to develop as fast as possible. Groovy and Grails talk in a much higher level language over than Java.

Something just like the creation of Java it self, is happening again. Software market place needs to talking in a more higher level languages rather than Java I think. Business on demand pressures the market to create and use more productive tools and technologies.

However, I as a developer need to write some parts of the applications using a little bit lower level language in many situations. Moreover, the huge amounts of developed and ready to use Java libraries such as source codes, jar files, and open source solutions warn me to prevent of reinventing the wheel. So, I align my approach with using the Groovy and Grails and Java classes in source code together just to boost the development and walk with bigger steps; Furthermore, I keep using of Java libraries whenever I need.

There are many situations that you need to use Java in a Grails application. This is more than using just JVM and importing jar libraries. I mean I prefer to user clear Java source code in the Grails application to have respect for the scripting concept of Groovy even in Java classes.

Here we will use a particular Java source code in a Grails application.
Assume below particular Java class:

package com.amir.util;

class HelloWorld {

public static String getMessage() {
return "Hello World!";
}

public static String getMessageFor(String str) {
return "Hello "+ str +"!";
}
}

I am going to use HelloWorld.java instead of HelloWorld.class. Then I will be able to modify it whenever I need and Grails will compile during each "grails run-app".

To create HelloWorld Grails application you first need to give the grails a create-app command:

$> grails create-app helloworld

This will create a new directory inside the current one that contains the project. You should now navigate to this directory in terminal:

$> cd helloworld/src/java

Now you need to create the packging folders:

$> mkdir com
$> mkdir com/amir
$> mkdir com/amir/util

The Java class should be find in this path:

helloworld/src/java/com/amir/util/HelloWorld.java

Now come back to the helloworld folder which is the root folder of the application and create a hello controller:

$> grails create-controller hello

This will create a new controller in the grails-app/controllers directory called HelloController.groovy. You need to modify it to call the Java HelloWorld class.

$> gedit grails-app/controllers/HelloController.groovy

And make it as below:

class HelloController {
def world = {
render HelloWorld.getMessage() + " , "
render HelloWorld.getMessageFor(" Amir ")
}
}

Now start-up the container with run-app command:

$> grails run-app

This will start-up a server on port 8080 and you should now be able to access your application with the URL: http://localhost:8080/helloworld

Job done! You used a clear source Java class instead of the compiled one in a Grails application. Everything is just scripted!

Monday, April 19, 2010

Building A Blog Using 7 Simple Steps In Grails

Building a regular web application using Grails should be the minimalistic activity that any Java developer may involved in. Just follow below 7 steps to develop your first Grails blog.

1. To create an application using Grails we should run a "grails create-app applicationName" command:

$> grails create-app com.datispars.blog

2. Change directory to the currently built directory of the application:

$> cd com.datispars.blog

3. At this point we create domain classes. Domain classes will consist the logic part of MVC model:

$> grails create-domain-class com.datispars.blog.Content
$> grails create-domain-class com.datispars.blog.Comment
$> grails create-domain-class com.datispars.blog.Author

4. To put some business policy into the model area we should modify domain classes

$> gedit grails-app/domain/com/datispars/blog/Content.groovy

You should modify Content class as below:

package com.datispars.blog

class Content {
String title
String body
Category category
Author author
static hasMany=[comments:Comment]
static constraints = {
title(blank:false, nullable: false, size:3..80)
body(blank:false, nullable:false,size:3..500)
author(nullable:false)
}
}
.....

$> gedit grails-app/domain/com/datispars/blog/Author.groovy

package com.datispars.blog

class Author {
String name
String email
String webpage
static constraints = {
name (blank:false, nullable:false, size:3..30,
matches:"[a-zA-Z1-9_]+")
email (email:true)
webpage (url:true)
}

String toString(){
return name;
}
}
....

$> gedit grails-app/domain/com/datispars/blog/Comment.groovy

package com.datispars.blog

class Comment {
static belongsTo=[content:Content]
Author author;
String comment
static constraints = {
comment (blank:false, nullable: false, size:5..200)
author (nullable: true) // Comments are allowed without a author
}

String toString(){
if (comment.size()>20){
return comment.substring(0,12);
} else
return comment;
}
}

5. To create the Controller part of MVC based on the domain classes you should run below commands:

$> grails generate-controller com.datispars.blog.Content
$> grails generate-controller com.datispars.blog.Author
$> grails generate-controller com.datispars.blog.Comment

6. Now you need to modify three controller that you have built to use Scaffold functionality of Grails:

$> gedit grails-app/controllers/com/datispars/
blog/ContentController.groovy

Scaffolding allows you to auto-generate a whole application for a given domain class. The simplest way to get started with scaffolding is to enable scaffolding via the "scaffold" property. So add the commented line (def scaffold = true) in the start part of the ContentController.groovy class:

package com.datispars.blog

class ContentController {

def scaffold = true // The only modification
// in the controller you should do...

static allowedMethods = [save: "POST", update: "POST",
delete: "POST"]

def index = {
redirect(action: "list", params: params)
}
....


Do just the same operatoion for AuthorController.groovy and CommentController.groovy (Same as 12 step) .

7. Run the application:

$> grails run-app

After running the server you should see below response in the terminal:

Server running. Browse to http://localhost:8080/com.datispars.blog

So, you browse http://localhost:8080/com.datispars.blog

Grails Over Rails

After a few research around rapid application development tools and frameworks, I started to try Groovy and Grails. This just happened after a couple of weeks using of Ruby on Rails. Ruby on Rails has excited me when I knew Groovy and Grails. I have had some difficulties with Ruby on Rails at initiating days that I didn't touch them again during using of Groovy and Grails.

At first I needed to install Grails. This was as much easy as you imagine. Later, I have explained how I installed Ruby on Rails on Ubuntu Linux in the previous article in current web log that shows rails installation needs some struggles to the end of deployment. But, Grails installation is just like a piece of cake in my experience. There is a binary version that is available as a zipped file that just needs to download and extract it. Then you should set two environmental variable. I am using Grails on Ubuntu Linux. So I had to fix a stupid bug at first try. In fact, Groovy and Grails are more indicated for Java developers because they can leverage their knowledge on Java platform and they can also turn to Java whenever they feel so, since Groovy runs on top of JVM. After all you have Spring and Hibernate ready available. Grails is very easy to get started with for Java developers. Also, You'll be able to continue using the same libraries that you used to (among many other things). The wide and enriched Java libraries are mostly accessible to your Grails application. All the code you developed and bug fixed routines are reusable in the Groovy applications. So you wont repeat all you have done.

I think Grails follows Rails track but in a more organized way. The main different is Java compatibility of Grails I think. If you try to deploy a Rails application on a JEE application server you will find that there is not a straight forward procedure at all. I found that setting up the environment to develop Rails application needs gathering some very dependent components that you have to arrange them just like a puzzle and I don't know why they did not put them all in the same package!. In the opposite, Grails gives the same functionality by a more consistent and integrated solution. For making a war of the Grails application that you developed, this is enough to give it a "grails war" in the root folder of the application.

Furthermore I found Groovy scripts meaningful and more conceptual. This may be just because the background I had from Java. I know Ruby is so abstracted and sharp but I used to explain exactly what I need, even by using a bit more symbols and reserve words. I cant feel the Japanese culture of Ruby short hands!

Here is a piece of Groovy code:

class User {
String name
String email
String webpage
static constraints = {
name (blank:false, nullable:false, size:3..30, matches:"[a-zA-Z1-9_]+")
email (email:true)
webpage (url:true)
}

String toString(){
return name;
}
}

I have never seen any implementation of the validation routines as much simplified as Groovy codes. The "constraints" block has described validation rules clearly. Email and webpage validations are brilliant:

email (email:true)
webpage (url:true)

I think Grails accelerates the outlet of the development process for the everyone who needs to build Java compatible web applications using new generation of RAD tools and scripts.

During working with Grail I've used this article from Lars Vogel. This could be an easy tutorial that shows how Grails works.

Sunday, April 4, 2010

Intstall "Ruby on Rails" on Ubuntu 9.04 and Dont Repeat Yourself Anymore

My new position during initiating days of the new Iranian's year just started by a weird project that outsourced by an over seas company to an Iranian one.

I have cooperated in some software projects with different technologies such as Java, .Net and Delphi. Indeed, the latest one is a Ruby on Rails web based project that I have selected by the boss as the technology specialist and the senior developer to find out the way of using this unfamiliar technology. The project requirements and specifications ride us toward cutting edge technologies. For the first day searching and surfing through technical pages have filled my working time.

I remember some months ago, a friend of mine tried to hire some Ruby on Rails developers in Tehran. He didn't succeed. This is why I feel so lucky. Ruby on Rails is designed based on the “Convention over Configuration” idea, and the rapid development principle of “Don't Repeat Yourself(DRY)”. I have found its agile motivations so practical. I as a web developer have wast a vast of time to implement repeated and simple parts of the applications such as database connectivity and configurations. Ruby on Rails reduces this times by emphasizing CoC and DRY concepts.

Ruby on Rails is often installed using RubyGems, a package manager which is included with Ruby. Many Linux flavors also support installation of Rails and its dependencies through their native package management system.
I tried to deploy it on Ubuntu 9.04. It wasn't easy and straight forward in my case. I tried a couple of times.

I found that I have to install ruby-full and ruby-elips packages first. Next, its time to install Ruby and Rails. Leave RubyGems for the latest installation. After all you need an IDE. RadRails is an Eclipse based Ruby on Rails IDE that facilitating the developments using its nice and fast tools and services.

Here are all commands in detail:
First remove all installed Ruby, Rails, Gems and everything you will install in next steps. Any remain parts may make some troubles for this process.

Make sure your repository is updated:
sudo apt-get update

Install rails by below command:
sudo apt-get install ruby ri rdoc irb libopenssl-ruby ruby-dev

Download RubyGem.

Extract it and then install Gem by following commands:
tar xvzf rubygems-1.3.5.tgz
cd rubygems-1.3.5
sudo ruby setup.rb

You don't need to the downloaded file and extracted folder any more. So remove it:
rm -r rubygems-1.3.5 rubygems-1.3.5.tgz

Now, create a set of simlinks:
sudo ln -s /usr/bin/gem1.8 /usr/local/bin/gem
sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby
sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc
sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri
sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb

Make sure ruby-dev installed:
sudo apt-get install ruby-dev

Next, install the Rails:
sudo gem install rails

You need Mongrel as server:
sudo gem install mongrel passenger capistrano


Setup databases (sqlite3 and MySQL):
sudo apt-get install sqlite3 swig libsqlite3-ruby libsqlite3-dev
sudo gem install sqlite3-ruby
sudo apt-get install mysql-client libmysqlclient15-dev
sudo gem install mysql

Test what have you done:
rails test_app

Test the application with MySQL support:
rails test-app -d mysql

Run the application:
cd test_app
script/server

Saturday, February 27, 2010

Parsing Huge Text Files Using Java and JSapar

Last week a friend of mine and I decided to parse a huge size text file that consists some reports of legacy devices. After few times trying we got that, opening and parsing huge text files in Java is very time and resource consuming. We started with a 35MB log file. We have never worked with such a huge size text files. So we tried to find the relevant solution. Indeed, Java is not the best solution for this kind of problems. I believe Python or Perl could perform this requirement by a higher performance. However regards to later developments and project requirements we decided to use Java. After some searching through web we found a brilliant tool. Tigris.org has some valueable open source projects. JSapar is one of them. JSapar is a Java library providing a schema based parser/producer of CSV (Comma Separated Values) and flat files. The goal of this project is to create a java library that contains a parser of flat files and csv files.
The file imports in to an object oriented model that we called it telegrams. The parser produces a Document class, representing the content of the file, or you can choose to receive events for each line that has been successfully parsed. Tigris claims that JSapar can handle huge files without loading everything into memory.
The library is simple to use and possible to extend. Our log file consists thousands of lines just the same as below sample line:

948853 : 47 E6 18 FF 04 CD 0B 1D B1 C1 D1 1E ;

This is a telegram. First part is row number (948853) and next bytes contains a message. This two part are separated by a ":". At first sight it seems it is a straight forward procedure, however, it is not as much easy as it looks. Millions of these lines makes a real slow running and unstable application if you use standard java scaner or parsers. First we defined a schema for csv files:

<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://jsapar.tigris.org/JSaParSchema/1.0">

<csvschema lineseparator="\n">

<line occurs="*" linetype="Telegram" cellseparator=":">

<cell name="Row No" />

<cell name="Body"/>

</line>

</csvschema>

</schema>

Then we just used a simple java code to read a 40MB text file into memory in less than 10 seconds.

public final void loadTelegrams() throws SchemaException, IOException, JSaParException {
Document
telegrams;
Reader schemaReader = new FileReader("schema/schema.xml");
Xml2SchemaBuilder xmlBuilder = new Xml2SchemaBuilder();
Reader fileReader = new FileReader("repo/dat.txt");
Parser parser = new Parser(xmlBuilder.build(schemaReader));
telegrams = parser.build(fileReader);
fileReader.close();
}

Using below command we moves whole of the file cell by cell so quickly.

telegrams.getLine(i).getCell(j).getStringValue()
;

Tuesday, February 23, 2010

OFBiz, The Apache Open for Business

I am writing this to introduce the next generation of web based business applications that I am developing. As you know I used to develop a small J2EE based application that I called it Easy Tracking. Easy Tracking consists inventory, orders and sales applications. Also it has some small features and a rich Ajax based multi-language client. This is more than 4 years that my customers use it as a rental software and data hosting service on my servers. I have gained a lot of valuable experience in this kind of business service providing among the years.
Now, different requests of individual customers makes me interested to use an open source professional business solution. During last months I reviewed a vast of ERP solutions and finely I have decided to use OFBiz. OFBiz is an Apache product; moreover, I reviewed its architecture and technologies and found them all matured and practical. Actually OFBiz is an open source framework to help developers in creating high quality business solutions. Development and customization both are available using straight forward procedures. However, OFBiz needs a mass of modification and development to be adapted with most of Iranian business companies requirements. In addition, it doesn't have a Farsi face yet.
After a while I will need a patient rich customer to let me implement his business requirements by the solution and a holding software company who needs to have a localized, reliable and ready to use product.

Wednesday, February 17, 2010

Distributed Application Debugging Tips

Every distributed application works based on a collaborative environment that consists of services and method calls. Moreover, in a distributed environment almost services are hosted on different platforms and far machines that talks through network. Distribution makes some difficulties in the debugging of this kind of applications.

During the development of a J2EE based core banking software, I as a member of the team have had many struggled challenges for debugging. The solution uses some far, distributed components through network that work together using message queuing following SOA disciplines.

DB2 Database, IBM MQ, IBM Websphere and Swing reach client are talking together using a vast of message sending and receiving. Also architecture uses Mule ESB, Spring, JMS and a vast of configuration files. We have found and fixed most of bugs using below simple techniques.

Use a Map. Just draw a symbolic map of the environment that causes the problem. Highlight servers, clients, firewalls, routers, with their specific IP and ports of each one that used in the scenario. May be an IP, a port or a firewall policy has been changed and the problem has arisen. Using Ping and Telnet be sure that service listeners are available at least.
If the service was working and a problem has arisen eventually; then, configuration changing is the most error prone factor.

Interpret Log Files. Review log files and exception messages carefully. Almost every raised exception message points to the problem exactly. In the other hand, logging is not a centralized activity in a layered distributed application. Indeed, each layer or component may logs its exceptions separately. So check them all. Check log files just like a detector and use your imagination to guess the problem.

Go Deeper. Adjust logging level to the proper value to let logger catch more detail messages.

Set Break Points to Watch. Set some break points to watch out what is happening during run time. Be sure messages send and receive by end points correctly. Then, step toward inner layers to find out what happened.

Use Replacements. Replace the component you suspected in with the correct one to find out if they are working probably. For example, you have another available application server, message queue or database use them regard the situation.

Check Configuration Files. Be sure the build routines has done their tasks perfectly. Maven and Ant write values of variables in the compiled version of configuration files. Such as context.xml, web.xml and other xml files. Lack of privilege or wrong configuration may prevent build process to finish its task completely. So check the compiled and built version of configuration files.

Be Patient. In a distributed system, method call doesn't perform as much fast as the standalone application. So check if timeout values are enough or not. Sometimes increasing timeout value is a key to solve the problem.

Do it Faster. Faster compiling and running on lighter machines is the an effective approach to test more situations rapidly. So use lighter application servers and databases. For example, use Tomcat over Websphere; Also, use MySQL rather than DB2 during development.

Wednesday, February 3, 2010

Routing multiple domains to an application using URL rewrite filter in JSP contatiners and JEE application servers

My client has assigned two separated domain address to a certain IP address. My client was going to map two domain addresses to an application which hosted on JBoss 4.2.2.
Each domain address should route requests to some certain pages of the application. I had to assign firstdomain.com and seconnddomain.com to the a certain application that I call it "app" here. My task was clear; indeed, I had to implement below mapping:
  • http://firstdomain.com .... http://1.2.3.4/app/first.html
  • http://seconddomain.com .... http://1.2.3.4/app/repo/second.html
As you can see, IP and application name are the common parts and inner paths goes different. I used UrlRewriteFilter that simulates Apache rewrite URL method in JSP containers and JEE app servers. It is just enough to follow the instruction. UrlRewriteFilter has a vast of samples but I have not found the proper rule that maps different url on a certain IP address. So, after some tries and errors; finally, below rule worked for me:
<rule>
<name>Changing Domain</name>
<condition name="host" operator="equal">firstdomain.com</condition>
<from>^/(.*)</from>
<to type="permanent-redirect">http://1.2.3.4/app/repo/second.html$1</to>
</rule>

This is just enough to add above rule into the urlrewrite.xml.
first.html is the default page. So you just need to handle the request which comes to the seconddomain.com. I have got my routing goals on Tomcat 6.0 and JBoss 4.2.2 with this simple URL re-writing.

Tuesday, February 2, 2010

Why I use Google Docs

Following extending an office automation, I worked on Apache Jackrabbit content repository some years ago. After it, I was looking for a free personal content repository to keep my documents, sample codes and commands over HTTP. Something just like a personal wiki, personal blog or a repository.
I preferred to use a free one that supported by the web giants such as Google and Yahoo. Finally, I have found it. My friends in Facebook suggested me to try Google Documents two weeks ago. Google Docs is exactly the tool I was looking for. Google means this is improving faster and wiser than others. Moreover, Google Documents is the most interesting tool that I found in the Google shelf. It works very fast; moreover, it is pretty much. Google Docs enables me to keep my valuable documents in a safety box on WWW. I shared some documents with the trusted friends of mine. Also, a friend and I are using Google Docs in an amazing way to keep synchronize together around an educational program. In addition, Google Docs, keeps all revisions on modifications. In fact, you can track the document growing life cycle. In my opinion every body should have his or her Google Documents account because of below reasons:
  • Your knowledge is always accessible.
  • You don't need to care about hard disk failures and viruses attack any more.
  • You can edit your documents as well as possible just using a browser.
  • Google Docs is a repository so, all changes are traceable.
  • Google Docs Sharing mechanism works pretty secured.
  • I don't care about currently 1024 MB capacity limitation. Google is growing always.
  • Good feeling of extending my memory using Google Docs features is another special kind of experience that I gain by using Google Documents. You will never loose it if you keep it by Google Documents.

Monday, February 1, 2010

Good points for putting breaks across Mule ESB classes

This is the second year that I work as a J2EE developer during development of a modern J2EE based core banking project. The architecture uses Spring and Mule ESB as its major components widely. Mule ESB (Enterprise Service Bus) allows us to connect the applications together as much simple as possible. The architecture uses Mule ESB as integration platform and service container. Mule ESB provides some easy to use methods for talking through JMS, HTTP, FTP and many more transfer protocols. Moreover, Mule ESB implemented a set of relevant mechanisms for message routing, message transformation and transaction management. However, debugging the started services which use Mule ESB functionalities is not an easy activity. Because there is a lot of layered listeners and complex network devices/services which make problems bolder. The framework we used is a well design art work of my friend, Ara Abrahamian and it works like a Mercedes Benz in most of areas. But, the modules added by developers almost have some problems. I noticed most of problems that we raise, has the origin in the wrong configuration files such as muleesb-config.xml and Spring's context.xml. Also, below classes could be a good place to start debugging:
  • JmsMessageDispatcher.java
  • JmsMessageConsumerImpl.java
JmsMessageDispatcher is the the most outer place just before sending message. Also, JmsMessageConsumerImpl is the first JMS message receiver.

In my experience debugging this two classes is an easy way to find what happened to the code or underlying services malfunctions.

Sunday, January 31, 2010

JBoss container managed basic authentication

These days I am working on an ACS (Automatic Configuration System) which runs on a JBoss app server. So, yesterday I configured the server for cotnainer-managed authentication. Here is a straight forward tutorial to demonstrate how to secure your applications which hosted in JBoss by BASIC authentication methods. Container-managed authentication methods control how a user's credentials are verified when a web app's protected resource is accessed. This is a straight forward tutorial that shows how to configure JBoss application server to protect resources by a role based simple mechanism. By receiving any request to the protected resources JBoss asks web browser for a username and password. With this authentication method, all passwords are sent across the network in base64-encoded text. This could be fine to review the commented parts of JBoss configurations to find some more facilities If you need.

First step, web.xml modifications
Find the web.xml in the WEB-INF folder of your deployed ear application. This could be find in somewhere like this depends on the OS you use. Add below lines at the bottom of web.xml just before tag.

<security-constraint>
<web-resource-collection>
<web-resource-name>ossmanager</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>ossmanager</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>OSS Security</realm-name>
</login-config>

<security-role>
<description>ossmanager is the role required to access under oss</description>
<role-name>ossmanager</role-name>
</security-role>

<security-role>
<description>cpeowner is the role required to access under cpeowner</description>
<role-name>cpeowner</role-name>
</security-role>

Second step, joss-web.xml modifications
Right besides web.xml in the WEB-INF folder you can find jboss-web.xml to add below lines in. Note, datak-oss is the context of the application and acs-security-policy is the optional name that you call your security policy using it.

<jboss-web>
<context-root>datak-oss</context-root>
<security-domain>java:/jaas/acs-security-policy</security-domain>
</jboss-web>

Third step, login-config.xml modifications
Find login-config.xml in server/default/conf folder and add below lines at the proper place.

<application-policy name ="acs-security-policy" >
<authentication>
<login-module code="org.jboss.security.auth.spi.UsersRolesLoginModule" flag="required">
<module-option name="usersProperties">acs-users.properties</module-option>
<module-option name="rolesProperties">
acs-roles.properties</module-option>
</login-module>
</authentication>
</application-policy>

Create acs-users.properties and acs-roles.properties text files into the WEB-INF/classes/ the same as below:

acs-users.properties:
amir=takeiteasy

kamran=mamooshi

acs-roles.properties:
amir=ossmanager,cpeowner
kamran=cpeowner

Exclude a URL by adding a blank security-constraint
I added this part two days later. Because the project manager asked me to exclude a certain URL (/cpe) from the secured zone. I have performed it just adding a new without role constraint. Below code added an excluded path to the secured zone, which every anonymous user can access it. Add below security-constraint to the web.xml the same as security-constraint that you added before.

<security-constraint>
<web-resource-collection>
<web-resource-name>CPE</web-resource-name>
<url-pattern>/cpe/*</url-pattern>
</web-resource-collection>
</security-constraint>

Saturday, January 30, 2010

Make programming a bit easier in Linux

Restarting services or killing them are the must repeated activities that I have to do during current project. Running maven commands and deleting '.svn' folders are so easy but those take time too. By the other hand all of them should accomplish in the operating system shell. Sometimes more than ten times per a day I have ran this commands. Moreover, everyone knew that it could be much easier to develop simple Linux bash scripts or define Aliases which contains more usage commands. Then, I developed below scripts and define some aliases to make it easier.

killj.sh is a bash script which kills my Java processes that consists 'javaagent' keyword. The application needs 'javaagent' as VM parameter; so, I am looking for it to kill the right process.
#!/bin/bash
for i in `ps -ef | grep javaagent | cut -c10-14`
do
/bin/kill $i
echo " $i has killed."
done
if [ $(ps ax | grep -c javaagent) -gt 1 ] ; then
echo "NOTE: There are more alive java process yet!!!"
else
echo "DONE"
fi

rmsvn.sh removes .svn folders recursively.
#!/bin/bash
echo
echo "Warning!!!"
echo "CHECK BELOW PATH. This will remove all .svn folders recursively!!!"
pwd
read -p "Are you sure (yes/no)? "
[ "$REPLY" == "yes" ] && rm -rf `find . -type d -name .svn` && echo "all .svn folders have been removed!!!"

Also I added below lines at the end of .bashrc file:
alias lt='ls -alt | head -20'
alias ld="ls -al -d * | egrep '^d'"
alias c="mvn clean"
alias ci="mvn -Pdev -DskipTests clean install"
alias i="mvn -Pdev -DskipTests install"
alias jkill="/bin/killj.sh"
alias rmsvn="/bin/rmsvn.sh"
alias go="cd /home/amir/projects/company/jcb2"
alias dpiproxy="sudo cp -f /etc/tor/torrc.dpi /etc/tor/torrc"
alias noproxy="sudo cp -f /etc/tor/torrc.noproxy /etc/tor/torrc"
alias tor="sudo /etc/init.d/tor restart"

Now, compiling, cleaning and killing processes are much easier...

Friday, January 29, 2010

Port mapping in Linux in Ubuntu and CentOS

If you used to setup web servers under windows easily, you will find Linux doesn't show itself bared on the network. Indeed, Linux has its own secured solutions to let your web servers listen to the 80 port. In fact, you can not use the ports under 1024 for the individual users. Because they belongs to the super user. Moreover, its recommended to run web servers with a limited user. So, the best way to run HTTP listeners on 80 port is to map upper than 1024 ports 80 port.
Last week I worked with a JBoss application server which has ran on 8080 port. In addition, the client needed to run it on 80 port, without any modification on JBoss settings. I make it so easily by running 3 iptable commands which I found in some tutorials on the web.

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

sudo iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 8080

NOTE: if you dont put -o lo option for the last command, all outgoing requestes will rout to the 8080 port on the localhost.

Finally you should save new rules into disk. below command persists changes.

sudo iptables-save > iptable.rules

Moreover you need to make sure rules will loaded in the next start up. So, add below line into /etc/network/interfaces after iface command:

pre-up iptables-restore < /etc/iptable.rules


If you are using CentOS then it is enough to run below command to persist iptables rules:

/sbin/service iptables save

Restart the network connection and enjoy it.