Android Open Source - libgdx-chat-example Abstract Dispatch






From Project

Back to project page libgdx-chat-example.

License

The source code is released under:

Apache License

If you think the Android project libgdx-chat-example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.stofkat.chat.server;
/*from w  ww .  ja v a  2 s  .  com*/
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.http.protocol.ExecutionContext;
import org.stofkat.chat.common.actions.Action;
import org.stofkat.chat.common.exceptions.ActionException;
import org.stofkat.chat.common.exceptions.DispatchException;
import org.stofkat.chat.common.exceptions.UnsupportedActionException;
import org.stofkat.chat.common.results.Result;

/**
 * Abstract base implementation of the {@link Dispatch}
 * 
 * <p>Provides basic action handler lookup and execution support. Lifecycle methods
 * may be overriden by implementors to receive notifications regarding action
 * execution and execution results.</p>
 * 
 * @author David Peterson
 * @author Robert Munteanu
 *
 */
public abstract class AbstractDispatch implements Dispatch {

    private static class DefaultExecutionContext implements ExecutionContext {
        private final AbstractDispatch dispatch;

        private final List<ActionResult<?, ?>> actionResults;

        private DefaultExecutionContext( AbstractDispatch dispatch ) {
            this.dispatch = dispatch;
            this.actionResults = new java.util.ArrayList<ActionResult<?, ?>>();
        }

        public <A extends Action<R>, R extends Result> R execute(A action, HttpSession session) throws DispatchException {
            return execute( action, true, session);
        }

        public <A extends Action<R>, R extends Result> R execute( A action, boolean allowRollback, HttpSession session)
                throws DispatchException {
            R result = dispatch.doExecute( action, this, session);
            if ( allowRollback )
                actionResults.add( new ActionResult<A, R>( action, result ) );
            return result;
        }

        /**
         * Rolls back all logged action/results.
         * 
         * @throws DispatchException
         */
        private void rollback() throws DispatchException {
            for ( int i = actionResults.size() - 1; i >= 0; i-- ) {
                ActionResult<?, ?> actionResult = actionResults.get( i );
                rollback( actionResult );
            }
        }

        private <A extends Action<R>, R extends Result> void rollback( ActionResult<A, R> actionResult )
                throws DispatchException {
            dispatch.doRollback( actionResult.getAction(), actionResult.getResult(), this );
        }

    };

    public <A extends Action<R>, R extends Result> R execute(A action, HttpSession session) throws DispatchException {
        DefaultExecutionContext ctx = new DefaultExecutionContext( this );
        try {
            return doExecute(action, ctx, session);
        } catch ( ActionException e ) {
            ctx.rollback();
            throw e;
        }
    }

    private <A extends Action<R>, R extends Result> R doExecute(A action, ExecutionContext ctx, HttpSession session)
            throws DispatchException {
        ActionHandler<A, R> handler = findHandler( action );
        
        try {
            executing(action, handler, ctx);
            R result  = handler.execute(action, ctx, session);
            executed(action, result, handler, ctx);
            return result;
        } catch ( DispatchException e) {
            failed(action, e, handler, ctx);
            throw e;
        } catch ( RuntimeException e) {
            failed(action, e, handler, ctx);
            throw e;
        } catch ( Error e) {
            failed(action, e, handler, ctx);
            throw e;
        }
        
    }

    private <A extends Action<R>, R extends Result> ActionHandler<A, R> findHandler( A action )
            throws UnsupportedActionException {
        ActionHandler<A, R> handler = getHandlerRegistry().findHandler( action );
        if ( handler == null )
            throw new UnsupportedActionException( action );

        return handler;
    }

    protected abstract ActionHandlerRegistry getHandlerRegistry();
    
    /**
     * Method invoked before executing the specified action with the specified handler.
     * 
     * <p>Any exception thrown from this method will prevent the normal execution of the action
     * and will be propagated.</p>
     * 
     * @param <A> the action type
     * @param <R> the result type
     * @param action the action to execute
     * @param handler the handler to execute it with
     * @param ctx the execution context
     * @throws DispatchException if the action execution should be cancelled 
     */
    protected <A extends Action<R>, R extends Result> void executing(A action, ActionHandler<A,R > handler, ExecutionContext ctx) throws DispatchException {
        
    }

