KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > JavaPlatform


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.helper;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedActionException JavaDoc;
26
27 import oracle.toplink.essentials.Version;
28 import oracle.toplink.essentials.exceptions.ValidationException;
29 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
30 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass;
31 import oracle.toplink.essentials.internal.security.PrivilegedClassForName;
32
33 /**
34  * INTERNAL:
35  * JavaPlatform abstracts the version of the JDK we are using. It allows any operation
36  * which is dependant on JDK version to be called from a single place and then delegates
37  * the call to its JDKPlatform
38  * @see JDPlatform
39  * @author Tom Ware
40  */

41 public class JavaPlatform {
42     private static JDKPlatform platform = null;
43
44     // 3 possible states are required for conforming like
45
public static final int FALSE = 0;
46     public static final int TRUE = 1;
47     public static final int UNDEFINED = 2;
48
49     /**
50      * INTERNAL:
51      * Get the version of JDK being used from the Version class.
52      * @return JDKPlatform a platform appropriate for the version of JDK being used.
53      */

54     private static JDKPlatform getPlatform() {
55         if (platform == null) {
56             if (Version.isJDK15()) {
57                 try {
58                     Class JavaDoc platformClass = null;
59                     // use class.forName() to avoid loading the JDK 1.5 class unless it is needed.
60
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
61                         try {
62                             platformClass = (Class JavaDoc)AccessController.doPrivileged(new PrivilegedClassForName("oracle.toplink.essentials.internal.helper.JDK15Platform"));
63                         } catch (PrivilegedActionException JavaDoc exception) {
64                         }
65                     } else {
66                         platformClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName("oracle.toplink.essentials.internal.helper.JDK15Platform");
67                     }
68                     if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
69                         try {
70                             platform = (JDKPlatform)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(platformClass));
71                         } catch (PrivilegedActionException JavaDoc exception) {
72                         }
73                     } else {
74                         platform = (JDKPlatform)PrivilegedAccessHelper.newInstanceFromClass(platformClass);
75                     }
76                 } catch (Exception JavaDoc exception) {
77                 }
78             }
79             if (platform == null) {
80                 platform = new JDK15Platform();
81             }
82         }
83         return platform;
84     }
85
86     /**
87      * INTERNAL:
88      * Conform an expression which uses the operator "like" for an in-memory query
89      * @return int
90      * int FALSE = 0 - if the expression does not conform
91      * int TRUE = 1 - if the expression does conform
92      * int UNDEFINED = 2 - if it cannot be determined if the expression conforms
93      */

94     public static int conformLike(Object JavaDoc left, Object JavaDoc right) {
95         return getPlatform().conformLike(left, right);
96     }
97
98     /**
99      * INTERNAL:
100      * Get the milliseconds from a Calendar.
101      * @param calendar the instance of calendar to get the millis from
102      * @return long the number of millis
103      */

104     public static long getTimeInMillis(java.util.Calendar JavaDoc calendar) {
105         return getPlatform().getTimeInMillis(calendar);
106     }
107
108     /**
109      * INTERNAL:
110      * Get the Map to store the query cache in
111      */

112     public static java.util.Map JavaDoc getQueryCacheMap() {
113         return getPlatform().getQueryCacheMap();
114     }
115
116     /**
117      * INTERNAL:
118      * Set the milliseconds for a Calendar.
119      */

120     public static void setTimeInMillis(java.util.Calendar JavaDoc calendar, long millis) {
121         getPlatform().setTimeInMillis(calendar, millis);
122     }
123
124     /**
125      * INTERNAL:
126      * Set the cause of an exception. This is useful for JDK 1.4 exception chaining
127      * @param java.lang.Throwable the exception to set the cause for
128      * @param java.lang.Throwable the cause of this exception
129      */

130     public static void setExceptionCause(Throwable JavaDoc exception, Throwable JavaDoc cause) {
131         getPlatform().setExceptionCause(exception, cause);
132     }
133
134     /**
135      * INTERNAL
136      * return a boolean which determines where TopLink should include the TopLink-stored
137      * Internal exception in it's stack trace. For JDK 1.4 VMs with exception chaining
138      * the Internal exception can be redundant and confusing.
139      * @return boolean
140      */

141     public static boolean shouldPrintInternalException() {
142         return getPlatform().shouldPrintInternalException();
143     }
144 }
145
Popular Tags