You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by rv...@apache.org on 2013/06/10 23:30:57 UTC

svn commit: r1491609 - in /jena/Experimental/jena-jdbc: jena-jdbc-driver-mem/src/main/java/org/apache/jena/jdbc/mem/ jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/ jena-jdbc-driver-tdb/src/main/java/org/apache/jena/jdbc/tdb/

Author: rvesse
Date: Mon Jun 10 21:30:57 2013
New Revision: 1491609

URL: http://svn.apache.org/r1491609
Log:
Add static initializer blocks to register drivers per JDBC spec

Modified:
    jena/Experimental/jena-jdbc/jena-jdbc-driver-mem/src/main/java/org/apache/jena/jdbc/mem/MemDriver.java
    jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java
    jena/Experimental/jena-jdbc/jena-jdbc-driver-tdb/src/main/java/org/apache/jena/jdbc/tdb/TDBDriver.java

Modified: jena/Experimental/jena-jdbc/jena-jdbc-driver-mem/src/main/java/org/apache/jena/jdbc/mem/MemDriver.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-driver-mem/src/main/java/org/apache/jena/jdbc/mem/MemDriver.java?rev=1491609&r1=1491608&r2=1491609&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-driver-mem/src/main/java/org/apache/jena/jdbc/mem/MemDriver.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-driver-mem/src/main/java/org/apache/jena/jdbc/mem/MemDriver.java Mon Jun 10 21:30:57 2013
@@ -37,11 +37,11 @@ import com.hp.hpl.jena.query.DatasetFact
  * A Jena JDBC driver which creates connections to in-memory datasets
  * </p>
  * <h3>
- * Connection URL
- * </h3>
+ * Connection URL</h3>
  * <p>
  * This driver expects a URL of the following form:
  * </p>
+ * 
  * <pre>
  * jdbc:jena:mem:dataset=file.nq
  * </pre>
