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.util.HashSet;
023    import java.util.Set;
024    
025    import net.sf.extcos.ComponentQuery;
026    import net.sf.extcos.ComponentScanner;
027    import net.sf.extcos.spi.ResourceAccessor;
028    import net.sf.extcos.spi.ResourceType;
029    
030    /**
031     * @author William DRAI
032     */
033    public class ExtCosRemoteAliasScanner implements RemoteAliasScanner {
034            
035            public Set<Class<?>> scan(final Set<String> packageNames) {
036                    ComponentScanner scanner = new ComponentScanner();
037                    
038                    final String[] basePackageNames = packageNames.toArray(new String[packageNames.size()]);
039                    
040                    Set<Class<?>> remoteClasses = scanner.getClasses(new ComponentQuery() {
041                            @Override
042                            protected void query() {
043                                    select(JavaClassResourceType.javaClasses()).from(basePackageNames).returning(allAnnotatedWith(RemoteAlias.class));
044                            }
045                    });
046                    
047                    Set<Class<?>> annotatedRemoteClasses = new HashSet<Class<?>>();
048                    for (Class<?> remoteClass : remoteClasses) {
049                        if (remoteClass.isAnnotationPresent(RemoteAlias.class))
050                            annotatedRemoteClasses.add(remoteClass);
051                    }
052                    
053                    return annotatedRemoteClasses;
054            }       
055            
056            public static class JavaClassResourceType implements ResourceType {
057                    
058                    private static final String JAVA_CLASS_SUFFIX = "class";
059                    private static JavaClassResourceType instance;
060    
061                    /**
062                     * Always instantiate via the {@link #javaClasses()} method.
063                     */
064                    private JavaClassResourceType() {
065                    }
066    
067                    @Override
068                    public String getFileSuffix() {
069                            return JAVA_CLASS_SUFFIX;
070                    }
071    
072                    /**
073                     * EDSL method
074                     */
075                    public static JavaClassResourceType javaClasses() {
076                            if (instance == null)
077                                    instance = new JavaClassResourceType();
078    
079                            return instance;
080                    }
081    
082                    @Override
083                    public ResourceAccessor getResourceAccessor() {
084                            return new JavaResourceAccessor();
085                    }
086            }
087    }