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.util.ArrayList;
024import java.util.List;
025
026import org.granite.config.GraniteConfig;
027import org.granite.context.GraniteContext;
028import org.granite.messaging.amf.io.convert.Converters;
029import org.granite.messaging.amf.io.util.externalizer.Externalizer;
030import org.granite.messaging.amf.io.util.instantiator.AbstractInstantiator;
031
032/**
033 * @author Franck WOLFF
034 */
035public abstract class ActionScriptClassDescriptor {
036
037    protected final String type;
038    protected final String instantiator;
039    protected final byte encoding;
040    protected final Externalizer externalizer;
041    protected final Converters converters;
042    protected final List<Property> properties;
043
044    protected ActionScriptClassDescriptor(String type, byte encoding) {
045        GraniteConfig config = GraniteContext.getCurrentInstance().getGraniteConfig();
046        this.type = (type == null ? "" : config.getTypeForAlias(type));
047        this.instantiator = config.getInstantiator(type);
048        this.encoding = encoding;
049        this.externalizer = findExternalizer();
050        this.converters = config.getConverters();
051        this.properties = new ArrayList<Property>();
052    }
053
054    private Externalizer findExternalizer() {
055        if (encoding != 0x01)
056            return null;
057        return GraniteContext.getCurrentInstance().getGraniteConfig().getExternalizer(type);
058    }
059
060    public String getType() {
061        return type;
062    }
063
064    public String getInstantiator() {
065        return instantiator;
066    }
067
068    public Externalizer getExternalizer() {
069        return externalizer;
070    }
071
072    public byte getEncoding() {
073        return encoding;
074    }
075
076    public boolean isExternalizable() {
077        return encoding == 0x01;
078    }
079
080    public boolean isDynamic() {
081        return encoding == 0x02;
082    }
083
084    public abstract void defineProperty(String name);
085    public abstract Object newJavaInstance();
086
087    public int getPropertiesCount() {
088        return properties.size();
089    }
090    public String getPropertyName(int index) {
091        return properties.get(index).getName();
092    }
093
094    public void setPropertyValue(int index, Object instance, Object value) {
095        Property prop = properties.get(index);
096        if (value instanceof AbstractInstantiator<?>)
097            ((AbstractInstantiator<?>)value).addReferer(instance, prop);
098        else
099            prop.setProperty(instance, value);
100    }
101
102    public void setPropertyValue(String name, Object instance, Object value) {
103        // instance must be an instance of Map...
104        Property prop = new MapProperty(converters, name);
105        if (value instanceof AbstractInstantiator<?>)
106            ((AbstractInstantiator<?>)value).addReferer(instance, prop);
107        else
108            prop.setProperty(instance, value);
109    }
110
111    @Override
112    public String toString() {
113        return getClass().getName() + " {\n" +
114            "  type=" + type + ",\n" +
115            "  instantiator=" + instantiator + ",\n" +
116            "  encoding=" + encoding + ",\n" +
117            "  externalizer=" + externalizer + ",\n" +
118            "  converters=" + converters + ",\n" +
119            "  properties=" + properties + "\n" +
120        "}";
121    }
122}