KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > handler > HandlerWrapper


1 /**
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.webservice.handler;
8
9 // $Id: HandlerWrapper.java,v 1.3.2.1 2004/10/24 15:35:24 tdiesler Exp $
10

11 import org.jboss.logging.Logger;
12
13 import javax.xml.namespace.QName JavaDoc;
14 import javax.xml.rpc.JAXRPCException JavaDoc;
15 import javax.xml.rpc.handler.Handler JavaDoc;
16 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
17 import javax.xml.rpc.handler.MessageContext JavaDoc;
18 import javax.xml.rpc.soap.SOAPFaultException JavaDoc;
19
20 /**
21  * A wrapper arround a {@link javax.xml.rpc.handler.Handler} that takes care of its lifecycle.
22  *
23  * @author thomas.diesler@jboss.org
24  */

25 public class HandlerWrapper implements Handler JavaDoc
26 {
27    private static Logger log = Logger.getLogger(HandlerWrapper.class);
28
29    public final static int DOES_NOT_EXIST = 0;
30    public final static int METHOD_READY = 1;
31
32    // The states as string
33
private static String JavaDoc[] stateNames = new String JavaDoc[]{"DOES_NOT_EXIST", "METHOD_READY"};
34
35    // The handler to delegate to
36
private Handler JavaDoc delegate;
37    // The handler state
38
private int state;
39
40    /**
41     * Delegate to the given handler
42     */

43    public HandlerWrapper(Handler JavaDoc handler)
44    {
45       delegate = handler;
46       state = DOES_NOT_EXIST; // this is somewhat a lie ;-)
47
}
48
49    /**
50     * Get the current state
51     */

52    public int getState()
53    {
54       return state;
55    }
56
57    /**
58     * Get the current state as string
59     */

60    public String JavaDoc getStateAsString()
61    {
62       return stateNames[state];
63    }
64
65    /**
66     * Gets the header blocks processed by this Handler instance.
67     */

68    public QName JavaDoc[] getHeaders()
69    {
70       return delegate.getHeaders();
71    }
72
73    /**
74     * The init method enables the Handler instance to initialize itself.
75     */

76    public void init(HandlerInfo JavaDoc config) throws JAXRPCException JavaDoc
77    {
78       log.debug("init: " + delegate);
79       delegate.init(config);
80       state = METHOD_READY;
81    }
82
83    /**
84     * The destroy method indicates the end of lifecycle for a Handler instance.
85     */

86    public void destroy() throws JAXRPCException JavaDoc
87    {
88       log.debug("destroy: " + delegate);
89       state = DOES_NOT_EXIST;
90       delegate.destroy();
91    }
92
93    /**
94     * The handleRequest method processes the request message.
95     */

96    public boolean handleRequest(MessageContext JavaDoc msgContext) throws JAXRPCException JavaDoc, SOAPFaultException JavaDoc
97    {
98       if (state == DOES_NOT_EXIST)
99       {
100          log.warn("Handler is in state DOES_NOT_EXIST, skipping Handler.handleRequest for: " + delegate);
101          return true;
102       }
103
104       try
105       {
106          return delegate.handleRequest(msgContext);
107       }
108       catch (RuntimeException JavaDoc e)
109       {
110          return handleRuntimeException(e);
111       }
112    }
113
114    /**
115     * The handleResponse method processes the response SOAP message.
116     */

117    public boolean handleResponse(MessageContext JavaDoc msgContext)
118    {
119       if (state == DOES_NOT_EXIST)
120       {
121          log.warn("Handler is in state DOES_NOT_EXIST, skipping Handler.handleResponse for: " + delegate);
122          return true;
123       }
124
125       try
126       {
127          return delegate.handleResponse(msgContext);
128       }
129       catch (RuntimeException JavaDoc e)
130       {
131          return handleRuntimeException(e);
132       }
133    }
134
135    /**
136     * The handleFault method processes the SOAP faults based on the SOAP message processing model.
137     */

138    public boolean handleFault(MessageContext JavaDoc msgContext)
139    {
140       if (state == DOES_NOT_EXIST)
141       {
142          log.warn("Handler is in state DOES_NOT_EXIST, skipping Handler.handleFault for: " + delegate);
143          return true;
144       }
145
146       try
147       {
148          return delegate.handleFault(msgContext);
149       }
150       catch (RuntimeException JavaDoc e)
151       {
152          return handleRuntimeException(e);
153       }
154    }
155
156    /**
157     * As defined by JAX-RPC, a RuntimeException(other than SOAPFaultException) thrown from any method of
158     * the Handler results in the destroymethod being invoked and transition to the “Does Not Exist” state.
159     */

160    private boolean handleRuntimeException(RuntimeException JavaDoc e)
161    {
162       if ((e instanceof SOAPFaultException JavaDoc) == false)
163       {
164          log.warn("RuntimeException in handler method, transition to DOES_NOT_EXIST");
165          destroy();
166       }
167
168       throw e;
169    }
170
171    /**
172     * Returns a hash code value for the object.
173     */

174    public int hashCode()
175    {
176       return delegate.hashCode();
177    }
178
179    /**
180     * Returns a string representation of the object.
181     */

182    public String JavaDoc toString()
183    {
184       return "[state=" + getStateAsString() + ",handler=" + delegate + "]";
185    }
186 }
187
Popular Tags