db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r547305 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/services/reflect/ testing/org/apache/derbyTesting/functionTests/tests/lang/
Date Thu, 14 Jun 2007 16:42:47 GMT
Author: djd
Date: Thu Jun 14 09:42:45 2007
New Revision: 547305

URL: http://svn.apache.org/viewvc?view=rev&rev=547305
Log:
DERBY-2331 (cleanup) Cleanup some code in the loading mechanism for installed jar files,
mainly adding comments. Add some testing of the order of loading classes.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot1.jar
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot2.jar
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot3.jar
  (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/UpdateLoader.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java?view=diff&rev=547305&r1=547304&r2=547305
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/JarLoader.java
Thu Jun 14 09:42:45 2007
@@ -51,7 +51,7 @@
 import org.apache.derby.io.StorageFile;
 
 
-class JarLoader extends SecureClassLoader {
+final class JarLoader extends SecureClassLoader {
     
     /**
      * Two part name for the jar file.
@@ -148,7 +148,7 @@
 	 * @exception ClassNotFoundException
 	 *                Class can not be found
 	 */
-	public Class loadClass(String className, boolean resolve) 
+	protected Class loadClass(String className, boolean resolve) 
 		throws ClassNotFoundException {
 
 		// we attempt the system class load even if we
@@ -497,5 +497,14 @@
                 MessageId.CM_SECURITY_EXCEPTION, className, getJarName(), e
                         .getLocalizedMessage());
         return new SecurityException(msg);
+    }
+    
+    /**
+     * Return the jar name if toString() is called
+     * on this class loader.
+     */
+    public String toString()
+    {
+        return getJarName() + ":" + super.toString();
     }
 }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/UpdateLoader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/UpdateLoader.java?view=diff&rev=547305&r1=547304&r2=547305
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/UpdateLoader.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/reflect/UpdateLoader.java
Thu Jun 14 09:42:45 2007
@@ -47,7 +47,22 @@
 import org.apache.derby.iapi.services.i18n.MessageService;
 import org.apache.derby.iapi.services.locks.CompatibilitySpace;
 
-class UpdateLoader {
+/**
+ * UpdateLoader implements then functionality of
+ * derby.database.classpath. It manages the ClassLoaders
+ * (instances of JarLoader) for each installed jar file.
+ * Jar files are installed through the sqlj.install_jar procedure.
+ * <BR>
+ * Each JarLoader delegates any request through standard mechanisms
+ * to load a class to this object, which will then ask each jarLoader in order of
+ * derby.database.classpath to load the class through an internal api.
+ * This means if the third jar in derby.database.classpath tries to load
+ * a class, say from the class for a procedure's method making some
+ * reference to it, then the request is delegated to UpdateLoader.
+ * UpdateLoader will then try to load the class from each of the jars
+ * in order of derby.database.classpath using the jar's installed JarLoader.
+ */
+final class UpdateLoader {
     
     /**
      * List of packages that Derby will not support being loaded
@@ -129,13 +144,29 @@
 	}
 
 	/**
-		Load the class from the class path.
+		Load the class from the class path. Called by JarLoader
+        when it has a request to load a class to fulfill
+        the sematics of derby.database.classpath.
+        <P>
+        Enforces two restrictions:
+        <UL>
+        <LI> Do not allow classes in certain name spaces to be loaded
+        from installed jars, see RESTRICTED_PACKAGES for the list.
+        <LI> Referencing Derby's internal classes (those outside the
+        public api) from installed is disallowed. This is to stop
+        user defined routines bypassing security or taking advantage
+        of security holes in Derby. E.g. allowing a routine to
+        call a public method in derby would allow such routines
+        to call public static methods for system procedures without
+        having been granted permission on them, such as setting database
+        properties.
+        </UL>
 
-		@exception ClassNotFoundException Class can not be found
+		@exception ClassNotFoundException Class can not be found or
+        the installed jar is restricted from loading it.
 	*/
 	Class loadClass(String className, boolean resolve) 
 		throws ClassNotFoundException {
-
 
 		JarLoader jl = null;
 

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java?view=diff&rev=547305&r1=547304&r2=547305
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
Thu Jun 14 09:42:45 2007
@@ -104,7 +104,6 @@
                 "testLoadJavaClassDirectly2",
                 "testLoadJavaClassDirectly3",
                 "testLoadDerbyClassIndirectly",
- 
             };
             
             for (int i = 0; i < orderedTests.length; i++)
@@ -116,7 +115,14 @@
                    new DatabaseClassLoadingTest("testDatabaseInJar"))); 
 
            suite.addTest(SecurityManagerSetup.noSecurityManager(
-                   new DatabaseClassLoadingTest("testDatabaseInClasspath"))); 
+                   new DatabaseClassLoadingTest("testDatabaseInClasspath")));
+           
+           // No security manager because the test uses getClass().getClassLoader()
+           // in an installed jar to ensure that the class loader for
+           // specific classes is correct. This operation is not allowed in general.
+           suite.addTest(SecurityManagerSetup.noSecurityManager(
+                   new DatabaseClassLoadingTest("testClassLoadOrdering")));
+           
 
            test = new SupportFilesSetup(suite,
                    new String[] {
@@ -127,6 +133,9 @@
                    "functionTests/tests/lang/dcl_emc2sm.jar",
                    "functionTests/tests/lang/dcl_emc2l.jar",
                    "functionTests/tests/lang/dcl_java.jar",
+                   "functionTests/tests/lang/dcl_ot1.jar",
+                   "functionTests/tests/lang/dcl_ot2.jar",
+                   "functionTests/tests/lang/dcl_ot3.jar",
                    });
            
            }
