You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2006/12/08 00:45:44 UTC

svn commit: r483738 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/execute/JarUtil.java engine/org/apache/derby/impl/store/raw/data/RFResource.java testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java

Author: djd
Date: Thu Dec  7 15:45:38 2006
New Revision: 483738

URL: http://svn.apache.org/viewvc?view=rev&rev=483738
Log:
DERBY-537 Fix sqlj.replace_jar and sqlj.remove_jar to work under a security manager.
Add a test to test the simple mechanics of the the sqlj functions separated from
the jar files being active on the database class path.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/JarUtil.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RFResource.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/sql/execute/JarUtil.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/JarUtil.java?view=diff&rev=483738&r1=483737&r2=483738
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/JarUtil.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/JarUtil.java Thu Dec  7 15:45:38 2006
@@ -133,7 +133,7 @@
             final String jarExternalName = JarDDL.mkExternalName(schemaName,
                     sqlName, fr.getSeparatorChar());
 
-            long generationId = setJar(jarExternalName, is);
+            long generationId = setJar(jarExternalName, is, true, 0L);
 
             fid = ddg.newFileInfoDescriptor(/*DJD*/null, sd, sqlName, generationId);
             dd.addDescriptor(fid, sd, DataDictionary.SYSFILES_CATALOG_NUM,
@@ -284,10 +284,9 @@
 
 			//
 			//Replace the file.
-			long generationId = 
-				fr.replace(jarExternalName,
-					fid.getGenerationId(), is);
-
+			long generationId = setJar(jarExternalName, is, false,
+					fid.getGenerationId());
+            
 			//
 			//Re-add the descriptor to the data dictionary.
 			FileInfoDescriptor fid2 = 
@@ -359,16 +358,27 @@
      * input stream into the database
      * @param jarExternalName Name of jar with database structure.
      * @param contents Contents of jar file.
+     * @param add true to add, false to replace
+     * @param currentGenerationId generation id of existing version, ignored when adding.
      */
-    private long setJar(final String jarExternalName, final InputStream contents)
+    private long setJar(final String jarExternalName,
+            final InputStream contents,
+            final boolean add,
+            final long currentGenerationId)
             throws StandardException {
         try {
             return ((Long) AccessController
                     .doPrivileged(new java.security.PrivilegedExceptionAction() {
 
                         public Object run() throws StandardException {
-                            long generatedId = fr.add(jarExternalName, contents);
-                            return new Long(generatedId);
+                            long generationId;
+                            
+                            if (add)
+                                generationId = fr.add(jarExternalName, contents);
+                            else
+                                generationId =  fr.replace(jarExternalName,
+                                        currentGenerationId, contents);
+                            return new Long(generationId);
                         }
                     })).longValue();
         } catch (PrivilegedActionException e) {

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RFResource.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RFResource.java?view=diff&rev=483738&r1=483737&r2=483738
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RFResource.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/data/RFResource.java Thu Dec  7 15:45:38 2006
@@ -43,6 +43,10 @@
 import java.io.OutputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 class RFResource implements FileResource {
 
@@ -223,7 +227,7 @@
 } // end of class RFResource
 
 
-class RemoveFile implements Serviceable
+final class RemoveFile implements Serviceable, PrivilegedExceptionAction
 {
 	private final StorageFile fileToGo;
 
@@ -235,15 +239,11 @@
 	public int performWork(ContextManager context)
         throws StandardException
     {
-        // SECURITY PERMISSION - MP1, OP5
-        if (fileToGo.exists())
-        {
-            if (!fileToGo.delete())
-            {
-                throw StandardException.newException(
-                    SQLState.FILE_CANNOT_REMOVE_FILE, fileToGo);
-            }
-        }
+        try {
+            AccessController.doPrivileged(this);
+        } catch (PrivilegedActionException e) {
+            throw (StandardException) (e.getException());
+         }
         return Serviceable.DONE;
 	}
 
@@ -261,5 +261,16 @@
 	public boolean serviceImmediately()
 	{
 		return true;
-	}	
+	}
+
+    public Object run() throws StandardException {
+        // SECURITY PERMISSION - MP1, OP5
+        if (fileToGo.exists()) {
+            if (!fileToGo.delete()) {
+                throw StandardException.newException(
+                        SQLState.FILE_CANNOT_REMOVE_FILE, fileToGo);
+            }
+        }
+        return 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=483738&r1=483737&r2=483738
==============================================================================
--- 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 Dec  7 15:45:38 2006
@@ -60,9 +60,11 @@
         Test test = suite;
         if (JDBC.vmSupportsJDBC3()) {
         
+          suite.addTest(new DatabaseClassLoadingTest("testJarHandling"));
         
           suite.addTest(new DatabaseClassLoadingTest("testWithNoInstalledJars"));
           suite.addTest(new DatabaseClassLoadingTest("testWithNoClasspath"));
+ 
           suite.addTest(
                 SecurityManagerSetup.noSecurityManager(
                         new DatabaseClassLoadingTest("testSetClasspath")));
@@ -173,6 +175,19 @@
     }
     
     /**
+     * Test the sqlj procedures without setting any database
+     * classpath. This allows testing with the security manager
+     * without hitting the bugs that exist when the database class path
+     * is set with the security manager.
+     */
+    public void testJarHandling() throws SQLException, MalformedURLException
+    {       
+        installJar("dcl_emc1.jar", "EMC.MAIL_APP_JHT");
+        replaceJar("dcl_emc2.jar", "EMC.MAIL_APP_JHT");
+        removeJar("EMC.MAIL_APP_JHT");
+    }
+    
+    /**
      * Install the jar, but don't set the classpath.
      * @throws SQLException
      * @throws MalformedURLException 
@@ -714,6 +729,14 @@
         cs.close();
     }
     
+    private void removeJar(String jarName) throws SQLException
+    {
+        CallableStatement cs = prepareCall("CALL SQLJ.REMOVE_JAR(?, 0)");       
+        cs.setString(1, jarName);       
+        cs.executeUpdate();        
+        cs.close();
+    }
+    
     private void setDBClasspath(String cp) throws SQLException
     {
         CallableStatement cs = prepareCall(
@@ -723,6 +746,9 @@
         cs.executeUpdate();
         cs.close();
     }
+    
+    
+    
     
     private void derby2035Workaround() throws SQLException
     {