Tuesday, December 25, 2012

prngd can be resolve with apr package with AIX v5.2

Ever wonder how AIX is going to look like? Thank GOD I was given a chance to interact with this alien. My task is to install subversion package into AIX, I was lucky that this alien know rpm, otherwise I have no idea how to continue the installation without using apt-get or yum. The installation process never get easy, I have to resolve the dependencies one by one. rpm really help me out in this situation where this command will list out the dependencies required when the particular dependency package isn't there.
At one point when I was installing apr package (which is one of the Subversion dependency), it prompt me an error complaining that prngd wasn't install.
# rpm -ivh apr-1.4.6-1.aix5.1.ppc.rpm
error: failed dependencies:
        prngd is needed by apr-1.4.6-1
        AIX-rpm < 5.2.0.0 is needed by apr-1.4.6-1
The documentation has stated clearly that prngd is required only in AIX v5.1 but mine is AIX v7.1, why this happen? Googling around couldn't find any solution, until I found the AIX open source packages, just download the latest version of apr, which is apr-1.4.6-1.aix5.2.ppc.rpm and install it to give it a try. And the solution is:

It works!

Tuesday, December 11, 2012

Don't extract gz file with tar command.

Usually when I unzip tar.gz file, I use this command: tar -zxvf file1.tar.gzBut this time is a gz file, tar command doesn't work that way. I tried gunzip, the file is extracted but the content gone weird. Then I try gzip -d file1.log.gz, check on the content, it looks perfect.

Monday, December 10, 2012

Spring-security 3.x doesn't work well with spring 2.x

Now only I spot that in my pom.xml file contain spring.jar is version 2 whereas the rest of the Spring component are version 3 and above. No wonder I have this error shown in the Eclipse console:

Caused by: java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.getLocalName(Lorg/w3c/dom/Node;)Ljava/lang/String;
 at org.springframework.security.config.SecurityNamespaceHandler.parse(SecurityNamespaceHandler.java:71)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1297)
 at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1287)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
 at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:92)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:507)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:398)
 ... 43 more

Found a related problem from StackOverflow. It is either upgrade spring.jar version or remove it.

Sunday, December 9, 2012

Don't flush a session after an exception occurs

Few days ago, the team and I was running a performance test using JMeter, and found this error happen during the test. I was seeing this error from the log:

ERROR 2012-12-06 16:00:43,721 [ModuleABoImpl:2626]- [Unable to Finish Calculation for record ID 11223344]
    org.hibernate.AssertionFailure: null id in org.huahsin.moduleA.model.PojoA entry (don't flush the Session after an exception occurs)

...
...
[ERROR   ] Non-atomic batch failure.  The batch was submitted, but at least one exception occurred on an individual member of the batch. Use getNextException() to retrieve the exceptions for specific batched elements.
[ERROR   ] Error for batch element #0: One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "1" constrains table "TableSpaceA.ModuleATable" from having duplicate values for the index key
...
...
[ERROR   ] an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)


It seems that the web app not able to handle concurrent access on the data, and took us very long hours effort to figure out the root cause. We revert the code, do a few round testing, and we found that the particular table, ModuleATable must have a primary key column define in the table. This column doesn't serve any purpose but just to resolve this problem.

Initially I was though that Hibernate have cause the problem, but eventually it is not. Experience learned is that I need to think out from a box every time looking at a problem.

Sorting 2 columns in an Pojo class

When I was about to off, there is one late change requirement which need to be done before next day morning. User want the data to be sort rather than just leave it wild. Not there yet, the sort criteria has to be done on two columns. Graphic below shows the work need to be done. Take note that null value must be in the first place.



I know this could be done by using the sort method from Collections class, but my experience is only on one column sort, but I'm not sure whether the sort method is smart enough to handle my requirement. My logic thinking shows that if Column A is sorted, then I further sort on column B. In the same time I was thinking to write a custom bubble sort to cater for this requirement, but it was already late night. Without further thinking, I choose to challenge sort method (>.<*), and I come out this code:
public void sorting(ArrayList< mypojo > theList) {
  
  Collections.sort(theList, new Comparator< mypojo >(){

    @Override
    public int compare(MyPojo o1, MyPojo o2) {
    
      if( o1.getColumnA().compareToIgnoreCase(o2.getColumnA()) < 0 ) {
        return -1;
      }
      else if( o1.getColumnA().compareToIgnoreCase(o2.getColumnA()) == 0 ) {
        if( o1.getColumnB() == null || o2.getColumnB() == null ) {
          return 1;
        }
        else if( o1.getColumnB() != null && o2.getColumnB() != null && o1.getColumnB().compareToIgnoreCase(o2.getColumnB()) < 0 ) {
          return -1;
        }
        else if( o1.getColumnB() != null && o2.getColumnB() != null && o1.getColumnB().compareToIgnoreCase(o2.getColumnB()) > 0 ) {
          return 1;
        }
        else {
          return 0;
        }
      }
      else {
        return 1;
      }
    }
  });
}

And it works like charm. :o)

Saturday, December 8, 2012

My future in C++


