You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by ni...@apache.org on 2007/11/06 17:03:43 UTC

svn commit: r592475 - in /mina/trunk: example/src/main/java/org/apache/mina/example/tapedeck/ statemachine/src/main/java/org/apache/mina/statemachine/ statemachine/src/main/java/org/apache/mina/statemachine/annotation/ statemachine/src/test/java/org/ap...

Author: niklas
Date: Tue Nov  6 08:03:42 2007
New Revision: 592475

URL: http://svn.apache.org/viewvc?rev=592475&view=rev
Log:
DIRMINA-463: Changed StateMachineFactory to make it simpler to specify the transition annotation to use. Removed IoHandlerStateMachineFactory and IoFilterStateMachineFactory.

Added:
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/TransitionAnnotation.java   (with props)
Removed:
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/IoFilterStateMachineFactory.java
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/IoHandlerStateMachineFactory.java
Modified:
    mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineFactory.java
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoFilterTransition.java
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoHandlerTransition.java
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/Transition.java
    mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineFactoryTest.java
    mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyFactoryTest.java

Modified: mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java
URL: http://svn.apache.org/viewvc/mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java?rev=592475&r1=592474&r2=592475&view=diff
==============================================================================
--- mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java (original)
+++ mina/trunk/example/src/main/java/org/apache/mina/example/tapedeck/Main.java Tue Nov  6 08:03:42 2007
@@ -24,9 +24,10 @@
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.filter.codec.ProtocolCodecFilter;
 import org.apache.mina.filter.codec.textline.TextLineEncoder;
-import org.apache.mina.statemachine.IoHandlerStateMachineFactory;
 import org.apache.mina.statemachine.StateMachine;
+import org.apache.mina.statemachine.StateMachineFactory;
 import org.apache.mina.statemachine.StateMachineProxyFactory;
+import org.apache.mina.statemachine.annotation.IoHandlerTransition;
 import org.apache.mina.statemachine.context.IoSessionStateContextLookup;
 import org.apache.mina.statemachine.context.StateContext;
 import org.apache.mina.statemachine.context.StateContextFactory;
