Author: rott
Date: Wed Mar  5 10:04:30 2008
New Revision: 633953

URL: http://svn.apache.org/viewvc?rev=633953&view=rev
Log:
Call jaxws handler instance @PreDestroy annotated methods

Modified:
    
webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/AddNumbersHandlerTests.java
    
webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/addnumbershandler/AddNumbersProtocolHandler.java
    
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/factory/HandlerLifecycleManager.java
    
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/impl/HandlerLifecycleManagerImpl.java
    
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/server/EndpointController.java

Modified: 
webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/AddNumbersHandlerTests.java
URL: 
http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/AddNumbersHandlerTests.java?rev=633953&r1=633952&r2=633953&view=diff
==============================================================================
--- 
webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/AddNumbersHandlerTests.java
 (original)
+++ 
webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/AddNumbersHandlerTests.java
 Wed Mar  5 10:04:30 2008
@@ -103,10 +103,18 @@
 
             assertEquals("With handler manipulation, total should be 3 less 
than a proper sumation.", 17, total);
             TestLogger.logger.debug("Total (after handler manipulation) = " + 
total);
+            
+            // also confirm that @PreDestroy method is called.  Since it only 
makes sense to call it on the managed
+            // (server) side and just before the handler instance goes out of 
scope, we are creating a file in the
+            // @PreDestroy method, and will check for its existance here.  If 
the file does not exist, it means
+            // @PreDestroy method was never called.  The file is set to 
.deleteOnExit(), so no need to delete it.
+            File file = new File("AddNumbersProtocolHandler.preDestroy.txt");
+            assertTrue("File AddNumbersProtocolHandler.preDestroy.txt does not 
exist, meaning the @PreDestroy method was not called.", file.exists());
+            
             TestLogger.logger.debug("----------------------------------");
                } catch(Exception e) {
                        e.printStackTrace();
-            fail(e.getMessage());
+                       fail(e.getMessage());
                }
        }
     

Modified: 
webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/addnumbershandler/AddNumbersProtocolHandler.java
URL: 
http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/addnumbershandler/AddNumbersProtocolHandler.java?rev=633953&r1=633952&r2=633953&view=diff
==============================================================================
--- 
webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/addnumbershandler/AddNumbersProtocolHandler.java
 (original)
+++ 
webservices/axis2/trunk/java/modules/jaxws-integration/test/org/apache/axis2/jaxws/sample/addnumbershandler/AddNumbersProtocolHandler.java
 Wed Mar  5 10:04:30 2008
@@ -18,13 +18,19 @@
  */
 package org.apache.axis2.jaxws.sample.addnumbershandler;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.Set;
+
+import javax.annotation.PreDestroy;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBElement;
 import javax.xml.namespace.QName;
 import javax.xml.ws.WebServiceException;
 import javax.xml.ws.handler.MessageContext;
 import javax.xml.ws.handler.soap.SOAPMessageContext;
-import java.util.Set;
+
+import org.apache.axis2.jaxws.ExceptionFactory;
 
 public class AddNumbersProtocolHandler implements 
javax.xml.ws.handler.soap.SOAPHandler<SOAPMessageContext> {
 
@@ -80,6 +86,26 @@
 
         tracker.handleMessage(messagecontext);
         return true;
+    }
+    
+    @PreDestroy
+    public void preDestroy() {
+       try {
+               /*
+                * since @PreDestroy methods are called just before the managed 
(server) side
+                * handler instance goes out of scope, there's not a good way 
to test if it is
+                * called.  So, we are creating a file that one of the 
AddNumbersHandlerTests tests
+                * checks the existance of.
+                */
+               File file = new 
File("AddNumbersProtocolHandler.preDestroy.txt");
+               file.createNewFile();
+               FileOutputStream fos = new FileOutputStream(file);
+               fos.write(new byte[]{'h','i'});
+               fos.close();
+               file.deleteOnExit();
+       } catch (Exception e) {
+               throw ExceptionFactory.makeWebServiceException(e);
+       }
     }
 
 }

Modified: 
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/factory/HandlerLifecycleManager.java
URL: 
http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/factory/HandlerLifecycleManager.java?rev=633953&r1=633952&r2=633953&view=diff
==============================================================================
--- 
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/factory/HandlerLifecycleManager.java
 (original)
+++ 
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/factory/HandlerLifecycleManager.java
 Wed Mar  5 10:04:30 2008
@@ -32,6 +32,9 @@
     public Handler createHandlerInstance(MessageContext mc, Class handlerClass)
             throws LifecycleException, ResourceInjectionException;
 
+    public void destroyHandlerInstance(MessageContext mc, Handler handler)
+               throws LifecycleException, ResourceInjectionException;
+    
     /*
       * Invokes method on endpoint marked with @PostConstruct annotation.
       */

