1 21 package oracle.toplink.essentials.descriptors; 23 24 import java.lang.reflect.*; 25 import java.security.AccessController ; 26 import java.security.PrivilegedActionException ; 27 28 import oracle.toplink.essentials.internal.helper.*; 29 import oracle.toplink.essentials.exceptions.*; 30 import oracle.toplink.essentials.descriptors.ClassDescriptor; 31 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 32 import oracle.toplink.essentials.sessions.Session; 33 import oracle.toplink.essentials.sessions.Record; 34 import oracle.toplink.essentials.internal.security.*; 35 36 44 public class MethodClassExtractor extends ClassExtractor { 45 protected transient ClassDescriptor descriptor; 46 protected String classExtractionMethodName; 47 protected transient Method classExtractionMethod; 48 49 53 public Method getClassExtractionMethod() { 54 return classExtractionMethod; 55 } 56 57 69 public String getClassExtractionMethodName() { 70 return classExtractionMethodName; 71 } 72 73 76 protected void setClassExtractionMethod(Method classExtractionMethod) { 77 this.classExtractionMethod = classExtractionMethod; 78 } 79 80 92 public void setClassExtractionMethodName(String staticClassClassExtractionMethod) { 93 this.classExtractionMethodName = staticClassClassExtractionMethod; 94 } 95 96 100 public void initialize(ClassDescriptor descriptor, Session session) throws DescriptorException { 101 setDescriptor(descriptor); 102 Class [] declarationParameters = new Class [1]; 103 declarationParameters[0] = ClassConstants.DatabaseRow_Class; 104 105 try { 106 setClassExtractionMethod(Helper.getDeclaredMethod(descriptor.getJavaClass(), getClassExtractionMethodName(), declarationParameters)); 107 } catch (NoSuchMethodException ignore) { 108 declarationParameters[0] = ClassConstants.Record_Class; 109 try { 110 setClassExtractionMethod(Helper.getDeclaredMethod(descriptor.getJavaClass(), getClassExtractionMethodName(), declarationParameters)); 111 } catch (NoSuchMethodException exception) { 112 throw DescriptorException.noSuchMethodWhileInitializingClassExtractionMethod(getClassExtractionMethodName(), descriptor, exception); 113 } 114 } catch (SecurityException exception) { 115 throw DescriptorException.securityWhileInitializingClassExtractionMethod(getClassExtractionMethodName(), descriptor, exception); 116 } 117 118 if (!Modifier.isStatic(getClassExtractionMethod().getModifiers())) { 120 throw DescriptorException.classExtractionMethodMustBeStatic(getClassExtractionMethodName(), descriptor); 121 } 122 } 123 124 130 public Class extractClassFromRow(Record row, oracle.toplink.essentials.sessions.Session session) { 131 Class classForRow; 132 133 try { 134 Object [] arguments = new Object [1]; 135 arguments[0] = row; 136 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 137 try { 138 classForRow = (Class )AccessController.doPrivileged(new PrivilegedMethodInvoker(getClassExtractionMethod(), null, arguments)); 139 } catch (PrivilegedActionException exception) { 140 Exception throwableException = exception.getException(); 141 if (throwableException instanceof IllegalAccessException ) { 142 throw DescriptorException.illegalAccessWhileInvokingRowExtractionMethod((AbstractRecord)row, getClassExtractionMethod(), getDescriptor(), throwableException); 143 } else { 144 throw DescriptorException.targetInvocationWhileInvokingRowExtractionMethod((AbstractRecord)row, getClassExtractionMethod(), getDescriptor(), throwableException); 145 } 146 } 147 } else { 148 classForRow = (Class )PrivilegedAccessHelper.invokeMethod(getClassExtractionMethod(), null, arguments); 149 } 150 } catch (IllegalAccessException exception) { 151 throw DescriptorException.illegalAccessWhileInvokingRowExtractionMethod((AbstractRecord)row, getClassExtractionMethod(), getDescriptor(), exception); 152 } catch (InvocationTargetException exception) { 153 throw DescriptorException.targetInvocationWhileInvokingRowExtractionMethod((AbstractRecord)row, getClassExtractionMethod(), getDescriptor(), exception); 154 } 155 156 return classForRow; 157 } 158 159 163 protected ClassDescriptor getDescriptor() { 164 return descriptor; 165 } 166 167 171 protected void setDescriptor(ClassDescriptor descriptor) { 172 this.descriptor = descriptor; 173 } 174 } 175 | Popular Tags |