private String makeEventMatcher(RemoteEvent event) { List<String> matchers = new LinkedList<String>(); Set<Entry<String, String>> entrySet = event.getNestedEventProperties().entrySet(); for (Entry<String, String> entry : entrySet) { matchers.add(String.format("%s == \"%s\"", entry.getKey(), entry.getValue())); } return event.getClassName() + "(" + StringUtils.join(matchers, ",") + ")"; }
public static RemoteEvent wrapEvent(Event event) { RemoteEvent result = new RemoteEvent(event.getClass().getName()); PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(event.getClass()); Map<String, String> nestedEventProperties = result.getNestedEventProperties(); for (PropertyDescriptor pd : propertyDescriptors) { Method readMethod = pd.getReadMethod(); LOGGER.debug("writing property {} to event", pd.getName()); try { Object value = readMethod.invoke(event); if (value != null) { nestedEventProperties.put(pd.getName(), value.toString()); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } return result; }
@Override public synchronized void registerEvent(RemoteEvent event, String portId, String returnAddress, String serviceId) { String name = String.format("Notify %s via %s when %s occurs", returnAddress.toString(), portId, event.getClassName()); name = getUniqueRuleName(name); RuleBaseElementId id = new RuleBaseElementId(RuleBaseElementType.Rule, name); String eventMatcher = makeEventMatcher(event); try { String osgiHelperStatement; if (serviceId == null) { osgiHelperStatement = String.format(OSGI_HELPER_TEMPLATE1, portId, returnAddress); } else { osgiHelperStatement = String.format(OSGI_HELPER_TEMPLATE2, portId, returnAddress, serviceId); } ruleManager.add(id, String.format(EVENT_REGISTRATION_RULE_TEMPLATE, eventMatcher, osgiHelperStatement)); } catch (RuleBaseException e) { throw new IllegalArgumentException(e); } LOGGER.info("registering Event: {}", event); }