Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, 9 January 2009

Eclipse "Dialog refresh" error in Ubuntu 8.x

I have been working on Ubuntu 8.x with Eclipse environment recently. If you are a Eclipse user, you might know that you can search a class file using "Open Type" dialogue by using shortcut "Ctrl+Shift+T" or in the right click menu. However, it started not working since a while ago. It prompt an error dialogue as below:



It has been very frustrating for me that I couldn't use that useful feature. So I started to investigate the problem.

I finally found that the reason that affect Eclipse is the "Assistive Technologies" is turned on. It seems it is unrelated. But it actually affect Eclipse (somehow?!?). So in order to make it works again, the "Assistive Technologies" has to be turned off.

To turn off, choose System->Preferences->Assistive Technologies, uncheck "Enable assistive technologies" as below:



Once finished, restart Gnome, it should work!

Saturday, 16 August 2008

Create Apache CXF Interceptor (2)

To manipulate the header when a soap fault is returning, a org.apache.cxf.headers.Header object has to be created.

In the handleMessage method inside FaultHeaderInterceptor,

import javax.xml.namespace.QName;
import org.apache.cxf.headers.Header;
import org.w3c.dom.Element;
import com.sun.xml.messaging.saaj.soap.SOAPDocumentImpl;
import com.sun.xml.messaging.saaj.soap.impl.ElementImpl;
import com.sun.xml.messaging.saaj.soap.name.NameImpl;
import com.sun.xml.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl;
import net.clockstudio.cxf.HeaderTransferObject;

public void handleMessage(SoapMessage message) throws Fault
{
  // previous code

  Header header = this.createHeader(headerTO);
  if (header != null)
  {
    message.getHeaders.add(header);
  }
}

private Header createHeader(HeaderTransferObject headerTO)
{
  if (headerTO != null)
  {
    QName qname = new QName("/some/qname");
    SOAPDocumentImpl doc = new SOAPDocmentImpl(new SOAPPart1_1Impl());
    doc.setErrorChecking(false);

    Element element = new ElementImpl(doc, NameImpl.createFromQualifiedName("MyHeader", "http://clockstudio.net/cxf/myheader"));

    Element subElement = new ElementImpl(doc, NameImpl.createFromQualifiedName("subHeader", "http://clockstudio.net/cxf/subheader"));
    subElement.setTextContent(headerTO.getInfo());

    // more headers can be added

    element.appendChild(subElement);
    return new Header(qname, element);
  }
  return null;
}

In the above code, HeaderTransferObject attributes hold the information for creating a header. To create a header, it takes a QName (Qualified Name) and Element objects. In this example, the Element objects are created using SOAPDocumentImpl as well as its qualified name.

As you can see, it's still fairly simple to create a header when a fault is returned. However, what if you want to capture the incoming platform specific header and return those information back? In an asynchronized environment, the client request comes along with it's session id, transaction info and those has to be returned when a fault is occured. So the client can then be able to trace back which thread made the request, what can you do with this case?

In this case, another Interceptor has to be created to capture the incoming header information. You may wonder that there is an Exchange object which hold all incoming, outgoing message in the whole process, however, from my experience, when a fault is occured, the incoming header information in the Exchange object will be erased, it is probably because of a fault routine is started. Or it maybe just a bug in CXF. A further investigation is needed. But now, the new SoapHeaderInterceptor will do the job which explicitly save the header information into the message.

Here is the code,

public SoapHeaderInterceptor()
{
  super(Phase.READ);
  addAfter(ReadHeadersInterceptor.class.getName());
}

public void handleMessage(SoapMessage message) throws Fault
{
  // retrieve the incoming header and saved it explicitly, if it's not saved, it will be lost when a fault is returned
  List list = (List) message.get("org.apache.cxf.headers.Header.list");
  if (list != null && !list.isEmpty() && list.get(0) instanceof SoapHeader)
  {
    SoapHeader header = (SoapHeader) list.get(0);
    message.put(SoapHeader.class, header);
  }
}

The above code capture the incoming SoapHeader and explicitly put into the message. So once a fault is occured, the FaultHeaderInterceptor can still find the SoapHeader message from the incoming request.

To retrieve the saved SoapHeader in FaultHeaderInterceptor,

public void handleMessage(SoapMessage message) throws Fault
{
  // code that create PlatFormException

  // retrieve the soapHeader saved in the inMessage in SoapHeaderInterceptor
  SoapHeader header = message.getExchange().getInMessage().get(SoapHeader.class);

  // code that manipulate the header with headerTO above, headerTO can now be replaced to this new header
}

In the configuration, this interceptor will be put under inInterceptors bucket,

<bean id="soapHeaderInterceptor"
class="com.telstra.sdfcore.csc.uup.interceptor.SoapHeaderInterceptor" />

<cxf:bus>
  <cxf:inInterceptors>
    <ref bean="soapHeaderInterceptor" />
  </cxf:inInterceptors>
  <cxf:outFaultInterceptors>
    <ref bean="faultHeaderInterceptor" />
  </cxf:outFaultInterceptors>