    /**
     * Method invoked after the specified action has been succesfully executed with the specified handler.
     * 
     * <p>This method must not throw any exceptions.</p>
     * 
     * @param <A> the action type
     * @param <R> the result type
     * @param action the action to execute
     * @param result the execution result
     * @param handler the handler to execute it with
     * @param ctx the execution context
     */
    protected <A extends Action<R>, R extends Result> void executed(A action, R result, ActionHandler<A,R> handler, ExecutionContext ctx) {
        
    }
    
    /**
     * Method invoked after the specified action has been unsuccesfully executed with the specified handler.
     * 
     * <p>This method must not throw any exceptions.</p>
     * 
     * @param <A> the action type
     * @param <R> the result type
     * @param action the action to execute
     * @param e the exception thrown by the handler or by the {@link #executing(Action, ActionHandler, ExecutionContext) executing method}
     * @param handler the handler to execute it with
     * @param ctx the execution context
     */    
    protected <A extends Action<R>, R extends Result> void failed(A action, Throwable e, ActionHandler<A,R> handler, ExecutionContext ctx) {
        
    }

    private <A extends Action<R>, R extends Result> void doRollback( A action, R result, ExecutionContext ctx )
            throws DispatchException {
        ActionHandler<A, R> handler = findHandler( action );
        handler.rollback( action, result, ctx );
    }
}




Java Source Code List

org.stofkat.chat.android.AndroidChat.java
org.stofkat.chat.android.AndroidDispatchServiceAsync.java
org.stofkat.chat.android.ChatActivity.java
org.stofkat.chat.common.ChatMessage.java
org.stofkat.chat.common.ClientInterface.java
org.stofkat.chat.common.actions.Action.java
org.stofkat.chat.common.actions.ChatAction.java
org.stofkat.chat.common.actions.UpdateAction.java
org.stofkat.chat.common.exceptions.ActionException.java
org.stofkat.chat.common.exceptions.DispatchException.java
org.stofkat.chat.common.exceptions.ServiceException.java
org.stofkat.chat.common.exceptions.UnsupportedActionException.java
org.stofkat.chat.common.results.ChatResult.java
org.stofkat.chat.common.results.Result.java
org.stofkat.chat.common.util.HashMapToOrderedArray.java
org.stofkat.chat.common.util.LowestIdFinder.java
org.stofkat.chat.core.ChatTextField.java
org.stofkat.chat.core.Chat.java
org.stofkat.chat.core.PickNameStage.java
org.stofkat.chat.core.ServerInterface.java
org.stofkat.chat.core.UpdateTask.java
org.stofkat.chat.html.ChatHtml.java
org.stofkat.chat.html.HtmlChat.java
org.stofkat.chat.html.dispatch.GwtAsyncCallbackHandler.java
org.stofkat.chat.html.dispatch.GwtDispatchServiceAsync.java
org.stofkat.chat.html.dispatch.GwtDispatchService.java
org.stofkat.chat.http.dispatch.AsyncCallbackHandler.java
org.stofkat.chat.http.dispatch.AsyncCallback.java
org.stofkat.chat.http.dispatch.CustomCookieStore.java
org.stofkat.chat.http.dispatch.HttpDispatchServiceAsync.java
org.stofkat.chat.http.dispatch.HttpUtils.java
org.stofkat.chat.java.ChatDesktop.java
org.stofkat.chat.java.DesktopChat.java
org.stofkat.chat.java.DesktopDispatchServiceAsync.java
org.stofkat.chat.server.AbstractDispatch.java
org.stofkat.chat.server.ActionHandlerRegistry.java
org.stofkat.chat.server.ActionHandler.java
org.stofkat.chat.server.ActionResult.java
org.stofkat.chat.server.ChatMessagesDatabase.java
org.stofkat.chat.server.DefaultActionHandlerRegistry.java
org.stofkat.chat.server.Dispatch.java
org.stofkat.chat.server.InstanceActionHandlerRegistry.java
org.stofkat.chat.server.SimpleDispatch.java
org.stofkat.chat.server.actionhandlers.ChatActionHandler.java
org.stofkat.chat.server.actionhandlers.UpdateActionHandler.java
org.stofkat.chat.server.gwt.dispatch.GwtDispatchServlet.java
org.stofkat.chat.server.http.dispatch.HttpDispatchServlet.java