001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 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
021package org.granite.messaging.jmf;
022
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collections;
026import java.util.Date;
027import java.util.HashMap;
028import java.util.HashSet;
029import java.util.List;
030import java.util.Map;
031import java.util.Set;
032
033import org.granite.messaging.jmf.reflect.Reflection;
034
035/**
036 * @author Franck WOLFF
037 */
038public class DefaultSharedContext implements SharedContext {
039
040        protected static List<String> JAVA_DEFAULT_STORED_STRINGS = Arrays.asList(
041                Boolean.class.getName(),
042                Character.class.getName(),
043                Byte.class.getName(),
044                Short.class.getName(),
045                Integer.class.getName(),
046                Long.class.getName(),
047                Float.class.getName(),
048                Double.class.getName(),
049                
050                String.class.getName(),
051                Object.class.getName(),
052
053                Date.class.getName(),
054                
055                List.class.getName(),
056                ArrayList.class.getName(),
057                
058                Set.class.getName(),
059                HashSet.class.getName(),
060                
061                Map.class.getName(),
062                HashMap.class.getName(),
063                
064                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentList",
065                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentMap",
066                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentSet",
067                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentBag",
068                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentSortedSet",
069                JMFConstants.CLIENT_PERSISTENCE_COLLECTION_PACKAGE + ".PersistentSortedMap"
070        );
071        
072        protected final CodecRegistry codecRegistry;
073        protected final Reflection reflection;
074        protected final List<String> defaultStoredStrings;
075        
076        public DefaultSharedContext() {
077                this(null, null, null);
078        }
079        
080        public DefaultSharedContext(CodecRegistry codecRegistry) {
081                this(codecRegistry, null, null);
082        }
083        
084        public DefaultSharedContext(CodecRegistry codecRegistry, ClassLoader classLoader) {
085                this(codecRegistry, classLoader, null);
086        }
087        
088        public DefaultSharedContext(CodecRegistry codecRegistry, ClassLoader classLoader, List<String> defaultStoredStrings) {
089                this.codecRegistry = (codecRegistry != null ? codecRegistry : new DefaultCodecRegistry());
090                
091                this.reflection = new Reflection(classLoader);
092                
093                Set<String> defaultStoredStringsSet = new HashSet<String>(JAVA_DEFAULT_STORED_STRINGS);
094                if (defaultStoredStrings != null)
095                        defaultStoredStringsSet.addAll(defaultStoredStrings);
096                this.defaultStoredStrings = Collections.unmodifiableList(new ArrayList<String>(defaultStoredStringsSet));
097        }
098
099        public CodecRegistry getCodecRegistry() {
100                return codecRegistry;
101        }
102
103        public Reflection getReflection() {
104                return reflection;
105        }
106
107        public List<String> getDefaultStoredStrings() {
108                return defaultStoredStrings;
109        }
110
111        public String getAlias(String className) {
112                return className;
113        }
114}