</cxf:bus>

Now, when a request comes in, it will save the header into the message by using SoapHeaderInterceptor. When a fault is occured, it will return a platform specific exception, as well as the platform specific header which comes from the request.

Friday, 15 August 2008

Create Apache CXF Interceptor (1)

Recently I am working on Apache CXF web service framework. There was an requirement from my project that any exception or fault has to be returned with a desired platform exception. Also, any returned soap fault message should include a platform specific soap header. So it means no matter whether it is a runtime exception thrown from the business logic, or it's just a schema validation exception, a specific platform exception with soap header has to be returned so that the client side could be able to interpret in it's specific way.

To fulfill this requirement, one of the option is to write an interceptor in CXF. I have been looking for a sample but since CXF is fairly new web service framework (up to this moment, the version 2.1 is still in Apache Incubator), I could not find any good one. The only document I can look for is the interceptor introduction page in CXF website. But it's actually a good document. So I would prefer you to have a look at the document to get the concept before you keep going on below.

In here (supposed you have read the interceptor concept), I will try to provide a solution that fulfill the requirements above.

First, I will create a FaultHeaderInterceptor which extends org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor.

Before you write an interceptor, you should decide which phase that the interceptor should intercept the message. In the above case, since I have to manipulate the returned message, I put my interceptor into PREPARE_SEND phase. Also I put my interceptor to run after MessageSenderInterceptor to make sure CXF have done everything before I manipulate the message.

In the constructor,

public FaultHeaderInterceptor()
{
  super(Phase.PREPARE_SEND);
  addAfter(MessageSenderInterceptor.class.getName());
}

Since this interceptor is only responsible to change the soap message when a fault is returned, in CXF configuration, I put this interceptor in outFaultInterceptor tag, which means this interceptor only run when a fault happened.

In the configuration,


<bean id="faultHeaderInterceptor class="package.name.FaultHeaderInterceptor"/>

<cxf:bus>
  <cxf:outfaultinterceptors>
    <ref bean="faultHeaderInterceptor"/>
  </cxf:outfaultinterceptors>
</cxf:bus>


In the FaultHeaderInterceptor, I need to implement the method handleMessage(SoapMessage message), here is the logic,


public void handleMessage(SoapMessage message) throws Fault
{
  Exception exception = message.getContent(Exception.class);

  if (exception != null)
  {
    PlatformException platformException =
     new PlatformException(exception.getMessage());
    // overwrite Exception to PlatFormException
    Fault fault = new Fault(platformException);
    message.setContent(Exception.class, fault);
  }
}


**Note that the PlatformException is an annotated class with @WebFault, which means it represents the soapFault message.

Right now, whenever it is checked exception, or runtime exception that thrown by the CXF, it will return the PlatformException. That's easy! However, it only fulfill the first part of the requirement. To fulfill the second part, which I have to include the header.

To be continue...

Thursday, 3 May 2007

Pass Java SCEA Part I !!!

I was inactive in my blog for long time. It's because I'm really busy on my work, also, study the top class SCEA exam.

Today I finally took the exam and got pass with 75%. Although it is not the mark I would be proud of, it would still make me relief, at least for a while. Why do i say that? because SCEA actually consists of 3 parts, this is only the beginning. I have still to fight for the rest of the parts, which are assignment and essay. I guess this exam is the most toughest Java exam which I ever had. To do the preparation, I read lots of book, participate forums, read online resources, do lots of mock exam. When I was doing SCJP and SCWCD, I only read 1 book for each of them, and do some mock exams. Then I can get good marks. But this won't work in this SCEA, which the exam include lots of scenario, situation and find the "BEST" solution for it. Although some people said, SCEA is not up to date, it is still using EJB1.1, J2EE1.2. But I think more than half of them are about architecture concept, design pattern, security, which are not tighty up to those technology. And it's really useful. But as part 2 and 3 doesn't need to be use specify EJB version, so I guess I would choose JavaEE 5 and EJB3 to take the exam. Cause I can not only pass the exam, but also learn new stuff through it.

Anyway, here I will provide what sorts of information that I used to pass the exam, I hope it helps other people who are interesting of that.

Books:
1. Sun Certified Enterprise Architect for J2EE™ Technology Study Guide - Mark Cade and Simon Roberts [Prentice Hall]

2. Head First Design Pattern [Oreilly]

3. Head First EJB [Oreilly]

4. Design Patterns: Elements of Reusable Object-Oriented Software - GoF [Addison Wesley]

5. UML Distilled: A Brief Guide to the Standard Object Modeling Language, 3rd Edition - Martin Fowler [Addison Wesley]

Forums:
1. http://www.javaranch.com
2. http://www.javaworld.com.tw (Chinese Only) - Notes from worookie is very good. Must have a look! (Notes is written in English)
3. http://tech.groups.yahoo.com/group/scea_prep
4. http://tech.groups.yahoo.com/group/scea_j2ee

