Tuesday, September 14, 2010

Running JBOSS Programmatically from java

Please find below code to run JBOSS Programmatically from JAVA. Please include jboss jars in your classpath.

I tested the following code for JBOSS-6.0.0.M2


package com.sudipta.jboss.conf;

import org.jboss.Main;

public class JbossOperation {
public static Main ob;
static Class jbossMain;
static{
try {
jbossMain=Class.forName("org.jboss.Main");
ob = (Main)jbossMain.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

}

/**
* START JBOSS SERVER
* @return true if started successfully
*/
public static boolean startServer(){
boolean status=true;

String str[]={"-c","default"};
try {
ob.boot(str);

} catch (Exception e) {
e.printStackTrace();
return false;
}
return status;
}

/**
* STOP JBOSS SERVER
* @return true if started successfully
*/
public static boolean stopServer(){
boolean status=true;
try {
ob.shutdown();
} catch (Throwable e) {
e.printStackTrace();
}

return status;
}

/**
* Main method
* @param args
*/
public static void main(String args[]){
System.out.println("---------------------Strating the server------------------");
startServer();
System.out.println("---------------------Stoping the server------------------");
stopServer();
System.out.println("---------------------SERVER STOPPED------------------");
}
}



Tuesday, June 8, 2010

Java Program to List the Directories,SubDirectories and Files in them

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;


/**
* List the Directories, SubDirection and Files in them
* @author Sudipta Kumar Laha
*
*/
public class FileStructure {
/**
* Print Directories, SubDirection and Files in them
* @param str
*
*/
public void getFiles(String str){
System.out.println("[D]\t"+str);
File f=new File(str);
File ss[]=f.listFiles();


if(ss!=null){
Arrays.sort(ss, new sortStructure());
for(int i=0;i<ss.length;i++){
if(ss[i].isDirectory()){
String currDir=ss[i].getAbsolutePath();
System.out.println("Total File & Directory "+ss.length);
this.getFiles(currDir);
}else{
System.out.println("[F]\t"
+getPermissions(ss[i].canRead(),ss[i].canWrite(),ss[i].canExecute())+
"\t"+
ss[i].getName());
}
}
}else{
System.out.println("Unable to read");
}

}
/**
* Checks for the access in files
* @param canRead
* @param canWrite
* @param canExecute
* @return
*/
private String getPermissions(boolean canRead, boolean canWrite,
boolean canExecute) {
StringBuffer buff=new StringBuffer();
if(canRead)
buff.append("r");
else
buff.append("-");
if(canWrite)
buff.append("w");
else
buff.append("-");
if(canExecute)
buff.append("e");
else
buff.append("-");
return buff.toString();
}

/**
* Start of Program
* @param args
*/
public static void main(String args[]){
FileStructure obj=new FileStructure();
obj.getFiles(args[0]);

}

}


/**
* Class extends comparator to sort files and directories
* @author Sudipta Kumar Laha
*
*/
class sortStructure implements Comparator<File>{
@Override
public int compare(File o1, File o2) {
// TODO Auto-generated method stub
if(o1.isDirectory() && o2.isDirectory()){
return o1.getName().compareTo(o1.getName());
}else if(o1.isFile() && o2.isFile())
return o1.getName().compareTo(o1.getName());
else if(o1.isDirectory())
return 1;
else
return 0;
}
}



Saturday, May 8, 2010

Seraching in Arrays in JAVA

We can search in a array for a value using the method binarySearch of Arrays class.
The search result will give correct output if the array is sorted.
If the array is not sorted the result is unpredictable.
Note: For predictable result array must be sorted before searching an element.

The following code explains this behavior:


Integer[] intArr={2,5,3,4,1};
for(Integer s: intArr)
System.out.print(s+",");

System.out.print(System.lineSeparator());
System.out.print("Searching 5:"+Arrays.binarySearch(intArr,5)); // Will return -6 which is incorrect
System.out.print(System.lineSeparator());
System.out.print("Searching 2:"+Arrays.binarySearch(intArr,2)); // will return correct result 0, but not for every search

Arrays.sort(intArr); //Sorting the Array
System.out.print(System.lineSeparator());
for(Integer s: intArr)
System.out.print(s+",");
System.out.println(System.lineSeparator()+Arrays.binarySearch(intArr,2)); // Always gives the correct result


equals method in JAVA

equals method of Object class and String class

Consider the below code


myClass obj1=new myClass();
myClass obj2=new myClass();


String str1=new String("AAAA");
String str2=new String("AAAA");

System.out.println(obj1.equals(obj2));
System.out.println(str1.equals(str2));

OUTPUT:
false
true

In both the cases two objects were created but for Strings the result is true and for myClass object the result is false.
The reason for this is
In object class the equals method compares the references and returns true if the references are point to the same object.
But in String class the equals method is overridden to check the values of the string object so it returns true.

About Me