1 19 20 package org.apache.cayenne.access.types; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 import org.apache.cayenne.util.Util; 31 32 39 public class ExtendedTypeMap { 40 41 protected Map typeMap = new HashMap (); 42 protected DefaultType defaultType = new DefaultType(); 43 44 Collection extendedTypeFactories; 45 46 Collection internalTypeFactories; 49 50 54 public ExtendedTypeMap() { 55 initDefaultTypes(); 56 initDefaultFactories(); 57 } 58 59 62 protected void initDefaultTypes() { 63 registerType(new VoidType()); 65 66 Iterator it = DefaultType.defaultTypes(); 68 while (it.hasNext()) { 69 registerType(new DefaultType((String ) it.next())); 70 } 71 } 72 73 79 protected void initDefaultFactories() { 80 internalTypeFactories = new ArrayList (3); 81 internalTypeFactories.add(new EnumTypeFactory()); 82 internalTypeFactories.add(new ByteOrCharArrayFactory(this)); 83 84 internalTypeFactories.add(new SerializableTypeFactory(this)); 87 } 88 89 94 public Collection getFactories() { 95 return extendedTypeFactories != null ? Collections 96 .unmodifiableCollection(extendedTypeFactories) : Collections.EMPTY_SET; 97 } 98 99 110 public void addFactory(ExtendedTypeFactory factory) { 111 if (factory == null) { 112 throw new IllegalArgumentException ("Attempt to add null factory"); 113 } 114 115 if (extendedTypeFactories == null) { 116 extendedTypeFactories = new ArrayList (); 117 } 118 119 extendedTypeFactories.add(factory); 120 } 121 122 127 public void removeFactory(ExtendedTypeFactory factory) { 128 if (factory != null && extendedTypeFactories != null) { 129 if (extendedTypeFactories.remove(factory) && extendedTypeFactories.isEmpty()) { 131 extendedTypeFactories = null; 132 } 133 } 134 } 135 136 141 public void registerType(ExtendedType type) { 142 typeMap.put(type.getClassName(), type); 143 144 addFactory(new SubclassTypeFactory(type)); 146 } 147 148 151 public ExtendedType getDefaultType() { 152 return defaultType; 153 } 154 155 174 public ExtendedType getRegisteredType(String javaClassName) { 175 ExtendedType type = getExplictlyRegisteredType(javaClassName); 176 177 if (type != null) { 178 return type; 179 } 180 181 type = getDefaultType(javaClassName); 182 183 if (type != null) { 184 registerType(type); 186 return type; 187 } 188 189 return getDefaultType(); 190 } 191 192 ExtendedType getExplictlyRegisteredType(String className) { 193 return (ExtendedType) typeMap.get(className); 194 } 195 196 201 public ExtendedType getRegisteredType(Class javaClass) { 202 String name = null; 203 204 if (javaClass.isArray()) { 205 name = javaClass.getComponentType().getName() + "[]"; 207 } 208 else { 209 name = javaClass.getName(); 210 } 211 212 return getRegisteredType(name); 213 } 214 215 219 public void unregisterType(String javaClassName) { 220 typeMap.remove(javaClassName); 221 } 222 223 226 public String [] getRegisteredTypeNames() { 227 Set keys = typeMap.keySet(); 228 int len = keys.size(); 229 String [] types = new String [len]; 230 231 Iterator it = keys.iterator(); 232 for (int i = 0; i < len; i++) { 233 types[i] = (String ) it.next(); 234 } 235 236 return types; 237 } 238 239 243 protected ExtendedType getDefaultType(String javaClassName) { 244 return createType(javaClassName); 245 } 246 247 256 protected ExtendedType createType(String className) { 257 258 if (className == null) { 259 return null; 260 } 261 262 Class typeClass; 263 try { 264 typeClass = Util.getJavaClass(className); 265 } 266 catch (Throwable th) { 267 return null; 269 } 270 271 if (extendedTypeFactories != null) { 273 274 Iterator it = extendedTypeFactories.iterator(); 275 while (it.hasNext()) { 276 ExtendedTypeFactory factory = (ExtendedTypeFactory) it.next(); 277 278 ExtendedType type = factory.getType(typeClass); 279 if (type != null) { 280 return type; 281 } 282 } 283 } 284 285 287 Iterator it = internalTypeFactories.iterator(); 288 while (it.hasNext()) { 289 ExtendedTypeFactory factory = (ExtendedTypeFactory) it.next(); 290 291 ExtendedType type = factory.getType(typeClass); 292 if (type != null) { 293 return type; 294 } 295 } 296 297 return null; 298 } 299 } 300 | Popular Tags |