Modified: 
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/impl/HandlerLifecycleManagerImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/impl/HandlerLifecycleManagerImpl.java?rev=633953&r1=633952&r2=633953&view=diff
==============================================================================
--- 
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/impl/HandlerLifecycleManagerImpl.java
 (original)
+++ 
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/handler/lifecycle/impl/HandlerLifecycleManagerImpl.java
 Wed Mar  5 10:04:30 2008
@@ -58,5 +58,30 @@
         
         return (Handler)this.instance;
     }
+    
+    /**
+     * destroyHandlerInstance calls the handler's annotated PreDestroy method,
+     * if it exists.  A handler instance that has been passed through this 
method SHOULD NOT be used again
+     * 
+     * @param handler
+     */
+    public void destroyHandlerInstance(MessageContext mc, Handler handler) 
throws LifecycleException, ResourceInjectionException {
+        if (handler == null) {
+            throw ExceptionFactory.makeWebServiceException("Handler class must 
be passed");
+        }
+        
+        this.instance = handler;
+        
+        ServiceDescription serviceDesc = 
mc.getEndpointDescription().getServiceDescription();        
+        ResourceInjectionServiceRuntimeDescription injectionDesc = null;
+        if (serviceDesc != null) {
+            injectionDesc = 
ResourceInjectionServiceRuntimeDescriptionFactory.get(serviceDesc, 
handler.getClass());            
+        }
+
+        //Invoke PreDestroy
+        if (injectionDesc != null && injectionDesc.getPreDestroyMethod() != 
null) {
+            invokePreDestroy(injectionDesc.getPreDestroyMethod());
+        }
+    }
   
 }

Modified: 
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/server/EndpointController.java
URL: 
http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/server/EndpointController.java?rev=633953&r1=633952&r2=633953&view=diff
==============================================================================
--- 
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/server/EndpointController.java
 (original)
+++ 
webservices/axis2/trunk/java/modules/jaxws/src/org/apache/axis2/jaxws/server/EndpointController.java
 Wed Mar  5 10:04:30 2008
@@ -38,6 +38,8 @@
 import org.apache.axis2.jaxws.handler.HandlerInvoker;
 import org.apache.axis2.jaxws.handler.HandlerResolverImpl;
 import org.apache.axis2.jaxws.handler.factory.HandlerInvokerFactory;
+import 
org.apache.axis2.jaxws.handler.lifecycle.factory.HandlerLifecycleManager;
+import 
org.apache.axis2.jaxws.handler.lifecycle.factory.HandlerLifecycleManagerFactory;
 import org.apache.axis2.jaxws.i18n.Messages;
 import org.apache.axis2.jaxws.message.Message;
 import org.apache.axis2.jaxws.message.Protocol;
@@ -53,6 +55,7 @@
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamReader;
+import javax.xml.ws.WebServiceException;
 import javax.xml.ws.handler.Handler;
 
 import java.io.StringReader;
@@ -274,7 +277,7 @@
                // If the message is one way, we should not invoke the response 
handlers.  There is no response
                // MessageContext since a one way invocation is considered to 
have a "void" return.
                
-               if 
(!isOneWay(eic.getRequestMessageContext().getAxisMessageContext())) {
+               if (!isOneWay(request.getAxisMessageContext())) {
                     response.setMEPContext(request.getMEPContext());
                     
                     HandlerInvocationContext hiContext = 
buildHandlerInvocationContext(request, eic.getHandlers(), 
@@ -291,6 +294,17 @@
             // TODO for now, throw it.  We probably should try to make an 
XMLFault object and set it on the message
             throw ExceptionFactory.makeWebServiceException(e);  
         } finally {
+               // at this point, we are done with handler instances on the 
server; call @PreDestroy on all of them
+            HandlerLifecycleManager hlm = createHandlerlifecycleManager();
+            for (Iterator it = eic.getHandlers().iterator(); it.hasNext();) {
+                               try {
+                                       Handler handler = (Handler) it.next();
+                                       hlm.destroyHandlerInstance(request, 
handler);
+                               } catch (Exception e) {
+                                       // TODO: can we ignore this?
+                                       throw 
ExceptionFactory.makeWebServiceException(e);
+                               }
+                       }
             responseReady(eic);
             restoreRequestMessage(request);
         }
@@ -597,5 +611,14 @@
                 }
             }
         }
+    }
+    
+    /**
+     * we need a HandlerLifecycleManager so we can call the @PreDestroy when 
we are done with the server-side handler instances
+     */
+    private HandlerLifecycleManager createHandlerlifecycleManager() {
+        HandlerLifecycleManagerFactory elmf = 
(HandlerLifecycleManagerFactory)FactoryRegistry
+                .getFactory(HandlerLifecycleManagerFactory.class);
+        return elmf.createHandlerLifecycleManager();
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to