Wednesday, November 26, 2008

The Jar Manifest

Today i learned something about the Manifest file inside JAR file.

I found out that path for the Class-Path property starts from the path where the jar is located.

For example, if you place your JAR file at C:/myApp/service/myJar.jar, and you want to add a library (C:/myApp/lib/A.jar) to your manifest file, you will NOT be able to achieve that! Remember this!

The root path for the Class-Path property in the manifest.mf file starts with the path where the jar file installed.

In this case, if you want to put your classes into manifest.mf, you have to move the C:/myApp/lib/ to C:/myApp/service/lib

Correct me if i am wrong.

Friday, November 21, 2008

BEA Tuxedo Hack

I had a tricky problem with the Wepshere Business Integration(WBI) Adaptor. There was a error message saying that the jar file in my customized adaptor (tuxedo.jar) failed to load a 'so' file. It uses loadFile method from java.lang.System and somehow that particular file could not be found. I double checked the LIB_PATH(AIX machine) and java.lib.property and they are correct.

In the end i decompiled the particular class from the tuxedo.jar, modified the source code by using load method in java.lang.System instead of loadFile method. The load method will require us to specify the absolute file path. It works!!!

Sunday, November 2, 2008

IBM Websphere Business Integrator Adapter

My recent work is much on maintaining an adapter called Kenan Adapter deployed on an IBM product called IBM Websphere Business Integrator(WBI). This product basically picks up XML message in the queue and processes the particular messages. I was struggling on getting the web services call to work. I tried a few methods but the web services call just did not work. My project manager who has a very solid technical background could not provide a solution too. In the end he concluded that an RMI program cannot just call another RMI. The WBI framework itself is RMI based. Hence he helped me to build a standalone Java application that listens to the queue all the time, picks up messages then calls the web services. Last Friday i tested one round. The web service call was successful!

Sunday, June 22, 2008

About Hibernate Save and Delete parent children again

I want to add new child object into children collections and remove 'selected' existing child object from the same children collections.

  1. First, i get the collection from 'attached' parent. For e.g Set children = parent.getChildren()
  2. Add the new Child object. Of course please save the Child object first. children.add(newChild).
  3. Remove the 'selected' existing Child object. children.remove(removedChild)
  4. Finally, save the Parent object.

It is kind of fundamental. However sometimes we may get confused due to whatever reasons.

Wednesday, June 18, 2008

OpenOffice mail merge problems

I need to merge document B to document A(master document) using OpenOffice SDK.

My font in document B is 'Arial' and there are 3 paragrahs in it.

After merging, the font of the last paragrah somehow changed to 'Times New Roman'!

Another problem is, document A is formatted with numbering, document B is supposed to be inserted at the first line of the numbering. However, the 2nd paragraph from document B ends up a new number within the numbering format! To be more specific, document B's content should be positioned at number 5.1, but right now document B's content has been splited into 2: 5.1 which is the 1st paragraph from document B and 5.2 which is the 2nd paragraph from document B. My intention is to put all paragraphs from document B into 5.1!

After few days of searching, i discovered that it is actually a bug in OpenOffice!
  1. http://qa.openoffice.org/issues/show_bug.cgi?id=60475


  2. http://www.oooforum.org/forum/viewtopic.phtml?t=60268&highlight=insertdocumentfromurl+format
My solution:

  1. To solve the numbering problem, document B must always have an extra paragraph break after the last paragraph! (If you are using OpenOffice, click View from menu and choose to show "Nonprinting Characters")


  2. To solve the style problem, i adjust the styles in document A again after the merging is done.















Thursday, June 12, 2008

Differences between response.sendRedirect & response.forward methods

Came across one good explanation below, share with you guys out there:

Forwarding the Request and Re-Directing URL are two totally different animal. Your server gets the request from the browser..job of server is to send back the response. How how that response is generated is totally up to your server..you can process that request in one servlet or can process the same request using multiple sevlet...To process request using multiple servlet, you need to pass the request to another servlet...and ofcouse we do not want to loose the parameters/data set into the request otherwise that request does not make sense anymore.....This is all request forwarding!...Request forwarding is done during one HTTP request-response cycle..Once you return the response, you are done!

Now, In the response, you can tell your browser to do URL redirect to any other URL..this is HTTP header thing and browser will interprete the HTTP header and will send brand new request...

Thursday, June 5, 2008

Delete procedure of One to One relationship in Hibernate

I found out that Cascading does not work for 1-to1 relationship. We have to cater for it specifically.

The story is, i mapped 2 entities with each of them having a "many-to-one" relationship to represent a 1-to-1 relationship. I did not use the "1-to-1" mapping because it caused some problems which i now can not remember anymore.

Let's call the 2 entities as "parent" and "friend". 1 parent has 1 friend.

Now, I want to just delete "parent" and hope that its children will get deleted automatically.


  1. Therefore i will do a "session.delete(parent)". However this will throw an exception saying that "parent" is referencing another entity "friend".

  2. So, I will now get the "friend" from "parent" first by doing "Friend friend = parent.getFriend()".

  3. Then, i do a "parent.setFriend(null)" and saved the parent by doing "session.save(parent)". The "parent-friend" 1-to-1 relationship is being removed now.

  4. Next i delete the friend by doing "session.delete(friend)".

  5. Lastly, i will do a "session.delete(parent)". The parent will now be removed together with its children!

Hibernate Dialect

Today one problem arose. User complaint that one of the screen's query process takes too long time! I spent hours trying to figure out why. In the end found out that the Hibernate Dialect should be changed to MySQL instead of MSSQl one!

That is funny because my system is running on a MSSQL database! How can i just put MySQL dialect for Hibernate!

Well, it really speeds up the searching process.

Hell....

Friday, May 30, 2008

Funny Unix Symbolic Link to Windows Server issue

I faced this while doing deployment of one system to a unix server.

There is a folder/directory which is the symbolic link to a folder hosts at a windows NT server.

It is meant for clustering. The idea is 2 servers put the files/documents at the same folder at one location.

So, everytime i build my source code and deploy the war file at the unix server, i will run a program which runs the data migration. During the process the program will make use of the "open office service" that resides behind the operating system to create document such as Word and put at that particular folder as mentioned above.

Nightmare came. System kept throwing errors. The error message said it has something to do with "wrong code blar..".

I thought it was due to my code. Then I spent days to investigate the problem and to examine my code. It was a wrong move, a waste of time because in the end i found out that it is actually the program failed to create new directory! Ok reason found. The funny thing is, i tried to create the directory using unix shell command manually but failed! I did a list files command and found out that there is no such folder! But why did Unix say the folder is there? Ain't it funny!

Later i discovered that the folder is actually hidden. However i still did not manage to remove the folder by using the "rm -f ." command. From the net i know that this command works all the time to remove hidden directory. Clearly it did not work out in my case. So how now?

Finally i recreated the directory that the Unix claimed exist at the Windows Server, which is the "parent" cluster machine that holds the folder. After that i removed the folder again from the Shell at the Unix server. Yahoo! This time everything is back to normal! My program ran successfully!

What a waste of time. Was in the wrong direction. The Open Office Service should throw a more "exact" or "meaningul" message.

Nevertheless, I still do not know WHY and HOW did this problem happen! I only know the solution so far!

Thursday, May 29, 2008

Somthing about Set in Java

Well, if you read about the Java API, you will know that Set will only add object that does not exist in the Set itself. You must be aware of this especially when working with Hibernate! Here's my experience:

I was trying to add a few objects in a Set and set this Set. Then I found out from the database that there is always only ONE object added. I did a debug and found the size of the Set is always > 1 !!!

What happened? Here is my answer from torough checking:

Only the 1st object had been added to the Set. It is because the rest are actually "equal" to the first object so Set does not take them anymore! Bear in mind that each object differs from each other using their ID field. They do have their own ID yet when they are being added into the Set so they are rejected by the Set!

Thus, i have to save each object first before adding them into the Set. The reason is the ID will be generated by Hibernate for each object. The Set knows that they are NOT EQUAL right now then they will be added.

Tuesday, May 13, 2008

Apache POI

I have one web application, everytime i deploy i will need to load initial reference data again and again.

The amount of these data is huge!

I used to hardcode all these data in my Java file. I organised them properly using data structure such as String array and so on. In my Java file i have a few functions that actually perform the data loading job.

The problem is, It's time consuming and error prone to hardcode each and every data in the Java file.

