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 package org.granite.messaging.amf.io.util;
023
024 import java.io.Externalizable;
025 import java.util.List;
026 import java.util.Map;
027
028 import org.granite.config.GraniteConfig;
029 import org.granite.context.GraniteContext;
030 import org.granite.messaging.amf.io.convert.Converters;
031 import org.granite.messaging.amf.io.util.externalizer.Externalizer;
032
033 /**
034 * @author Franck WOLFF
035 */
036 public abstract class JavaClassDescriptor {
037
038 protected final Class<?> type;
039 protected final String name;
040 protected final Externalizer externalizer;
041 protected final Converters converters;
042 protected final byte encoding;
043 protected final List<Property> properties;
044
045 protected JavaClassDescriptor(Class<?> type) {
046 GraniteConfig config = GraniteContext.getCurrentInstance().getGraniteConfig();
047 this.type = type;
048 this.name = getClassName(type);
049 this.externalizer = config.getExternalizer(type.getName());
050 this.converters = config.getConverters();
051 this.encoding = findEncoding(type);
052 this.properties = introspectProperties();
053 }
054
055 private byte findEncoding(Class<?> type) {
056 if (externalizer != null || Externalizable.class.isAssignableFrom(type))
057 return 0x01;
058 if (Map.class.isAssignableFrom(type))
059 return 0x02;
060 return 0x00;
061 }
062
063 protected abstract List<Property> introspectProperties();
064
065 public static String getClassName(Class<?> clazz) {
066 if (Map.class.isAssignableFrom(clazz) && !Externalizable.class.isAssignableFrom(clazz)) {
067 Externalizer externalizer = GraniteContext.getCurrentInstance().getGraniteConfig().getExternalizer(clazz.getName());
068 if (externalizer == null)
069 return "";
070 }
071 return GraniteContext.getCurrentInstance().getGraniteConfig().getAliasRegistry().getAliasForType(clazz.getName());
072 }
073
074 public Class<?> getType() {
075 return type;
076 }
077
078 public String getName() {
079 return name;
080 }
081
082 public Externalizer getExternalizer() {
083 return externalizer;
084 }
085
086 public byte getEncoding() {
087 return encoding;
088 }
089
090 public boolean isExternalizable() {
091 return encoding == 0x01;
092 }
093
094 public boolean isDynamic() {
095 return encoding == 0x02;
096 }
097
098 public int getPropertiesCount() {
099 return (properties != null ? properties.size() : 0);
100 }
101
102 public Property getProperty(int index) {
103 if (properties == null)
104 throw new ArrayIndexOutOfBoundsException(index);
105 return properties.get(index);
106 }
107
108 public String getPropertyName(int index) {
109 if (properties == null)
110 throw new ArrayIndexOutOfBoundsException(index);
111 return properties.get(index).getName();
112 }
113
114 public Object getPropertyValue(int index, Object instance) {
115 if (properties == null)
116 throw new ArrayIndexOutOfBoundsException(index);
117 Property prop = properties.get(index);
118 return prop.getProperty(instance);
119 }
120 }