Wednesday, June 22, 2011

How to set jboss.server.log.dir in JBoss


Target audience: Beginners
Version: JBoss 4.2.x

By Default, JBoss writes the server log files to /jboss-4.2.0.GA/server/default/log location. We can change this location by specifying a system parameter jboss.server.log.dir .

For Example, if you want JBoss to write the server log file to “C:/Logs/JBoss/myapp”, then what you should do is to set a system property as this -Djboss.server.log.dir=C:/Logs/JBoss/myapp

As this property is used during bootstrap, this should be set when JVM starts. To do this, open “run.bat” file and set bellow entry. Remember to keep a space in front of %JAVA_OPTS%

set JAVA_OPTS=-Djboss.server.log.dir=C:/Logs/JBoss/myapp %JAVA_OPTS%
After setting this, start the JBoss server. you would see the log files would be created under “C:/Logs/JBoss/myapp

In the same way, you can set some other JBoss properties too. Some are given bellow

jboss.home.dir - The base directory of the jboss distribution - default: $JBOSS_HOME
jboss.home.url - The base url of the jboss distribution - default $JBOSS_HOME
jboss.lib.url - The url where the kernel jars exist - default: $jboss.home.url/lib
jboss.patch.url - A directory where patch jars exist - default: none
jboss.server.name - The configuration name of the server - default: default
jboss.server.base.dir - The directory where server configurations exist - default: $jboss.home.dir/server
jboss.server.base.url - The url where server configurations exist - default: $jboss.home.url/server
jboss.server.home.dir - The directory for the current configuration - default: $jboss.server.base.dir/$jboss.server.name
jboss.server.home.url - The url for the current configuration - default: $jboss.server.base.url/$jboss.server.name
jboss.server.temp.dir - The directory for temporary files - default: $jboss.server.home.dir/tmp
jboss.server.data.dir - The directory for data files - default: $jboss.server.home.dir/data
jboss.server.config.url - The url for configuration files - default: $jboss.server.home.url/conf
jboss.server.lib.url - The url for static jar files - default: $jboss.server.home.url/lib
jboss.server.log.dir - The directory where the server logs are written - default: $jboss.server.home.dir/log

Hope this is helpful for you.

Thursday, June 16, 2011

How to configure JBoss's log4j to log messages separately for different categories


Target audience: Beginners
Version: JBoss 4.2.x

We all know how to configure JBoss to log messages (DEBUG, INFO, WARN and ERROR) to a file. Let’s look at how to configure JBoss to log different categories to different files.

The configuration file can be found in [JBOSS]\server\default\conf\jboss-log4j.xml. Lets open the file. You will be able to see a default appender configure slimier to bellow

The above configuration to log the messages to a file ${jboss.server.log.dir}/server.log . And , we’ll sat that we have two categories are defined for logging the messages ${jboss.server.log.dir}/server.log

Currently all logs will be logged to "${jboss.server.log.dir}/server.log as we have configured in “FILE” appender.

Now we will configure the log messages of category “java.sql” to be logged in a different log file than the one already configured(“FILE”). Lest create another file appender configuration. We’ll name the appender as “FILE_SQL” and configure to log to ${jboss.server.log.dir}/server_sql.log

We have created the file appender and now we need to attach the category of which log messages should be logged to this file. The bellow configuration would do that.

After doing the above configuration,if you start the JBoss server and run your application, then all messages related to category “java.sql” will be logged in ${jboss.server.log.dir}/server_sql.log file.

Hope this note would be helpful for you.

Tuesday, June 7, 2011

Basics of Java Generics


Target audience: Beginners


Lets start with a simple example with normal java code

import java.util.ArrayList;
import java.util.List;

public class FirstExample {

public static void main(String[] args) {
List testList = new ArrayList();
testList.add(new Integer(6));

String string = (String) testList.get(0);
}
}

The above class compiles perfectly, but at run-time... Bang! Throws exception

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer
at com.learner.generics.FirstExample.main(FirstExample.java:24)

If we could caught this exception at compile time our application would be very safe at run-time. This is where Generics come to play.

Generics makes sure the classes are type safe at compile time when those classes are working with arbitrary objects. The generics are only used at compile time .The generic code that are written will be translated ( type erased ) into the normal code while compiling. So we’ll say it again... “ Generics makes sure the classes are type safe at compile time”

Now I rewrite the code with Generics.

import java.util.ArrayList;
import java.util.List;

public class FirstExample {

public static void main(String[] args) {
List<Integer> testList = new ArrayList<Integer> ();
testList.add(new Integer(6));

String string = (String)testList.get(0); //(1)
}
}

When I write the code (1), the compiler would shout at me; hey” can not cast from Integer to String”. So I can be more careful when I write code and I will be safe. Thanks to generics!

Generics are mostly used with Collections. Collections deal with objects which are java.lang.Object type. When we save any object to Collections it saves as java.lang.Object type and it returns as such. So a casting is needed when we get it out. Generics solve this casting issue. See the following example.

import java.util.ArrayList;
import java.util.List;

public class FirstExample {

public static void main(String[] args) {
List<Integer> testList = new ArrayList<Integer>();
testList.add(new Integer(6));

Integer intValue = testList.get(0);
}
}

Simple as that!. Now we’ll look into the types of Generics and how to define Generic classes.