@@ -34,8 +35,8 @@
 import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
 
 /**
- * Simple example on how to build a state machine for MINA's {@link IoHandler}
- * interface.
+ * Simple example demonstrating how to build a state machine for MINA's 
+ * {@link IoHandler} interface.
  *
  * @author The Apache MINA Project (dev@mina.apache.org)
  * @version $Rev$, $Date$
@@ -45,7 +46,8 @@
     private static final int PORT = 12345;
     
     private static IoHandler createIoHandler() {
-        StateMachine sm = IoHandlerStateMachineFactory.create(TapeDeckServer.EMPTY, new TapeDeckServer());
+        StateMachine sm = StateMachineFactory.getInstance(IoHandlerTransition.class)
+                                .create(TapeDeckServer.EMPTY, new TapeDeckServer());
         
         return (IoHandler) StateMachineProxyFactory.create(IoHandler.class, sm, 
                 new IoSessionStateContextLookup(new StateContextFactory() {

Modified: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineFactory.java
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineFactory.java?rev=592475&r1=592474&r2=592475&view=diff
==============================================================================
--- mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineFactory.java (original)
+++ mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineFactory.java Tue Nov  6 08:03:42 2007
@@ -33,6 +33,7 @@
 import java.util.Map;
 
 import org.apache.mina.statemachine.annotation.Transition;
+import org.apache.mina.statemachine.annotation.TransitionAnnotation;
 import org.apache.mina.statemachine.annotation.Transitions;
 import org.apache.mina.statemachine.event.Event;
 import org.apache.mina.statemachine.transition.MethodTransition;
@@ -40,7 +41,7 @@
 
 /**
  * Creates {@link StateMachine}s by reading {@link org.apache.mina.statemachine.annotation.State},
- * {@link Transition} and {@link Transitions} annotations from one or more arbitrary 
+ * {@link Transition} and {@link Transitions} (or equivalent) annotations from one or more arbitrary 
  * objects.
  * 
  *
@@ -48,8 +49,23 @@
  * @version $Rev$, $Date$
  */
 public class StateMachineFactory {
+    private final Class<? extends Annotation> transitionAnnotation;
+    private final Class<? extends Annotation> transitionsAnnotation;
 
-    private StateMachineFactory() {
+    protected StateMachineFactory(Class<? extends Annotation> transitionAnnotation, 
+                Class<? extends Annotation> transitionsAnnotation) {
+        this.transitionAnnotation = transitionAnnotation;
+        this.transitionsAnnotation = transitionsAnnotation;
+    }
+    
+    public static StateMachineFactory getInstance(Class<? extends Annotation> transitionAnnotation) {
+        TransitionAnnotation a = transitionAnnotation.getAnnotation(TransitionAnnotation.class);
+        if (a == null) {
+            throw new IllegalArgumentException("The annotation class " 
+                    + transitionAnnotation + " has not been annotated with the " 
+                    + TransitionAnnotation.class.getName() + " annotation");
+        }
+        return new StateMachineFactory(transitionAnnotation, a.value());
     }
     
     /**
@@ -60,7 +76,7 @@
      *        state machine.
      * @return the {@link StateMachine} object.
      */
-    public static StateMachine create(Object handler) {
+    public StateMachine create(Object handler) {
         return create(handler, new Object[0]);
     }
 
@@ -73,7 +89,7 @@
      *        state machine.
      * @return the {@link StateMachine} object.
      */
-    public static StateMachine create(String start, Object handler) {
+    public StateMachine create(String start, Object handler) {
         return create(start, handler, new Object[0]);
     }
 
@@ -87,7 +103,7 @@
      *        annotations describing the state machine.
      * @return the {@link StateMachine} object.
      */
-    public static StateMachine create(Object handler, Object... handlers) {
+    public StateMachine create(Object handler, Object... handlers) {
         return create("start", handler, handlers);
     }
     
@@ -102,31 +118,7 @@
      *        annotations describing the state machine.
      * @return the {@link StateMachine} object.
      */
-    public static StateMachine create(String start, Object handler, Object... handlers) {
-        return create(Transition.class, Transitions.class, start, handler, handlers);
-    }
-    
-    /**
-     * Creates a new {@link StateMachine} from the specified handler objects and
-     * using the {@link State} with the specified id as start state. Use this
-     * method if you want to use your own alternatives to the {@link Transition}
-     * and {@link Transitions} annotations.
-     * 
-     * @param transitionAnnotation the annotation to use instead of {@link Transition}. 
-     *        The annotation must have the same parameters as {@link Transition} 
-     *        but the <code>on</code> parameter may be of an enum type instead of string.
-     * @param transitionsAnnotation the annotation to use instead of {@link Transitions}. 
-     *        The annotation must have the same parameters as {@link Transitions}. 
-     * @param start the id of the start {@link State} to use.
-     * @param handler the first object containing the annotations describing the 
-     *        state machine.
-     * @param handlers zero or more additional objects containing the 
-     *        annotations describing the state machine.
-     * @return the {@link StateMachine} object.
-     */
-    public static StateMachine create(Class<? extends Annotation> transitionAnnotation, 
-            Class<? extends Annotation> transitionsAnnotation, 
-            String start, Object handler, Object... handlers) {
+    public StateMachine create(String start, Object handler, Object... handlers) {
         
         Map<String, State> states = new HashMap<String, State>();
         List<Object> handlersList = new ArrayList<Object>(1 + handlers.length);

Modified: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoFilterTransition.java
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoFilterTransition.java?rev=592475&r1=592474&r2=592475&view=diff
==============================================================================
--- mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoFilterTransition.java (original)
+++ mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoFilterTransition.java Tue Nov  6 08:03:42 2007
@@ -39,9 +39,8 @@
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
+@TransitionAnnotation(IoFilterTransitions.class)
 public @interface IoFilterTransition {
-    public static final String SELF = "__self__";
-
     /**
      * Specifies the ids of one or more events handled by the annotated method. If
      * not specified the handler method will be executed for any event.
@@ -59,7 +58,7 @@
      * executing the annotated method. If not specified the {@link StateMachine}
      * will remain in the same state.
      */
-    String next() default SELF;
+    String next() default Transition.SELF;
 
     /**
      * The weight used to order handler annotations which match the same event 

Modified: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoHandlerTransition.java
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoHandlerTransition.java?rev=592475&r1=592474&r2=592475&view=diff
==============================================================================
--- mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoHandlerTransition.java (original)
+++ mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/IoHandlerTransition.java Tue Nov  6 08:03:42 2007
@@ -39,9 +39,8 @@
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
+@TransitionAnnotation(IoHandlerTransitions.class)
 public @interface IoHandlerTransition {
-    public static final String SELF = "__self__";
-
     /**
      * Specifies the ids of one or more events handled by the annotated method. If
      * not specified the handler method will be executed for any event.
@@ -59,7 +58,7 @@
      * executing the annotated method. If not specified the {@link StateMachine}
      * will remain in the same state.
      */
-    String next() default SELF;
+    String next() default Transition.SELF;
 
     /**
      * The weight used to order handler annotations which match the same event 

Modified: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/Transition.java
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/Transition.java?rev=592475&r1=592474&r2=592475&view=diff
==============================================================================
--- mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/Transition.java (original)
+++ mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/Transition.java Tue Nov  6 08:03:42 2007
@@ -36,6 +36,7 @@
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
+@TransitionAnnotation(Transitions.class)
 public @interface Transition {
     public static final String SELF = "__self__";
 

Added: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/TransitionAnnotation.java
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/TransitionAnnotation.java?rev=592475&view=auto
==============================================================================
--- mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/TransitionAnnotation.java (added)
+++ mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/TransitionAnnotation.java Tue Nov  6 08:03:42 2007
@@ -0,0 +1,40 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.statemachine.annotation;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation used to mark other annotations as being transition annotations.
+ * The annotation used to group transition annotations must be given as 
+ * parameter.
+ *
+ * @author The Apache MINA Project (dev@mina.apache.org)
+ * @version $Rev$, $Date$
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.ANNOTATION_TYPE)
+public @interface TransitionAnnotation {
+    Class<? extends Annotation> value();
+}

Propchange: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/TransitionAnnotation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/annotation/TransitionAnnotation.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date Id

Modified: mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineFactoryTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineFactoryTest.java?rev=592475&r1=592474&r2=592475&view=diff
==============================================================================
--- mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineFactoryTest.java (original)
+++ mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineFactoryTest.java Tue Nov  6 08:03:42 2007
@@ -57,7 +57,7 @@
 
     public void testCreate() throws Exception {
         States states = new States();
-        StateMachine sm = StateMachineFactory.create(States.A, states);
+        StateMachine sm = StateMachineFactory.getInstance(Transition.class).create(States.A, states);
 
         State a = sm.getState(States.A);
         State b = sm.getState(States.B);

Modified: mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyFactoryTest.java
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyFactoryTest.java?rev=592475&r1=592474&r2=592475&view=diff
==============================================================================
--- mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyFactoryTest.java (original)
+++ mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyFactoryTest.java Tue Nov  6 08:03:42 2007
@@ -99,7 +99,7 @@
     public void testTapeDeckStateMachineAnnotations() throws Exception {
         TapeDeckStateMachineHandler handler = new TapeDeckStateMachineHandler();
 
-        StateMachine sm = StateMachineFactory.create(TapeDeckStateMachineHandler.S1, handler);
+        StateMachine sm = StateMachineFactory.getInstance(Transition.class).create(TapeDeckStateMachineHandler.S1, handler);
 
         TapeDeck player = (TapeDeck) StateMachineProxyFactory.create(TapeDeck.class, sm);
         player.insert("Kings of convenience - Riot on an empty street");