1 21 package oracle.toplink.essentials.descriptors.copying; 23 24 import java.lang.reflect.*; 25 import java.security.AccessController ; 26 import java.security.PrivilegedActionException ; 27 28 import oracle.toplink.essentials.exceptions.*; 29 import oracle.toplink.essentials.internal.helper.*; 30 import oracle.toplink.essentials.queryframework.ObjectBuildingQuery; 31 import oracle.toplink.essentials.sessions.*; 32 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 33 import oracle.toplink.essentials.internal.descriptors.ObjectBuilder; 34 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 35 import oracle.toplink.essentials.internal.security.PrivilegedMethodInvoker; 36 37 40 public class CloneCopyPolicy extends AbstractCopyPolicy { 41 42 43 protected String methodName; 44 protected String workingCopyMethodName; 45 protected transient Method method; 46 protected transient Method workingCopyMethod; 47 48 public CloneCopyPolicy() { 49 super(); 50 } 51 52 55 public Object buildClone(Object domainObject, Session session) throws DescriptorException { 56 if (this.getMethodName() == null) { 58 return getDescriptor().getObjectBuilder().buildNewInstance(); 59 } 60 try { 61 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 62 try { 63 return AccessController.doPrivileged(new PrivilegedMethodInvoker(this.getMethod(), domainObject, new Object [0])); 64 } catch (PrivilegedActionException exception) { 65 Exception throwableException = exception.getException(); 66 if (throwableException instanceof IllegalAccessException ) { 67 throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException); 68 } else { 69 throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException); 70 71 } 72 } 73 } else { 74 return PrivilegedAccessHelper.invokeMethod(this.getMethod(), domainObject, new Object [0]); 75 } 76 } catch (IllegalAccessException exception) { 77 throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception); 78 } catch (InvocationTargetException exception) { 79 throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception); 80 } 81 } 82 83 86 public Object buildWorkingCopyClone(Object domainObject, Session session) throws DescriptorException { 87 if (this.getWorkingCopyMethodName() == null) { 88 return this.buildClone(domainObject, session); 90 } 91 92 try { 93 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 94 try { 95 return AccessController.doPrivileged(new PrivilegedMethodInvoker(this.getWorkingCopyMethod(), domainObject, new Object [0])); 96 } catch (PrivilegedActionException exception) { 97 Exception throwableException = exception.getException(); 98 if (throwableException instanceof IllegalAccessException ) { 99 throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException); 100 } else { 101 throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException); 102 } 103 } 104 } else { 105 return PrivilegedAccessHelper.invokeMethod(this.getWorkingCopyMethod(), domainObject, new Object [0]); 106 } 107 108 } catch (IllegalAccessException exception) { 109 throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception); 110 } catch (InvocationTargetException exception) { 111 throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception); 112 } 113 } 114 115 118 public Object buildWorkingCopyCloneFromRow(Record row, ObjectBuildingQuery query) throws DescriptorException { 119 ObjectBuilder builder = getDescriptor().getObjectBuilder(); 122 if (getWorkingCopyMethodName() != null) { 123 Object original = builder.buildNewInstance(); 124 builder.buildAttributesIntoShallowObject(original, (AbstractRecord)row, query); 125 return buildWorkingCopyClone(original, query.getSession()); 126 } else { 127 return builder.buildNewInstance(); 128 } 129 } 130 131 134 protected Method getMethod() { 135 return method; 136 } 137 138 141 public String getMethodName() { 142 return methodName; 143 } 144 145 149 protected Method getWorkingCopyMethod() { 150 return workingCopyMethod; 151 } 152 153 157 public String getWorkingCopyMethodName() { 158 return workingCopyMethodName; 159 } 160 161 164 public void initialize(Session session) throws DescriptorException { 165 try { 166 if (this.getMethodName() != null) { 168 this.setMethod(Helper.getDeclaredMethod(this.getDescriptor().getJavaClass(), this.getMethodName(), new Class [0])); 169 } 170 } catch (NoSuchMethodException exception) { 171 session.getIntegrityChecker().handleError(DescriptorException.noSuchMethodWhileInitializingCopyPolicy(this.getMethodName(), this.getDescriptor(), exception)); 172 } catch (SecurityException exception) { 173 session.getIntegrityChecker().handleError(DescriptorException.securityWhileInitializingCopyPolicy(this.getMethodName(), this.getDescriptor(), exception)); 174 } 175 if (this.getWorkingCopyMethodName() != null) { 176 try { 177 this.setWorkingCopyMethod(Helper.getDeclaredMethod(this.getDescriptor().getJavaClass(), this.getWorkingCopyMethodName(), new Class [0])); 178 } catch (NoSuchMethodException exception) { 179 session.getIntegrityChecker().handleError(DescriptorException.noSuchMethodWhileInitializingCopyPolicy(this.getMethodName(), this.getDescriptor(), exception)); 180 } catch (SecurityException exception) { 181 session.getIntegrityChecker().handleError(DescriptorException.securityWhileInitializingCopyPolicy(this.getMethodName(), this.getDescriptor(), exception)); 182 } 183 } 184 } 185 186 189 protected void setMethod(Method method) { 190 this.method = method; 191 } 192 193 196 public void setMethodName(String methodName) { 197 this.methodName = methodName; 198 } 199 200 204 protected void setWorkingCopyMethod(Method method) { 205 this.workingCopyMethod = method; 206 } 207 208 212 public void setWorkingCopyMethodName(String methodName) { 213 this.workingCopyMethodName = methodName; 214 } 215 216 219 public boolean buildsNewInstance() { 220 return getMethodName() == null; 221 } 222 223 public String toString() { 224 return Helper.getShortClassName(this) + "(" + this.getMethodName() + ")"; 225 } 226 } 227 | Popular Tags |