|
| 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.cloudstack.framework.messaging; |
| 20 | + |
| 21 | +import java.lang.reflect.InvocationTargetException; |
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +public class EventDispatcher { |
| 27 | + private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>(); |
| 28 | + |
| 29 | + public static boolean dispatch(Object target, String subject, String senderAddress, Object args) { |
| 30 | + assert(subject != null); |
| 31 | + assert(target != null); |
| 32 | + |
| 33 | + Method handler = resolveHandler(target.getClass(), subject); |
| 34 | + if(handler == null) |
| 35 | + return false; |
| 36 | + |
| 37 | + try { |
| 38 | + handler.invoke(target, subject, senderAddress, args); |
| 39 | + } catch (IllegalArgumentException e) { |
| 40 | + throw new RuntimeException("IllegalArgumentException when invoking event handler for subject: " + subject); |
| 41 | + } catch (IllegalAccessException e) { |
| 42 | + throw new RuntimeException("IllegalAccessException when invoking event handler for subject: " + subject); |
| 43 | + } catch (InvocationTargetException e) { |
| 44 | + throw new RuntimeException("InvocationTargetException when invoking event handler for subject: " + subject); |
| 45 | + } |
| 46 | + |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + public static Method resolveHandler(Class<?> handlerClz, String subject) { |
| 51 | + synchronized(s_handlerCache) { |
| 52 | + Method handler = s_handlerCache.get(handlerClz); |
| 53 | + if(handler != null) |
| 54 | + return handler; |
| 55 | + |
| 56 | + for(Method method : handlerClz.getMethods()) { |
| 57 | + EventHandler annotation = method.getAnnotation(EventHandler.class); |
| 58 | + if(annotation != null) { |
| 59 | + if(match(annotation.topic(), subject)) { |
| 60 | + s_handlerCache.put(handlerClz, method); |
| 61 | + return method; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + private static boolean match(String expression, String param) { |
| 71 | + return param.matches(expression); |
| 72 | + } |
| 73 | +} |
0 commit comments