1 5 package com.tc.object; 6 7 import com.tc.exception.TCRuntimeException; 8 import com.tc.object.applicator.AccessibleObjectApplicator; 9 import com.tc.object.applicator.ArrayApplicator; 10 import com.tc.object.applicator.ChangeApplicator; 11 import com.tc.object.applicator.FileApplicator; 12 import com.tc.object.applicator.LiteralTypesApplicator; 13 import com.tc.object.applicator.PhysicalApplicator; 14 import com.tc.object.applicator.ProxyApplicator; 15 import com.tc.object.config.DSOClientConfigHelper; 16 import com.tc.object.config.TransparencyClassSpec; 17 import com.tc.object.dna.impl.DNAEncoding; 18 import com.tc.object.field.TCFieldFactory; 19 import com.tc.object.loaders.ClassProvider; 20 import com.tc.util.ClassUtils; 21 22 import java.lang.reflect.Constructor ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 29 public class TCClassFactoryImpl implements TCClassFactory { 30 private static final LiteralValues literalValues = new LiteralValues(); 31 private static Class [] APPLICATOR_CSTR_SIGNATURE = new Class [] { DNAEncoding.class }; 32 33 private final Map classes = new HashMap (); 34 private final TCFieldFactory fieldFactory; 35 private final DSOClientConfigHelper config; 36 private final ClassProvider classProvider; 37 private final DNAEncoding encoding; 38 39 public TCClassFactoryImpl(TCFieldFactory fieldFactory, DSOClientConfigHelper config, ClassProvider classProvider) { 40 this.fieldFactory = fieldFactory; 41 this.config = config; 42 this.classProvider = classProvider; 43 this.encoding = new DNAEncoding(classProvider); 44 } 45 46 public TCClass getOrCreate(Class clazz, ClientObjectManager objectManager) { 47 synchronized (classes) { 48 TCClass rv = (TCClass) classes.get(clazz); 49 if (rv == null) { 50 Class tcPeerClass = config.getTCPeerClass(clazz); 51 String loaderDesc = classProvider.getLoaderDescriptionFor(tcPeerClass); 52 String className = clazz.getName(); 53 rv = new TCClassImpl(fieldFactory, this, objectManager, tcPeerClass, 54 getLogicalSuperClassWithDefaultConstructor(clazz), loaderDesc, config 55 .getLogicalExtendingClassName(className), config.isLogical(className), config 56 .isCallConstructorOnLoad(className), config.getOnLoadScriptIfDefined(className), 57 config.getOnLoadMethodIfDefined(className), config.isUseNonDefaultConstructor(clazz)); 58 classes.put(clazz, rv); 59 } 60 return rv; 61 } 62 } 63 64 public Class getLogicalSuperClassWithDefaultConstructor(Class clazz) { 65 TransparencyClassSpec spec = config.getSpec(clazz.getName()); 66 if (spec == null) { return null; } 67 68 while (clazz != null) { 69 if (spec == null) { return null; } 70 if (spec.isLogical()) { 71 Constructor c = null; 72 try { 73 c = clazz.getDeclaredConstructor(new Class [0]); 74 } catch (SecurityException e) { 75 throw new TCRuntimeException(e); 76 } catch (NoSuchMethodException e) { 77 c = null; 78 } 79 if (c != null) { return clazz; } 80 } 81 clazz = clazz.getSuperclass(); 82 if (clazz != null) { 83 spec = config.getSpec(clazz.getName()); 84 } else { 85 spec = null; 86 } 87 } 88 return null; 89 } 90 91 public ChangeApplicator createApplicatorFor(TCClass clazz, boolean indexed) { 92 if (indexed) { return new ArrayApplicator(encoding); } 93 String name = clazz.getName(); 94 Class applicatorClazz = config.getChangeApplicator(clazz.getPeerClass()); 95 96 if (applicatorClazz == null) { 97 if (literalValues.isLiteral(name)) { 98 return new LiteralTypesApplicator(clazz, encoding); 99 } else if (clazz.isProxyClass()) { 100 return new ProxyApplicator(encoding); 101 } else if (ClassUtils.isPortableReflectionClass(clazz.getPeerClass())) { 102 return new AccessibleObjectApplicator(encoding); 103 } else if ("java.io.File".equals(name)) { 104 return new FileApplicator(clazz, encoding); 105 } else { 106 return new PhysicalApplicator(clazz, encoding); 107 } 108 } 109 110 try { 111 Constructor cstr = applicatorClazz.getConstructor(APPLICATOR_CSTR_SIGNATURE); 112 Object [] params = new Object [] { encoding }; 113 return (ChangeApplicator) cstr.newInstance(params); 114 } catch (Exception e) { 115 throw new AssertionError (e); 116 } 117 } 118 } 119 | Popular Tags |