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.messaging.jmf;
023
024import java.util.ArrayList;
025import java.util.Arrays;
026import java.util.Collections;
027import java.util.Date;
028import java.util.HashMap;
029import java.util.HashSet;
030import java.util.List;
031import java.util.Map;
032import java.util.Set;
033
034import org.granite.messaging.AliasRegistry;
035import org.granite.messaging.DefaultAliasRegistry;
036import org.granite.messaging.jmf.codec.std.impl.util.ClassNameUtil;
037import org.granite.messaging.reflect.Reflection;
038
039/**
040 * @author Franck WOLFF
041 */
042public class DefaultSharedContext implements SharedContext {
043
044        protected static List<String> JAVA_DEFAULT_CLASS_NAMES = Arrays.asList(
045                
046                Boolean.class.getName(),
047                Character.class.getName(),
048                Byte.class.getName(),
049                Short.class.getName(),
050                Integer.class.getName(),
051                Long.class.getName(),
052                Float.class.getName(),
053                Double.class.getName(),
054                
055                String.class.getName(),
056                Object.class.getName(),
057
058                Date.class.getName(),
059                
060                List.class.getName(),
061                ArrayList.class.getName(),
062                
063                Set.class.getName(),
064                HashSet.class.getName(),
065                
066                Map.class.getName(),
067                HashMap.class.getName(),
068                
069                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentList",
070                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentMap",
071                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentSet",
072                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentBag",
073                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentSortedSet",
074                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentSortedMap"
075        );
076        
077        protected final CodecRegistry codecRegistry;
078        protected final Reflection reflection;
079        protected final List<String> initialClassNameDictionary;
080        protected final AliasRegistry aliasRegistry;
081        
082        public DefaultSharedContext() {
083                this(null, null, null, null);
084        }
085        
086        public DefaultSharedContext(CodecRegistry codecRegistry) {
087                this(codecRegistry, null, null, null);
088        }
089
090        public DefaultSharedContext(CodecRegistry codecRegistry, List<String> defaultClassNames) {
091                this(codecRegistry, defaultClassNames, null, null);
092        }
093        
094        public DefaultSharedContext(CodecRegistry codecRegistry, List<String> defaultClassNames, Reflection reflection, AliasRegistry aliasRegistry) {
095                this.codecRegistry = (codecRegistry != null ? codecRegistry : new DefaultCodecRegistry());
096                
097                List<String> initialClassNameDictionary = new ArrayList<String>();
098                for (String className : JAVA_DEFAULT_CLASS_NAMES)
099                        ClassNameUtil.initClassNameDictionary(initialClassNameDictionary, className);
100                if (defaultClassNames != null) {
101                        for (String className : defaultClassNames)
102                                ClassNameUtil.initClassNameDictionary(initialClassNameDictionary, className);
103                }
104                this.initialClassNameDictionary = Collections.unmodifiableList(initialClassNameDictionary);
105                
106                this.reflection = (reflection != null ? reflection : new Reflection(null));
107                
108                this.aliasRegistry = aliasRegistry != null ? aliasRegistry : new DefaultAliasRegistry();
109        }
110
111        public CodecRegistry getCodecRegistry() {
112                return codecRegistry;
113        }
114
115        public Reflection getReflection() {
116                return reflection;
117        }
118
119        @Override
120        public List<String> getInitialClassNameDictionary() {
121                return initialClassNameDictionary;
122        }
123        
124        public AliasRegistry getAliasRegistry() {
125                return aliasRegistry;
126        }
127        
128        public String getRemoteAlias(String className) {
129                return aliasRegistry.getAliasForType(className);
130        }
131
132        public String getClassName(String remoteAlias) {
133                return aliasRegistry.getTypeForAlias(remoteAlias);
134        }
135}