Therefore, today i decided to put all the data in Spreadsheet program. It is easier to maintain data at Spreadsheet rather than in the Java file itself.

Then i used Apache POI library to read me those data from the Spreadsheet (Microsoft Excel).

Just one time try and it worked fine!

So simple! I was really delighted!

Wednesday, May 7, 2008

One day effort for unknown vm crach of Eclipse during Subclipse installation

WTF,

It took me one day to get the problem fixed. The solution is so simple!

I setup Eclipse at my new laptop. As usual, i also installed the Subclipse plugin. By right it should work properly. Well, when i tried to create a new remote source repository using Subclipse Perspective in Eclipse, Eclipse just quitted suddenly followed by a prompt saying that it is some kind of memory crash.

Also, there is an error log file generated each time Eclipse exited. However the message is so so not comprehensive.

Here's the message:


## An unexpected error has been detected by Java Runtime Environment:
## EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x3b397bfc,
pid=3824, id=5160
## Java VM: Java HotSpot(TM) Client VM (10.0-b22 mixed mode
windows-x86)
# Problematic frame:# C [libapr-1.dll+0x7bfc]
## If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.


With this only clue, i kept Googling for the whole day and still did not get the problem fixed! How frustated was i!

Besides trying out the solutions found online, I also tried to tackle the problem by doing a few times of trial and error, which is actually install-uninstall this kind of stuff.

We really got to be patient enough when dealing with this kind of problem.

Yet, the problems stayed. All the tries did not bring positive outcome.

Office hour ended. Today i did nothing but just configuring stupid Subversion on Eclipse. Boss, please don't get mad.

I started over the puzzle solving game again at home after coming back from dinner.

With calm mind, and of course enough of rest, i found the answer within an hour!

I came across this article online:

http://subclipse.tigris.org/faq.html#win32-crash

Here's the excerpt:


As soon as I do something with Subclipse on Windows, Eclipse just
crashes. Why does this happen?



This is a fairly recent problem that is caused by a DLL incompatibility. Subversion uses a library called APR or the Apache Portable Runtime. This library has a component called APR-ICONV which is used for translating characters in path and file names to/from UTF8 and the local character set. The release of Apache 2.2 brought with it new releases of APR and APR-ICONV and these are not binary compatible with previous releases.

Subclipse ships with the Apache 2.0 version of these libraries. This crash
occurs if you install an application that installs the Apache 2.2 version of
these libraries AND ALSO sets the APR_ICONV_PATH environment variable to a path that contains the Apache 2.2 version of the APR-ICONV .so objects.



The fix is actually simple. Change the name of the environment variable to APR_ICONV1_PATH. The Apache 2.2 library will look for this environment variable name first, and only fallback to the older name if it is not found. You can safely have an APR_ICONV_PATH environment variable pointing to the Apache 2.0 version of these libraries and the APR_ICONV1_PATH environment variable pointing to the Apache 2.2 version. Subversion 1.5 has resolved this problem by discontinuing the use of APR-ICONV. Instead, Subversion will use translation routines that are provided by the Windows operating system.


So, i uninstalled the Subversion Server built against Apache 2.2 and replaced it with the one built against Apache 2.0.

Eclipse looks fine now.

Finally!

Friday, April 4, 2008

Hibernate Tips - delete existing item from children

Scenario as below:

I am at a screen now from a web application.

There is a swap-select box on the screen. I select 5 items and click Save button.

Next, the system creates 1 parent object and saves 5 items (selected from screen's swap-select box) as children(a Set) into their parent.

I leave the screen.

Upon next visit to the same screen again, i am trying to remove 2 among the 5 selected items. After that i click Save button.

How should i handle this?

What i need to do is:

1) Get the original Set from the parent object. (Let's name it 'children')

2) At the same time, create new Set(let's name it 'removedChildren'), add those objects that are meant to be removed. (In this case this new Set will have 2 objects)

3) Using removeAll method from Set, put the 'removedChildren' Set in it (children.removeAll(removedChildren))

4) Set the 'children' Set back to the parent. (parent.setChildren(children))

5) Save the parent. (saveOrUpdate(parent))

How to get the last 2 decimal digits using JAVA?

Example:

To get 0.99
from
double abcd =123456789012345.99;

Solution:
double a = 12345678901234.99*100;
String f = Long.toString(((long) a)%100);