001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.jsonws;
021:
022: import java.lang.reflect.ParameterizedType;
023: import java.lang.reflect.Type;
024: import java.util.Calendar;
025: import java.util.Date;
026: import java.util.GregorianCalendar;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030:
031: import de.schlund.pfixcore.beans.BeanDescriptorFactory;
032: import de.schlund.pfixcore.webservice.jsonws.serializers.ArraySerializer;
033: import de.schlund.pfixcore.webservice.jsonws.serializers.BeanSerializer;
034: import de.schlund.pfixcore.webservice.jsonws.serializers.BooleanSerializer;
035: import de.schlund.pfixcore.webservice.jsonws.serializers.CalendarSerializer;
036: import de.schlund.pfixcore.webservice.jsonws.serializers.ListSerializer;
037: import de.schlund.pfixcore.webservice.jsonws.serializers.MapSerializer;
038: import de.schlund.pfixcore.webservice.jsonws.serializers.NumberSerializer;
039: import de.schlund.pfixcore.webservice.jsonws.serializers.StringSerializer;
040:
041: /**
042: * @author mleidig@schlund.de
043: */
044: public class SerializerRegistry {
045:
046: Map<Class<?>, Serializer> serializers;
047: BeanSerializer beanSerializer;
048: ArraySerializer arraySerializer;
049: ListSerializer listSerializer;
050: MapSerializer mapSerializer;
051:
052: public SerializerRegistry(BeanDescriptorFactory beanDescFactory) {
053:
054: serializers = new HashMap<Class<?>, Serializer>();
055:
056: beanSerializer = new BeanSerializer(beanDescFactory);
057: arraySerializer = new ArraySerializer();
058: listSerializer = new ListSerializer();
059: mapSerializer = new MapSerializer();
060:
061: serializers.put(String.class, new StringSerializer());
062: Serializer ser = new NumberSerializer();
063: serializers.put(Byte.class, ser);
064: serializers.put(Short.class, ser);
065: serializers.put(Integer.class, ser);
066: serializers.put(Long.class, ser);
067: serializers.put(Float.class, ser);
068: serializers.put(Double.class, ser);
069: serializers.put(Boolean.class, new BooleanSerializer());
070: ser = new CalendarSerializer();
071: serializers.put(Calendar.class, ser);
072: serializers.put(GregorianCalendar.class, ser);
073: serializers.put(Date.class, ser);
074:
075: }
076:
077: public void register(Class<?> clazz, Serializer serializer) {
078: serializers.put(clazz, serializer);
079: }
080:
081: public Serializer getSerializer(Class<?> clazz) {
082: Serializer ser = serializers.get(clazz);
083: if (ser == null) {
084: if (clazz.isArray())
085: ser = arraySerializer;
086: else if (List.class.isAssignableFrom(clazz))
087: ser = listSerializer;
088: else if (Map.class.isAssignableFrom(clazz))
089: ser = mapSerializer;
090: else
091: ser = beanSerializer;
092: }
093: return ser;
094: }
095:
096: public Serializer getSerializer(Type type) {
097: Class<?> clazz = null;
098: if (type instanceof Class)
099: clazz = (Class<?>) type;
100: else if (type instanceof ParameterizedType) {
101: Type rawType = ((ParameterizedType) type).getRawType();
102: if (rawType instanceof Class)
103: clazz = (Class<?>) rawType;
104: }
105: if (clazz != null)
106: return getSerializer(clazz);
107: else
108: throw new RuntimeException("Type not supported: "
109: + type.getClass() + " " + type);
110: }
111:
112: }
|