Return-Path: Delivered-To: apmail-directory-dev-archive@www.apache.org Received: (qmail 21316 invoked from network); 23 Nov 2006 01:20:50 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Nov 2006 01:20:50 -0000 Received: (qmail 80460 invoked by uid 500); 23 Nov 2006 01:20:58 -0000 Delivered-To: apmail-directory-dev-archive@directory.apache.org Received: (qmail 80428 invoked by uid 500); 23 Nov 2006 01:20:58 -0000 Mailing-List: contact dev-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Apache Directory Developers List" Delivered-To: mailing list dev@directory.apache.org Received: (qmail 80417 invoked by uid 99); 23 Nov 2006 01:20:58 -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 17:20:58 -0800 X-ASF-Spam-Status: No, hits=2.0 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: domain of trustin@gmail.com designates 64.233.166.177 as permitted sender) Received: from [64.233.166.177] (HELO py-out-1112.google.com) (64.233.166.177) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 22 Nov 2006 17:20:46 -0800 Received: by py-out-1112.google.com with SMTP id a29so200038pyi for ; Wed, 22 Nov 2006 17:20:25 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=EXjL8Fn+pVZ8FduHSF+QBUXEvfNjPh2odT0uRjJyXR9o7Y2XOJlutfXZsbxHSs6zO788h+vNVyqxsi/tAw0nUHVZ0M2wCKGTYTeOZHDPlK8vtSAihv71KUUV3CcHmkB/AXu653AQUWoVj5P6InnqR3YQUZr75YVlPMWToVCJpVY= Received: by 10.35.101.1 with SMTP id d1mr2265745pym.1164244825404; Wed, 22 Nov 2006 17:20:25 -0800 (PST) Received: by 10.35.62.18 with HTTP; Wed, 22 Nov 2006 17:20:25 -0800 (PST) Message-ID: <768dcb2e0611221720w6766a3dev8db0835041313077@mail.gmail.com> Date: Thu, 23 Nov 2006 10:20:25 +0900 From: "Trustin Lee" To: dev@directory.apache.org Subject: Re: svn commit: r478209 - in /directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm: BreakAndCallException.java BreakAndReturnException.java StateControl.java StateMachine.java In-Reply-To: <20061122161225.243B31A984D@eris.apache.org> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_89881_11126054.1164244825349" References: <20061122161225.243B31A984D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org ------=_Part_89881_11126054.1164244825349 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline You could move this code to https://svn.apache.org/repos/asf/mina/sandbox. :) Trustin On 11/23/06, niklas@apache.org wrote: > > 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 ) > > > -- what we call human nature is actually human habit -- http://gleamynode.net/ -- PGP key fingerprints: * E167 E6AF E73A CBCE EE41 4A29 544D DE48 FE95 4E7E * B693 628E 6047 4F8F CFA4 455E 1C62 A7DC 0255 ECA6 ------=_Part_89881_11126054.1164244825349 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline You could move this code to https://svn.apache.org/repos/asf/mina/sandbox. :)

Trustin

On 11/23/06, niklas@apache.org <niklas@apache.org> wrote:
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 )





--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41  4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4  455E 1C62 A7DC 0255 ECA6 ------=_Part_89881_11126054.1164244825349--