author | prr |
Thu, 19 Oct 2017 11:45:23 -0700 | |
changeset 47394 | c28e2522ac81 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
6529 | 1 |
/* |
18564 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
6529 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.sql.rowset; |
|
27 |
||
28 |
import java.security.AccessController; |
|
29 |
import java.security.PrivilegedAction; |
|
30 |
import java.sql.SQLException; |
|
23914 | 31 |
import java.util.PropertyPermission; |
6847
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
32 |
import java.util.ServiceConfigurationError; |
6529 | 33 |
import java.util.ServiceLoader; |
23914 | 34 |
import sun.reflect.misc.ReflectUtil; |
6529 | 35 |
|
36 |
/** |
|
37 |
* A factory API that enables applications to obtain a |
|
38 |
* {@code RowSetFactory} implementation that can be used to create different |
|
39 |
* types of {@code RowSet} implementations. |
|
40 |
* <p> |
|
41 |
* Example: |
|
42 |
* </p> |
|
43 |
* <pre> |
|
44 |
* RowSetFactory aFactory = RowSetProvider.newFactory(); |
|
45 |
* CachedRowSet crs = aFactory.createCachedRowSet(); |
|
46 |
* ... |
|
47 |
* RowSetFactory rsf = RowSetProvider.newFactory("com.sun.rowset.RowSetFactoryImpl", null); |
|
48 |
* WebRowSet wrs = rsf.createWebRowSet(); |
|
49 |
* </pre> |
|
50 |
*<p> |
|
51 |
* Tracing of this class may be enabled by setting the System property |
|
52 |
* {@code javax.sql.rowset.RowSetFactory.debug} to any value but {@code false}. |
|
53 |
* </p> |
|
54 |
* |
|
55 |
* @author Lance Andersen |
|
56 |
* @since 1.7 |
|
57 |
*/ |
|
58 |
public class RowSetProvider { |
|
59 |
||
60 |
private static final String ROWSET_DEBUG_PROPERTY = "javax.sql.rowset.RowSetProvider.debug"; |
|
61 |
private static final String ROWSET_FACTORY_IMPL = "com.sun.rowset.RowSetFactoryImpl"; |
|
62 |
private static final String ROWSET_FACTORY_NAME = "javax.sql.rowset.RowSetFactory"; |
|
63 |
/** |
|
64 |
* Internal debug flag. |
|
65 |
*/ |
|
66 |
private static boolean debug = true; |
|
67 |
||
68 |
||
69 |
static { |
|
70 |
// Check to see if the debug property is set |
|
71 |
String val = getSystemProperty(ROWSET_DEBUG_PROPERTY); |
|
72 |
// Allow simply setting the prop to turn on debug |
|
73 |
debug = val != null && !"false".equals(val); |
|
74 |
} |
|
75 |
||
18564 | 76 |
/** |
77 |
* RowSetProvider constructor |
|
78 |
*/ |
|
6529 | 79 |
protected RowSetProvider () { |
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* <p>Creates a new instance of a <code>RowSetFactory</code> |
|
84 |
* implementation. This method uses the following |
|
85 |
* look up order to determine |
|
86 |
* the <code>RowSetFactory</code> implementation class to load:</p> |
|
87 |
* <ul> |
|
88 |
* <li> |
|
6667
3e631ea81f40
6985725: RowSetProvider has typo for the property javax.sql.rowset.RowSetFactory in the javadoc
lancea
parents:
6529
diff
changeset
|
89 |
* The System property {@code javax.sql.rowset.RowSetFactory}. For example: |
6529 | 90 |
* <ul> |
91 |
* <li> |
|
6667
3e631ea81f40
6985725: RowSetProvider has typo for the property javax.sql.rowset.RowSetFactory in the javadoc
lancea
parents:
6529
diff
changeset
|
92 |
* -Djavax.sql.rowset.RowSetFactory=com.sun.rowset.RowSetFactoryImpl |
6529 | 93 |
* </li> |
94 |
* </ul> |
|
95 |
* <li> |
|
6684
2708e0ba15a8
6987638: javadoc update to RowSetProvider and Statement
lancea
parents:
6667
diff
changeset
|
96 |
* The {@link ServiceLoader} API. The {@code ServiceLoader} API will look |
2708e0ba15a8
6987638: javadoc update to RowSetProvider and Statement
lancea
parents:
6667
diff
changeset
|
97 |
* for a class name in the file |
6529 | 98 |
* {@code META-INF/services/javax.sql.rowset.RowSetFactory} |
25976
4de01a56e3ee
8054555: javadoc cleanup for java.sql and javax.sql
lancea
parents:
23914
diff
changeset
|
99 |
* in jars available to the runtime. For example, to have the RowSetFactory |
6529 | 100 |
* implementation {@code com.sun.rowset.RowSetFactoryImpl } loaded, the |
101 |
* entry in {@code META-INF/services/javax.sql.rowset.RowSetFactory} would be: |
|
102 |
* <ul> |
|
103 |
* <li> |
|
104 |
* {@code com.sun.rowset.RowSetFactoryImpl } |
|
105 |
* </li> |
|
106 |
* </ul> |
|
107 |
* </li> |
|
108 |
* <li> |
|
109 |
* Platform default <code>RowSetFactory</code> instance. |
|
110 |
* </li> |
|
111 |
* </ul> |
|
112 |
* |
|
113 |
* <p>Once an application has obtained a reference to a {@code RowSetFactory}, |
|
114 |
* it can use the factory to obtain RowSet instances.</p> |
|
115 |
* |
|
116 |
* @return New instance of a <code>RowSetFactory</code> |
|
117 |
* |
|
118 |
* @throws SQLException if the default factory class cannot be loaded, |
|
119 |
* instantiated. The cause will be set to actual Exception |
|
120 |
* |
|
121 |
* @see ServiceLoader |
|
122 |
* @since 1.7 |
|
123 |
*/ |
|
124 |
public static RowSetFactory newFactory() |
|
125 |
throws SQLException { |
|
126 |
// Use the system property first |
|
127 |
RowSetFactory factory = null; |
|
128 |
String factoryClassName = null; |
|
129 |
try { |
|
130 |
trace("Checking for Rowset System Property..."); |
|
131 |
factoryClassName = getSystemProperty(ROWSET_FACTORY_NAME); |
|
132 |
if (factoryClassName != null) { |
|
133 |
trace("Found system property, value=" + factoryClassName); |
|
36511 | 134 |
if (factoryClassName.equals(ROWSET_FACTORY_IMPL)) { |
135 |
return defaultRowSetFactory(); |
|
136 |
} |
|
137 |
// getFactoryClass takes care of adding the read edge if |
|
138 |
// necessary |
|
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
139 |
@SuppressWarnings("deprecation") |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
140 |
Object o = getFactoryClass(factoryClassName, null, false).newInstance(); |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
141 |
factory = (RowSetFactory) o; |
6529 | 142 |
} |
36511 | 143 |
} catch (Exception e) { |
23914 | 144 |
throw new SQLException( "RowSetFactory: " + factoryClassName + |
145 |
" could not be instantiated: ", e); |
|
6529 | 146 |
} |
147 |
||
148 |
// Check to see if we found the RowSetFactory via a System property |
|
149 |
if (factory == null) { |
|
150 |
// If the RowSetFactory is not found via a System Property, now |
|
151 |
// look it up via the ServiceLoader API and if not found, use the |
|
152 |
// Java SE default. |
|
153 |
factory = loadViaServiceLoader(); |
|
154 |
} |
|
36511 | 155 |
return factory == null ? defaultRowSetFactory() : factory; |
156 |
} |
|
157 |
||
158 |
private static RowSetFactory defaultRowSetFactory() { |
|
159 |
return new com.sun.rowset.RowSetFactoryImpl(); |
|
6529 | 160 |
} |
161 |
||
162 |
/** |
|
163 |
* <p>Creates a new instance of a <code>RowSetFactory</code> from the |
|
164 |
* specified factory class name. |
|
165 |
* This function is useful when there are multiple providers in the classpath. |
|
166 |
* It gives more control to the application as it can specify which provider |
|
167 |
* should be loaded.</p> |
|
168 |
* |
|
169 |
* <p>Once an application has obtained a reference to a <code>RowSetFactory</code> |
|
170 |
* it can use the factory to obtain RowSet instances.</p> |
|
171 |
* |
|
172 |
* @param factoryClassName fully qualified factory class name that |
|
173 |
* provides an implementation of <code>javax.sql.rowset.RowSetFactory</code>. |
|
174 |
* |
|
175 |
* @param cl <code>ClassLoader</code> used to load the factory |
|
176 |
* class. If <code>null</code> current <code>Thread</code>'s context |
|
177 |
* classLoader is used to load the factory class. |
|
178 |
* |
|
179 |
* @return New instance of a <code>RowSetFactory</code> |
|
180 |
* |
|
181 |
* @throws SQLException if <code>factoryClassName</code> is |
|
182 |
* <code>null</code>, or the factory class cannot be loaded, instantiated. |
|
183 |
* |
|
184 |
* @see #newFactory() |
|
185 |
* |
|
186 |
* @since 1.7 |
|
187 |
*/ |
|
188 |
public static RowSetFactory newFactory(String factoryClassName, ClassLoader cl) |
|
189 |
throws SQLException { |
|
190 |
||
191 |
trace("***In newInstance()"); |
|
23914 | 192 |
|
193 |
if(factoryClassName == null) { |
|
194 |
throw new SQLException("Error: factoryClassName cannot be null"); |
|
195 |
} |
|
196 |
try { |
|
197 |
ReflectUtil.checkPackageAccess(factoryClassName); |
|
198 |
} catch (java.security.AccessControlException e) { |
|
199 |
throw new SQLException("Access Exception",e); |
|
200 |
} |
|
201 |
||
6529 | 202 |
try { |
36511 | 203 |
// getFactoryClass takes care of adding the read edge if |
204 |
// necessary |
|
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
9257
diff
changeset
|
205 |
Class<?> providerClass = getFactoryClass(factoryClassName, cl, false); |
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
36511
diff
changeset
|
206 |
@SuppressWarnings("deprecation") |
6529 | 207 |
RowSetFactory instance = (RowSetFactory) providerClass.newInstance(); |
208 |
if (debug) { |
|
209 |
trace("Created new instance of " + providerClass + |
|
210 |
" using ClassLoader: " + cl); |
|
211 |
} |
|
212 |
return instance; |
|
213 |
} catch (ClassNotFoundException x) { |
|
214 |
throw new SQLException( |
|
215 |
"Provider " + factoryClassName + " not found", x); |
|
216 |
} catch (Exception x) { |
|
217 |
throw new SQLException( |
|
218 |
"Provider " + factoryClassName + " could not be instantiated: " + x, |
|
219 |
x); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
/* |
|
224 |
* Returns the class loader to be used. |
|
225 |
* @return The ClassLoader to use. |
|
226 |
* |
|
227 |
*/ |
|
228 |
static private ClassLoader getContextClassLoader() throws SecurityException { |
|
9257
53e6d7bc31b0
7035615: Address lint warnings for javax.sql.rowset & com.sun.rowset
lancea
parents:
6847
diff
changeset
|
229 |
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { |
6529 | 230 |
|
9257
53e6d7bc31b0
7035615: Address lint warnings for javax.sql.rowset & com.sun.rowset
lancea
parents:
6847
diff
changeset
|
231 |
public ClassLoader run() { |
6529 | 232 |
ClassLoader cl = null; |
233 |
||
234 |
cl = Thread.currentThread().getContextClassLoader(); |
|
235 |
||
236 |
if (cl == null) { |
|
237 |
cl = ClassLoader.getSystemClassLoader(); |
|
238 |
} |
|
239 |
||
240 |
return cl; |
|
241 |
} |
|
242 |
}); |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* Attempt to load a class using the class loader supplied. If that fails |
|
247 |
* and fall back is enabled, the current (i.e. bootstrap) class loader is |
|
248 |
* tried. |
|
249 |
* |
|
250 |
* If the class loader supplied is <code>null</code>, first try using the |
|
251 |
* context class loader followed by the current class loader. |
|
252 |
* @return The class which was loaded |
|
253 |
*/ |
|
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
9257
diff
changeset
|
254 |
static private Class<?> getFactoryClass(String factoryClassName, ClassLoader cl, |
6529 | 255 |
boolean doFallback) throws ClassNotFoundException { |
36511 | 256 |
Class<?> factoryClass = null; |
257 |
||
6529 | 258 |
try { |
259 |
if (cl == null) { |
|
260 |
cl = getContextClassLoader(); |
|
261 |
if (cl == null) { |
|
262 |
throw new ClassNotFoundException(); |
|
263 |
} else { |
|
36511 | 264 |
factoryClass = cl.loadClass(factoryClassName); |
6529 | 265 |
} |
266 |
} else { |
|
36511 | 267 |
factoryClass = cl.loadClass(factoryClassName); |
6529 | 268 |
} |
269 |
} catch (ClassNotFoundException e) { |
|
270 |
if (doFallback) { |
|
271 |
// Use current class loader |
|
36511 | 272 |
factoryClass = Class.forName(factoryClassName, true, RowSetFactory.class.getClassLoader()); |
6529 | 273 |
} else { |
274 |
throw e; |
|
275 |
} |
|
276 |
} |
|
36511 | 277 |
|
278 |
ReflectUtil.checkPackageAccess(factoryClass); |
|
279 |
return factoryClass; |
|
6529 | 280 |
} |
281 |
||
282 |
/** |
|
283 |
* Use the ServiceLoader mechanism to load the default RowSetFactory |
|
284 |
* @return default RowSetFactory Implementation |
|
285 |
*/ |
|
6847
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
286 |
static private RowSetFactory loadViaServiceLoader() throws SQLException { |
6529 | 287 |
RowSetFactory theFactory = null; |
6847
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
288 |
try { |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
289 |
trace("***in loadViaServiceLoader():"); |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
290 |
for (RowSetFactory factory : ServiceLoader.load(javax.sql.rowset.RowSetFactory.class)) { |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
291 |
trace(" Loading done by the java.util.ServiceLoader :" + factory.getClass().getName()); |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
292 |
theFactory = factory; |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
293 |
break; |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
294 |
} |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
295 |
} catch (ServiceConfigurationError e) { |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
296 |
throw new SQLException( |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
297 |
"RowSetFactory: Error locating RowSetFactory using Service " |
b45ff1bbe52f
6988317: RowSetProvider.newFactory() may throw unspecified exception
lancea
parents:
6684
diff
changeset
|
298 |
+ "Loader API: " + e, e); |
6529 | 299 |
} |
300 |
return theFactory; |
|
301 |
||
302 |
} |
|
303 |
||
304 |
/** |
|
305 |
* Returns the requested System Property. If a {@code SecurityException} |
|
306 |
* occurs, just return NULL |
|
6667
3e631ea81f40
6985725: RowSetProvider has typo for the property javax.sql.rowset.RowSetFactory in the javadoc
lancea
parents:
6529
diff
changeset
|
307 |
* @param propName - System property to retrieve |
6529 | 308 |
* @return The System property value or NULL if the property does not exist |
309 |
* or a {@code SecurityException} occurs. |
|
310 |
*/ |
|
311 |
static private String getSystemProperty(final String propName) { |
|
312 |
String property = null; |
|
313 |
try { |
|
9257
53e6d7bc31b0
7035615: Address lint warnings for javax.sql.rowset & com.sun.rowset
lancea
parents:
6847
diff
changeset
|
314 |
property = AccessController.doPrivileged(new PrivilegedAction<String>() { |
6529 | 315 |
|
9257
53e6d7bc31b0
7035615: Address lint warnings for javax.sql.rowset & com.sun.rowset
lancea
parents:
6847
diff
changeset
|
316 |
public String run() { |
6529 | 317 |
return System.getProperty(propName); |
318 |
} |
|
23914 | 319 |
}, null, new PropertyPermission(propName, "read")); |
6529 | 320 |
} catch (SecurityException se) { |
23914 | 321 |
trace("error getting " + propName + ": "+ se); |
6529 | 322 |
if (debug) { |
323 |
se.printStackTrace(); |
|
324 |
} |
|
325 |
} |
|
326 |
return property; |
|
327 |
} |
|
328 |
||
329 |
/** |
|
330 |
* Debug routine which will output tracing if the System Property |
|
331 |
* -Djavax.sql.rowset.RowSetFactory.debug is set |
|
332 |
* @param msg - The debug message to display |
|
333 |
*/ |
|
334 |
private static void trace(String msg) { |
|
335 |
if (debug) { |
|
336 |
System.err.println("###RowSets: " + msg); |
|
337 |
} |
|
338 |
} |
|
339 |
} |