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