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) &&
066                !Externalizable.class.isAssignableFrom(clazz) &&
067                GraniteContext.getCurrentInstance().getGraniteConfig().getExternalizer(clazz.getName()) == null)
068                return "";
069            return clazz.getName();
070        }
071    
072        public Class<?> getType() {
073            return type;
074        }
075    
076        public String getName() {
077            return name;
078        }
079    
080        public Externalizer getExternalizer() {
081            return externalizer;
082        }
083    
084        public byte getEncoding() {
085            return encoding;
086        }
087    
088        public boolean isExternalizable() {
089            return encoding == 0x01;
090        }
091    
092        public boolean isDynamic() {
093            return encoding == 0x02;
094        }
095    
096        public int getPropertiesCount() {
097            return (properties != null ? properties.size() : 0);
098        }
099    
100        public String getPropertyName(int index) {
101            if (properties == null)
102                throw new ArrayIndexOutOfBoundsException(index);
103            return properties.get(index).getName();
104        }
105    
106        public Object getPropertyValue(int index, Object instance) {
107            if (properties == null)
108                throw new ArrayIndexOutOfBoundsException(index);
109            Property prop = properties.get(index);
110            return prop.getProperty(instance);
111        }
112    }