directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
Subject svn commit: r412347 [1/2] - in /directory/sandbox/niklas: mina-sm/ mina-sm/src/ mina-sm/src/main/ mina-sm/src/main/java/ mina-sm/src/main/java/org/ mina-sm/src/main/java/org/apache/ mina-sm/src/main/java/org/apache/mina/ mina-sm/src/main/java/org/apach...
Date Wed, 07 Jun 2006 09:49:19 GMT
Author: niklas
Date: Wed Jun  7 02:49:17 2006
New Revision: 412347

URL: http://svn.apache.org/viewvc?rev=412347&view=rev
Log:
Initail check in of state machine prototype

Added:
    directory/sandbox/niklas/mina-sm/
    directory/sandbox/niklas/mina-sm/pom.xml   (with props)
    directory/sandbox/niklas/mina-sm/src/
    directory/sandbox/niklas/mina-sm/src/main/
    directory/sandbox/niklas/mina-sm/src/main/java/
    directory/sandbox/niklas/mina-sm/src/main/java/org/
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/AbstractState.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/DefaultEventFactory.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/Event.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventArgumentsInterceptor.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventFactory.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/State.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachineProxyFactory.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContext.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContextLookup.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContext.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContextFactory.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/SingletonStateContextLookup.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContext.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextFactory.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextLookup.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AbstractTransition.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AmbiguousMethodException.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/FsmState.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodInvocationException.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodTransition.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoSuchMethodException.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoopTransition.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/Transition.java   (with props)
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/mina/
    directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/mina/MinaStateContextLookup.java   (with props)
    directory/sandbox/niklas/mina-sm/src/test/
    directory/sandbox/niklas/mina-sm/src/test/java/
    directory/sandbox/niklas/mina-sm/src/test/java/org/
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/AbstractStateTest.java   (with props)
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java   (with props)
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/context/
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/context/AbstractStateContextLookupTest.java   (with props)
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/FsmStateTest.java   (with props)
    directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/MethodTransitionTest.java   (with props)
Removed:
    directory/sandbox/niklas/mina.old/

