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
021package org.granite.messaging.amf.io.util;
022
023import java.io.Externalizable;
024import java.util.List;
025import java.util.Map;
026
027import org.granite.config.GraniteConfig;
028import org.granite.context.GraniteContext;
029import org.granite.messaging.amf.RemoteClass;
030import org.granite.messaging.amf.io.convert.Converters;
031import org.granite.messaging.amf.io.util.externalizer.Externalizer;
032
033/**
034 * @author Franck WOLFF
035 */
036public 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        RemoteClass alias = clazz.getAnnotation(RemoteClass.class);
072        return alias != null ? alias.value() : clazz.getName();
073    }
074    
075    public Class<?> getType() {
076        return type;
077    }
078
079    public String getName() {
080        return name;
081    }
082
083    public Externalizer getExternalizer() {
084        return externalizer;
085    }
086
087    public byte getEncoding() {
088        return encoding;
089    }
090
091    public boolean isExternalizable() {
092        return encoding == 0x01;
093    }
094
095    public boolean isDynamic() {
096        return encoding == 0x02;
097    }
098
099    public int getPropertiesCount() {
100        return (properties != null ? properties.size() : 0);
101    }
102
103    public String getPropertyName(int index) {
104        if (properties == null)
105            throw new ArrayIndexOutOfBoundsException(index);
106        return properties.get(index).getName();
107    }
108
109    public Object getPropertyValue(int index, Object instance) {
110        if (properties == null)
111            throw new ArrayIndexOutOfBoundsException(index);
112        Property prop = properties.get(index);
113        return prop.getProperty(instance);
114    }
115}