001 /**
002 * GRANITE DATA SERVICES
003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 * This file is part of the Granite Data Services Platform.
006 *
007 * Granite Data Services is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * Granite Data Services is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 * General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 * USA, or see <http://www.gnu.org/licenses/>.
021 */
022 package org.granite.config;
023
024 import java.util.List;
025 import java.util.Map;
026 import java.util.Set;
027
028 import org.granite.messaging.service.tide.TideComponentAnnotatedWithMatcher;
029 import org.granite.messaging.service.tide.TideComponentInstanceOfMatcher;
030 import org.granite.messaging.service.tide.TideComponentMatcher;
031 import org.granite.messaging.service.tide.TideComponentNameMatcher;
032 import org.granite.messaging.service.tide.TideComponentTypeMatcher;
033
034 /**
035 * @author Franck WOLFF
036 */
037 public class TideComponentMatcherFactory {
038
039 public TideComponentMatcher getTypeMatcher(String type, boolean disabled) throws GraniteConfigException {
040 try {
041 return new TideComponentTypeMatcher(type, disabled);
042 } catch (Exception e) {
043 throw new GraniteConfigException("Could not instantiate Tide component matcher for type: " + type, e);
044 }
045 }
046
047 public TideComponentMatcher getNameMatcher(String name, boolean disabled) throws GraniteConfigException {
048 try {
049 return new TideComponentNameMatcher(name, disabled);
050 } catch (Exception e) {
051 throw new GraniteConfigException("Could not instantiate Tide component matcher for name: " + name, e);
052 }
053 }
054
055 public TideComponentMatcher getInstanceOfMatcher(String type, boolean disabled) throws GraniteConfigException {
056 try {
057 return new TideComponentInstanceOfMatcher(type, disabled);
058 } catch (Exception e) {
059 throw new GraniteConfigException("Could not instantiate Tide component matcher for instance of: " + type, e);
060 }
061 }
062
063 public TideComponentMatcher getAnnotatedWithMatcher(String type, boolean disabled) throws GraniteConfigException {
064 try {
065 return new TideComponentAnnotatedWithMatcher(type, disabled);
066 } catch (Exception e) {
067 throw new GraniteConfigException("Could not instantiate Tide component matcher for annotated with: " + type, e);
068 }
069 }
070
071
072 public static boolean isComponentTideEnabled(
073 Map<String, Object[]> tideComponentsByName,
074 List<TideComponentMatcher> tideComponentMatchers,
075 String componentName, Set<Class<?>> componentClasses, Object componentInstance) throws GraniteConfigException {
076
077 String key = componentName != null ? componentName : componentClasses.toString();
078 if (tideComponentsByName.containsKey(key)) {
079 if ((Integer)tideComponentsByName.get(key)[1] == componentClasses.hashCode())
080 return (Boolean)tideComponentsByName.get(key)[0];
081 }
082
083 boolean enabled = false;
084 for (TideComponentMatcher matcher : tideComponentMatchers) {
085 if (matcher.matches(componentName, componentClasses, componentInstance, false)) {
086 enabled = true;
087 break;
088 }
089 }
090
091 tideComponentsByName.put(key, new Object[] { enabled, componentClasses.hashCode()});
092 return enabled;
093 }
094
095 public static boolean isComponentTideDisabled(
096 Map<String, Object[]> tideComponentsByName,
097 List<TideComponentMatcher> tideComponentMatchers,
098 String componentName, Set<Class<?>> componentClasses, Object componentInstance) throws GraniteConfigException {
099
100 String key = componentName != null ? componentName : componentClasses.toString();
101 if (tideComponentsByName.containsKey(key)) {
102 if ((Integer)tideComponentsByName.get(key)[1] == componentClasses.hashCode())
103 return (Boolean)tideComponentsByName.get(key)[0];
104 }
105
106 boolean disabled = false;
107 for (TideComponentMatcher matcher : tideComponentMatchers) {
108 if (matcher.matches(componentName, componentClasses, componentInstance, true)) {
109 disabled = true;
110 break;
111 }
112 }
113
114 tideComponentsByName.put(key, new Object[] { disabled, componentClasses.hashCode()});
115 return disabled;
116 }
117 }