@@ -806,6 +815,98 @@
         }
     }
     
+    /**
+     * Test ordering of class loading.
+     * @throws MalformedURLException 
+     */
+    public void testClassLoadOrdering() throws SQLException, MalformedURLException
+    {
+        Statement s = createStatement();
+        
+        s.executeUpdate("CREATE SCHEMA OT");
+        
+        // Functions to get the class loader of a specific class.
+        // Thre variants that are loaded out of each installed jar
+        // file to ensure that loading is delegated from one jar
+        // to another correctly.
+        // We use the added feature that the toString() of the
+        // ClassLoader for installed jars returns the jar name
+        // first. The RETURNS VARCHAR(10) trims the string to
+        // the correct length for our compare purposes, ie. the
+        // length of "OT"."OT{1,2,3}"
+        s.execute("create function OT.WHICH_LOADER1(classname VARCHAR(256)) " +
+        "RETURNS VARCHAR(10) " +
+        "NO SQL " +
+        "external name " +
+        "'org.apache.derbyTesting.databaseclassloader.ot.OrderTest1.whichLoader' " +
+        "language java parameter style java");
+        
+        s.execute("create function OT.WHICH_LOADER2(classname VARCHAR(256)) " +
+                "RETURNS VARCHAR(10) " +
+                "NO SQL " +
+                "external name " +
+                "'org.apache.derbyTesting.databaseclassloader.ot.OrderTest2.whichLoader'
" +
+                "language java parameter style java");
+
+        s.execute("create function OT.WHICH_LOADER3(classname VARCHAR(256)) " +
+                "RETURNS VARCHAR(10) " +
+                "NO SQL " +
+                "external name " +
+                "'org.apache.derbyTesting.databaseclassloader.ot.OrderTest3.whichLoader'
" +
+                "language java parameter style java");
+
+
+        installJar("dcl_ot1.jar", "OT.OT1");
+        installJar("dcl_ot2.jar", "OT.OT2");
+        installJar("dcl_ot3.jar", "OT.OT3");
+        
+        setDBClasspath("OT.OT1:OT.OT2:OT.OT3");
+        
+        PreparedStatement ps1 = prepareStatement(
+            "VALUES OT.WHICH_LOADER1(?)");
+        PreparedStatement ps2 = prepareStatement(
+            "VALUES OT.WHICH_LOADER2(?)");
+        PreparedStatement ps3 = prepareStatement(
+            "VALUES OT.WHICH_LOADER3(?)");
+        
+        // Tests the classes loaded as a direct entry point for a routine
+        checkCorrectLoader("OrderTest1", ps1, ps2, ps3);
+        checkCorrectLoader("OrderTest2", ps1, ps2, ps3);
+        checkCorrectLoader("OrderTest3", ps1, ps2, ps3);
+        
+        // Tests the classes loaded directly (Class.forName()) by
+        // code in an installed jar file.
+        checkCorrectLoader("OrderLoad1", ps1, ps2, ps3);
+        checkCorrectLoader("OrderLoad2", ps1, ps2, ps3);
+        checkCorrectLoader("OrderLoad3", ps1, ps2, ps3);
+               
+        ps1.close();
+        ps2.close();
+        ps3.close();
+        s.close();
+        
+    }
+    
+    private void checkCorrectLoader(String className,
+            PreparedStatement ps1,
+            PreparedStatement ps2,
+            PreparedStatement ps3)
+       throws SQLException
+    {
+        className = "org.apache.derbyTesting.databaseclassloader.ot." + className;
+        String expectedLoader = 
+            "\"OT\".\"OT" + className.charAt(className.length() -1) + "\"";
+        
+        ps1.setString(1, className);
+        JDBC.assertSingleValueResultSet(ps1.executeQuery(), expectedLoader);
+        
+        ps2.setString(1, className);
+        JDBC.assertSingleValueResultSet(ps2.executeQuery(), expectedLoader);
+
+        ps3.setString(1, className);
+        JDBC.assertSingleValueResultSet(ps3.executeQuery(), expectedLoader);
+    }
+            
   
     private void installJar(String resource, String jarName) throws SQLException, MalformedURLException
     {        

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot1.jar
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot1.jar?view=auto&rev=547305
==============================================================================
Binary file - no diff available.

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot1.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot2.jar
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot2.jar?view=auto&rev=547305
==============================================================================
Binary file - no diff available.

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot2.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot3.jar
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot3.jar?view=auto&rev=547305
==============================================================================
Binary file - no diff available.

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/dcl_ot3.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message