Showing posts with label coding 编程. Show all posts
Showing posts with label coding 编程. Show all posts

Friday, January 16, 2009

Something funny has happened!

I am not sure why. Somehow the XML message that was passed to my request queue contains response tag which is not supposed to be attached to it.

What annoyed me was my WBI adapter for Kenan kept processing the same XML message over and over again! Thank to this and only i found out that there was one database connection remained opened thus causing more and more database sessions created!!! This could be a serious issue and luckily i tackled the problem before the system goes for production! However, i still need to find out why sometimes the XML message is redirected to the request queue even tough it has been processed by the adapter without encountering any Tuxedo connection error! The only clue i know now is this kind of 'problematic' XML message contains response tag! Could it be an error from the portal side? Could it be they acquired the processed XML message from response queue and then sent to request queue again? Well, it is doubtful.

Scratching head... continue tomorrow....

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);

Monday, September 17, 2007

Hibernate Notes

Here's some Hibernate notes for beginners out there. I've been facing lots of troubles dealing with Hibernate. I hope my notes would be short and clear enough so that you guys could save some time instead of looking for answers in the thick book!

1) Don't get confused with Flush Session & Commit Transaction
I used to have a wrong concept that flush session usually means data is already sync from Hibernate Session to the Database and everything cannot be roll back anymore! This is obviously wrong! Both are irrelevant with each other in fact! Even tough flush session would sync everything from Hibernate Session to the Database, however if the unit of work is within a transaction scope, the developer should trigger a commit so that everything can be committed!

2) Session Save will assign an identifier value to the corresponding saved object
Calling hibernate session save() will not only persist your pojo, but will also assign an identifier to that pojo according to the Hibernate Mapping. The identifier assignment will not be that late until database-session sync process happen.

Wednesday, July 25, 2007

What makes a software project fail?

Came across an article discussing the factors of why software project fails:

Take a look!

I agree with the 3 pillars, which are:
  1. Version control
  2. Work item tracking
  3. Build system
When i review these points on my current project, which implemented a great build procedure, good version controlling, good bug tracking system, and compare to previous projects which did not implement one of the 3 important items, the time saved is really significant!

First of all, a better tool for version controlling was introduced: SVN, then he started to come out with all kinds of ant build script to basically compile the source codes, moving files, deleting files, ftp files and etc. We even have a build script to refresh the whole database schema by deleting tables, drop users, creating new tables, running data scripts and etc. Lastly, we integrated these two important features into our LDAP system. We applied LuntBuild as a better framework to control the build, we applied Trac which provides a lot of features to govern or track bugs.

With the help of all these tools, we managed to save really lots of time on the deployment! The deployment can be easy & clean! Everything is handled at one end, no other parties could interfere the integrity!

Of course, there are weaknesses too, we did not manage to deploy part of the system. For every deployment we are about to do, we got to build the whole project & deploy to the server directory & in the end run some unix scripts which basically unjar the war file & restarts the web application.

Saturday, June 16, 2007

Hibernate Tips - managed sessions

This is a good Hibernate tips. It's another way of handling Hibernate Session.



http://www.17od.com/2006/11/06/using-managed-sessions-in-hibernate-to-ease-unit-testing/





Powered by ScribeFire.

Wednesday, April 25, 2007

Java Programming Tips

Share with you guys of some coding performance optimization tips:

1) Always use isEmpty() rather than size()>0 to check whether a collection contains any object. For further explanation, please read http://hanuska.blogspot.com/search/label/java

2) Always use iterator to iterate your list rather than using for loop and get(int). For further explanation please read http://forum.java.sun.com/thread.jspa?threadID=530365&messageID=2555464

Friday, February 16, 2007

Web Service at JRun 4

Would like to share my way of developing web service & testing it at JRun4 Web Server. I assume you know the concept of web service as this tutorial is specific to web service running on JRun web server. Also i assume you have already known how to use the Eclipse Web Service function which allows you to generate web service related files within a few seconds.

  1. To publish a web service at JRun 4 is very simple, what you need to do is to only create the "wsdd"(web service deployment descriptor) file and place it under your "PROJECT_HOME/WEB-INF/" folder.
  2. Start JRun server for your project. Create WSDL file by just running your project's web service's URL appended with '?WSDL'. For e.g (http://localhost:8103/services/ProgramWs?WSDL). For this case, "http://localhost:8103/services/ProgramWs" is the path to test the validity of web service ProgramWs.
  3. Using the generated WSDL file, go to command prompt, go to your WSDL location, run this command (before that, make sure that you have set JRUN_HOME/bin to your environment path) wsdl2java xxx.wsdl d:/ where xxx.wsdl is your wsdl file while d:/ is the output directory of the generated java classes. Thus, the bean object will be generated. What you need to do now is to replace your original bean object with it. For e.g. Your web service is about to get all programs of an institution, you will have a "getPrograms()" method which returns an array of Program bean object. So just replace this Program object with the auto-generated one which contains additional code for the web services.