1 21 package oracle.toplink.essentials.internal.queryframework; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.util.*; 27 import java.lang.reflect.*; 28 29 import oracle.toplink.essentials.exceptions.*; 30 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 31 import oracle.toplink.essentials.internal.security.PrivilegedMethodInvoker; 32 import oracle.toplink.essentials.internal.security.PrivilegedClassForName; 33 import oracle.toplink.essentials.internal.security.PrivilegedGetMethod; 34 35 43 public abstract class InterfaceContainerPolicy extends ContainerPolicy { 44 45 46 protected Class containerClass; 47 protected String containerClassName; 48 49 50 protected transient Method cloneMethod; 51 52 56 public InterfaceContainerPolicy() { 57 super(); 58 } 59 60 64 public InterfaceContainerPolicy(Class containerClass) { 65 setContainerClass(containerClass); 66 } 67 68 72 public InterfaceContainerPolicy(String containerClassName) { 73 setContainerClassName(containerClassName); 74 } 75 76 80 public Object cloneFor(Object container) { 81 if (container == null) { 82 return null; 83 } 84 85 try { 86 return invokeCloneMethodOn(getCloneMethod(), container); 87 } catch (IllegalArgumentException ex) { 88 return invokeCloneMethodOn(getCloneMethod(container.getClass()), container); 91 } 92 } 93 94 101 public void convertClassNamesToClasses(ClassLoader classLoader){ 102 super.convertClassNamesToClasses(classLoader); 103 if (containerClassName == null){ 104 return; 105 } 106 Class containerClass = null; 107 try{ 108 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 109 try { 110 containerClass = (Class )AccessController.doPrivileged(new PrivilegedClassForName(containerClassName, true, classLoader)); 111 } catch (PrivilegedActionException exception) { 112 throw ValidationException.classNotFoundWhileConvertingClassNames(containerClassName, exception.getException()); 113 } 114 } else { 115 containerClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(containerClassName, true, classLoader); 116 } 117 } catch (ClassNotFoundException exc){ 118 throw ValidationException.classNotFoundWhileConvertingClassNames(containerClassName, exc); 119 } 120 setContainerClass(containerClass); 121 }; 122 123 128 public Method getCloneMethod() { 129 if (cloneMethod == null) { 130 setCloneMethod(getCloneMethod(getContainerClass())); 131 } 132 return cloneMethod; 133 } 134 135 140 protected Method getCloneMethod(Class javaClass) { 141 try { 142 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 144 try { 145 return (Method)AccessController.doPrivileged(new PrivilegedGetMethod(javaClass, "clone", (Class [])null, false)); 146 } catch (PrivilegedActionException exception) { 147 throw QueryException.methodDoesNotExistInContainerClass("clone", javaClass); 148 } 149 } else { 150 return PrivilegedAccessHelper.getMethod(javaClass, "clone", (Class [])null, false); 151 } 152 } catch (NoSuchMethodException ex) { 153 throw QueryException.methodDoesNotExistInContainerClass("clone", javaClass); 154 } 155 } 156 157 161 public Class getContainerClass() { 162 return containerClass; 163 } 164 165 public String getContainerClassName() { 166 if ((containerClassName == null) && (containerClass != null)) { 167 containerClassName = containerClass.getName(); 168 } 169 return containerClassName; 170 } 171 172 public abstract Class getInterfaceType(); 173 174 178 public boolean hasNext(Object iterator) { 179 return ((Iterator)iterator).hasNext(); 180 } 181 182 187 protected Object invokeCloneMethodOn(Method method, Object container) { 188 try { 189 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 190 try { 191 return AccessController.doPrivileged(new PrivilegedMethodInvoker(method, container, (Object [])null)); 192 } catch (PrivilegedActionException exception) { 193 Exception throwableException = exception.getException(); 194 if (throwableException instanceof IllegalAccessException ) { 195 throw QueryException.cannotAccessMethodOnObject(method, container); 196 } else { 197 throw QueryException.methodInvocationFailed(method, container, throwableException); 198 } 199 } 200 } else { 201 return PrivilegedAccessHelper.invokeMethod(method, container, (Object [])null); 202 } 203 } catch (IllegalAccessException ex1) { 204 throw QueryException.cannotAccessMethodOnObject(method, container); 205 } catch (InvocationTargetException ex2) { 206 throw QueryException.methodInvocationFailed(method, container, ex2); 207 } 208 } 209 210 214 public boolean isValidContainerType(Class containerType) { 215 return oracle.toplink.essentials.internal.helper.Helper.classImplementsInterface(containerType, getInterfaceType()); 216 } 217 218 223 protected Object next(Object iterator) { 224 return ((Iterator)iterator).next(); 225 } 226 227 231 public void setCloneMethod(Method cloneMethod) { 232 this.cloneMethod = cloneMethod; 233 } 234 235 239 public void setContainerClass(Class containerClass) { 240 this.containerClass = containerClass; 241 initializeConstructor(); 242 } 243 244 public void setContainerClassName(String containerClassName) { 245 this.containerClassName = containerClassName; 246 } 247 248 protected Object toStringInfo() { 249 return this.getContainerClass(); 250 } 251 } 252 | Popular Tags |