KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.tc.object;
5
6 import com.tc.object.bytecode.Manageable;
7 import com.tc.object.dna.api.DNA;
8 import com.tc.util.UnsafeUtil;
9
10 import java.io.IOException JavaDoc;
11 import java.lang.reflect.Array JavaDoc;
12 import java.lang.reflect.Constructor JavaDoc;
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 /**
16  * @author orion
17  */

18 public class TCObjectFactoryImpl implements TCObjectFactory {
19   private final static Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[] {};
20
21   private ClientObjectManager objectManager;
22   private final TCClassFactory clazzFactory;
23
24   public TCObjectFactoryImpl(TCClassFactory clazzFactory) {
25     this.clazzFactory = clazzFactory;
26   }
27
28   public void setObjectManager(ClientObjectManager objectManager) {
29     this.objectManager = objectManager;
30   }
31
32   public TCObject getNewInstance(ObjectID id, Object JavaDoc peer, Class JavaDoc clazz) {
33     TCClass tcc = clazzFactory.getOrCreate(clazz, objectManager);
34     TCObject rv = tcc.createTCObject(id, peer);
35
36     if (peer instanceof Manageable) {
37       ((Manageable) peer).__tc_managed(rv);
38     }
39
40     return rv;
41   }
42
43   public TCObject getNewInstance(ObjectID id, Class JavaDoc clazz) {
44     return getNewInstance(id, null, clazz);
45   }
46
47   public Object JavaDoc getNewPeerObject(TCClass type, DNA dna) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
48     return type.getNewInstanceFromNonDefaultConstructor(dna);
49   }
50
51   public Object JavaDoc getNewPeerObject(TCClass type, Object JavaDoc parent) throws IllegalArgumentException JavaDoc, SecurityException JavaDoc,
52       InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc {
53     // This one is for non-static inner classes
54
return getNewPeerObject(type.getConstructor(), type, parent);
55   }
56
57   public Object JavaDoc getNewArrayInstance(TCClass type, int size) {
58     return Array.newInstance(type.getComponentType(), size);
59   }
60
61   public Object JavaDoc getNewPeerObject(TCClass type) throws IllegalArgumentException JavaDoc, InstantiationException JavaDoc,
62       IllegalAccessException JavaDoc, InvocationTargetException JavaDoc, SecurityException JavaDoc, NoSuchMethodException JavaDoc {
63     Constructor JavaDoc ctor = type.getConstructor();
64     if (ctor == null) throw new AssertionError JavaDoc("type:" + type.getName());
65     return getNewPeerObject(ctor, EMPTY_OBJECT_ARRAY);
66   }
67
68   private Object JavaDoc getNewPeerObject(Constructor JavaDoc ctor, Object JavaDoc[] args) throws IllegalArgumentException JavaDoc,
69       InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
70     final Object JavaDoc rv;
71
72     // XXX: hack to workaround issue with commons logging dependence on context loader
73
Thread JavaDoc thread = Thread.currentThread();
74     ClassLoader JavaDoc cl = thread.getContextClassLoader();
75     try {
76       thread.setContextClassLoader(ctor.getDeclaringClass().getClassLoader());
77       rv = ctor.newInstance(args);
78     } finally {
79       thread.setContextClassLoader(cl);
80     }
81     return rv;
82   }
83
84   private Object JavaDoc getNewPeerObject(Constructor JavaDoc ctor, TCClass type, Object JavaDoc parent) throws IllegalArgumentException JavaDoc,
85       InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
86     final Object JavaDoc rv;
87
88     // XXX: hack to workaround issue with commons logging dependence on context loader
89
Thread JavaDoc thread = Thread.currentThread();
90     ClassLoader JavaDoc cl = thread.getContextClassLoader();
91     try {
92       thread.setContextClassLoader(ctor.getDeclaringClass().getClassLoader());
93       rv = ctor.newInstance(EMPTY_OBJECT_ARRAY);
94       UnsafeUtil.setField(rv, type.getParentField(), parent);
95     } finally {
96       thread.setContextClassLoader(cl);
97     }
98     return rv;
99   }
100
101 }
Popular Tags