Added: directory/sandbox/niklas/mina-sm/pom.xml
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/pom.xml?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/pom.xml (added)
+++ directory/sandbox/niklas/mina-sm/pom.xml Wed Jun  7 02:49:17 2006
@@ -0,0 +1,56 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.directory.mina</groupId>
+    <artifactId>build</artifactId>
+    <version>0.9.5-SNAPSHOT</version>
+  </parent>
+  <artifactId>mina-sm</artifactId>
+  <name>Apache MINA State Machine Module</name>
+  <inceptionYear>2006</inceptionYear>
+
+  <url>http://directory.apache.org/projects/mina/</url>
+
+  <dependencies>
+
+    <dependency>
+      <groupId>org.apache.directory.mina</groupId>
+      <artifactId>mina-core</artifactId>
+      <version>${pom.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-simple</artifactId>
+      <version>1.0</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>com.agical.rmock</groupId>
+      <artifactId>rmock</artifactId>
+      <version>2.0.0-rc-6</version>
+      <scope>test</scope>
+    </dependency>
+
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.4</source>
+          <target>1.4</target>
+          <debug>true</debug>
+          <optimize>true</optimize>
+          <showDeprecations>true</showDeprecations>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>

Propchange: directory/sandbox/niklas/mina-sm/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/AbstractState.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/AbstractState.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/AbstractState.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/AbstractState.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,79 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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 abstract class AbstractState implements State {
+    private final String name;
+    private final State parent;
+    private final boolean endState;
+
+    public AbstractState(String name) {
+        this(name, null, false);
+    }
+    
+    public AbstractState(String name, State parent) {
+        this(name, parent, false);
+    }
+    
+    public AbstractState(String name, boolean endState) {
+        this(name, null, endState);
+    }
+    
+    public AbstractState(String name, State parent, boolean endState) {
+        if (name == null) {
+            throw new NullPointerException("Tried to set name to null");
+        }
+        this.name = name;
+        this.parent = parent;
+        this.endState = endState;
+    }
+    
+    public boolean isEndState() {
+        return endState;
+    }
+
+    public void handle(Event event) {
+        if (doHandle(event)) {
+            if (event.getNextState() != event.getCurrentState()) {
+                event.getCurrentState().exit(event);
+                event.getNextState().enter(event);
+            }
+        } else if (parent != null) {
+            parent.handle(event);
+        }
+    }
+    
+    public void enter(Event event) {
+    }
+
+    public void exit(Event event) {
+    }
+
+    protected abstract boolean doHandle(Event event);
+    
+    public String getName() {
+        return name;
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/AbstractState.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/DefaultEventFactory.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/DefaultEventFactory.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/DefaultEventFactory.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/DefaultEventFactory.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,41 @@
+/*
+ *   @(#) $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;
+
+import java.lang.reflect.Method;
+
+import org.apache.mina.sm.context.StateContext;
+
+/**
+ * Default {@link EventFactory} implementation. Uses the method's name as
+ * event id.
+ * 
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class DefaultEventFactory implements EventFactory
+{
+
+    public Event create( Method method, State currentState,
+            StateContext context, Object[] arguments )
+    {
+        return new Event( method.getName(), currentState, context, arguments );
+    }
+
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/DefaultEventFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/Event.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/Event.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/Event.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/Event.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,85 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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;
+
+import org.apache.mina.sm.context.StateContext;;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class Event {
+    private final Object id;
+    private final State currentState;
+    private final StateContext context;
+    private final Object[] arguments;
+    private State nextState;
+    
+    public Event(Object id, State currentState, StateContext context) {
+        this(id, currentState, context, new Object[0]);
+    }
+    public Event(Object id, State currentState, StateContext context, Object[] arguments) {
+        if (id == null) {
+            throw new NullPointerException("id");
+        }
+        if (currentState == null) {
+            throw new NullPointerException("currentState");
+        }
+        if (context == null) {
+            throw new NullPointerException("context");
+        }
+        if (arguments == null) {
+            throw new NullPointerException("arguments");
+        }
+        this.id = id;
+        this.currentState = currentState;
+        this.nextState = currentState;
+        this.context = context;
+        this.arguments = arguments;
+    }
+
+    public StateContext getContext() {
+        return context;
+    }
+
+    public State getCurrentState() {
+        return currentState;
+    }
+
+    public State getNextState() {
+        return nextState;
+    }
+    
+    public void setNextState(State nextState) {
+        if (nextState == null) {
+            throw new NullPointerException("nextState");
+        }
+        this.nextState = nextState;
+    }
+    
+    public Object getId() {
+        return id;
+    }
+    
+    public Object[] getArguments() {
+        return arguments;
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/Event.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventArgumentsInterceptor.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventArgumentsInterceptor.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventArgumentsInterceptor.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventArgumentsInterceptor.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,31 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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 interface EventArgumentsInterceptor {
+
+    Object[] modify(Object[] arguments);
+    
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventArgumentsInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventFactory.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventFactory.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventFactory.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventFactory.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,45 @@
+/*
+ *   @(#) $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;
+
+import java.lang.reflect.Method;
+
+import org.apache.mina.sm.context.StateContext;
+
+/**
+ * Used by {@link StateMachineProxyFactory} to create {@link Event} objects when 
+ * methods are invoked on the proxy.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface EventFactory
+{
+    /**
+     * Creates a new {@link Event} from the specified method and method 
+     * arguments.
+     * 
+     * @param method the method being invoked.
+     * @param currentState the current {@link State}.
+     * @param context the current {@link StateContext}.
+     * @param args the method arguments.
+     * @return the {@link Event} object.
+     */
+    Event create( Method method, State currentState, StateContext context, Object[] arguments );
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/EventFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/State.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/State.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/State.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/State.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,33 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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 interface State {
+    String getName();
+    boolean isEndState();
+    void handle(Event event);
+    void enter(Event event);
+    void exit(Event event);
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/State.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachineProxyFactory.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachineProxyFactory.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachineProxyFactory.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachineProxyFactory.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,147 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Map;
+
+import org.apache.mina.sm.context.SingletonStateContextLookup;
+import org.apache.mina.sm.context.StateContext;
+import org.apache.mina.sm.context.StateContextLookup;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class StateMachineProxyFactory {
+    private static final Logger log = LoggerFactory.getLogger(StateMachineProxyFactory.class);
+    
+    private static final String CURRENT_STATE = StateMachineProxyFactory.class.getName() + ".CurrentState";
+    private static final Object[] EMPTY_ARGUMENTS = new Object[0];
+
+    private StateMachineProxyFactory() {
+    }
+
+    public static Object create(Class iface, State startState) {
+        return create(new Class[] {iface}, startState);
+    }
+    
+    public static Object create(Class iface, State startState, 
+                                StateContextLookup contextLookup) {
+        return create(new Class[] {iface}, startState, contextLookup);
+    }
+    
+    public static Object create(Class iface, State startState, 
+                                StateContextLookup contextLookup,
+                                EventArgumentsInterceptor interceptor) {
+        return create( new Class[] {iface}, startState, contextLookup, interceptor, 
+                       new DefaultEventFactory() );
+    }
+
+    public static Object create( Class iface, State startState, 
+                                 StateContextLookup contextLookup,
+                                 EventArgumentsInterceptor interceptor, 
+                                 EventFactory eventFactory )
+    {
+        return create( new Class[] {iface}, startState, contextLookup, interceptor, eventFactory );
+}
+    
+    public static Object create(Class[] ifaces, State startState) {
+        return create(ifaces, startState, new SingletonStateContextLookup());
+    }
+    
+    public static Object create(Class[] ifaces, State startState, 
+                                StateContextLookup contextLookup) {
+
+        return create( ifaces, startState, contextLookup, null, new DefaultEventFactory() );
+    }
+    
+    public static Object create(Class[] ifaces, State startState, 
+                                StateContextLookup contextLookup, 
+                                EventArgumentsInterceptor interceptor,
+                                EventFactory eventFactory) {
+
+        ClassLoader cl = StateMachineProxyFactory.class.getClassLoader();
+        InvocationHandler handler = 
+            new MethodInvocationHandler(startState, contextLookup, interceptor, 
+                                        eventFactory);
+
+        return Proxy.newProxyInstance(cl, ifaces, handler);
+    }
+    
+    private static class MethodInvocationHandler implements InvocationHandler {
+        private final State startState;
+        private final StateContextLookup contextLookup;
+        private final EventArgumentsInterceptor interceptor;
+        private final EventFactory eventFactory;
+        
+        public MethodInvocationHandler( State startState, 
+                                       StateContextLookup contextLookup, 
+                                       EventArgumentsInterceptor interceptor,
+                                       EventFactory eventFactory )
+        {
+            this.contextLookup = contextLookup;
+            this.startState = startState;
+            this.interceptor = interceptor;
+            this.eventFactory = eventFactory;
+        }
+        
+        public Object invoke(Object proxy, Method method, Object[] args) 
+                throws Throwable {
+            
+            if (log.isDebugEnabled()) {
+                log.debug("Method invoked: " + method);
+            }
+            
+            args = args == null ? EMPTY_ARGUMENTS : args;
+            if (interceptor != null) {
+                args = interceptor.modify(args);
+            }
+            StateContext context = contextLookup.lookup(args);
+            
+            if (context != null) {
+                synchronized (context) {
+                    Map attributes = context.getAttributes();
+                    State state = (State) attributes.get(CURRENT_STATE);
+                    if (state == null) {
+                        state = startState;
+                    }
+                    Event event = eventFactory.create(method, state, context, args);
+                    state.handle(event);
+                    if (log.isDebugEnabled()) {
+                        if (event.getNextState() != state) {
+                            log.debug("State changed: " + state.getName() + " -> " 
+                                    + event.getNextState().getName());
+                        }
+                    }
+                    state = event.getNextState();
+                    attributes.put(CURRENT_STATE, state);
+                }
+            }
+            
+            return null;
+        }
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/StateMachineProxyFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContext.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContext.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContext.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContext.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,39 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.context;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public abstract class AbstractStateContext implements StateContext {
+    private Map attributes = null;
+
+    public Map getAttributes() {
+        if (attributes == null) {
+            attributes = new HashMap();
+        }
+        return attributes;
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContext.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContextLookup.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContextLookup.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContextLookup.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContextLookup.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,56 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.context;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public abstract class AbstractStateContextLookup implements StateContextLookup {
+    private final StateContextFactory contextFactory;
+
+    public AbstractStateContextLookup(StateContextFactory contextFactory) {
+        if (contextFactory == null) {
+            throw new NullPointerException("contextFactory");
+        }
+        this.contextFactory = contextFactory;
+    }
+    
+    public StateContext lookup(Object[] eventArgs) {
+        for (int i = 0; i < eventArgs.length; i++) {
+            if (supports(eventArgs[i].getClass())) {
+                StateContext sc = lookup(eventArgs[i]);
+                if (sc == null) {
+                    sc = contextFactory.create();
+                    store(eventArgs[i], sc);
+                }
+                return sc;
+            }
+        }
+        return null;
+    }
+    
+    protected abstract StateContext lookup(Object eventArg);
+    
+    protected abstract void store(Object eventArg, StateContext context);
+    
+    protected abstract boolean supports(Class c);
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/AbstractStateContextLookup.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContext.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContext.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContext.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContext.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,28 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.context;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class DefaultStateContext extends AbstractStateContext {
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContext.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContextFactory.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContextFactory.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContextFactory.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContextFactory.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,33 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.context;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class DefaultStateContextFactory implements StateContextFactory {
+
+    public StateContext create() {
+        return new DefaultStateContext();
+    }
+
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/DefaultStateContextFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/SingletonStateContextLookup.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/SingletonStateContextLookup.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/SingletonStateContextLookup.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/SingletonStateContextLookup.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,45 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.context;
+
+/**
+ * {@link StateContextLookup} implementation which always returns the same
+ * {@link StateContext} instance.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class SingletonStateContextLookup implements StateContextLookup {
+    private final StateContext context;
+
+    public SingletonStateContextLookup() {
+        context = new DefaultStateContext();
+    }
+    
+    public SingletonStateContextLookup(StateContextFactory contextFactory) {
+        if (contextFactory == null) {
+            throw new NullPointerException("contextFactory");
+        }
+        context = contextFactory.create();
+    }
+    
+    public StateContext lookup(Object[] eventArgs) {
+        return context;
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/SingletonStateContextLookup.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContext.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContext.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContext.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContext.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,31 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.context;
+
+import java.util.Map;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface StateContext {
+    Map getAttributes();
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContext.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextFactory.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextFactory.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextFactory.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextFactory.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,29 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.context;
+
+/**
+ * Factory which creates {@link StateContext} instances.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface StateContextFactory {
+    StateContext create();
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextLookup.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextLookup.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextLookup.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextLookup.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,29 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.context;
+
+/**
+ * Lookups a {@link StateContext} from a collection of event arguments.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface StateContextLookup {
+    StateContext lookup(Object[] eventArgs);
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/context/StateContextLookup.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AbstractTransition.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AbstractTransition.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AbstractTransition.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AbstractTransition.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,60 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.finite;
+
+import org.apache.mina.sm.Event;
+import org.apache.mina.sm.State;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public abstract class AbstractTransition implements Transition {
+    public static final String WILDCARD_EVENT_ID = "*";
+    
+    private final Object eventId; 
+    private final State nextState;
+
+    public AbstractTransition(Object eventId) {
+        this(eventId, null);
+    }
+    
+    public AbstractTransition(Object eventId, State nextState) {
+        this.eventId = eventId;
+        this.nextState = nextState;
+    }
+    
+    public boolean execute(Event event) {
+        if (!eventId.equals(WILDCARD_EVENT_ID) && !eventId.equals(event.getId())) {
+            return false;
+        }
+        
+        if (doExecute(event)) {
+            if (nextState != null) {
+                event.setNextState(nextState);
+            }
+            return true;
+        }
+        return false;
+    }
+
+    protected abstract boolean doExecute(Event event);
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AbstractTransition.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AmbiguousMethodException.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AmbiguousMethodException.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AmbiguousMethodException.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AmbiguousMethodException.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,62 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.finite;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class AmbiguousMethodException extends RuntimeException {
+
+    /**
+     * 
+     */
+    public AmbiguousMethodException() {
+        super();
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param message
+     */
+    public AmbiguousMethodException(String message) {
+        super(message);
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param message
+     * @param cause
+     */
+    public AmbiguousMethodException(String message, Throwable cause) {
+        super(message, cause);
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param cause
+     */
+    public AmbiguousMethodException(Throwable cause) {
+        super(cause);
+        // TODO Auto-generated constructor stub
+    }
+
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/AmbiguousMethodException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/FsmState.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/FsmState.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/FsmState.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/FsmState.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,84 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.finite;
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.mina.sm.AbstractState;
+import org.apache.mina.sm.Event;
+import org.apache.mina.sm.State;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class FsmState extends AbstractState {
+    private List transitions = new LinkedList();
+    
+    public FsmState(String name) {
+        super(name);
+    }
+    
+    public FsmState(String name, State parent) {
+        super(name, parent);
+    }
+
+    public FsmState(String name, boolean endState) {
+        super(name, endState);
+    }
+
+    public FsmState(String name, State parent, boolean endState) {
+        super(name, parent, endState);
+    }
+
+    public void enter(Event event) {
+    }
+
+    public void exit(Event event) {
+    }
+
+    protected boolean doHandle(Event event) {
+        for (Iterator it = transitions.iterator(); it.hasNext();) {
+            Transition t = (Transition) it.next();
+            if (t.execute(event)) {
+                return true;
+            }
+        }
+        
+        return false;
+    }
+
+    public List getTransitions() {
+        return Collections.unmodifiableList(transitions);
+    }
+
+    public FsmState addTransition(Transition transition) {
+        if (transition == null) {
+            throw new NullPointerException("Tried to add null as Transition");
+        }
+        transitions.add(transition);
+        return this;
+    }
+    
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/FsmState.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodInvocationException.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodInvocationException.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodInvocationException.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodInvocationException.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,62 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.finite;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class MethodInvocationException extends RuntimeException {
+
+    /**
+     * 
+     */
+    public MethodInvocationException() {
+        super();
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param message
+     */
+    public MethodInvocationException(String message) {
+        super(message);
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param message
+     * @param cause
+     */
+    public MethodInvocationException(String message, Throwable cause) {
+        super(message, cause);
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param cause
+     */
+    public MethodInvocationException(Throwable cause) {
+        super(cause);
+        // TODO Auto-generated constructor stub
+    }
+
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodInvocationException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodTransition.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodTransition.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodTransition.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodTransition.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,167 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.finite;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.apache.mina.sm.Event;
+import org.apache.mina.sm.State;
+import org.apache.mina.sm.context.StateContext;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class MethodTransition extends AbstractTransition {
+    private static final Object[] EMPTY_ARGUMENTS = new Object[0];
+    
+    private final Method method;
+    private final Object target;
+
+    public MethodTransition(Object eventId, State nextState, Method method, 
+                            Object target) {
+        super(eventId, nextState);
+        this.method = method;
+        this.target = target;
+    }
+
+    public MethodTransition(Object eventId, Method method, Object target) {
+        this(eventId, null, method, target);
+    }
+    
+    public MethodTransition(Object eventId, State nextState, Object target) {
+        this(eventId, nextState, eventId.toString(), target);
+    }
+    
+    public MethodTransition(Object eventId, Object target) {
+        this(eventId, eventId.toString(), target);
+    }
+
+    public MethodTransition(Object eventId, String methodName, Object target) {
+        this(eventId, null, methodName, target);
+    }
+    
+    public MethodTransition(Object eventId, State nextState, String methodName, 
+                            Object target) {
+        super(eventId, nextState);
+
+        this.target = target;
+        
+        Method[] candidates = target.getClass().getMethods();
+        Method result = null;
+        for (int i = 0; i < candidates.length; i++) {
+            if (candidates[i].getName().equals(methodName)) {
+                if (result != null) {
+                    throw new AmbiguousMethodException(methodName);
+                }
+                result = candidates[i];
+            }
+        }
+        
+        if (result == null) {
+            throw new NoSuchMethodException(methodName);
+        }
+        
+        this.method = result;
+    }
+    
+    public boolean doExecute(Event event) {
+        Class[] types = method.getParameterTypes();
+        
+        if (types.length == 0) {
+            invokeMethod(EMPTY_ARGUMENTS);
+            return true;
+        }
+        
+        if (types.length > 2 + event.getArguments().length) {
+            return false;
+        }
+        
+        Object[] args = new Object[types.length];
+        
+        int i = 0;
+        if (match(types[i], event, Event.class)) {
+            args[i++] = event;
+        }
+        if (i < args.length && match(types[i], event.getContext(), StateContext.class)) {
+            args[i++] = event.getContext();
+        }
+        Object[] eventArgs = event.getArguments();
+        for (int j = 0; i < args.length && j < eventArgs.length; j++) {
+            if (match(types[i], eventArgs[j], Object.class)) {
+                args[i++] = eventArgs[j];
+            }
+        }
+        
+        if (args.length > i) {
+            return false;
+        }
+        
+        invokeMethod(args);
+        
+        return true;
+    }
+    
+    private boolean match(Class paramType, Object arg, Class argType) {
+        if (paramType.isPrimitive()) {
+            if (paramType.equals(Boolean.TYPE)) {
+                return arg instanceof Boolean;
+            }
+            if (paramType.equals(Integer.TYPE)) {
+                return arg instanceof Integer;
+            }
+            if (paramType.equals(Long.TYPE)) {
+                return arg instanceof Long;
+            }
+            if (paramType.equals(Short.TYPE)) {
+                return arg instanceof Short;
+            }
+            if (paramType.equals(Byte.TYPE)) {
+                return arg instanceof Byte;
+            }
+            if (paramType.equals(Double.TYPE)) {
+                return arg instanceof Double;
+            }
+            if (paramType.equals(Float.TYPE)) {
+                return arg instanceof Float;
+            }
+            if (paramType.equals(Character.TYPE)) {
+                return arg instanceof Character;
+            }
+        }
+        return argType.isAssignableFrom(paramType) 
+                && paramType.isAssignableFrom(arg.getClass());
+    }
+
+    private void invokeMethod(Object[] arguments) {
+        try {
+            method.invoke(target, arguments);
+        } catch (InvocationTargetException ite) {
+            if (ite.getCause() instanceof RuntimeException) {
+                throw (RuntimeException) ite.getCause();
+            }
+            throw new MethodInvocationException(ite);
+        } catch (IllegalAccessException iae) {
+            throw new MethodInvocationException(iae);
+        }
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/MethodTransition.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoSuchMethodException.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoSuchMethodException.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoSuchMethodException.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoSuchMethodException.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,62 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.finite;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class NoSuchMethodException extends RuntimeException {
+
+    /**
+     * 
+     */
+    public NoSuchMethodException() {
+        super();
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param message
+     */
+    public NoSuchMethodException(String message) {
+        super(message);
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param message
+     * @param cause
+     */
+    public NoSuchMethodException(String message, Throwable cause) {
+        super(message, cause);
+        // TODO Auto-generated constructor stub
+    }
+
+    /**
+     * @param cause
+     */
+    public NoSuchMethodException(Throwable cause) {
+        super(cause);
+        // TODO Auto-generated constructor stub
+    }
+
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoSuchMethodException.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoopTransition.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoopTransition.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoopTransition.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoopTransition.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,43 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.finite;
+
+import org.apache.mina.sm.Event;
+import org.apache.mina.sm.State;
+
+/**
+ * {@link Transition} implementation which does nothing but change the state.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class NoopTransition extends AbstractTransition {
+
+    public NoopTransition(Object eventId) {
+        super(eventId);
+    }
+
+    public NoopTransition(Object eventId, State nextState) {
+        super(eventId, nextState);
+    }
+    
+    protected boolean doExecute(Event event) {
+        return true;
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/NoopTransition.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/Transition.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/Transition.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/Transition.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/Transition.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,31 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.finite;
+
+import org.apache.mina.sm.Event;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface Transition {
+    boolean execute(Event event);
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/finite/Transition.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/mina/MinaStateContextLookup.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/mina/MinaStateContextLookup.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/mina/MinaStateContextLookup.java (added)
+++ directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/mina/MinaStateContextLookup.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,58 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.mina;
+
+import org.apache.mina.common.IoSession;
+import org.apache.mina.sm.context.AbstractStateContextLookup;
+import org.apache.mina.sm.context.DefaultStateContextFactory;
+import org.apache.mina.sm.context.StateContext;
+import org.apache.mina.sm.context.StateContextFactory;
+
+/**
+ * 
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class MinaStateContextLookup extends AbstractStateContextLookup {
+    public static final String STATE_CONTEXT = 
+        MinaStateContextLookup.class.getName() + ".stateContext";
+    
+    public MinaStateContextLookup() {
+        this(new DefaultStateContextFactory());
+    }
+    
+    public MinaStateContextLookup(StateContextFactory contextFactory) {
+        super(contextFactory);
+    }
+
+    protected StateContext lookup(Object eventArg) {
+        IoSession session = (IoSession) eventArg;
+        return (StateContext) session.getAttribute(STATE_CONTEXT);
+    }
+
+    protected void store(Object eventArg, StateContext context) {
+        IoSession session = (IoSession) eventArg;
+        session.setAttribute(STATE_CONTEXT, context);
+    }
+
+    protected boolean supports(Class c) {
+        return IoSession.class.isAssignableFrom(c);
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/main/java/org/apache/mina/sm/mina/MinaStateContextLookup.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/AbstractStateTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/AbstractStateTest.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/AbstractStateTest.java (added)
+++ directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/AbstractStateTest.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,112 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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;
+
+import org.apache.mina.sm.context.StateContext;
+
+import com.agical.rmock.core.expectation.section.OrderedSection;
+import com.agical.rmock.extension.junit.RMockTestCase;
+
+/**
+ * Tests {@link AbstractState}.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class AbstractStateTest extends RMockTestCase {
+    Event event;
+    State currentState;
+    State nextState;
+    State parentState;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        
+        currentState = (State) mock(State.class);
+        nextState = (State) mock(State.class);
+        parentState = (State) mock(State.class);
+        
+        event = new Event("event", currentState, (StateContext) mock(StateContext.class));
+    }
+
+    public void testHandleDelegatesToParent() throws Exception {
+        parentState.handle(event);
+        startVerification();
+        TestState state = new TestState(null, parentState);
+        state.handle(event);
+    }
+    
+    public void testHandleDoesNotDelegateToParent() throws Exception {
+        startVerification();
+        TestState state = new TestState(currentState, parentState);
+        state.handle(event);
+        assertSame(currentState, event.getNextState());
+    }
+    
+    public void testHandleExecuteEnterAndExitOnTransition() throws Exception {
+        beginSection(new OrderedSection(""));
+        {
+            currentState.exit(event);
+            nextState.enter(event);
+        }
+        endSection();
+        startVerification();
+        TestState state = new TestState(nextState, parentState);
+        state.handle(event);
+    }
+    
+    public void testHandleDoesNotExecuteEnterOrExitOnLoopback() throws Exception {
+        startVerification();
+        TestState state = new TestState();
+        state.next = currentState;
+        state.handle(event);
+    }
+    
+    public void testHandleDoesNotExecuteEnterOrExitOnNoNext() throws Exception {
+        startVerification();
+        TestState state = new TestState();
+        state.handle(event);
+    }
+    
+    static class TestState extends AbstractState {
+        State next = null;
+
+        public TestState() {
+            this(null, null);
+        }
+        
+        public TestState(State next) {
+            this(next, null);
+        }
+
+        public TestState(State next, State parent) {
+            super("test", parent);
+            this.next = next;
+        }
+
+        protected boolean doHandle(Event event) {
+            if (next != null) {
+                event.setNextState(next);
+                return true;
+            }
+            return false;
+        }
+        
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/AbstractStateTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java (added)
+++ directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,114 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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;
+
+import java.util.LinkedList;
+
+import junit.framework.TestCase;
+
+import org.apache.mina.sm.finite.FsmState;
+import org.apache.mina.sm.finite.MethodTransition;
+
+/**
+ * Tests {@link StateMachineProxyFactory}.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class StateMachineProxyFactoryTest extends TestCase
+{
+    private LinkedList messages;
+
+    public void testCreate() throws Exception
+    {
+        messages = new LinkedList();
+        
+        FsmState parent = new FsmState( "parent" );
+        FsmState s1 = new FsmState( "s1", parent );
+        FsmState s2 = new FsmState( "s2", parent );
+        FsmState s3 = new FsmState( "s3", parent );
+        FsmState s4 = new FsmState( "s4", parent );
+        FsmState s5 = new FsmState( "s5", parent );
+        
+        parent.addTransition( new MethodTransition( "*", "error", this ) );
+        s1.addTransition( new MethodTransition( "insert", s2, "inserted", this ) );
+        s2.addTransition( new MethodTransition( "start",  s3, "playing",  this ) );
+        s3.addTransition( new MethodTransition( "stop",   s4, "stopped",  this ) );
+        s3.addTransition( new MethodTransition( "pause",  s5, "paused",   this ) );
+        s4.addTransition( new MethodTransition( "eject",  s1, "ejected",  this ) );
+        s5.addTransition( new MethodTransition( "pause",  s3, "playing",  this ) );
+        
+        TapeDeck player = ( TapeDeck ) StateMachineProxyFactory.create( TapeDeck.class, s1 );
+        player.insert( "Kings of convenience - Riot on an empty street" );
+        player.start();
+        player.pause();
+        player.pause();
+        player.eject();
+        player.stop();
+        player.eject();
+        
+        assertEquals( "Tape 'Kings of convenience - Riot on an empty street' inserted", messages.removeFirst() );
+        assertEquals( "Playing", messages.removeFirst() );
+        assertEquals( "Paused", messages.removeFirst() );
+        assertEquals( "Playing", messages.removeFirst() );
+        assertEquals( "Error: Cannot eject at this time", messages.removeFirst() );
+        assertEquals( "Stopped", messages.removeFirst() );
+        assertEquals( "Tape ejected", messages.removeFirst() );
+        assertTrue( messages.isEmpty() );
+    }
+    
+    public void inserted( String name )
+    {
+        messages.add( "Tape '" + name + "' inserted" );
+    }
+    
+    public void ejected()
+    {
+        messages.add( "Tape ejected" );
+    }
+    
+    public void playing()
+    {
+        messages.add( "Playing" );
+    }
+    
+    public void paused()
+    {
+        messages.add( "Paused" );
+    }
+    
+    public void stopped()
+    {
+        messages.add( "Stopped" );
+    }
+    
+    public void error( Event event )
+    {
+        messages.add( "Error: Cannot " + event.getId() + " at this time" );
+    }
+    
+    public interface TapeDeck
+    {
+        void insert( String name );
+        void eject();
+        void start();
+        void pause();
+        void stop();
+    }
+}

Propchange: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/StateMachineProxyFactoryTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/context/AbstractStateContextLookupTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/context/AbstractStateContextLookupTest.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/context/AbstractStateContextLookupTest.java (added)
+++ directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/context/AbstractStateContextLookupTest.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,58 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.context;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests {@link AbstractStateContextLookup}.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class AbstractStateContextLookupTest extends TestCase {
+
+    public void testLookup() throws Exception {
+        Map map = new HashMap();
+        AbstractStateContextLookup lookup = new AbstractStateContextLookup(
+                                             new DefaultStateContextFactory()) {
+            protected boolean supports(Class c) {
+                return Map.class.isAssignableFrom(c);
+            }
+            protected StateContext lookup(Object eventArg) {
+                Map map = (Map) eventArg;
+                return (StateContext) map.get("context");
+            }
+            protected void store(Object eventArg, StateContext context) {
+                Map map = (Map) eventArg;
+                map.put("context", context);
+            }
+        };
+        Object[] args1 = new Object[] {new Object(), map, new Object()};
+        Object[] args2 = new Object[] {map, new Object()};
+        StateContext sc = lookup.lookup(args1);
+        assertSame(map.get("context"), sc);
+        assertSame(map.get("context"), lookup.lookup(args1));
+        assertSame(map.get("context"), lookup.lookup(args2));
+    }
+    
+}

Propchange: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/context/AbstractStateContextLookupTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/FsmStateTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/FsmStateTest.java?rev=412347&view=auto
==============================================================================
--- directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/FsmStateTest.java (added)
+++ directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/FsmStateTest.java Wed Jun  7 02:49:17 2006
@@ -0,0 +1,147 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 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.finite;
+
+import org.apache.mina.sm.Event;
+import org.apache.mina.sm.State;
+import org.apache.mina.sm.context.StateContext;
+
+import com.agical.rmock.core.Action;
+import com.agical.rmock.core.MethodHandle;
+import com.agical.rmock.core.expectation.section.OrderedSection;
+import com.agical.rmock.extension.junit.RMockTestCase;
+
+/**
+ * Tests {@link FsmState}.
+ *
+ * @author The Apache Directory Project (mina-dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class FsmStateTest extends RMockTestCase {
+    Event event;
+    State parentState;
+    State currentState;
+    State nextState;
+    FsmState state;
+    Transition transition1;
+    Transition transition2;
+    Transition transition3;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        
+        currentState = (State) mock(State.class);
+        nextState = (State) mock(State.class);
+        parentState = (State) mock(State.class);
+        state = new FsmState("test");
+        transition1 = (Transition) mock(Transition.class);
+        transition2 = (Transition) mock(Transition.class);
+        transition3 = (Transition) mock(Transition.class);
+        
+        event = new Event("event", currentState, (StateContext) mock(StateContext.class));
+    }
+
+    public void testAddFirstTransition() throws Exception {
+        assertTrue(state.getTransitions().isEmpty());
+        state.addTransition(transition1);
+        assertFalse(state.getTransitions().isEmpty());
+    }
+    
+    public void testAddTransitionReturnsSelf() throws Exception {
+        assertSame(state, state.addTransition(transition1));
+    }
+    
+    public void testAddNullTransitionThrowsException() throws Exception {
+        try {
+            state.addTransition(null);
+            fail("null transition added. NullPointerException expected.");
+        } catch (NullPointerException npe) {
+        }
+    }
+    
+    public void testHandle() throws Exception {
+        beginSection(new OrderedSection(""));
+        {
+            transition1.execute(event);
+            modify().returnValue(false);
+            transition2.execute(event);
+            modify().perform(new Action() {
+                public Object invocation(Object[] args, MethodHandle mh) throws Throwable {
+                    event.setNextState(nextState);
+                    return Boolean.TRUE;
+                }
+            });
+            currentState.exit(event);
+            nextState.enter(event);
+        }
+        endSection();
+        
+        startVerification();
+        
+        state.addTransition(transition1);
+        state.addTransition(transition2);
+        state.addTransition(transition3);
+        
+        state.handle(event);
+    }
+    
+    public void testHandleNoNextState() throws Exception {
+        beginSection(new OrderedSection(""));
+        {
+            transition1.execute(event);
+            modify().returnValue(false);
+            transition2.execute(event);
+            modify().returnValue(false);
+            transition3.execute(event);
+            modify().returnValue(false);
+        }
+        endSection();
+        
+        startVerification();
+        
+        state.addTransition(transition1);
+        state.addTransition(transition2);
+        state.addTransition(transition3);
+        
+        state.handle(event);
+    }
+    
+    public void testHandleDelegatesToParent() throws Exception {
+        state = new FsmState("test", parentState);
+        beginSection(new OrderedSection(""));
+        {
+            transition1.execute(event);
+            modify().returnValue(false);
+            transition2.execute(event);
+            modify().returnValue(false);
+            transition3.execute(event);
+            modify().returnValue(false);
+            parentState.handle(event);
+        }
+        endSection();
+        
+        startVerification();
+        
+        state.addTransition(transition1);
+        state.addTransition(transition2);
+        state.addTransition(transition3);
+        
+        state.handle(event);
+    }       
+}

Propchange: directory/sandbox/niklas/mina-sm/src/test/java/org/apache/mina/sm/finite/FsmStateTest.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message