@@ -53,35 +53,54 @@ import com.hp.hpl.jena.query.DatasetFact
  * {@link #connect(String, Properties)} method instead.
  * </p>
  * <p>
- * If you simply want to start with an empty dataset you may instead set the {@code empty}
- * parameter to be true e.g.
+ * If you simply want to start with an empty dataset you may instead set the
+ * {@code empty} parameter to be true e.g.
  * </p>
+ * 
  * <pre>
  * jdbc:jena:mem:empty=true
  * </pre>
  */
 public class MemDriver extends JenaDriver {
-    
+
     /**
-     * Constant for the memory driver prefix, this is appended to the base {@link JenaDriver#DRIVER_PREFIX} to form the URL prefix for JDBC Connection URLs for this driver
+     * Constant for the memory driver prefix, this is appended to the base
+     * {@link JenaDriver#DRIVER_PREFIX} to form the URL prefix for JDBC
+     * Connection URLs for this driver
      */
     public static final String MEM_DRIVER_PREFIX = "mem:";
-    
+
     /**
-     * Constant for the connection URL parameter used to specify a dataset file/instance to use
+     * Constant for the connection URL parameter used to specify a dataset
+     * file/instance to use
      */
     public static final String PARAM_DATASET = "dataset";
-    
+
     /**
-     * Constant for the connection URL parameter used to specify that an empty dataset should be used.  If {@link #PARAM_DATASET} is present then that parameter has precedence.
+     * Constant for the connection URL parameter used to specify that an empty
+     * dataset should be used. If {@link #PARAM_DATASET} is present then that
+     * parameter has precedence.
      */
     public static final String PARAM_EMPTY = "empty";
-    
+
+    /**
+     * Static initializer block which ensures the driver gets registered
+     */
+    static {
+        try {
+            register();
+        } catch (SQLException e) {
+            throw new RuntimeException("Failed to register Jena In-Memory JDBC Driver", e);
+        }
+    }
+
     /**
      * Registers the driver with the JDBC {@link DriverManager}
-     * @throws SQLException Thrown if the driver cannot be registered
+     * 
+     * @throws SQLException
+     *             Thrown if the driver cannot be registered
      */
-    public static void register() throws SQLException {
+    public static synchronized void register() throws SQLException {
         DriverManager.registerDriver(new MemDriver());
     }
 
@@ -96,27 +115,35 @@ public class MemDriver extends JenaDrive
     protected JenaConnection connect(Properties props, int compatibilityLevel) throws SQLException {
         Object dsObj = props.get(PARAM_DATASET);
         String empty = props.getProperty(PARAM_EMPTY);
-        if (dsObj == null && empty == null) throw new SQLException("Neither one of the " + PARAM_DATASET + " or " + PARAM_EMPTY + " connection parameters is present in the JDBC Connection URL or the provided Properties object");
-        
+        if (dsObj == null && empty == null)
+            throw new SQLException("Neither one of the " + PARAM_DATASET + " or " + PARAM_EMPTY
+                    + " connection parameters is present in the JDBC Connection URL or the provided Properties object");
+
         if (dsObj != null) {
             if (dsObj instanceof Dataset) {
                 // Dataset provided directly
-                return new MemConnection((Dataset)dsObj, JenaConnection.DEFAULT_HOLDABILITY, JenaConnection.DEFAULT_AUTO_COMMIT, JenaConnection.DEFAULT_ISOLATION_LEVEL, compatibilityLevel);
+                return new MemConnection((Dataset) dsObj, JenaConnection.DEFAULT_HOLDABILITY, JenaConnection.DEFAULT_AUTO_COMMIT,
+                        JenaConnection.DEFAULT_ISOLATION_LEVEL, compatibilityLevel);
             } else {
                 // Load dataset from a file
                 try {
                     Dataset ds = DatasetFactory.createMem();
                     RDFDataMgr.read(ds, dsObj.toString());
-                    return new MemConnection(ds, JenaConnection.DEFAULT_HOLDABILITY, JenaConnection.DEFAULT_AUTO_COMMIT, JenaConnection.DEFAULT_ISOLATION_LEVEL, compatibilityLevel);
+                    return new MemConnection(ds, JenaConnection.DEFAULT_HOLDABILITY, JenaConnection.DEFAULT_AUTO_COMMIT,
+                            JenaConnection.DEFAULT_ISOLATION_LEVEL, compatibilityLevel);
                 } catch (Exception e) {
-                    throw new SQLException("Error occurred while reading from the specified RDF dataset file - " + dsObj.toString(), e);
+                    throw new SQLException("Error occurred while reading from the specified RDF dataset file - "
+                            + dsObj.toString(), e);
                 }
             }
         } else if (this.isTrue(props, PARAM_EMPTY)) {
             // Use an empty dataset
-            return new MemConnection(DatasetFactory.createMem(), JenaConnection.DEFAULT_HOLDABILITY, JenaConnection.DEFAULT_AUTO_COMMIT, JenaConnection.DEFAULT_ISOLATION_LEVEL, compatibilityLevel);
+            return new MemConnection(DatasetFactory.createMem(), JenaConnection.DEFAULT_HOLDABILITY,
+                    JenaConnection.DEFAULT_AUTO_COMMIT, JenaConnection.DEFAULT_ISOLATION_LEVEL, compatibilityLevel);
         } else {
-            throw new SQLException("Insufficient parameters to create a Jena JDBC in-memory connection, please supply a Dataset file/instance via the " + PARAM_DATASET + " parameter or supply " + PARAM_EMPTY + "=true to connect to a new empty dataset");
+            throw new SQLException(
+                    "Insufficient parameters to create a Jena JDBC in-memory connection, please supply a Dataset file/instance via the "
+                            + PARAM_DATASET + " parameter or supply " + PARAM_EMPTY + "=true to connect to a new empty dataset");
         }
     }
 
@@ -125,37 +152,39 @@ public class MemDriver extends JenaDrive
         DriverPropertyInfo[] driverProps;
         if (connProps.containsKey(PARAM_DATASET) || !this.isTrue(connProps, PARAM_EMPTY)) {
             driverProps = new DriverPropertyInfo[1 + baseDriverProps.size()];
-            
+
             // Dataset parameter
             driverProps[0] = new DriverPropertyInfo(PARAM_DATASET, connProps.getProperty(PARAM_DATASET));
             driverProps[0].required = true;
             driverProps[0].description = "Sets a path to a file that should be read in to form an in-memory dataset";
-            
-            this.copyBaseProperties(driverProps, baseDriverProps, 1);            
+
+            this.copyBaseProperties(driverProps, baseDriverProps, 1);
         } else if (connProps.containsKey(PARAM_EMPTY)) {
             driverProps = new DriverPropertyInfo[1 + baseDriverProps.size()];
-            
+
             // Empty parameter
             driverProps[0] = new DriverPropertyInfo(PARAM_EMPTY, connProps.getProperty(PARAM_EMPTY));
             driverProps[0].required = true;
             driverProps[0].choices = new String[] { "true", "false" };
-            driverProps[0].description = "Sets that the driver will use an empty in-memory dataset as the initial dataset, when set to true the " + PARAM_DATASET + " parameter is not required";
-            
+            driverProps[0].description = "Sets that the driver will use an empty in-memory dataset as the initial dataset, when set to true the "
+                    + PARAM_DATASET + " parameter is not required";
+
             this.copyBaseProperties(driverProps, baseDriverProps, 1);
         } else {
             driverProps = new DriverPropertyInfo[2 + baseDriverProps.size()];
-            
+
             // Dataset parameter
             driverProps[0] = new DriverPropertyInfo(PARAM_DATASET, connProps.getProperty(PARAM_DATASET));
             driverProps[0].required = true;
             driverProps[0].description = "Sets a path to a file that should be read in to form an in-memory dataset";
-            
+
             // Empty parameter
             driverProps[1] = new DriverPropertyInfo(PARAM_EMPTY, connProps.getProperty(PARAM_EMPTY));
             driverProps[1].required = false;
             driverProps[1].choices = new String[] { "true", "false" };
-            driverProps[1].description = "Sets that the driver will use an empty in-memory dataset as the initial dataset, when set to true the " + PARAM_DATASET + " parameter is not required";
-            
+            driverProps[1].description = "Sets that the driver will use an empty in-memory dataset as the initial dataset, when set to true the "
+                    + PARAM_DATASET + " parameter is not required";
+
             copyBaseProperties(driverProps, baseDriverProps, 2);
         }
         return driverProps;

Modified: jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java?rev=1491609&r1=1491608&r2=1491609&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-driver-remote/src/main/java/org/apache/jena/jdbc/remote/RemoteEndpointDriver.java Mon Jun 10 21:30:57 2013
@@ -145,6 +145,27 @@ public class RemoteEndpointDriver extend
     public static final String PARAM_MODEL_RESULTS_TYPE = "model-results-type";
 
     /**
+     * Static initializer block which ensures the driver gets registered
+     */
+    static {
+        try {
+            register();
+        } catch (SQLException e) {
+            throw new RuntimeException("Failed to register Jena Remote Endpoint JDBC Driver", e);
+        }
+    }
+
+    /**
+     * Registers the driver with the JDBC {@link DriverManager}
+     * 
+     * @throws SQLException
+     *             Thrown if the driver cannot be registered
+     */
+    public static synchronized void register() throws SQLException {
+        DriverManager.registerDriver(new RemoteEndpointDriver());
+    }
+
+    /**
      * Creates a new driver
      */
     public RemoteEndpointDriver() {
@@ -166,16 +187,6 @@ public class RemoteEndpointDriver extend
         super(majorVersion, minorVersion, driverPrefix);
     }
 
-    /**
-     * Registers the driver with the JDBC {@link DriverManager}
-     * 
-     * @throws SQLException
-     *             Thrown if the driver cannot be registered
-     */
-    public static void register() throws SQLException {
-        DriverManager.registerDriver(new RemoteEndpointDriver());
-    }
-
     @Override
     protected JenaConnection connect(Properties props, int compatibilityLevel) throws SQLException {
         String queryEndpoint = props.getProperty(PARAM_QUERY_ENDPOINT);
@@ -199,7 +210,7 @@ public class RemoteEndpointDriver extend
         String password = props.getProperty(PARAM_PASSWORD, null);
         if (password != null && password.trim().length() == 0)
             password = null;
-        
+
         // Result Types
         String selectResultsType = props.getProperty(PARAM_SELECT_RESULTS_TYPE, null);
         String modelResultsType = props.getProperty(PARAM_MODEL_RESULTS_TYPE, null);
@@ -300,7 +311,7 @@ public class RemoteEndpointDriver extend
         driverProps[5] = new DriverPropertyInfo(PARAM_USING_NAMED_GRAPH_URI, null);
         driverProps[5].required = false;
         driverProps[5].description = "Sets the URI for a named graph for updates, may be specified multiple times to specify multiple named graph which should be accessible";
-        
+
         // Results Types
         driverProps[6] = new DriverPropertyInfo(PARAM_SELECT_RESULTS_TYPE, connProps.getProperty(PARAM_SELECT_RESULTS_TYPE));
         driverProps[6].required = false;

Modified: jena/Experimental/jena-jdbc/jena-jdbc-driver-tdb/src/main/java/org/apache/jena/jdbc/tdb/TDBDriver.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-jdbc/jena-jdbc-driver-tdb/src/main/java/org/apache/jena/jdbc/tdb/TDBDriver.java?rev=1491609&r1=1491608&r2=1491609&view=diff
==============================================================================
--- jena/Experimental/jena-jdbc/jena-jdbc-driver-tdb/src/main/java/org/apache/jena/jdbc/tdb/TDBDriver.java (original)
+++ jena/Experimental/jena-jdbc/jena-jdbc-driver-tdb/src/main/java/org/apache/jena/jdbc/tdb/TDBDriver.java Mon Jun 10 21:30:57 2013
@@ -19,6 +19,7 @@
 package org.apache.jena.jdbc.tdb;
 
 import java.io.File;
+import java.sql.DriverManager;
 import java.sql.DriverPropertyInfo;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -89,6 +90,27 @@ public class TDBDriver extends JenaDrive
     public static final String PARAM_MUST_EXIST = "must-exist";
 
     /**
+     * Static initializer block which ensures the driver gets registered
+     */
+    static {
+        try {
+            register();
+        } catch (SQLException e) {
+            throw new RuntimeException("Failed to register Jena TDB JDBC Driver", e);
+        }
+    }
+
+    /**
+     * Registers the driver with the JDBC {@link DriverManager}
+     * 
+     * @throws SQLException
+     *             Thrown if the driver cannot be registered
+     */
+    public static synchronized void register() throws SQLException {
+        DriverManager.registerDriver(new TDBDriver());
+    }
+
+    /**
      * Creates a new TDB driver
      */
     public TDBDriver() {
@@ -146,18 +168,20 @@ public class TDBDriver extends JenaDrive
     protected DriverPropertyInfo[] getPropertyInfo(Properties connProps, List<DriverPropertyInfo> baseDriverProps) {
         DriverPropertyInfo[] driverProps = new DriverPropertyInfo[2 + baseDriverProps.size()];
         this.copyBaseProperties(driverProps, baseDriverProps, 2);
-        
+
         // Location parameter
         driverProps[0] = new DriverPropertyInfo(PARAM_LOCATION, connProps.getProperty(PARAM_LOCATION));
         driverProps[0].required = true;
-        driverProps[0].description = "Sets the location of a TDB dataset, should be a file system path.  The value " + LOCATION_MEM + " may be used for a non-persistent in-memory dataset but this should only be used for testing";
-        
+        driverProps[0].description = "Sets the location of a TDB dataset, should be a file system path.  The value "
+                + LOCATION_MEM + " may be used for a non-persistent in-memory dataset but this should only be used for testing";
+
         // Must Exist parameter
         driverProps[1] = new DriverPropertyInfo(PARAM_MUST_EXIST, connProps.getProperty(PARAM_MUST_EXIST));
         driverProps[1].required = false;
         driverProps[1].choices = new String[] { "true", "false" };
-        driverProps[1].description = "If set to true requests that the driver check whether the " + PARAM_LOCATION + " parameter refers to an existing location before establishing a connection";      
-        
+        driverProps[1].description = "If set to true requests that the driver check whether the " + PARAM_LOCATION
+                + " parameter refers to an existing location before establishing a connection";
+
         return driverProps;
     }