KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > queryframework > InterfaceContainerPolicy


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.queryframework;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedActionException JavaDoc;
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 /**
36  * <p><b>Purpose</b>: The abstract class for ContainerPolicy's whose container class implements
37  * a container interface.
38  * <p>
39  *
40  * @see CollectionContainerPolicy
41  * @see MapContainerPolicy
42  */

43 public abstract class InterfaceContainerPolicy extends ContainerPolicy {
44
45     /** The concrete container class. */
46     protected Class JavaDoc containerClass;
47     protected String JavaDoc containerClassName;
48
49     /** The method which will return a clone of an instance of the containerClass. */
50     protected transient Method cloneMethod;
51
52     /**
53      * INTERNAL:
54      * Construct a new policy.
55      */

56     public InterfaceContainerPolicy() {
57         super();
58     }
59
60     /**
61      * INTERNAL:
62      * Construct a new policy for the specified class.
63      */

64     public InterfaceContainerPolicy(Class JavaDoc containerClass) {
65         setContainerClass(containerClass);
66     }
67
68     /**
69      * INTERNAL:
70      * Construct a new policy for the specified class name.
71      */

72     public InterfaceContainerPolicy(String JavaDoc containerClassName) {
73         setContainerClassName(containerClassName);
74     }
75
76     /**
77      * INTERNAL:
78      * Return a clone of the specified container.
79      */

80     public Object JavaDoc cloneFor(Object JavaDoc container) {
81         if (container == null) {
82             return null;
83         }
84
85         try {
86             return invokeCloneMethodOn(getCloneMethod(), container);
87         } catch (IllegalArgumentException JavaDoc ex) {
88             // container may be a superclass of the concrete container class
89
// so we have to use the right clone method...
90
return invokeCloneMethodOn(getCloneMethod(container.getClass()), container);
91         }
92     }
93
94     /**
95      * INTERNAL:
96      * Convert all the class-name-based settings in this ContainerPolicy to actual class-based
97      * settings. This method is used when converting a project that has been built
98      * with class names to a project with classes.
99      * @param classLoader
100      */

101     public void convertClassNamesToClasses(ClassLoader JavaDoc classLoader){
102         super.convertClassNamesToClasses(classLoader);
103         if (containerClassName == null){
104             return;
105         }
106         Class JavaDoc containerClass = null;
107         try{
108             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
109                 try {
110                     containerClass = (Class JavaDoc)AccessController.doPrivileged(new PrivilegedClassForName(containerClassName, true, classLoader));
111                 } catch (PrivilegedActionException JavaDoc 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 JavaDoc exc){
118             throw ValidationException.classNotFoundWhileConvertingClassNames(containerClassName, exc);
119         }
120         setContainerClass(containerClass);
121     };
122
123     /**
124      * INTERNAL:
125      * Return the 'clone()' Method for the container class.
126      * Lazy initialization is used, so we can serialize these things.
127      */

128     public Method getCloneMethod() {
129         if (cloneMethod == null) {
130             setCloneMethod(getCloneMethod(getContainerClass()));
131         }
132         return cloneMethod;
133     }
134
135     /**
136      * INTERNAL:
137      * Return the 'clone()' Method for the specified class.
138      * Return null if the method does not exist anywhere in the hierarchy
139      */

140     protected Method getCloneMethod(Class JavaDoc javaClass) {
141         try {
142             // This must not be set "accessible" - clone() must be public, and some JVM's do not allow access to JDK classes.
143
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
144                 try {
145                     return (Method)AccessController.doPrivileged(new PrivilegedGetMethod(javaClass, "clone", (Class JavaDoc[])null, false));
146                 } catch (PrivilegedActionException JavaDoc exception) {
147                     throw QueryException.methodDoesNotExistInContainerClass("clone", javaClass);
148                 }
149             } else {
150                 return PrivilegedAccessHelper.getMethod(javaClass, "clone", (Class JavaDoc[])null, false);
151             }
152         } catch (NoSuchMethodException JavaDoc ex) {
153             throw QueryException.methodDoesNotExistInContainerClass("clone", javaClass);
154         }
155     }
156
157     /**
158      * INTERNAL:
159      * Returns the container class to be used with this policy.
160      */

161     public Class JavaDoc getContainerClass() {
162         return containerClass;
163     }
164
165     public String JavaDoc getContainerClassName() {
166         if ((containerClassName == null) && (containerClass != null)) {
167             containerClassName = containerClass.getName();
168         }
169         return containerClassName;
170     }
171
172     public abstract Class JavaDoc getInterfaceType();
173
174     /**
175      * INTERNAL:
176      * Return whether the iterator has more objects,
177      */

178     public boolean hasNext(Object JavaDoc iterator) {
179         return ((Iterator)iterator).hasNext();
180     }
181
182     /**
183      * INTERNAL:
184      * Invoke the specified clone method on the container,
185      * handling the necessary exceptions.
186      */

187     protected Object JavaDoc invokeCloneMethodOn(Method method, Object JavaDoc container) {
188         try {
189             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
190                 try {
191                     return AccessController.doPrivileged(new PrivilegedMethodInvoker(method, container, (Object JavaDoc[])null));
192                 } catch (PrivilegedActionException JavaDoc exception) {
193                     Exception JavaDoc throwableException = exception.getException();
194                     if (throwableException instanceof IllegalAccessException JavaDoc) {
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 JavaDoc[])null);
202             }
203         } catch (IllegalAccessException JavaDoc ex1) {
204             throw QueryException.cannotAccessMethodOnObject(method, container);
205         } catch (InvocationTargetException ex2) {
206             throw QueryException.methodInvocationFailed(method, container, ex2);
207         }
208     }
209
210     /**
211      * INTERNAL:
212      * Validate the container type.
213      */

214     public boolean isValidContainerType(Class JavaDoc containerType) {
215         return oracle.toplink.essentials.internal.helper.Helper.classImplementsInterface(containerType, getInterfaceType());
216     }
217
218     /**
219      * INTERNAL:
220      * Return the next object on the queue.
221      * Valid for some subclasses only.
222      */

223     protected Object JavaDoc next(Object JavaDoc iterator) {
224         return ((Iterator)iterator).next();
225     }
226
227     /**
228      * INTERNAL:
229      * Set the Method that will return a clone of an instance of the containerClass.
230      */

231     public void setCloneMethod(Method cloneMethod) {
232         this.cloneMethod = cloneMethod;
233     }
234
235     /**
236      * INTERNAL:
237      * Set the class to use as the container.
238      */

239     public void setContainerClass(Class JavaDoc containerClass) {
240         this.containerClass = containerClass;
241         initializeConstructor();
242     }
243
244     public void setContainerClassName(String JavaDoc containerClassName) {
245         this.containerClassName = containerClassName;
246     }
247
248     protected Object JavaDoc toStringInfo() {
249         return this.getContainerClass();
250     }
251 }
252
Popular Tags