1 2 12 package com.versant.core.jdo.sco; 13 14 import com.versant.core.metadata.FieldMetaData; 15 import com.versant.core.metadata.MDStaticUtils; 16 import com.versant.core.metadata.MDStatics; 17 import com.versant.core.util.classhelper.ClassHelper; 18 19 import java.util.*; 20 21 import com.versant.core.common.BindingSupportImpl; 22 23 26 public class VersantSCOFactoryRegistry { 27 28 private static final VersantSCOFactoryRegistry defaultMapping = new VersantSCOFactoryRegistry(); 29 30 private HashMap typeFactory = new HashMap(32); 31 32 protected VersantSCOFactoryRegistry() { 33 put(MDStatics.DATE, new DateSCOFactory()); 34 put(MDStatics.HASHSET, new SCOHashSetFactory()); 35 put(MDStatics.SET, new SCOHashSetFactory()); 36 put(MDStatics.TREESET, new SCOTreeSetFactory()); 37 put(MDStatics.SORTEDSET, new SCOTreeSetFactory()); 38 put(MDStatics.COLLECTION, new SCOListFactory()); 39 put(MDStatics.LIST, new SCOListFactory()); 40 put(MDStatics.ARRAYLIST, new SCOArrayListFactory()); 41 put(MDStatics.LINKEDLIST, new SCOLinkedListFactory()); 42 43 44 put(MDStatics.VECTOR, new SCOVectorFactory()); 45 46 47 put(MDStatics.MAP, new SCOHashMapFactory()); 48 put(MDStatics.HASHMAP, new SCOHashMapFactory()); 49 put(MDStatics.HASHTABLE, new SCOHashtableFactory()); 50 put(MDStatics.TREEMAP, new SCOTreeMapFactory()); 51 put(MDStatics.SORTEDMAP, new SCOTreeMapFactory()); 52 } 53 54 private void put(int type, Object factory) { 55 if (factory == null) return; 56 String name = MDStaticUtils.toSimpleName(type); 57 if (name == null) return; 58 Class c = MDStaticUtils.toSimpleClass(name); 59 if (c == null) return; 60 typeFactory.put(c, factory); 61 } 62 63 public VersantSCOFactoryRegistry(Map mapping, ClassLoader loader) { 64 this(); 65 if (mapping == null) return; 66 for (Iterator it = mapping.keySet().iterator(); it.hasNext();) { 67 String key = (String ) it.next(); 68 String value = (String ) mapping.get(key); 69 try { 70 Class javaClass = ClassHelper.get().classForName(key, true, loader); 71 Class scoFactoryClass = ClassHelper.get().classForName(value, true, loader); 72 Object factory = scoFactoryClass.newInstance(); 73 typeFactory.put(javaClass, factory); 74 } catch (Exception e) { 75 throw BindingSupportImpl.getInstance().runtime("Unable to add SCO factory mapping:\n" + 76 e.getMessage(), e); 77 } 78 } 79 } 80 81 84 public Object getFactory(FieldMetaData fmd) { 85 if (fmd == null) return null; 86 switch (fmd.category) { 88 case MDStatics.CATEGORY_SIMPLE: 89 return getJdoGenieSCOFactory(fmd); 90 case MDStatics.CATEGORY_COLLECTION: 91 return getJDOGenieSCOCollectionFactory(fmd); 92 case MDStatics.CATEGORY_MAP: 93 return getJDOGenieSCOMapFactory(fmd); 94 } 95 return null; 96 } 97 98 101 public VersantSCOFactory getJdoGenieSCOFactory(FieldMetaData fmd) { 102 return (VersantSCOFactory) getFactory(fmd, MDStatics.CATEGORY_SIMPLE, VersantSCOFactory.class); 103 } 104 105 108 public VersantSCOCollectionFactory getJDOGenieSCOCollectionFactory(FieldMetaData fmd) { 109 return (VersantSCOCollectionFactory) getFactory(fmd, 110 MDStatics.CATEGORY_COLLECTION, VersantSCOCollectionFactory.class); 111 } 112 113 116 public VersantSCOMapFactory getJDOGenieSCOMapFactory(FieldMetaData fmd) { 117 return (VersantSCOMapFactory) getFactory(fmd, 118 MDStatics.CATEGORY_MAP, VersantSCOMapFactory.class); 119 } 120 121 private Object getFactory(FieldMetaData fmd, int category, Class factoryCls) { 122 if (fmd.category != category) { 123 return null; 124 } 125 Class cls = fmd.type; 126 127 Object o = typeFactory.get(cls); 128 129 if (o == null) { 130 throw BindingSupportImpl.getInstance().notImplemented("No SCO factory registered for type: " + 131 MDStaticUtils.toSimpleName(fmd.typeCode) + ".\nClass: " + 132 fmd.classMetaData.cls.getName() + " Field: " + fmd.name); 133 } 134 if (!factoryCls.isAssignableFrom(o.getClass())) { 135 throw BindingSupportImpl.getInstance().notImplemented("Incorrect SCO factory type.\nExpected:" + 136 factoryCls.getName() + "\nFound:" + o.getClass().getName() + ".\nfor Class: " + 137 fmd.classMetaData.cls.getName() + " Field: " + fmd.name); 138 } 139 return o; 140 } 141 142 public static String getDefaultMapping(Class javaType) { 143 Object factory = defaultMapping.typeFactory.get(javaType); 144 if (factory != null) { 145 return factory.getClass().getName(); 146 } 147 return null; 148 } 149 150 public static void fillMapWithDefaults(Map map) { 151 for (Iterator it = defaultMapping.typeFactory.keySet().iterator(); it.hasNext();) { 152 Class javaType = (Class ) it.next(); 153 Object factory = defaultMapping.typeFactory.get(javaType); 154 map.put(javaType, factory.getClass().getName()); 155 } 156 } 157 158 public static List getValidSCOFactoryList(Class fieldType) { 159 List factoryList = new ArrayList(); 160 for (Iterator it = defaultMapping.typeFactory.keySet().iterator(); it.hasNext();) { 161 Class javaType = (Class ) it.next(); 162 if (fieldType.isAssignableFrom(javaType)) { 163 Object factory = defaultMapping.typeFactory.get(javaType); 164 if (factory != null) { 165 String factoryName = factory.getClass().getName(); 166 if (!factoryList.contains(factoryName)) { 167 factoryList.add(factoryName); 168 } 169 } 170 } 171 } 172 Collections.sort(factoryList); 173 return factoryList; 174 } 175 176 public static void removeDefaults(Map map) { 177 for (Iterator it = defaultMapping.typeFactory.keySet().iterator(); it.hasNext();) { 178 Class javaType = (Class ) it.next(); 179 String factoryName1 = (String ) map.get(javaType); 180 if (factoryName1 != null) { 181 Object factory = defaultMapping.typeFactory.get(javaType); 182 if (factory != null) { 183 String factoryName2 = factory.getClass().getName(); 184 if (factoryName1.equals(factoryName2)) { 185 map.remove(javaType); 186 } 187 } 188 } 189 } 190 } 191 192 } 193 | Popular Tags |