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