| 1 | |
package org.apache.ojb.otm.copy; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
import org.apache.ojb.broker.PersistenceBroker; |
| 19 | |
import org.apache.ojb.broker.metadata.*; |
| 20 | |
import org.apache.ojb.broker.metadata.fieldaccess.PersistentField; |
| 21 | |
import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl; |
| 22 | |
import org.apache.ojb.broker.core.proxy.ProxyHelper; |
| 23 | |
import org.apache.ojb.broker.util.ConstructorHelper; |
| 24 | |
import org.apache.ojb.broker.util.IdentityMapFactory; |
| 25 | |
|
| 26 | |
import java.lang.reflect.Constructor; |
| 27 | |
import java.util.Map; |
| 28 | |
import java.util.Collection; |
| 29 | |
import java.util.Iterator; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
public final class MetadataObjectCopyStrategy implements ObjectCopyStrategy |
| 38 | |
{ |
| 39 | |
private static final ReflectiveObjectCopyStrategy _reflective = new ReflectiveObjectCopyStrategy(); |
| 40 | |
private static final SerializeObjectCopyStrategy _serialize = new SerializeObjectCopyStrategy(); |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public Object copy(final Object obj, final PersistenceBroker broker) |
| 49 | |
{ |
| 50 | |
return clone(obj, IdentityMapFactory.getIdentityMap(), broker); |
| 51 | |
} |
| 52 | |
|
| 53 | |
private static Object clone(final Object toCopy, final Map objMap, final PersistenceBroker broker) |
| 54 | |
{ |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
if (objMap.containsKey(toCopy)) return objMap.get(toCopy); |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
if (toCopy == null) |
| 64 | |
return null; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
if (ProxyHelper.isVirtualOjbProxy(toCopy)) |
| 70 | |
{ |
| 71 | |
return _reflective.copy(toCopy, null); |
| 72 | |
} |
| 73 | |
else if (ProxyHelper.isNormalOjbProxy(toCopy)) |
| 74 | |
{ |
| 75 | |
return _serialize.copy(toCopy, null); |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
final ClassDescriptor cld = broker.getClassDescriptor(toCopy.getClass()); |
| 83 | |
if (cld == null) |
| 84 | |
{ |
| 85 | |
return _reflective.copy(toCopy, null); |
| 86 | |
} |
| 87 | |
|
| 88 | |
final Object retval; |
| 89 | |
try |
| 90 | |
{ |
| 91 | |
final Constructor con = cld.getZeroArgumentConstructor(); |
| 92 | |
retval = ConstructorHelper.instantiate(con); |
| 93 | |
objMap.put(toCopy,retval); |
| 94 | |
} |
| 95 | |
catch (InstantiationException e) |
| 96 | |
{ |
| 97 | |
throw new ObjectCopyException("InstantiationException", e); |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
final FieldDescriptor[] fieldDescs = cld.getFieldDescriptions(); |
| 105 | |
|
| 106 | |
for (int i = 0; i < fieldDescs.length; i++) |
| 107 | |
{ |
| 108 | |
final FieldDescriptor fd = fieldDescs[i]; |
| 109 | |
final PersistentField f = fd.getPersistentField(); |
| 110 | |
Object fieldValue = f.get(toCopy); |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
f.set(retval, fieldValue); |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
final Collection refDescsCol = cld.getObjectReferenceDescriptors(); |
| 130 | |
final ObjectReferenceDescriptor[] rds = (ObjectReferenceDescriptor[]) refDescsCol.toArray(new ObjectReferenceDescriptor[refDescsCol.size()]); |
| 131 | |
for (int i = 0; i < rds.length; i++) |
| 132 | |
{ |
| 133 | |
final ObjectReferenceDescriptor rd = rds[i]; |
| 134 | |
final PersistentField f = rd.getPersistentField(); |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
final Object object = f.get(toCopy); |
| 140 | |
final Object clone = clone(object, objMap, broker); |
| 141 | |
objMap.put(object, clone); |
| 142 | |
f.set(retval, clone); |
| 143 | |
} |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
final Collection colDescsCol = cld.getCollectionDescriptors(); |
| 148 | |
final Iterator it = colDescsCol.iterator(); |
| 149 | |
while (it.hasNext()) |
| 150 | |
{ |
| 151 | |
final CollectionDescriptor cd = (CollectionDescriptor) it.next(); |
| 152 | |
final PersistentField f = cd.getPersistentField(); |
| 153 | |
final Object collection = f.get(toCopy); |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
if (collection == null) |
| 159 | |
{ |
| 160 | |
f.set(retval, null); |
| 161 | |
} |
| 162 | |
else if (collection instanceof CollectionProxyDefaultImpl) |
| 163 | |
{ |
| 164 | |
f.set(retval, _reflective.copy(collection, null)); |
| 165 | |
} |
| 166 | |
else if (collection instanceof Collection) |
| 167 | |
{ |
| 168 | |
try |
| 169 | |
{ |
| 170 | |
final Collection newCollection = (Collection) collection.getClass().newInstance(); |
| 171 | |
final Iterator tempIter = ((Collection) collection).iterator(); |
| 172 | |
Object obj; |
| 173 | |
while (tempIter.hasNext()) |
| 174 | |
{ |
| 175 | |
obj = tempIter.next(); |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
if (ProxyHelper.isNormalOjbProxy(obj)) |
| 180 | |
{ |
| 181 | |
newCollection.add(obj); |
| 182 | |
} |
| 183 | |
else |
| 184 | |
{ |
| 185 | |
final Object clone = clone(obj, objMap, broker); |
| 186 | |
objMap.put(obj, clone); |
| 187 | |
newCollection.add(clone); |
| 188 | |
} |
| 189 | |
} |
| 190 | |
f.set(retval, newCollection); |
| 191 | |
} |
| 192 | |
catch (InstantiationException e) |
| 193 | |
{ |
| 194 | |
throw new ObjectCopyException("InstantiationException", e); |
| 195 | |
} |
| 196 | |
catch (IllegalAccessException e) |
| 197 | |
{ |
| 198 | |
throw new ObjectCopyException("IllegalAccessException", e); |
| 199 | |
} |
| 200 | |
} |
| 201 | |
else |
| 202 | |
{ |
| 203 | |
throw new java.lang.UnsupportedOperationException("MetadataObjectCopyStrategy cannot handle Collection of type: " + collection.getClass().getName()); |
| 204 | |
} |
| 205 | |
} |
| 206 | |
return retval; |
| 207 | |
} |
| 208 | |
} |