View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.statemachine.transition;
21  
22  import java.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  import java.util.Arrays;
25  
26  import org.apache.mina.statemachine.State;
27  import org.apache.mina.statemachine.StateMachine;
28  import org.apache.mina.statemachine.context.StateContext;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * {@link SelfTransition} which invokes a {@link Method}. The {@link Method} can
34   * have zero or any number of StateContext and State regarding order
35   * <p>
36   * Normally you wouldn't create instances of this class directly but rather use
37   * the {@link SelfTransition} annotation to define the methods which should be
38   * used as transitions in your state machine and then let
39   * {@link org.apache.mina.statemachine.StateMachineFactory} create a
40   * {@link StateMachine} for you.
41   * </p>
42   * 
43   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
44   */
45  public class MethodSelfTransition extends AbstractSelfTransition {
46      private static final Logger LOGGER = LoggerFactory.getLogger(MethodTransition.class);
47  
48      private Method method;
49  
50      private final Object target;
51  
52      private static final Object[] EMPTY_ARGUMENTS = new Object[0];
53  
54      /**
55       * Creates a new MethodSelfTransition instance
56       * 
57       * @param method The method to invoke 
58       * @param target The target object
59       */
60      public MethodSelfTransition(Method method, Object target) {
61          super();
62          this.method = method;
63          this.target = target;
64      }
65  
66      /**
67       * Creates a new instance
68       * 
69       * @param methodName the target method.
70       * @param target the target object.
71       */
72      public MethodSelfTransition(String methodName, Object target) {
73  
74          this.target = target;
75  
76          Method[] candidates = target.getClass().getMethods();
77          Method result = null;
78  
79          for (Method candidate : candidates) {
80              if (candidate.getName().equals(methodName)) {
81                  if (result != null) {
82                      throw new AmbiguousMethodException(methodName);
83                  }
84                  
85                  result = candidate;
86              }
87          }
88  
89          if (result == null) {
90              throw new NoSuchMethodException(methodName);
91          }
92  
93          this.method = result;
94  
95      }
96  
97      /**
98       * @return the target {@link Method}.
99       */
100     public Method getMethod() {
101         return method;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public boolean doExecute(StateContext stateContext, State state) {
109         Class<?>[] types = method.getParameterTypes();
110 
111         if (types.length == 0) {
112             invokeMethod(EMPTY_ARGUMENTS);
113             
114             return true;
115         }
116 
117         if (types.length > 2) {
118             return false;
119         }
120 
121         Object[] args = new Object[types.length];
122 
123         int i = 0;
124 
125         if (types[i].isAssignableFrom(StateContext.class)) {
126             args[i++] = stateContext;
127         }
128         
129         if ((i < types.length) && types[i].isAssignableFrom(State.class)) {
130             args[i++] = state;
131         }
132 
133         invokeMethod(args);
134 
135         return true;
136     }
137 
138     private void invokeMethod(Object[] arguments) {
139         try {
140             if (LOGGER.isDebugEnabled()) {
141                 LOGGER.debug("Executing method " + method + " with arguments " + Arrays.asList(arguments));
142             }
143             
144             method.invoke(target, arguments);
145         } catch (InvocationTargetException ite) {
146             if (ite.getCause() instanceof RuntimeException) {
147                 throw (RuntimeException) ite.getCause();
148             }
149             
150             throw new MethodInvocationException(method, ite);
151         } catch (IllegalAccessException iae) {
152             throw new MethodInvocationException(method, iae);
153         }
154     }
155 }