Thursday, November 29, 2012

AutoCommit enabled in MySQL DB

In MySQL default engine is set to "MyISAM". In  "MyISAM" engine, auto commit doesn't work. That means, you can't change commit time in application level. And, you can't roll back connections when needed when using "MyISAM" engine.

If you need to set auto commit enabled/ disabled in application level change MySQL DB engine to "INNODB" engine. In "INNODB" engine, you can roll back connection as needed.

CREATE TABLE Test1 (
  refNo varchar(10) default NULL,
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


CREATE TABLE Test1 (
  refNo varchar(10) default NULL,
) ENGINE=INNODB DEFAULT CHARSET=latin1;


Tuesday, November 27, 2012

Linux command hard disk space

Following command can be used to find hard disk space in Linux environment.

df -h

Wednesday, November 21, 2012

Copy data from one table to another table

You can copy data from one table to another table using following SQL command. This worked fine in informix database.

insert into TableA (field1, field2,field3)
select  field1, field2,field3 from TableB where added_on=today

Tuesday, November 20, 2012

Words matching in two FILES

Suppose you have to check two files with having same words in both files, you can use the following command in Linux.

fgrep -wf file1 file2 | wc -l

This will return word count in both file1 & file2.

For more details, man fgrep in Linux.

Java Trigger ( Quartz )

Quartz  is a library that can be used to implement scheduler in JAVA application.

This library supports for modify Triggering in many variations.
You have to implement your class implementing Job class as "TestJob.java".

TestJob.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class TestJob implements Job {
    @Override
    public void execute(JobExecutionContext jec) throws JobExecutionException {
        System.out.println("Hello Quartz!");
    }
}

This "Schedule.java" will be triggered by 5 seconds.


Schedule.java 

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class Schedule {

    public static void main(String[] args) throws Exception {
        JobDetail job = JobBuilder.newJob(TestJob.class).withIdentity("testJob", "group1").build();

        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("dummyTriggerName", "group1")
                .withSchedule(
                SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(5).repeatForever())
                .build();

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);

    }
}


And you can use Quartz as a cron job.

Cron.java

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class Cron 
{
    public static void main( String[] args ) throws Exception
    {
    JobDetail job = JobBuilder.newJob(TestJob.class)
.withIdentity("testJobName", "group1").build();

    Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();

    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
    scheduler.scheduleJob(job, trigger);

    }
}


Following libraries must be added to the projects.

  • quartz-2.1.6.jar
  • log4j-1.2.16.jar
  • c3p0-0.9.1.1.jar
  • slf4j-log4j12-1.6.1.jar
  • slf4j-api-1.6.1.jar


Split large TEXT file in JAVA

In some cases, large files has to be split into smaller ones for the fast processing. In my case, file contained about 10 million records. Processing 10 million is bit low than parallel processing smaller files.

Split.java can be used to create files with number of predefined lines.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Split {

    private final static String NEWLINE = System.getProperty("line.separator");

    public static void readFileData(String filename, int lines) throws IOException {
try {
   BufferedReader bufferedReader = new BufferedReader(new FileReader(
   filename));
   StringBuffer stringBuffer = new StringBuffer();

   String line;
   int i = 0;
   int counter = 1;
   while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append(NEWLINE);
i++;
if (i >= lines) {
   saveFile(stringBuffer, filename + counter);
   stringBuffer = new StringBuffer();
   i = 0;
   counter++;
}
   }
   bufferedReader.close();
} catch (IOException e) {
   throw new IOException("read file error " + filename);
}
    }

    private static void createFile(StringBuffer stringBuffer, String filename) {
String path = (new File("")).getAbsolutePath();
File file = new File(path + "/" + filename);
FileWriter output = null;
try {
   output = new FileWriter(file);
   output.write(stringBuffer.toString());
   System.out.println("file " + path + filename + " written");
} catch (IOException e) {
   e.printStackTrace();
} finally {

   try {
output.close();
   } catch (IOException e) {
   }
}
    }

    public static void main(String[] args) {

String fileName = "/usr/sujith/filename.txt"
int lines = 1000000;

try {
    readFileData(fileName, lines);
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
    }
}  

