mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
Subject svn commit: r641052 - in /mina/trunk/statemachine/src: main/java/org/apache/mina/statemachine/ main/java/org/apache/mina/statemachine/event/ test/java/org/apache/mina/statemachine/
Date Tue, 25 Mar 2008 22:22:41 GMT
Author: niklas
Date: Tue Mar 25 15:22:35 2008
New Revision: 641052

URL: http://svn.apache.org/viewvc?rev=641052&view=rev
Log:
Fixed DIRMINA-559: StateMachineProxyFactory has been renamed to StateMachineProxyBuilder and
there are now some boolean flags which control whether methods on the proxied interfaces which
cannot be converted into events should be ignored or not.

Added:
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyBuilder.java
      - copied, changed from r635320, mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyFactory.java
    mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyBuilderTest.java
      - copied, changed from r635320, mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyFactoryTest.java
Removed:
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyFactory.java
    mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyFactoryTest.java
Modified:
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachine.java
    mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/event/EventFactory.java

Modified: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachine.java
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachine.java?rev=641052&r1=641051&r2=641052&view=diff
==============================================================================
--- mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachine.java (original)
+++ mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachine.java Tue
Mar 25 15:22:35 2008
@@ -116,7 +116,7 @@
     /**
      * Processes the specified {@link Event} through this {@link StateMachine}.
      * Normally you wouldn't call this directly but rather use
-     * {@link StateMachineProxyFactory} to create a proxy for an interface of
+     * {@link StateMachineProxyBuilder} to create a proxy for an interface of
      * your choice. Any method calls on the proxy will be translated into
      * {@link Event} objects and then fed to the {@link StateMachine} by the
      * proxy using this method.

Copied: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyBuilder.java
(from r635320, mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyFactory.java)
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyBuilder.java?p2=mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyBuilder.java&p1=mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyFactory.java&r1=635320&r2=641052&rev=641052&view=diff
==============================================================================
--- mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyFactory.java
(original)
+++ mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/StateMachineProxyBuilder.java
Tue Mar 25 15:22:35 2008
@@ -30,6 +30,7 @@
 import org.apache.mina.statemachine.event.Event;
 import org.apache.mina.statemachine.event.EventArgumentsInterceptor;
 import org.apache.mina.statemachine.event.EventFactory;
+import org.apache.mina.statemachine.event.UnhandledEventException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -40,49 +41,63 @@
  * @author The Apache MINA Project (dev@mina.apache.org)
  * @version $Rev$, $Date$
  */