Online resources:
1. Java Applet Security

Mock Exam:
1. Whizlabs SCEA
2. http://moelholm.com
3. http://www.stormpages.com/jnagal/mock.html
4. http://www.harishramchandani.com

I actually search lots of other resources, but the above are the most useful and I keep re-visit before I take the exam.

Anyway, good luck to anyone who wish to sit on this exam!

Tuesday, 24 April 2007

Java static inner class in MyEclipse

Our project has been faced a weird bug recently.

We are using Apache Xmlbeans to perform some xml construction. However, whenever it construct a complex type object, it mis-uses a different object implementation to do the construction. However, in Mac environment, it works correctly. We are using MyEclipse as our IDE.

We digged into the construction process. We found that the actual error was ClassNotFoundException. We do a bit of research on this. We found that the cause of the problem was on static inner class.

The thing is, we found that Java does not load the static inner class properly, in Windows JVM 1.5.11b03

The first resolution was to load the inner class explicitly before it does the actual call. So we grab the ClassLoader from the Outer class, and load it explicitly [further reading in P.S.]. At first, we thought the problem has been solved. But after tidy up the code and redeploy. It doesn't work again.

However, bugs always has a reason. We keep digging into the problem. At last, we found that the cause of the error was from MyEclipse IDE. The MyEclipse IDE is somehow doesn't compile the static inner class at the beginning, although it shows no compile error at design time. It looks like it happens occasionally as well. So at last, we compile it explicitly. Then jar the Xmlbeans generated files and do the reference from our project. At last, it is working fine!

P.S.
When we try to load up the inner class explicitly, we should use $ sign to refer inner class instead of . sign in ClassLoader.loadclass(String arg) method. The reason for this is because once the application started, the inner class been compiled. The reference to the inner class will use $ sign.

net.A.B -> net.A$B

Reference:
1) http://mail-archives.apache.org/mod_mbox/myfaces-users/200611.mbox/%3C885AADC3D2318E45807C702ABC2637DD133501@MOES0037.tcc.local%3E

2) http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4491755

Monday, 12 February 2007

MyEclipse, Maven2, Tomcat5.5 Hot deploy

Thanks to my colleague who find out how to do a hot deploy under using these tools combination - MyEclipse, Maven and Tomcat. It is a problem which last long for a certain period of time.

In his solution, what he does is basically

1. create a Java Build Path Library which point to local maven repository
2. change the project web root directory to maven build structure, which is "/src/main/webapp", in .mymetadata file.
3. use maven-eclipse-plugin in your pom.xml

So next time you run "mvn eclipse:eclipse", you should be able to locate all classes from your local maven repository. And after that, whenever you press "save" in eclipse, it will automatically deploy all the classes to your tomcat webapps folder.

In here, I will give a bit more amendment.

Since the hot deploy will copy all needed jar file from your local repository to Tomcat webapps folder, sometimes you don't want some classes to be deployed to the Tomcat, for example, servlet-api.jar. It is because it is provided by Tomcat already. You don't want any conflict between your application and Tomcat. Please note that deployment in MyEclipse will copy every jar from maven local repository even if you specify "provided" in the dependency scope from your pom.xml.

Luckily, Tomcat is clever enough on not to use your servlet-api.jar. Instead, it will use it's own. However, if you are using servlet 2.4 and jsp 2.0, since you need to provide jsp-api.jar file. Tomcat will not be able to determine not to use it. Therefore, a conflict will happen, the error usually will happen like this if you are using JSTL:

org.apache.jasper.JasperException: Unable to read TLD "META-INF/c.tld".

In this case, the current solution that I have is to delete the jsp-api.jar file from your application deployed folder manually, which is in /webapps/{your app}/WEB-INF/lib

Then the problem will be solved. However, as you can see, the trouble is whenever you redeploy using the hot deploy solution above. You will need to delete the necessary file manually.

-----------------------------------------------------
-----------------------------------------------------
Further amendment:

As I mentioned above, Tomcat is NOT clever enough to skip the jsp-api.jar deployment. However, for my further investigation, I found that this symptom only happened in Tomcat 5.5.12 and 5.5.17. (I assume it happens in between these version as well). So if you are using the most updated Tomcat version, which I have updated to 5.5.23, it will work perfectly fine.

Happy programming!!!

Wednesday, 31 January 2007

Switch Java JDK version in Mac OSX

In case you need to switch Java version in Mac OSX, here is the solution:

In Mac OSX 10.4.x, JAVA_HOME is located at /Library/Java/Home, but it is actually a symbolic link to /System/Frameworks/JavaJVM.Framework/Versions. You will see a list of JDK version in this folder. Also, you will find another symbolic link called CurrentJDK which actually point to your current JDK. You can simply change the symbolic link to your desired version. Then it's done.

For symbolic link usage, here is a sample:
>ln -s ActualFilename LinkFileName

The above solution is tested on Mac OSX 10.4.x. I am not sure whether the path is the same in previous Mac OSX. Please check by yourself.