0

I have a set of webservices which are hosted on a server.

I use axis to generate the classes and stubs for the webservices (java org.apache.axis.wsdl.WSDL2Java http://whatever1?wsdl, java org.apache.axis.wsdl.WSDL2Java http://whatever2?wsdl etc)

This generates a lot of classes from which I create a jar file.

For a particular project, I have to create a wrapper class which uses the different classes from the jar to perform 3 tasks. This is to abstract away all the different classes in the jar & present 1 class on which you call 3 different methods to achieve 3 different tasks).

I have completed the coding for the wrapper class, but I am very confused as to how to handle the exceptions thrown from the webservice client jar files.

One of the requirements is that clients of my wrapper would never need to know about any classes from the original webservice client jar. So other than my wrapper jar I need to create a few more exception classes.

In the webservice client jar there are 3-4 different types of user defined exception classes all of which are derived from WSException which in turn in derived from org.apache.axis.AxisFault (this is because I have used axis to generate the client jar).

Now, I am trying to figure out how best I could design my exception classes - so that code which calls my wrapper doesn't lose any of the info which it could have got if it were calling the webservice client directly.

Since I have 3 methods (say m1, m2, m3) in my wrapper doing three different tasks, I was thinking I would have 3 exception classes (m1exc, m2exc, m3exc).

This is a sample Exception class

public class m1exc
{
    WSException ws;

    public m1exc(WSException e)
    {
        ws = e;
    }

    public String toString()
    {
        return ws.toString();
    }
    // Returns the error code
    public int getCode()
    {
        return ws.getCode();
    }

}

In my wrapper, I would have

class Wrap
{
    void m1() throws m1exc, javax.xml.rpc.ServiceException, java.rmi.RemoteException
    {
        try
        {
            // call stuff from the webservice client jar to get things done
        }
        catch(WSException w)
        {
            throw new m1exc(w);
        }
    }
}

In the wrapper code, I would have try catch blocks where I would catch WSException & let everything else pass through.

Does this sound like the right strategy? Is there a better way to do this - the main criteria is the code calling my wrapper should not lose any exception information. Is there a standard design for this purpose?

1 Answer 1

1

You won't be hiding the WSException class from your client which I believe is what you are trying to accomplish.

Instead make your m1exc class accept code and toString as arguments which you pass from the WSException. You can add any other fields you like from the WSException class.

To enforce that m1exc is created by WSExceptions and not just any values the programmer wants, you could create an m1exc factory which accepts a WSException and generates an m1exc. The factory and the m1exc should be in the same package with no access modifier (public) on the constructor for the m1exc class which makes the m1exc constructor only accessible within the package.

public class m1excFactory {

  public static m1exc createM1Exc(WSExcetion e){
    return new m1exc(e.toString(), e.getCode());
  }
}

public class m1exc{
  String message;
  int code;

  m1exc(String message, int code)
  {
     this.message = message;
     this.code = code;
  }

  public String toString()
  {
    return message;
  }
  // Returns the error code
  public int getCode()
  {
      return code;
  }
}
4
  • Not sure if hide is the right word - I am looking to shield them from the complexity of using multiple classes from the jar and instead just use 1 class - my wrapper and the associated exceptions. My main concern is that I don't want to lose any exception information which would otherwise be available. I am still not clear how the above changes help me in this.
    – user93353
    Feb 2, 2013 at 3:15
  • Since your wrapper uses the WSException class any code using your wrapper will have to have access to it (include in classpath). I assume that is what you want when you say "clients of my wrapper would never need to know about any classes from the original webservice client jar".
    – dsc
    Feb 2, 2013 at 17:09
  • I can make the WSException object private inside the m1exc class - then clients wouldn't need to know about it.
    – user93353
    Feb 2, 2013 at 17:18
  • I was thinking you didn't want your clients to have to include whatever jar has the WSException in it. Like you were calling another webservice and you didn't want your clients to have a dependency on it. As I re-read your question and comments I would suggest you just have m1() throw WSException and get rid of the try/catch.
    – dsc
    Feb 2, 2013 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.