001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 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    package org.granite.client.messaging;
021    
022    import java.io.IOException;
023    import java.util.HashSet;
024    import java.util.Set;
025    
026    import org.granite.logging.Logger;
027    import org.granite.scan.ScannedItem;
028    import org.granite.scan.ScannedItemHandler;
029    import org.granite.scan.Scanner;
030    import org.granite.scan.ScannerFactory;
031    
032    public class StandardRemoteAliasScanner implements RemoteAliasScanner {
033            
034            private static final Logger log = Logger.getLogger(StandardRemoteAliasScanner.class);
035    
036            @Override
037            public Set<Class<?>> scan(Set<String> packageNames) {
038                    Set<Class<?>> classes = new HashSet<Class<?>>();
039                    
040                    Scanner scanner = ScannerFactory.createScanner(new MessagingScannedItemHandler(packageNames, classes), null);
041            try {
042                scanner.scan();
043            }
044            catch (Exception e) {
045                log.error(e, "Could not scan classpath for @RemoteAlias");
046            }
047                    
048            return classes;
049            }
050            
051            class MessagingScannedItemHandler implements ScannedItemHandler {
052    
053                    final String[] packageNames;
054                    final Set<Class<?>> classes;
055                    
056                    MessagingScannedItemHandler(Set<String> packageNames, Set<Class<?>> classes) {
057                            this.packageNames = new String[packageNames.size()];
058                            int i = 0;
059                            for (String packageName : packageNames)
060                                    this.packageNames[i++] = packageName.replace('.', '/') + '/';
061                            
062                            this.classes = classes;
063                    }
064                    
065                    @Override
066                    public boolean handleMarkerItem(ScannedItem item) {
067                            return false;
068                    }
069    
070                    @Override
071                    public void handleScannedItem(ScannedItem item) {
072                            if ("class".equals(item.getExtension())) {
073                                    boolean scan = false;
074                                    
075                                    String path = item.getRelativePath();
076                                    for (String packageName : packageNames) {
077                                            if (path.startsWith(packageName)) {
078                                                    scan = true;
079                                                    break;
080                                            }
081                                    }
082                                    
083                                    if (scan) {
084                                            try {
085                                                    Class<?> cls = item.loadAsClass();
086                                                    RemoteAlias alias = cls.getAnnotation(RemoteAlias.class);
087                                                    if (alias != null)
088                                                            classes.add(cls);
089                                            }
090                                            catch (ClassFormatError e) {
091                                            }
092                                            catch (ClassNotFoundException e) {
093                                            }
094                                            catch (IOException e) {
095                                                    log.error(e, "Could not load class: %s", item);
096                                            }
097                                    }
098                            }
099                    }
100            }
101    }