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.HashMap;
023    import java.util.Map;
024    import java.util.Set;
025    
026    import org.granite.client.platform.Platform;
027    import org.granite.logging.Logger;
028    import org.granite.messaging.AliasRegistry;
029    
030    
031    /**
032     * @author William DRAI
033     */
034    public class ClientAliasRegistry implements AliasRegistry {
035            
036            private static final Logger log = Logger.getLogger(ClientAliasRegistry.class);
037            
038            private Map<String, String> serverToClientAliases = new HashMap<String, String>();
039            private Map<String, String> clientToServerAliases = new HashMap<String, String>();
040            
041            public void scan(Set<String> packageNames) {
042                    if (packageNames != null && !packageNames.isEmpty()) {
043                            RemoteAliasScanner scanner = Platform.getInstance().newRemoteAliasScanner();
044                            
045                            Set<Class<?>> aliases = scanner.scan(packageNames);
046                            for (Class<?> alias : aliases)
047                                    registerAlias(alias);
048                            
049                            log.debug("Using remote aliases: %s", aliases);
050                    }
051            }
052            
053            public void registerAlias(Class<?> remoteAliasAnnotatedClass) {
054                    RemoteAlias remoteAlias = remoteAliasAnnotatedClass.getAnnotation(RemoteAlias.class);
055                    if (remoteAlias == null)
056                            throw new IllegalArgumentException(remoteAliasAnnotatedClass.getName() + " isn't annotated with " + RemoteAlias.class.getName());
057                    registerAlias(remoteAliasAnnotatedClass.getName(), remoteAlias.value());
058            }
059    
060            public void registerAliases(Class<?>... remoteAliasAnnotatedClasses) {
061                    for (Class<?> remoteAliasAnnotatedClass : remoteAliasAnnotatedClasses)
062                            registerAlias(remoteAliasAnnotatedClass);
063            }
064    
065            public void registerAlias(String clientClassName, String serverClassName) {
066                    if (clientClassName.length() == 0 || serverClassName.length() == 0)
067                            throw new IllegalArgumentException("Empty class name: " + clientClassName + " / " + serverClassName);
068                    
069                    clientToServerAliases.put(clientClassName, serverClassName);
070                    serverToClientAliases.put(serverClassName, clientClassName);
071            }
072    
073            public void registerAliases(Map<String, String> clientToServerAliases) {
074                    for (Map.Entry<String, String> clientToServerAlias : clientToServerAliases.entrySet())
075                            registerAlias(clientToServerAlias.getKey(), clientToServerAlias.getValue());
076            }
077    
078            public String getAliasForType(String className) {
079                    String alias = clientToServerAliases.get(className);
080                    return (alias != null ? alias : className);
081            }
082            
083            public String getTypeForAlias(String alias) {
084                    String className = serverToClientAliases.get(alias);
085                    return className != null ? className : alias;
086            }
087    }