[java-opensaml COMMIT] /trunk/opensaml-messaging-api/src/main/java/org/opensaml/messaging/handler/AbstractMessageHand...

noreply at shibboleth.net noreply at shibboleth.net
Sun Apr 21 20:20:13 EDT 2013


Author: scantor
Date: Sun Apr 21 20:20:12 2013
New Revision: 3312

URL: http://svn.shibboleth.net/view/java-opensaml?rev=3312&view=rev
Log:
Add pre/post hooks to match profile actions.

Modified:
    trunk/opensaml-messaging-api/src/main/java/org/opensaml/messaging/handler/AbstractMessageHandler.java

Modified: trunk/opensaml-messaging-api/src/main/java/org/opensaml/messaging/handler/AbstractMessageHandler.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-messaging-api/src/main/java/org/opensaml/messaging/handler/AbstractMessageHandler.java?rev=3312&r1=3311&r2=3312&view=diff
==============================================================================
--- trunk/opensaml-messaging-api/src/main/java/org/opensaml/messaging/handler/AbstractMessageHandler.java (original)
+++ trunk/opensaml-messaging-api/src/main/java/org/opensaml/messaging/handler/AbstractMessageHandler.java Sun Apr 21 20:20:12 2013
@@ -23,6 +23,7 @@
 import net.shibboleth.utilities.java.support.component.AbstractIdentifiableInitializableComponent;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 
+import org.apache.log4j.Logger;
 import org.opensaml.messaging.context.MessageContext;
 
 
@@ -44,17 +45,95 @@
     public void invoke(@Nonnull final MessageContext<MessageType> messageContext) throws MessageHandlerException {
         Constraint.isNotNull(messageContext, "Message context cannot be null");
         
-        doInvoke(messageContext);
+        // The try/catch logic is designed to favor a checked ProfileException raised by
+        // the doExecute step over any unchecked errors. It also favors an unchecked error
+        // from doExecute over one raised by doPostExecute.
+        
+        if (doPreInvoke(messageContext)) {
+            try {
+                doInvoke(messageContext);
+            } catch (MessageHandlerException e) {
+                try {
+                    doPostInvoke(messageContext, e);
+                } catch (RuntimeException re) {
+                    Logger.getInstance(AbstractMessageHandler.class).warn(
+                            "Runtime exception thrown by doPostInvoke of " + getId(), re);
+                }
+                throw e;
+            } catch (RuntimeException e) {
+                try {
+                    doPostInvoke(messageContext, e);
+                } catch (RuntimeException re) {
+                    Logger.getInstance(AbstractMessageHandler.class).warn(
+                            "Runtime exception thrown by doPostInvoke of " + getId(), re);
+                }
+                throw e;
+            }
+
+            doPostInvoke(messageContext);
+        }
     }
 
+    /**
+     * Called prior to execution, handlers may override this method to perform pre-processing for a
+     * request.
+     * 
+     * <p>If false is returned, execution will not proceed.</p>
+     * 
+     * <p>If returning successfully, the last step should be to return the result of the
+     * superclass version of this method.</p>
+     * 
+     * @param messageContext the message context on which to invoke the handler
+     * @return  true iff execution should proceed
+     * 
+     * @throws MessageHandlerException if there is a problem executing the handler pre-routine
+     */
+    protected boolean doPreInvoke(@Nonnull final MessageContext<MessageType> messageContext)
+            throws MessageHandlerException {
+        return true;
+    }    
 
     /**
      * Performs the handler logic.
      * 
      * @param messageContext the message context on which to invoke the handler
-     * @throws MessageHandlerException if the there is an error invoking the handler on the message context
+     * @throws MessageHandlerException if there is an error invoking the handler on the message context
      */
     protected abstract void doInvoke(@Nonnull final MessageContext<MessageType> messageContext)
             throws MessageHandlerException;
 
+
+    /**
+     * Called after execution, handlers may override this method to perform post-processing for a
+     * request.
+     * 
+     * <p>Handlers must not "fail" during this step. This method will not be called if {@link #doPreInvoke}
+     * fails, but is called if an exception is raised by {@link #doInvoke}.</p>
+     * 
+     * @param messageContext the message context on which the handler was invoked
+     */
+    protected void doPostInvoke(@Nonnull final MessageContext<MessageType> messageContext) {
+    }    
+
+    /**
+     * Called after execution, handlers may override this method to perform post-processing for a
+     * request.
+     * 
+     * <p>Handlers must not "fail" during this step. This method will not be called if {@link #doPreInvoke}
+     * fails, but is called if an exception is raised by {@link #doInvoke}.</p>
+     * 
+     * <p>This version of the method will be called if an exception is raised during execution of
+     * the handler. The overall handler result will be to raise this error, so any errors inadvertently
+     * raised by this method will be logged and superseded.</p>
+     * 

[... 12 lines stripped ...]


More information about the commits mailing list