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  package org.apache.shiro.event.support;
20  
21  import java.lang.reflect.Method;
22  import java.lang.reflect.Modifier;
23  
24  /**
25   * A event listener that invokes a target object's method that accepts a single event argument.
26   *
27   * @since 1.3
28   */
29  public class SingleArgumentMethodEventListener implements TypedEventListener {
30  
31      private final Object target;
32      private final Method method;
33  
34      public SingleArgumentMethodEventListener(Object target, Method method) {
35          this.target = target;
36          this.method = method;
37          //assert that the method is defined as expected:
38          getMethodArgumentType(method);
39  
40          assertPublicMethod(method);
41      }
42  
43      public Object getTarget() {
44          return this.target;
45      }
46  
47      public Method getMethod() {
48          return this.method;
49      }
50  
51      private void assertPublicMethod(Method method) {
52          int modifiers = method.getModifiers();
53          if (!Modifier.isPublic(modifiers)) {
54              throw new IllegalArgumentException("Event handler method [" + method + "] must be public.");
55          }
56      }
57  
58      public boolean accepts(Object event) {
59          return event != null && getEventType().isInstance(event);
60      }
61  
62      public Class getEventType() {
63          return getMethodArgumentType(getMethod());
64      }
65  
66      public void onEvent(Object event) {
67          Method method = getMethod();
68          try {
69              method.invoke(getTarget(), event);
70          } catch (Exception e) {
71              throw new IllegalStateException("Unable to invoke event handler method [" + method + "].", e);
72          }
73      }
74  
75      protected Class getMethodArgumentType(Method method) {
76          Class[] paramTypes = method.getParameterTypes();
77          if (paramTypes.length != 1) {
78              //the default implementation expects a single typed argument and nothing more:
79              String msg = "Event handler methods must accept a single argument.";
80              throw new IllegalArgumentException(msg);
81          }
82          return paramTypes[0];
83      }
84  }