KickJava   Java API By Example, From Geeks To Geeks.

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


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
22
23 package oracle.toplink.essentials.internal.helper;
24
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.BufferedInputStream JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33 import java.util.regex.PatternSyntaxException JavaDoc;
34 import java.security.AccessController JavaDoc;
35 import java.security.PrivilegedAction JavaDoc;
36
37 import oracle.toplink.essentials.logging.SessionLog;
38
39
40 /**
41  * @author Mitesh Meswani
42  * This class is responsible to translate given database name to a DatabasePlatform.
43  */

44 public class DBPlatformHelper {
45     private static final String JavaDoc DEFAULTPLATFORM = "oracle.toplink.essentials.platform.database.DatabasePlatform"; // NOI18N
46

47     private final static String JavaDoc PROPERTY_PATH = "oracle/toplink/essentials/internal/helper/"; // NOI18N
48

49     private final static String JavaDoc VENDOR_NAME_TO_PLATFORM_RESOURCE_NAME =
50         PROPERTY_PATH + "VendorNameToPlatformMapping.properties"; //NOI18N
51

52     /**
53      * Holds mapping between possible vendor names to internal platforms defined above.
54      * vendor names are treated as regular expressions.
55      */

56     private static Properties JavaDoc _nameToVendorPlatform = null;
57
58     /** Get Database Platform from vendor name.
59      * @param vendorName Input vendor name. Typically this is obtained by querying
60      * <code>DatabaseMetaData</code>.
61      * @param logger The logger.
62      * @return Database platform that corresponds to <code>vendorName</code>.
63      * If vendorName does not match any of predefined vendor names, <code>
64      * DEFAULTPLATFORM </code> is returned.
65      */

66     public static String JavaDoc getDBPlatform(String JavaDoc vendorName, SessionLog logger) {
67
68         initializeNameToVendorPlatform(logger);
69
70         String JavaDoc detectedDbPlatform = null;
71         if(vendorName != null) {
72             detectedDbPlatform = matchVendorNameInProperties(vendorName, _nameToVendorPlatform, logger);
73         }
74         if (logger.shouldLog(SessionLog.FINE) ) {
75             logger.log(SessionLog.FINE, "dbPlaformHelper_detectedVendorPlatform", detectedDbPlatform ); // NOI18N
76
}
77         if (detectedDbPlatform == null) {
78             if(logger.shouldLog(SessionLog.INFO)) {
79                 logger.log(SessionLog.INFO, "dbPlaformHelper_defaultingPlatform", vendorName, DEFAULTPLATFORM); // NOI18N
80
}
81             detectedDbPlatform = DEFAULTPLATFORM;
82         }
83         return detectedDbPlatform;
84     }
85
86     /**
87      * Allocate and initialize nameToVendorPlatform if not already done.
88      */

89     private static Properties JavaDoc initializeNameToVendorPlatform(SessionLog logger) {
90         synchronized(DBPlatformHelper.class) {
91             if(_nameToVendorPlatform == null) {
92                 _nameToVendorPlatform = new Properties JavaDoc();
93                 try {
94                     loadFromResource(_nameToVendorPlatform, VENDOR_NAME_TO_PLATFORM_RESOURCE_NAME,
95                                             DBPlatformHelper.class.getClassLoader() );
96                 } catch (IOException JavaDoc e) {
97                     logger.log(SessionLog.WARNING, "dbPlaformHelper_noMappingFound", VENDOR_NAME_TO_PLATFORM_RESOURCE_NAME);
98                 }
99             }
100         }
101         return _nameToVendorPlatform;
102     }
103
104     /**
105      * Match vendorName in properties specifieid by _nameToVendorPlatform.
106      */

107     private static String JavaDoc matchVendorNameInProperties(String JavaDoc vendorName,
108             Properties JavaDoc nameToVendorPlatform, SessionLog logger) {
109         String JavaDoc dbPlatform = null;
110         //Iterate over all properties till we find match.
111
for( Iterator JavaDoc iterator = nameToVendorPlatform.entrySet().iterator();
112                 dbPlatform == null && iterator.hasNext();) {
113             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
114             String JavaDoc regExpr = (String JavaDoc) entry.getKey();
115             String JavaDoc value = (String JavaDoc) entry.getValue();
116             if(logger.shouldLog(SessionLog.FINEST)) {
117                 logger.log(SessionLog.FINEST, "dbPlaformHelper_regExprDbPlatform", regExpr, value); // NOI18N
118
}
119             if( matchPattern(regExpr, vendorName, logger) ) {
120                 dbPlatform = value;
121             }
122         }
123         return dbPlatform;
124     }
125
126    /** Matches target to pattern specified regExp. Returns false if there is
127     * any error compiling regExp.
128     * @param regExp The regular expression.
129     * @param target The target against which we are trying to match regExp.
130     * @param logger
131     * @return false if there is error compiling regExp or target does not
132     * match regExp. true if regExp matches pattern.
133     */

134     private static boolean matchPattern(String JavaDoc regExp, String JavaDoc target, SessionLog logger) {
135         boolean matches = false;
136         try {
137             matches = Pattern.matches(regExp,target);
138         } catch (PatternSyntaxException JavaDoc e){
139             if(logger.shouldLog(SessionLog.FINE)) {
140                 logger.log(SessionLog.FINE, "dbPlaformHelper_patternSyntaxException", e); // NOI18N
141
}
142         }
143         return matches;
144     }
145
146     //-----Property Loading helper methods ----/
147
private static void loadFromResource(Properties JavaDoc properties, String JavaDoc resourceName, ClassLoader JavaDoc classLoader)
148             throws IOException JavaDoc {
149         load(properties, resourceName, classLoader);
150     }
151
152     /**
153      * Loads properties list from the specified resource
154      * into specified Properties object.
155      * @param properties Properties object to load
156      * @param resourceName Name of resource.
157      * If loadFromFile is true, this is fully qualified path name to a file.
158      * param classLoader is ignored.
159      * If loadFromFile is false,this is resource name.
160      * @param classLoader The class loader that should be used to load the resource. If null,primordial
161      * class loader is used.
162      */

163     private static void load(Properties JavaDoc properties, final String JavaDoc resourceName,
164             final ClassLoader JavaDoc classLoader)
165                             throws IOException JavaDoc {
166
167         InputStream JavaDoc bin = new BufferedInputStream JavaDoc(openResourceInputStream(resourceName,classLoader));
168
169         try {
170             properties.load(bin);
171         } finally {
172             try {
173                 bin.close();
174             } catch (Exception JavaDoc e) {
175                 // no action
176
}
177         }
178     }
179
180     /**
181      * Open resourcenName as input stream inside doPriviledged block
182      */

183     private static InputStream JavaDoc openResourceInputStream(final String JavaDoc resourceName, final ClassLoader JavaDoc classLoader) {
184         return (InputStream JavaDoc) AccessController.doPrivileged(
185             new PrivilegedAction JavaDoc() {
186                 public Object JavaDoc run() {
187                     if (classLoader != null) {
188                         return classLoader.getResourceAsStream(resourceName);
189                     } else {
190                         return ClassLoader.getSystemResourceAsStream(resourceName);
191                     }
192                 }
193             }
194         );
195     }
196
197 }
198
Popular Tags