Monday, November 19, 2012

No. of lines in file Linux

Following command can be used to count number of lines in the file using Linux command.

cat fileName | wc -l

wc -l is used to count & list down number.

Call AXIS web service with security

AXIS web service uses .wsdd XML file to read username & passwordCallbackClass. Here, it shown sample .wsdd file.

client_deploy.wsdd

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>
<globalConfiguration >
<requestFlow >
<handler type="java:org.apache.ws.axis.security.WSDoAllSender" >
<parameter name="action" value="UsernameToken"/>
<parameter name="user" value="M_TICKET"/>
<parameter name="passwordType" value="PasswordText" />
<parameter name="passwordCallbackClass"
value="PWCallBackClass"/>
</handler>
</requestFlow >
</globalConfiguration >
</deployment>

Then you have to implement "PWCallBackClass" class as below.

PWCallBackClass .java


public class PWCallBackClass implements CallbackHandler {

     private static final byte[] key = {
   (byte)0x31, (byte)0xfd, (byte)0xcb, (byte)0xda,
   (byte)0xfb, (byte)0xcd, (byte)0x6b, (byte)0xa8,
   (byte)0xe6, (byte)0x19, (byte)0xa7, (byte)0xbf,
   (byte)0x51, (byte)0xf7, (byte)0xc7, (byte)0x3e,
   (byte)0x80, (byte)0xae, (byte)0x98, (byte)0x51,
   (byte)0xc8, (byte)0x51, (byte)0x34, (byte)0x04,
     };
public void handle(Callback[] callbacks)
   throws IOException, UnsupportedCallbackException {
   for (int i = 0; i < callbacks.length; i++) {
     if (callbacks[i] instanceof WSPasswordCallback) {
       WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
       /*
        * here call a function/method to lookup the password for
        * the given identifier (e.g. a user name or keystore alias)
        * e.g.: pc.setPassword(passStore.getPassword(pc.getIdentfifier))
        * for testing we supply a fixed name/fixed key here.
        */
       if (pc.getUsage() == WSPasswordCallback.KEY_NAME) {
         pc.setKey(key);
       }
       else {
         pc.setPassword("Hsy84#ep$8&@v");
       }
     } else {
       throw new UnsupportedCallbackException(
         callbacks[i], "Unrecognized Callback");
     }
   }
}

Following code can be used to call web service with security implemented with AXIS.

TestWS.java

import java.rmi.Remote;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.AxisFault;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.client.Stub;
import org.apache.axis.configuration.FileProvider;

import com.comverse_in.prepaid.ccws.ServiceLocator;
import com.comverse_in.prepaid.ccws.ServiceSoap;
import com.comverse_in.prepaid.ccws.ServiceSoapProxy;
import com.comverse_in.prepaid.ccws.ServiceSoapStub;

import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.apache.ws.security.message.token.UsernameToken;

public class TestWS {

public static void main(String args[]) throws ServiceException, RemoteException{

EngineConfiguration config = new FileProvider("client_deploy.wsdd");
ServiceLocator locator = new ServiceLocator(config);
Remote remote = locator.getPort(ServiceSoap.class);
Stub axisPort = (Stub)remote;

axisPort._setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
axisPort._setProperty(UsernameToken.PASSWORD_TYPE, WSConstants.PASSWORD_TEXT);
axisPort._setProperty(WSHandlerConstants.USER, "sujith");

axisPort._setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, "PWCallBackClass");

ServiceSoap service = (ServiceSoapStub)axisPort;
System.out.println("Calling service...");
int ver = service.getVersionId();
System.out.println("get version id service returned " + ver);
}
}

Reference :- http://ws.apache.org/wss4j/package.html

Following jars needed to run web service client than AXIS jars.

  1. opensaml-1.0.1.jar
  2. wss4j-2.1.jar
  3. ws-security-5.1.0-M1.jar
  4. xmlsec-1.3.0.jar