package org.opennaas.core.events; /* * #%L * OpenNaaS :: Core :: Resources * %% * Copyright (C) 2007 - 2014 FundaciĆ³ Privada i2CAT, Internet i InnovaciĆ³ a Catalunya * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. * * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * <http://www.gnu.org/licenses/lgpl-3.0.html>. * #L% */ import java.util.Enumeration; import java.util.Properties; /** * * This class is used for EventHandlers in order to specify events they are interested in. * * There are two handler properties that specify the type of events you are interested in: Event Topic and Event Properties filter. * * The value of the Topic Name pattern property must be of type String[]. This value contains the names of the topics that this handler is interested * in. A wildcard '*' can be used as the last token of a Topic Name, e.g., the value 'org/osgi/framework/*' matches topic name * 'org/osgi/framework/BundleEvent/REGISTERED', but the value 'org/osgi/framework/Bundle*' is considered invalid. * * The value of the Event Properties filter is of type String and contains an LDAP filter that will be matched against event properties, e.g., The * Event Properties Filter '(&(productCategory=books)(price>=80))' filters all events that has the 'books' category and price is more than or equal * 80. * * Event Topic property is mandatory! If the handler service doesn't provide this property it will receive no events. If you want to subscribe to all * of the events in the environment then you should specify the value '*' for it. * * @author isart * */ public class EventFilter { protected String[] topics = new String[] { "*" }; // match all topics by default protected String propertiesFilter = ""; public EventFilter(String[] topics, String propertiesFilter) { this.topics = topics; this.propertiesFilter = propertiesFilter; } public EventFilter(String[] topics, Properties properties) { this(topics, buildPropertiesFilter(properties)); } public EventFilter(String topic, String propertiesFilter) { this(new String[] { topic }, propertiesFilter); } public EventFilter(String[] topics) { this.topics = topics; } public EventFilter(String topic) { this(new String[] { topic }); } public String getPropertiesFilter() { return propertiesFilter; } public String[] getTopics() { return topics; } public static String buildPropertiesFilter(Properties properties) { String filterString = ""; Enumeration<Object> keys = properties.keys(); while (keys.hasMoreElements()) { String currentKey = (String) keys.nextElement(); filterString += "(" + currentKey + "=" + properties.getProperty(currentKey) + ")"; } filterString = "(&" + filterString + ")"; return filterString; } }