db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r504317 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/ client/org/apache/derby/jdbc/ shared/org/apache/derby/shared/common/reference/ testing/org/apache/derbyTesting/functionTests/tests/derbynet/
Date Tue, 06 Feb 2007 22:02:51 GMT
Author: mamta
Date: Tue Feb  6 14:02:51 2007
New Revision: 504317

URL: http://svn.apache.org/viewvc?view=rev&rev=504317
Log:
Commiting patch DERBY1275EnableClientTracingDiffV5.txt attached to DERBY-1275. This patch
adds 2 JVM properties to enable client side
tracing. The properties are derby.client.traceLevel and derby.client.traceDirectory
More info can be found at http://wiki.apache.org/db-derby/UndocumentedDerbyBehavior


Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ClientSideSystemPropertiesTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/LogWriter.java
    db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java
    db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/Attribute.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/LogWriter.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/LogWriter.java?view=diff&rev=504317&r1=504316&r2=504317
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/LogWriter.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/LogWriter.java Tue Feb  6 14:02:51
2007
@@ -24,10 +24,12 @@
 import java.sql.SQLException;
 import java.util.Enumeration;
 import java.util.Properties;
+import java.security.AccessController;
 
 import javax.naming.NamingException;
 import javax.naming.RefAddr;
 import javax.naming.Reference;
+
 import org.apache.derby.jdbc.ClientBaseDataSource;
 
 import org.apache.derby.jdbc.ClientDataSource;
@@ -1210,19 +1212,36 @@
         org.apache.derby.client.am.Version.writeDriverConfiguration(printWriter_);
     }
 
-    public static java.io.PrintWriter getPrintWriter(String fileName, boolean fileAppend)
throws SqlException {
+    public static java.io.PrintWriter getPrintWriter(final String fileName, final boolean
fileAppend) throws SqlException {
+    	java.io.PrintWriter printWriter = null;
+    	//Using an anonymous class to deal with the PrintWriter because the  
+    	//method java.security.AccessController.doPrivileged requires an 
+    	//instance of a class(which implements 
+    	//java.security.PrivilegedExceptionAction). Since getPrintWriter method
+    	//is static, we can't simply pass "this" to doPrivileged method and 
+    	//have LogWriter implement PrivilegedExceptionAction.
+    	//To get around the static nature of method getPrintWriter, have an
+    	//anonymous class implement PrivilegedExceptionAction. That class will 
+    	//do the work related to PrintWriter in it's run method and return 
+    	//PrintWriter object.
         try {
-            java.io.PrintWriter printWriter = null;
-            String fileCanonicalPath = new java.io.File(fileName).getCanonicalPath();
-            printWriter =
-                    new java.io.PrintWriter(new java.io.BufferedOutputStream(new java.io.FileOutputStream(fileCanonicalPath,
fileAppend), 4096), true);
-            return printWriter;
-        } catch (java.io.IOException e) {
+    	printWriter = (java.io.PrintWriter)AccessController.doPrivileged(
+    			new java.security.PrivilegedExceptionAction(){
+    				public Object run()throws java.io.IOException{
+    			        String fileCanonicalPath = new java.io.File(fileName).getCanonicalPath();
+    					return new java.io.PrintWriter(
+    							new java.io.BufferedOutputStream(
+    									new java.io.FileOutputStream(
+    											fileCanonicalPath, fileAppend), 4096), true);
+            			}
+            		});
+        } catch (java.security.PrivilegedActionException pae) {
             throw new SqlException(null, 
                 new ClientMessageId(SQLState.UNABLE_TO_OPEN_FILE),
-                new Object[] { fileName, e.getMessage() },
-                e);
+                new Object[] { fileName, pae.getMessage() },
+                pae);
         }
+        return printWriter;
     }
     
     /**

Modified: db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java?view=diff&rev=504317&r1=504316&r2=504317
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java Tue Feb
 6 14:02:51 2007
@@ -23,16 +23,14 @@
 
 import java.io.Serializable;
 import java.io.PrintWriter;
-import java.util.Enumeration;
+import java.security.AccessController;
 import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.NoSuchElementException;
-import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 
-import javax.naming.RefAddr;
 import javax.naming.Referenceable;
 import javax.naming.Reference;
 import javax.naming.NamingException;
@@ -341,8 +339,45 @@
     //
     private String traceDirectory;
     
+    /**
+     * Check if derby.client.traceDirectory is provided as a JVM property. 
+     * If yes, then we use that value. If not, then we look for traceDirectory 
+     * in the the properties parameter.
+     *
+     * @param properties jdbc url properties 
+     * @return value of traceDirectory property
+     */
     public static String getTraceDirectory(Properties properties) {
-        return properties.getProperty(Attribute.CLIENT_TRACE_DIRECTORY);
+    	String traceDirectoryString;
+    	traceDirectoryString  = readSystemProperty(Attribute.CLIENT_JVM_PROPERTY_PREFIX+Attribute.CLIENT_TRACE_DIRECTORY);
+		if (traceDirectoryString == null) 
+			return properties.getProperty(Attribute.CLIENT_TRACE_DIRECTORY);
+		else
+			return traceDirectoryString;
+    }
+    
+    /**
+     * Read the value of the passed system property.
+     * @param key name of the system property
+     * @return value of the system property
+     */
+    private static String readSystemProperty(final String key) {
+    	//Using an anonymous class to read the system privilege because the
+    	//method java.security.AccessController.doPrivileged requires an 
+    	//instance of a class(which implements java.security.PrivilegedAction). 
+    	//Since readSystemProperty method is static, we can't simply pass "this"  
+    	//to doPrivileged method and have ClientBaseDataSource implement 
+    	//PrivilegedAction. To get around the static nature of method 
+    	//readSystemProperty, have an anonymous class implement PrivilegeAction.
+    	//This class will read the system property in it's run method and
+    	//return the value to the caller.
+    	return (String )AccessController.doPrivileged
+    	    (new java.security.PrivilegedAction(){
+    		    public Object run(){
+    			return System.getProperty(key);
+    		    }
+    	    }
+    	    );
     }
 
     // ---------------------------- traceFileAppend -----------------------------------