I have been watching this video for the fifth time. I never get bore with this video as I was really inspired by Herb Sutter to continue my journey in C++. Remember last time I made up my mind to discontinue MSVC++ 6.0 journey as Microsoft has change the nature behavior of C++ since the launch of .NET. 

Now Microsoft has bring back the C++11 standard, a more organize foundation has been setup (isocpp.org) which is a single platform to get all news about C++ (Herb Sutter's blog used to be my main reference last time) and now I can even write C++ code for Windows Phone device. This could be my second device which allowing me to write C++ code beside Samsung Bada device.

There are so much fun in C++ now, I'm so excited about it. "The C++ King Is Back! Hold The King."

Sunday, December 2, 2012

The relationship between Serialization and Inheritance

There is no method defined in Serializable interface. It is just a marker interface that marked on a class that it support serialization. It is also important to know that ObjectOutputStream can write both objects and primitive types, as it implement ObjectInput and DataInput interfaces. The serielization mechanism will follow object references and write whole hierarchies of objects. However, if any superclass of an object is not serializable, then the normal object creation using constructor is call, start from the very first non-serializable superclass, all the way up to the Object class.

A code is worth a thousand nonsense:
public class Person {

   private String name;
 
   Person() {
      System.out.println("Person constructor is called.");
   }

   ...
}

public class Student extends Person implements Serializable {

  private static final long serialVersionUID = 8306482247141552618L;

  public Student(String name, long ID) {
    super(name);
    this.ID = ID;
  }
  ...
}

The code shows that Person class is not implement Serializable whereas Student class did. By the time during the deserialization happened to Student object, the default constructor of Person class is called. Thus "Person constructor is called" will output in the console.
FileInputStream inputFile = new FileInputStream("saveObject");
ObjectInputStream inputStream = new ObjectInputStream(inputFile);
  
Student student = (Student) inputStream.readObject();

Customizing Serialization in JAVA

Take a look at this program. If I try to compile this, the compiler will throw NoSerializableException.
public class Unicycle implements Serializable {

 private static final long serialVersionUID = 3843396491741625744L;

 private Wheel wheel;
 
 public Unicycle( Wheel wheel ) { 
  this.wheel = wheel; 
 }
 
 public String toString() {
  return "Unicycle with " + wheel;
 }
}

public class Wheel {

 private static final long serialVersionUID = -8786651610841969855L;

 private int wheelSize;
 
 public Wheel( int ws ) { 
  wheelSize = ws; 
 }
 
 public String toString() { 
  return "wheel size: " + wheelSize; 
 }
}

public class Main {

 public static void main(String args[]) throws IOException, ClassNotFoundException {

  Main main = new Main();
  
  main.writeData();
  main.readData();
 }
 
 public void writeData() throws IOException {
  
  FileOutputStream outputFile = new FileOutputStream("storage.dat");
  ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
  
  Wheel wheel = new Wheel(65);
  Unicycle uc = new Unicycle(wheel);
  
  System.out.println("Before writing: " + uc);
  
  outputStream.writeObject(uc);
  outputStream.flush();
  outputStream.close();
 }
 
 public void readData() throws IOException, ClassNotFoundException {
  
  FileInputStream inputFile = new FileInputStream("storage.dat");
  ObjectInputStream inputStream = new ObjectInputStream(inputFile);
  
  Unicycle uc = (Unicycle) inputStream.readObject();
  
  System.out.println("After reading: " + uc);
  
  inputStream.close();
 }

}

Perform 2 tasks in order to get this fix:
  1. The Wheel class has to be declared as transient in Unicycle class. (Note)
  2. Implement private void writeObject(ObjectOutputStream) and private void readObject(ObjectInputStream) in Unicycle class.
Note that these 2 methods isn't part of any interface, and accessible by the JVM even though it is declared as private. Below is the sample code of Unicycle class that implement these 2 methods:
public class Unicycle implements Serializable {
 ...

 transient private Wheel wheel;

 private void writeObject(ObjectOutputStream oos) {
  try {
   oos.defaultWriteObject();
   oos.writeInt(wheel.getWheelSize());
  }
  catch( IOException e ) {
   e.printStackTrace();
  }
 }
 
 private void readObject(ObjectInputStream ois) {
  try {
   ois.defaultReadObject();
   int wheelSize = ois.readInt();
   wheel = new Wheel(wheelSize);
  }
  catch( IOException e ) {
   e.printStackTrace();
  }
  catch( ClassNotFoundException e ) {
   e.printStackTrace();
  }
 }
}

This is what JAVA called Customizing Serialization.

rsync is a very good tool for backup

Before starting my Fedora upgrade, I am required to backup my /home directory before unexpected thing happened to my code. Last time I used the most stupid by copying the whole directory from one hard disk to another, now I am smart, I use rsync. A very friendly command I found in this article and this article.

Since I'm doing it at my local PC, basically the syntax is like this:
rsync -av --exclude 'rubbish' --exclude 'Downloads' \
/home/kokhoe /media/external_disk/home/kokhoe
The command above will backup my /home directory except for rubbish and Downloads directories.