KickJava   Java API By Example, From Geeks To Geeks.

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


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.Comparator JavaDoc;
27 import java.lang.reflect.Constructor JavaDoc;
28 import oracle.toplink.essentials.exceptions.*;
29 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
30 import oracle.toplink.essentials.internal.security.PrivilegedGetConstructorFor;
31 import oracle.toplink.essentials.internal.security.PrivilegedInvokeConstructor;
32 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass;
33
34 /**
35  * <p><b>Purpose</b>: A SortedCollectionContainerPolicy is ContainerPolicy whose
36  * container class implements the SortedInterface interface.
37  * Added for BUG # 3233263
38  * <p>
39  * <p><b>Responsibilities</b>:
40  * Provide the functionality to operate on an instance of a SortedSet.
41  *
42  * @see ContainerPolicy
43  * @see MapContainerPolicy
44  */

45 public class SortedCollectionContainerPolicy extends CollectionContainerPolicy {
46     protected Comparator JavaDoc m_comparator = null;
47
48     /**
49      * INTERNAL:
50      * Construct a new policy.
51      */

52     public SortedCollectionContainerPolicy() {
53         super();
54     }
55
56     /**
57      * INTERNAL:
58      * Construct a new policy for the specified class.
59      */

60     public SortedCollectionContainerPolicy(Class JavaDoc containerClass) {
61         super(containerClass);
62     }
63
64     /**
65      * INTERNAL:
66      * Sets a comparator object for this policy to use when instantiating
67      * a new SortedSet object.
68      */

69     public void setComparator(Comparator JavaDoc comparator) {
70         m_comparator = comparator;
71     }
72
73     /**
74      * INTERNAL:
75      * Return the stored comparator
76      */

77     public Comparator JavaDoc getComparator() {
78         return m_comparator;
79     }
80
81     /**
82      * INTERNAL
83      * Override from ContainerPolicy. Need to maintain the comparator in the
84      * new instance
85      */

86     public Object JavaDoc containerInstance() {
87         try {
88             if (m_comparator != null) {
89                 Object JavaDoc[] arguments = new Object JavaDoc[] { m_comparator };
90                 Class JavaDoc[] constructClass = new Class JavaDoc[] { Comparator JavaDoc.class };
91                 Constructor JavaDoc constructor = null;
92                 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
93                     try {
94                         constructor = (Constructor JavaDoc)AccessController.doPrivileged(new PrivilegedGetConstructorFor(getContainerClass(), constructClass, false));
95                         return AccessController.doPrivileged(new PrivilegedInvokeConstructor(constructor, arguments));
96                     } catch (PrivilegedActionException JavaDoc exception) {
97                         throw QueryException.couldNotInstantiateContainerClass(getContainerClass(), exception.getException());
98                     }
99                 } else {
100                     constructor = PrivilegedAccessHelper.getConstructorFor(getContainerClass(), constructClass, false);
101                     return PrivilegedAccessHelper.invokeConstructor(constructor, arguments);
102                 }
103             } else {
104                 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
105                     try {
106                         return AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(getContainerClass()));
107                     } catch (PrivilegedActionException JavaDoc exception) {
108                         throw QueryException.couldNotInstantiateContainerClass(getContainerClass(), exception.getException());
109                     }
110                 } else {
111                     return PrivilegedAccessHelper.newInstanceFromClass(getContainerClass());
112                 }
113             }
114         } catch (Exception JavaDoc ex) {
115             throw QueryException.couldNotInstantiateContainerClass(getContainerClass(), ex);
116         }
117     }
118 }
119
Popular Tags