Generics and Classes/Interfaces
When a class is defined with the parameter (or parameters) then it’s called as Parameterized class. It’s another name for generic class. The parameter is called as Type parameter. This is applicable for Interface too.

class CheckArrayList<E> extends ArrayList<E> {
// implementation goes here.
}


In then above code, CheckArrayList is parameterized with one parameter type ‘E’’ but it can have more than one parameter. Type parameter is an unqualified identifier that is used as a place holder .This will be replaced by the compiler with the specified Type when an object is created for the generic class. In bellow, the Type parameter E is replaced with Integer.

CheckArrayList <Integer> testList = new CheckArrayList<Integer> ();

When it comes in hierarchies of generic classes, one rule applies. A class or Type parameter can not have two different parameterization of the same class or interface at the same time. See the following example.

interface MyInterface<T> {

// Implementation goes here.
}

class Second<T> implements MyInterface<T> {

// Implementation goes here.
}

class Third<T> extends Second<Integer> implements MyInterface<String> {

// Implementation goes here.
}


Here, the compiler will give you an error you saying “The interface MyInterface cannot be implemented more than once with different arguments “.
The correct way would be

class Third<T> extends Second<String> implements MyInterface<String> {

// Implementation goes here.
}


Generics and Methods
Generics can be used in methods too. The generic type is declared after all the modifiers of the Class and right before the return type. See the following example

public <E> get(int index) {
// Implementation goes here.

return elementData[index];
}

The syntax is same as generic class. Two methods can not have same name and argument types if they have then the compiler will give an error.

Generics and Arrays
You can not create a array of generic type and arrays of type variable. The following code will generate a compile time error

Vector<Integer> vector [] = new Vector<Integer>[10];


A workaround is to construct an array of objects with a type-cast:

Vector<Integer> vector [] = (Vector<Integer>[]) new Object[10];

But this is not a good idea to use in this way

Raw Types and Type erasure
Now lets see about Raw types. What are Raw types?
Raw types are the one without its type parameters. MyInterface is a generic type and MyInterface is the raw type. Let’s say it again.... “Raw types are the one without its type parameters”

When a generic type is instantiated, the compiler translates those types by a technique called 'type erasure' which the compiler removes all information related to type parameters and type arguments within a class or method. In other words the compiler translates into normal java code (without Generics). Raw types are used for legacy codes that use non generic codes.

Generics and Exceptions
Generics can be used for exception in throws clause. But java doesn’t allow to be used in catch clauses. See the following example where the Generics is used in throws clause.

package com.learner.generics;

import java.io.IOException;

public class SecondExample<V> {
public void sample() throws V {
// Implementation goes here.
}

public static void main(String[] args) {

SecondExample<IOException> example = new SecondExample<IOException>();
try {
example.sample();
} catch (IOException e) {
e.printStackTrace();
}
}
}


As I stated above java doesn’t allow to be used in catch clauses. The below is not allowed

try {
//Implementation goes here.
} catch (MyException<Integer> e) {
//Implementation goes here.
} catch (MyException<String> e) {
//Implementation goes here.
}

Because the type is erased at the compile time, in other words, the generics code will be translated into normal code and the types will be removed. After the translation MyException and MyException will become MyException. Java does not allow catching same exception two times in one try bock. That’s the reason why java doesn’t allow Generics to be used in catch clauses

Thursday, June 2, 2011

How to list all files and folders in windows

Today i wanted a list of all files and folders for my deployment list. I was looking for a DOS command which would do that for me and found the bellow command would do so.

dir /s /b

Example, if i run this command in my directory “C:\Downloads\softwares” then it’ll list all files and folders


C:\Downloads\softwares>dir /s /b
C:\Downloads\softwares\browsers
C:\Downloads\softwares\texteditors
C:\Downloads\softwares\browsers\ChromeSetup.exe

Monday, May 23, 2011

Beware of contains() method of List implementations


Target audience: Beginners

Every day, we deal with java.util.List that contains value objects. Some people might have misunderstood the outcome of contains() method of List implementation.

Just look at the bellow code example

public class ContainsEx {

public static void main(String[] args) {

// Creating a list of Person objects
List list = new ArrayList();
Person p1 = new Person("One");
list.add(p1);
Person p2 = new Person("Two");
list.add(p2);
Person p3 = new Person("Three");
list.add(p3);

// One more object similar to p1
Person oneMoreP1 = new Person("One");

// Check whether the list contains "One"
System.out.println(list.contains(oneMoreP1));
}
}

class Person {

private String name;

Person(String name) {
this.name = name;
}
}


If we run ContainsEx then we might think the output would be true. But wrong!!! the output will be


false


Why is that?

When a List contain objects, if we invoke contains(obj) method of that List, then the contains method actually iterates though all its objects and then one by one it checks whether that object is equal to the one which is passed through contains(obj) method. How does it check the quality is by invoking the equals(obj) method of Object. So you have to define how the object should be equal to another by defining (overriding) its equals() method. For example, if we want the two Person objects should be treated same if their names are equal then what we need to do is just override the equals() method of Person class in that manner. See the bellow code


class Person {

private String name;

Person(String name) {
this.name = name;
}

// This is not properly implemented.This is just for explanation
public boolean equals(Object obj) {
Person other = (Person) obj;
return name.equals(other.name);
}

}


Now we have overridden the equals() of the Person class. Two person objects will be treated as equal if both has same name.

If we run ContainsEx now, the results will be

true