@@ -839,9 +874,20 @@
     
     protected int traceLevel = propertyDefault_traceLevel;
 
+    /**
+     * Check if derby.client.traceLevel is provided as a JVM property. 
+     * If yes, then we use that value. If not, then we look for traceLevel 
+     * in the the properties parameter.
+     *
+     * @param properties jdbc url properties 
+     * @return value of traceLevel property
+     */
     public static int getTraceLevel(Properties properties) {
-        String traceLevelString = properties.getProperty(Attribute.CLIENT_TRACE_LEVEL);
-        return parseInt(traceLevelString, propertyDefault_traceLevel);
+    	String traceLevelString;
+    	traceLevelString  = readSystemProperty(Attribute.CLIENT_JVM_PROPERTY_PREFIX+Attribute.CLIENT_TRACE_LEVEL);
+		if (traceLevelString == null) 
+			traceLevelString = properties.getProperty(Attribute.CLIENT_TRACE_LEVEL);
+		return parseInt(traceLevelString, propertyDefault_traceLevel);
     }
 
     synchronized public void setTraceLevel(int traceLevel) {
@@ -851,7 +897,6 @@
     public int getTraceLevel() {
         return this.traceLevel;
     }
-
 
     public synchronized void setTraceFile(String traceFile) {
         this.traceFile = traceFile;

Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/Attribute.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/Attribute.java?view=diff&rev=504317&r1=504316&r2=504317
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/Attribute.java
(original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/Attribute.java
Tue Feb  6 14:02:51 2007
@@ -68,6 +68,16 @@
     String JCC_PROTOCOL = "jdbc:derby:net:";
 
     /**
+     * User should use this prefix for the client attributes traceLevel 
+     * and traceDirectory when they are sending those attributes as JVM 
+     * properties. 
+     * These 2 attributes can be sent through jdbc url directly (supported
+     * way) or as JVM properties with the following prefix (undocumented 
+     * way). DERBY-1275
+     */
+    String CLIENT_JVM_PROPERTY_PREFIX = "derby.client.";
+
+    /**
      * Attribute name to encrypt the database on disk. If set to true, all user
      * data is stored encrypted on disk.
      */

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ClientSideSystemPropertiesTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ClientSideSystemPropertiesTest.java?view=auto&rev=504317
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ClientSideSystemPropertiesTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ClientSideSystemPropertiesTest.java
Tue Feb  6 14:02:51 2007
@@ -0,0 +1,138 @@
+/*
+ 
+   Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.ClientSideSystemProperties
+ 
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+ 
+      http://www.apache.org/licenses/LICENSE-2.0
+ 
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+ 
+ */
+
+package org.apache.derbyTesting.functionTests.tests.derbynet;
+
+import java.io.File;
+import java.security.AccessController;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+/** The test of the jvm properties for enabling client side tracing(DERBY-1275)
+  */
+public class ClientSideSystemPropertiesTest extends BaseJDBCTestCase { 
+	/** Establish a connection and that should start client side tracing
+     *  because we have set the system properties to enable client side
+     *  tracing. */
+    public void testConnection() throws Exception {
+        getConnection().setAutoCommit(false);
+        //Make sure the connection above created a trace file. This check is 
+        //made in the privilege block below by looking inside the 
+        //trace Directory and making sure the file count is greater than 0.
+        AccessController.doPrivileged
+		    (new java.security.PrivilegedAction(){
+		    	public Object run(){
+		    		File dir = new File(getSystemProperty("derby.client.traceDirectory"));
+		    		int fileCounter = 0;
+    	            File[] list = dir.listFiles();
+    	            File tempFile;
+    	            for (;fileCounter<list.length; fileCounter++)
+    	            	tempFile = list[fileCounter];
+    	            junit.framework.Assert.assertTrue(fileCounter>0);
+    	            return null;
+    		    }
+    		}	 
+    	    );
+    }
+    
+    /** If the trace Directory doesn't exist then create one. If there is one
+     *  already there, then delete everything under it. */
+    protected void setUp() throws Exception
+    {
+    	AccessController.doPrivileged(
+    			new java.security.PrivilegedAction(){
+    				public Object run(){
+    					File dir = new File(getSystemProperty("derby.client.traceDirectory"));
+    					if (dir.exists() == false) //create the trace Directory
+    						junit.framework.Assert.assertTrue(dir.mkdirs());
+    					else {//cleanup the trace Directory which already exists
+    						int fileCounter = 0;
+    						File[] list = dir.listFiles();
+    						File tempFile;
+    						for (;fileCounter<list.length; fileCounter++) {
+    							tempFile = list[fileCounter];
+    							tempFile.delete();
+        					}
+		        }
+	            return null;
+		    }
+		}	 
+	    );
+    }
+    
+    /** Delete the trace Directory so that the test environment is clean for the
+     *  next test run. */
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        //Making ClientSideSystemPropertiesTest class implement 
+        //java.security.PrivilegedExceptionAction didn't work because compiler
+        //kept getting confused between the run method in
+        //java.security.PrivilegedExceptionAction and the run method in
+        //junit.framework.TestCase
+        //To get around this, I have created an inline class which implements
+        //java.security.PrivilegedAction and implements the run method 
+        //to delete the traceDirector and all the files under it.
+    	AccessController.doPrivileged
+	    (new java.security.PrivilegedAction(){
+		    public Object run(){
+		        File dir = new File(getSystemProperty("derby.client.traceDirectory"));
+	    		int fileCounter = 0;
+	            File[] list = dir.listFiles();
+	            File tempFile;
+	            //delete all the files under trace Directory
+	            for (;fileCounter<list.length; fileCounter++)
+	            {
+	            	tempFile = list[fileCounter];
+	            	tempFile.delete();
+	            }
+	            //now delete the trace Directory
+				dir.delete();
+	            return null;
+		    }
+		}	 
+	    );
+    }
+    
+    /* ------------------- end helper methods  -------------------------- */
+    public ClientSideSystemPropertiesTest(String name) {
+        super(name);
+    }
+
+    /*
+     * Set the system properties related to client side tracing.
+     */
+    public static Test suite() {
+        //Create the traceDirectory required by the tests in this class
+    	Properties traceRelatedProperties = new Properties();
+        traceRelatedProperties.setProperty("derby.client.traceLevel", "64");
+        traceRelatedProperties.setProperty("derby.client.traceDirectory", "TraceDir");
+        Test suite = TestConfiguration.clientServerSuite(ClientSideSystemPropertiesTest.class);
+        return new SystemPropertyTestSetup(suite, traceRelatedProperties); 
+    }
+    
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ClientSideSystemPropertiesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java?view=diff&rev=504317&r1=504316&r2=504317
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
Tue Feb  6 14:02:51 2007
@@ -49,6 +49,7 @@
         suite.addTest(ShutDownDBWhenNSShutsDownTest.suite());
         suite.addTest(SuicideOfStreamingTest.suite());
         suite.addTest(DRDAProtocolTest.suite());
+        suite.addTest(ClientSideSystemPropertiesTest.suite());
         
         // These tests references a client class directly
         // thus causing class not found exceptions if the



Mime
View raw message