KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > TCClassFactoryImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * @author steve
28  */

29 public class TCClassFactoryImpl implements TCClassFactory {
30   private static final LiteralValues literalValues = new LiteralValues();
31   private static Class JavaDoc[] APPLICATOR_CSTR_SIGNATURE = new Class JavaDoc[] { DNAEncoding.class };
32
33   private final Map JavaDoc classes = new HashMap JavaDoc();
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 JavaDoc clazz, ClientObjectManager objectManager) {
47     synchronized (classes) {
48       TCClass rv = (TCClass) classes.get(clazz);
49       if (rv == null) {
50         Class JavaDoc tcPeerClass = config.getTCPeerClass(clazz);
51         String JavaDoc loaderDesc = classProvider.getLoaderDescriptionFor(tcPeerClass);
52         String JavaDoc 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 JavaDoc getLogicalSuperClassWithDefaultConstructor(Class JavaDoc 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 JavaDoc c = null;
72         try {
73           c = clazz.getDeclaredConstructor(new Class JavaDoc[0]);
74         } catch (SecurityException JavaDoc e) {
75           throw new TCRuntimeException(e);
76         } catch (NoSuchMethodException JavaDoc 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 JavaDoc name = clazz.getName();
94     Class JavaDoc 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 JavaDoc cstr = applicatorClazz.getConstructor(APPLICATOR_CSTR_SIGNATURE);
112       Object JavaDoc[] params = new Object JavaDoc[] { encoding };
113       return (ChangeApplicator) cstr.newInstance(params);
114     } catch (Exception JavaDoc e) {
115       throw new AssertionError JavaDoc(e);
116     }
117   }
118 }
119
Popular Tags