Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 38441 invoked from network); 22 Nov 2006 16:13:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 22 Nov 2006 16:13:00 -0000 Received: (qmail 15021 invoked by uid 500); 22 Nov 2006 16:13:10 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 14991 invoked by uid 500); 22 Nov 2006 16:13:10 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 14961 invoked by uid 99); 22 Nov 2006 16:13:10 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 22 Nov 2006 08:13:10 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 22 Nov 2006 08:12:59 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 243B31A984D; Wed, 22 Nov 2006 08:12:25 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r478209 - in /directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm: BreakAndCallException.java BreakAndReturnException.java StateControl.java StateMachine.java Date: Wed, 22 Nov 2006 16:12:24 -0000 To: commits@directory.apache.org From: niklas@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20061122161225.243B31A984D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: niklas Date: Wed Nov 22 08:12:24 2006 New Revision: 478209 URL: http://svn.apache.org/viewvc?view=rev&rev=478209 Log: Implemented breakAndCall()/breakAndReturn() Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndCallException.java (with props) directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndReturnException.java (with props) Modified: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateControl.java directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachine.java Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndCallException.java URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndCallException.java?view=auto&rev=478209 ============================================================================== --- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndCallException.java (added) +++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndCallException.java Wed Nov 22 08:12:24 2006 @@ -0,0 +1,62 @@ +/* + * @(#) $Id$ + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.mina.sm; + +/** + * + * @author The Apache Directory Project (mina-dev@directory.apache.org) + * @version $Rev$, $Date$ + */ +public class BreakAndCallException extends RuntimeException +{ + private final String stateId; + private final String returnToStateId; + private final boolean now; + + public BreakAndCallException( String stateId, boolean now ) + { + this( stateId, null, now ); + } + + public BreakAndCallException( String stateId, String returnToStateId, boolean now ) + { + if( stateId == null ) + { + throw new NullPointerException( "stateId" ); + } + this.stateId = stateId; + this.returnToStateId = returnToStateId; + this.now = now; + } + + public boolean isNow() + { + return now; + } + + public String getStateId() + { + return stateId; + } + + public String getReturnToStateId() + { + return returnToStateId; + } +} Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndCallException.java ------------------------------------------------------------------------------ svn:keywords = Id Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndReturnException.java URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndReturnException.java?view=auto&rev=478209 ============================================================================== --- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndReturnException.java (added) +++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndReturnException.java Wed Nov 22 08:12:24 2006 @@ -0,0 +1,39 @@ +/* + * @(#) $Id$ + * + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.mina.sm; + +/** + * + * @author The Apache Directory Project (mina-dev@directory.apache.org) + * @version $Rev$, $Date$ + */ +public class BreakAndReturnException extends RuntimeException +{ + private final boolean now; + + public BreakAndReturnException( boolean now ) + { + this.now = now; + } + + public boolean isNow() + { + return now; + } +} Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/BreakAndReturnException.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateControl.java URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateControl.java?view=diff&rev=478209&r1=478208&r2=478209 ============================================================================== --- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateControl.java (original) +++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateControl.java Wed Nov 22 08:12:24 2006 @@ -41,4 +41,34 @@ { throw new BreakAndGotoException( state, false ); } + + public static void breakAndCallNow( String state ) + { + throw new BreakAndCallException( state, true ); + } + + public static void breakAndCallNext( String state ) + { + throw new BreakAndCallException( state, false ); + } + + public static void breakAndCallNow( String state, String returnTo ) + { + throw new BreakAndCallException( state, returnTo, true ); + } + + public static void breakAndCallNext( String state, String returnTo ) + { + throw new BreakAndCallException( state, returnTo, false ); + } + + public static void breakAndReturnNow() + { + throw new BreakAndReturnException( true ); + } + + public static void breakAndReturnNext() + { + throw new BreakAndReturnException( false ); + } } Modified: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachine.java URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachine.java?view=diff&rev=478209&r1=478208&r2=478209 ============================================================================== --- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachine.java (original) +++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachine.java Wed Nov 22 08:12:24 2006 @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.Map; +import java.util.Stack; import org.apache.mina.sm.context.StateContext; import org.apache.mina.sm.event.Event; @@ -39,6 +40,7 @@ public class StateMachine { private static final Logger log = LoggerFactory.getLogger( StateMachine.class ); + private static final String CALL_STACK = StateMachine.class.getName() + ".callStack"; private final State startState; private final Map states; @@ -202,6 +204,68 @@ } return; } + catch( BreakAndCallException bace ) + { + State newState = getState( bace.getStateId() ); + + Stack callStack = getCallStack( context ); + State returnTo = bace.getReturnToStateId() != null + ? getState( bace.getReturnToStateId() ) : context.getCurrentState(); + callStack.push( returnTo ); + + if( bace.isNow() ) + { + if( log.isDebugEnabled() ) + { + log.debug( "BreakAndCallException thrown in " + + "transition " + t + + ". Moving to state " + newState.getId() + + " now." ); + } + handle( newState, event ); + } + else + { + if( log.isDebugEnabled() ) + { + log.debug( "BreakAndCallException thrown in " + + "transition " + t + + ". Moving to state " + newState.getId() + + " next." ); + } + setCurrentState( context, newState ); + } + return; + } + catch( BreakAndReturnException bare ) + { + Stack callStack = getCallStack( context ); + State newState = callStack.pop(); + + if( bare.isNow() ) + { + if( log.isDebugEnabled() ) + { + log.debug( "BreakAndReturnException thrown in " + + "transition " + t + + ". Moving to state " + newState.getId() + + " now." ); + } + handle( newState, event ); + } + else + { + if( log.isDebugEnabled() ) + { + log.debug( "BreakAndReturnException thrown in " + + "transition " + t + + ". Moving to state " + newState.getId() + + " next." ); + } + setCurrentState( context, newState ); + } + return; + } } /* @@ -217,6 +281,17 @@ { throw new UnhandledEventException( "Unhandled event: " + event ); } + } + + private Stack getCallStack( StateContext context ) + { + Stack callStack = ( Stack ) context.getAttribute( CALL_STACK ); + if( callStack == null ) + { + callStack = new Stack(); + context.setAttribute( CALL_STACK, callStack ); + } + return callStack; } private void setCurrentState( StateContext context, State newState )