-public class StateMachineProxyFactory {
-    private static final Logger log = LoggerFactory.getLogger(StateMachineProxyFactory.class);
-    
+public class StateMachineProxyBuilder {
+    private static final Logger log = LoggerFactory
+            .getLogger(StateMachineProxyBuilder.class);
+
     private static final Object[] EMPTY_ARGUMENTS = new Object[0];
 
-    private StateMachineProxyFactory() {
+    private StateContextLookup contextLookup = new SingletonStateContextLookup();
+
+    private EventFactory eventFactory = new DefaultEventFactory();
+
+    private EventArgumentsInterceptor interceptor = null;
+
+    private boolean ignoreUnhandledEvents = false;
+
+    private boolean ignoreStateContextLookupFailure = false;
+
+    public StateMachineProxyBuilder() {
     }
 
-    @SuppressWarnings("unchecked")
-    public static <T> T create(Class<T> iface, StateMachine sm) {
-        return (T) create(new Class[] { iface }, sm);
+    public StateMachineProxyBuilder setStateContextLookup(
+            StateContextLookup contextLookup) {
+        this.contextLookup = contextLookup;
+        return this;
     }
-    
-    @SuppressWarnings("unchecked")
-    public static <T> T create(Class<T> iface, StateMachine sm, StateContextLookup
contextLookup) {
-        return (T) create(new Class[] { iface }, sm, contextLookup);
+
+    public StateMachineProxyBuilder setEventFactory(EventFactory eventFactory) {
+        this.eventFactory = eventFactory;
+        return this;
     }
-    
-    @SuppressWarnings("unchecked")
-    public static <T> T create(Class<T> iface, StateMachine sm, StateContextLookup
contextLookup,
+
+    public StateMachineProxyBuilder setEventArgumentsInterceptor(
             EventArgumentsInterceptor interceptor) {
-        return (T) create(new Class[] { iface }, sm, contextLookup, interceptor, new DefaultEventFactory());
+        this.interceptor = interceptor;
+        return this;
     }
 
-    @SuppressWarnings("unchecked")
-    public static <T> T create(Class<T> iface, StateMachine sm, StateContextLookup
contextLookup,
-            EventArgumentsInterceptor interceptor, EventFactory eventFactory) {
-        return (T) create(new Class[] { iface }, sm, contextLookup, interceptor, eventFactory);
+    public StateMachineProxyBuilder setIgnoreUnhandledEvents(boolean b) {
+        this.ignoreUnhandledEvents = b;
+        return this;
     }
-    
-    public static Object create(Class<?>[] ifaces, StateMachine sm) {
-        return create(ifaces, sm, new SingletonStateContextLookup());
+
+    public StateMachineProxyBuilder setIgnoreStateContextLookupFailure(boolean b) {
+        this.ignoreStateContextLookupFailure = b;
+        return this;
     }
-    
-    public static Object create(Class<?>[] ifaces, StateMachine sm, StateContextLookup
contextLookup) {
-        return create(ifaces, sm, contextLookup, null, new DefaultEventFactory());
+
+    @SuppressWarnings("unchecked")
+    public <T> T create(Class<T> iface, StateMachine sm) {
+        return (T) create(new Class[] { iface }, sm);
     }
-    
-    public static Object create(Class<?>[] ifaces, StateMachine sm, StateContextLookup
contextLookup,
-            EventArgumentsInterceptor interceptor, EventFactory eventFactory) {
 
-        ClassLoader cl = StateMachineProxyFactory.class.getClassLoader();
-        InvocationHandler handler = new MethodInvocationHandler(sm, contextLookup, interceptor,
eventFactory);
+    public Object create(Class<?>[] ifaces, StateMachine sm) {
+
+        ClassLoader cl = StateMachineProxyBuilder.class.getClassLoader();
+        InvocationHandler handler = new MethodInvocationHandler(sm,
+                contextLookup, interceptor, eventFactory,
+                ignoreUnhandledEvents, ignoreStateContextLookupFailure);
 
         return Proxy.newProxyInstance(cl, ifaces, handler);
     }
@@ -92,14 +107,19 @@
         private final StateContextLookup contextLookup;
         private final EventArgumentsInterceptor interceptor;
         private final EventFactory eventFactory;
+        private final boolean ignoreUnhandledEvents;
+        private final boolean ignoreStateContextLookupFailure;
         
         public MethodInvocationHandler(StateMachine sm, StateContextLookup contextLookup,
-                EventArgumentsInterceptor interceptor, EventFactory eventFactory) {
+                EventArgumentsInterceptor interceptor, EventFactory eventFactory,
+                boolean ignoreUnhandledEvents, boolean ignoreStateContextLookupFailure) {
             
             this.contextLookup = contextLookup;
             this.sm = sm;
             this.interceptor = interceptor;
             this.eventFactory = eventFactory;
+            this.ignoreUnhandledEvents = ignoreUnhandledEvents;
+            this.ignoreStateContextLookupFailure = ignoreStateContextLookupFailure;
         }
         
         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
@@ -126,13 +146,22 @@
             StateContext context = contextLookup.lookup(args);
 
             if (context == null) {
-                throw new IllegalStateException("Cannot determine state " 
+                if (ignoreStateContextLookupFailure) {
+                    return null;
+                }
+                throw new IllegalStateException("Cannot determine state "
                         + "context for method invocation: " + method);
             }
 
             Event event = eventFactory.create(context, method, args);
 
-            sm.handle(event);
+            try {
+                sm.handle(event);
+            } catch (UnhandledEventException uee) {
+                if (!ignoreUnhandledEvents) {
+                    throw uee;
+                }
+            }
 
             return null;
         }

Modified: mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/event/EventFactory.java
URL: http://svn.apache.org/viewvc/mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/event/EventFactory.java?rev=641052&r1=641051&r2=641052&view=diff
==============================================================================
--- mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/event/EventFactory.java
(original)
+++ mina/trunk/statemachine/src/main/java/org/apache/mina/statemachine/event/EventFactory.java
Tue Mar 25 15:22:35 2008
@@ -21,11 +21,11 @@
 
 import java.lang.reflect.Method;
 
-import org.apache.mina.statemachine.StateMachineProxyFactory;
+import org.apache.mina.statemachine.StateMachineProxyBuilder;
 import org.apache.mina.statemachine.context.StateContext;
 
 /**
- * Used by {@link StateMachineProxyFactory} to create {@link Event} objects when 
+ * Used by {@link StateMachineProxyBuilder} to create {@link Event} objects when 
  * methods are invoked on the proxy.
  *
  * @author The Apache MINA Project (dev@mina.apache.org)

Copied: mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyBuilderTest.java
(from r635320, 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/StateMachineProxyBuilderTest.java?p2=mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyBuilderTest.java&p1=mina/trunk/statemachine/src/test/java/org/apache/mina/statemachine/StateMachineProxyFactoryTest.java&r1=635320&r2=641052&rev=641052&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/StateMachineProxyBuilderTest.java
Tue Mar 25 15:22:35 2008
@@ -29,12 +29,12 @@
 import org.apache.mina.statemachine.transition.MethodTransition;
 
 /**
- * Tests {@link StateMachineProxyFactory}.
+ * Tests {@link StateMachineProxyBuilder}.
  *
  * @author The Apache MINA Project (dev@mina.apache.org)
  * @version $Rev$, $Date$
  */
-public class StateMachineProxyFactoryTest extends TestCase {
+public class StateMachineProxyBuilderTest extends TestCase {
 
     public void testReentrantStateMachine() throws Exception {
         ReentrantStateMachineHandler handler = new ReentrantStateMachineHandler();
@@ -48,7 +48,7 @@
         s3.addTransition(new MethodTransition("call3", handler));
 
         StateMachine sm = new StateMachine(new State[] { s1, s2, s3 }, "s1");
-        Reentrant reentrant = StateMachineProxyFactory.create(Reentrant.class, sm);
+        Reentrant reentrant = new StateMachineProxyBuilder().create(Reentrant.class, sm);
         reentrant.call1(reentrant);
         assertTrue(handler.finished);
     }
@@ -72,7 +72,7 @@
         s5.addTransition(new MethodTransition("pause", s3, "playing", handler));
 
         StateMachine sm = new StateMachine(new State[] { s1, s2, s3, s4, s5 }, "s1");
-        TapeDeck player = StateMachineProxyFactory.create(TapeDeck.class, sm);
+        TapeDeck player = new StateMachineProxyBuilder().create(TapeDeck.class, sm);
         player.insert("Kings of convenience - Riot on an empty street");
         player.start();
         player.pause();
@@ -97,7 +97,7 @@
 
         StateMachine sm = StateMachineFactory.getInstance(Transition.class).create(TapeDeckStateMachineHandler.S1,
handler);
 
-        TapeDeck player = StateMachineProxyFactory.create(TapeDeck.class, sm);
+        TapeDeck player = new StateMachineProxyBuilder().create(TapeDeck.class, sm);
         player.insert("Kings of convenience - Riot on an empty street");
         player.start();
         player.pause();



Mime
View raw message