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 */
022package org.granite.tide.simple;
023
024import org.granite.logging.Logger;
025import org.granite.messaging.service.annotations.RemoteDestination;
026import org.granite.scan.ScannedItem;
027import org.granite.scan.ScannedItemHandler;
028import org.granite.tide.util.Observer;
029
030import java.lang.reflect.Method;
031import java.util.HashMap;
032import java.util.HashSet;
033import java.util.Map;
034import java.util.Set;
035
036/**
037 * @author Franck WOLFF
038 */
039public class SimpleScannedItemHandler implements ScannedItemHandler {
040
041        private static final Logger log = Logger.getLogger(SimpleScannedItemHandler.class);
042        private static final SimpleScannedItemHandler instance = new SimpleScannedItemHandler();
043
044        private final Map<Class<?>, Class<?>> scannedClasses = new HashMap<Class<?>, Class<?>>();
045    private final Map<String, Class<?>> scannedClassesById = new HashMap<String, Class<?>>();
046        private final Map<String, Set<Method>> observers = new HashMap<String, Set<Method>>();
047
048        public static SimpleScannedItemHandler instance() {
049                return instance;
050        }
051
052        static SimpleScannedItemHandler instance(boolean reset) {
053                instance.scannedClasses.clear();
054                instance.observers.clear();
055                return instance;
056        }
057
058        private SimpleScannedItemHandler() {
059        }
060        
061        public boolean handleMarkerItem(ScannedItem item) {
062                return false;
063        }
064
065        public void handleScannedItem(ScannedItem item) {
066                if ("class".equals(item.getExtension()) && item.getName().indexOf('$') == -1) {
067                        try {
068                                Class<?> clazz = item.loadAsClass();
069                if (clazz.isAnnotationPresent(RemoteDestination.class)) {
070                    scannedClasses.put(clazz, clazz);
071                    if (clazz.getAnnotation(RemoteDestination.class).id().length() > 0)
072                        scannedClassesById.put(clazz.getAnnotation(RemoteDestination.class).id(), clazz);
073                }
074
075                for (Class<?> i : clazz.getInterfaces()) {
076                    if (i.isAnnotationPresent(RemoteDestination.class)) {
077                        scannedClasses.put(i, clazz);
078                        if (i.getAnnotation(RemoteDestination.class).id().length() > 0)
079                            scannedClassesById.put(i.getAnnotation(RemoteDestination.class).id(), clazz);
080                    }
081                }
082
083                for (Method method : clazz.getMethods()) {
084                    if (method.isAnnotationPresent(Observer.class)) {
085                        Observer o = method.getAnnotation(Observer.class);
086                        Set<Method> methods = observers.get(o.value());
087                        if (methods == null) {
088                            methods = new HashSet<Method>();
089                            observers.put(o.value(), methods);
090                        }
091                        methods.add(method);
092                    }
093                }
094                        }
095                        catch (Exception e) {
096                                log.debug(e, "Could not introspect scanned item: %s", item);
097                        }
098                }
099        }
100        
101        public Map<Class<?>, Class<?>> getScannedClasses() {
102                return scannedClasses;
103        }
104
105    public Map<String, Class<?>> getScannedClassesById() {
106        return scannedClassesById;
107    }
108
109    public Map<String, Set<Method>> getObservers() {
110                return observers;
111        }
112}