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