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     */
022    /**
023     * www.openamf.org
024     *
025     * Distributable under LGPL license. See terms of license at gnu.org.
026     */
027    
028    package flex.messaging.io;
029    
030    import java.util.HashMap;
031    
032    /**
033     * Implementation of MM's flashgateway.io.ASObject so that we can use
034     * ASTranslator
035     *
036     * @author Jason Calabrese <mail@jasoncalabrese.com>
037     * @author Sean C. Sullivan
038     *
039     * @version $Revision: 1.11 $, $Date: 2004/02/06 02:48:59 $
040     */
041    public class ASObject extends HashMap<String, Object> {
042    
043        /**
044         *
045         */
046        private static final long serialVersionUID = 1L;
047    
048        /**
049         * Object type
050         */
051        private String type;
052    
053        public ASObject() {
054            super();
055        }
056    
057        /**
058         * Creates ASObject with type
059         *
060         * @param type
061         */
062        public ASObject(String type) {
063            super();
064            this.type = type;
065        }
066    
067        /**
068         * Gets object type
069         *
070         * @return type 
071         * @see #setType(String)
072         */
073        public String getType() {
074            return type;
075        }
076    
077        /**
078         * Sets object type
079         *
080         * @param type
081         *
082         * @see #getType()
083         *
084         */
085        public void setType(String type) {
086            this.type = type;
087        }
088    
089        /**
090         * Returns <tt>true</tt> if this map contains a mapping for the specified
091         * key. <br>
092         *
093         * @param key
094         *                The key whose presence in this map is to be tested
095         * @return <tt>true</tt> if this map contains a mapping for the specified
096         *            key.
097         */
098        @Override
099        public boolean containsKey(Object key) {
100            return super.containsKey(toLowerCase(key));
101        }
102    
103        /**
104         * Returns the value to which the specified key is mapped in this identity
105         * hash map, or <tt>null</tt> if the map contains no mapping for this
106         * key. A return value of <tt>null</tt> does not <i>necessarily</i>
107         * indicate that the map contains no mapping for the key; it is also
108         * possible that the map explicitly maps the key to <tt>null</tt>. The
109         * <tt>containsKey</tt> method may be used to distinguish these two
110         * cases.
111         *
112         * @param key
113         *                the key whose associated value is to be returned.
114         * @return the value to which this map maps the specified key, or <tt>null</tt>
115         *            if the map contains no mapping for this key.
116         * @see #put(Object, Object)
117         */
118        @Override
119        public Object get(Object key) {
120            return super.get(toLowerCase(key));
121        }
122    
123        /**
124         * Associates the specified value with the specified key in this map. If
125         * the map previously contained a mapping for this key, the old value is
126         * replaced.
127         *
128         * @param key
129         *                key with which the specified value is to be associated.
130         * @param value
131         *                value to be associated with the specified key.
132         * @return previous value associated with specified key, or <tt>null</tt>
133         *            if there was no mapping for key. A <tt>null</tt> return can
134         *            also indicate that the HashMap previously associated <tt>null</tt>
135         *            with the specified key.
136         */
137        @Override
138        public Object put(String key, Object value) {
139            return super.put((String)toLowerCase(key), value);
140        }
141    
142        /**
143         * Removes the mapping for this key from this map if present.
144         *
145         * @param key
146         *                key whose mapping is to be removed from the map.
147         * @return previous value associated with specified key, or <tt>null</tt>
148         *            if there was no mapping for key. A <tt>null</tt> return can
149         *            also indicate that the map previously associated <tt>null</tt>
150         *            with the specified key.
151         */
152        @Override
153        public Object remove(Object key) {
154            return super.remove(toLowerCase(key));
155        }
156    
157        /**
158         * Gets lower case object if object was instance of String
159         *
160         * @param key
161         * @return lower case
162         */
163        private Object toLowerCase(Object key) {
164            /*if (key != null
165                && key instanceof String
166                && amfSerializerConfig.forceLowerCaseKeys()) {
167                key = ((String) key).toLowerCase();
168            }*/
169            return key;
170        }
171    
172        /**
173         * @return this method may return null
174         *
175         * @see #setType(String)
176         * @see #getType()
177         *
178         */
179        public Object instantiate() {
180            Object ret;
181            try {
182                ClassLoader loader = Thread.currentThread().getContextClassLoader();
183                Class<?> clazz = loader.loadClass(type);
184                ret = clazz.newInstance();
185            } catch (Exception e) {
186                ret = null;
187            }
188            return ret;
189        }
190    
191        @Override
192        public String toString() {
193            return "ASObject[type=" + getType() + "," + super.toString() + "]";
194        }
195    }