|
1 | | - /* |
2 | | - * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | - * contributor license agreements. See the NOTICE file distributed with |
4 | | - * this work for additional information regarding copyright ownership. |
5 | | - * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | - * (the "License"); you may not use this file except in compliance with |
7 | | - * the License. You may obtain a copy of the License at |
8 | | - * |
9 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | - * |
11 | | - * Unless required by applicable law or agreed to in writing, software |
12 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | - * See the License for the specific language governing permissions and |
15 | | - * limitations under the License. |
16 | | - */ |
| 1 | +/* |
| 2 | +* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +* contributor license agreements. See the NOTICE file distributed with |
| 4 | +* this work for additional information regarding copyright ownership. |
| 5 | +* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +* (the "License"); you may not use this file except in compliance with |
| 7 | +* the License. You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | +*/ |
17 | 17 |
|
18 | | - /** |
| 18 | +/** |
19 | 19 | * Project : IQQ_V2.1 |
20 | 20 | * Package : iqq.app.action |
21 | 21 | * File : IMEventHandlerProxy.java |
22 | 22 | * Author : solosky < [email protected] > |
23 | 23 | * Created : 2013-3-16 |
24 | | - * License : Apache License 2.0 |
| 24 | + * License : Apache License 2.0 |
25 | 25 | */ |
26 | 26 | package iqq.api.event; |
27 | 27 |
|
28 | | - import iqq.api.annotation.IMEventHandler; |
29 | | - import org.slf4j.Logger; |
30 | | - import org.slf4j.LoggerFactory; |
| 28 | +import iqq.api.annotation.IMEventHandler; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import java.lang.reflect.Method; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +/** |
| 37 | + * 使用这个类简化事件的注册,分发 |
| 38 | + * 只需在被代理的类使用@IMEventHandler注解需要处理的事件类型即可 |
| 39 | + * |
| 40 | + * @author solosky <[email protected]> |
| 41 | + */ |
| 42 | +public class IMEventDispatcher implements IMEventListener { |
| 43 | + private static final Logger LOG = LoggerFactory.getLogger(IMEventDispatcher.class); |
| 44 | + private Map<IMEventType, Method> methodMap = new HashMap<IMEventType, Method>(); |
31 | 45 |
|
32 | | - import java.lang.reflect.Method; |
33 | | - import java.util.HashMap; |
34 | | - import java.util.Map; |
| 46 | + public IMEventDispatcher() { |
| 47 | + for (Method m : this.getClass().getDeclaredMethods()) { |
| 48 | + if (m.isAnnotationPresent(IMEventHandler.class)) { |
| 49 | + IMEventHandler handler = m.getAnnotation(IMEventHandler.class); |
| 50 | + this.methodMap.put(handler.value(), m); |
| 51 | + if (!m.isAccessible()) { |
| 52 | + m.setAccessible(true); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + for (Method m : this.getClass().getMethods()) { |
| 57 | + if (m.isAnnotationPresent(IMEventHandler.class)) { |
| 58 | + IMEventHandler handler = m.getAnnotation(IMEventHandler.class); |
| 59 | + this.methodMap.put(handler.value(), m); |
| 60 | + if (!m.isAccessible()) { |
| 61 | + m.setAccessible(true); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
35 | 66 |
|
36 | | - /** |
37 | | - * |
38 | | - * 使用这个类简化事件的注册,分发 |
39 | | - * 只需在被代理的类使用@IMEventHandler注解需要处理的事件类型即可 |
40 | | - * |
41 | | - * @author solosky <[email protected]> |
42 | | - * |
43 | | - */ |
44 | | - public class IMEventDispatcher implements IMEventListener{ |
45 | | - private static final Logger LOG = LoggerFactory.getLogger(IMEventDispatcher.class); |
46 | | - private Map<IMEventType, Method> methodMap = new HashMap<IMEventType, Method>(); |
47 | | - public IMEventDispatcher(){ |
48 | | - for (Method m : this.getClass().getDeclaredMethods()) { |
49 | | - if(m.isAnnotationPresent(IMEventHandler.class)){ |
50 | | - IMEventHandler handler = m.getAnnotation(IMEventHandler.class); |
51 | | - this.methodMap.put(handler.value(), m); |
52 | | - if(!m.isAccessible()){ |
53 | | - m.setAccessible(true); |
54 | | - } |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | - @Override |
59 | | - public void onIMEvent(IMEvent imEvent){ |
60 | | - Method m = methodMap.get(imEvent.getType()); |
61 | | - if(m != null){ |
62 | | - try { |
63 | | - m.invoke(this, imEvent); |
64 | | - } catch (Throwable e) { |
65 | | - LOG.warn("invoke IMEventHandler Error!! {}.{}(..)", getClass(), m.getName(), e); |
66 | | - } |
67 | | - }else{ |
68 | | - LOG.warn("IMEventHandler Not Found!! imEvent = {}", imEvent); |
69 | | - } |
70 | | - } |
71 | | - } |
| 67 | + @Override |
| 68 | + public void onIMEvent(IMEvent imEvent) { |
| 69 | + Method m = methodMap.get(imEvent.getType()); |
| 70 | + if (m != null) { |
| 71 | + try { |
| 72 | + m.invoke(this, imEvent); |
| 73 | + } catch (Throwable e) { |
| 74 | + LOG.warn("invoke IMEventHandler Error!! {}.{}(..)", getClass(), m.getName(), e); |
| 75 | + } |
| 76 | + } else { |
| 77 | + LOG.warn("IMEventHandler Not Found!! imEvent = {}", imEvent); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments