directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
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 GMT
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<String, State> states;
@@ -202,6 +204,68 @@
                 }
                 return;
             }
+            catch( BreakAndCallException bace )
+            {
+                State newState = getState( bace.getStateId() );
+                
+                Stack<State> 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<State> 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<State> getCallStack( StateContext context )
+    {
+        Stack<State> callStack = ( Stack<State> ) context.getAttribute( CALL_STACK
);
+        if( callStack == null )
+        {
+            callStack = new Stack<State>();
+            context.setAttribute( CALL_STACK, callStack );
+        }
+        return callStack;
     }
 
     private void setCurrentState( StateContext context, State newState )



Mime
View raw message