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