001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2012 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
021 package org.granite.client.messaging;
022
023 import java.util.HashSet;
024 import java.util.Set;
025
026 import net.sf.extcos.ComponentQuery;
027 import net.sf.extcos.ComponentScanner;
028 import net.sf.extcos.spi.ResourceAccessor;
029 import net.sf.extcos.spi.ResourceType;
030
031 import org.granite.client.configuration.Configuration;
032 import org.granite.config.GraniteConfig;
033 import org.granite.messaging.amf.RemoteClass;
034
035 /**
036 * @author William DRAI
037 */
038 public class RemoteClassScanner implements Configuration.Configurator {
039
040 private Set<String> packageNames = new HashSet<String>();
041
042 public void addPackageName(String packageName) {
043 this.packageNames.add(packageName);
044 }
045
046 public void setPackageNames(Set<String> packageNames) {
047 this.packageNames.clear();
048 this.packageNames.addAll(packageNames);
049 }
050
051 public void configure(GraniteConfig graniteConfig) {
052 if (packageNames.isEmpty())
053 return;
054
055 ComponentScanner scanner = new ComponentScanner();
056
057 final String[] basePackageNames = packageNames.toArray(new String[packageNames.size()]);
058
059 for (Class<?> remoteClass : scanner.getClasses(new ComponentQuery() {
060 @Override
061 protected void query() {
062 select(JavaClassResourceType.javaClasses()).from(basePackageNames).returning(allAnnotatedWith(RemoteClass.class));
063 }
064 })) {
065 graniteConfig.registerClassAlias(remoteClass);
066 }
067 }
068
069 public static class JavaClassResourceType implements ResourceType {
070 private static final String JAVA_CLASS_SUFFIX = "class";
071 private static JavaClassResourceType instance;
072
073 /**
074 * Always instantiate via the {@link #javaClasses()} method.
075 */
076 private JavaClassResourceType() {
077 }
078
079 @Override
080 public String getFileSuffix() {
081 return JAVA_CLASS_SUFFIX;
082 }
083
084 /**
085 * EDSL method
086 */
087 public static JavaClassResourceType javaClasses() {
088 if (instance == null)
089 instance = new JavaClassResourceType();
090
091 return instance;
092 }
093
094 @Override
095 public ResourceAccessor getResourceAccessor() {
096 return new JavaResourceAccessor();
097 }
098 }
099 }