db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmars...@apache.org
Subject svn commit: r664124 - in /db/derby/code/trunk/java: drda/org/apache/derby/impl/drda/ engine/org/apache/derby/iapi/util/ testing/org/apache/derbyTesting/functionTests/tests/derbynet/
Date Fri, 06 Jun 2008 21:04:32 GMT
Author: kmarsden
Date: Fri Jun  6 14:04:31 2008
New Revision: 664124

URL: http://svn.apache.org/viewvc?rev=664124&view=rev
Log:
DERBY-3701 java.lang.Exception: DRDA_UnableToAccept.S:Unable to accept connections and client
hang if tracing is turned on but traceDirectory does not exist

- attempt to create directory if it does not exist.
- log an error but do not hang the client if session tracing file cannot be created.



Modified:
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DssTrace.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Session.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/PrivilegedFileOps.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DssTrace.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DssTrace.java?rev=664124&r1=664123&r2=664124&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DssTrace.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DssTrace.java Fri Jun  6 14:04:31
2008
@@ -20,12 +20,15 @@
  */
 package org.apache.derby.impl.drda;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 
+import org.apache.derby.iapi.util.PrivilegedFileOps;
+
 
 // Generic process and error tracing encapsulation.
 // This class also traces a DRDA communications buffer.
@@ -167,14 +170,21 @@
   // Start the communications buffer trace.
   // The name of the file to place the trace is passed to this method.
   // After calling this method, calls to isComBufferTraceOn() will return true.
-  protected void startComBufferTrace (final String fileName) throws IOException 
+  protected void startComBufferTrace (final String fileName) throws Exception 
   {
     synchronized (comBufferSync) {
         // Only start the trace if it is off.
         if (comBufferTraceOn == false) {
-          // The writer will be buffered for effeciency.
             try {
-                
+                // Attempt to make the trace directory if it does not exist.
+                // If we can't create the directory the exception will occur 
+                // when trying to create the trace file.
+                File traceDirectory = new File(fileName).getParentFile();
+                if (!PrivilegedFileOps.exists(traceDirectory))
+                {
+                    PrivilegedFileOps.mkdirs(traceDirectory);
+                }
+                // The writer will be buffered for effeciency.
                 comBufferWriter =  ((PrintWriter)AccessController.doPrivileged(
                             new PrivilegedExceptionAction() {
                                 public Object run() throws SecurityException, IOException
{
@@ -182,11 +192,8 @@
                                 }
                             }));
             } catch (PrivilegedActionException pae) {
-               Exception e = pae.getException();
-               if (e instanceof SecurityException)
-                   throw (SecurityException)pae.getException();
-               else
-                   throw (IOException) pae.getException();
+               throw  pae.getException();
+               
             }
           
           // Turn on the trace flag.

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java?rev=664124&r1=664123&r2=664124&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
(original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
Fri Jun  6 14:04:31 2008
@@ -3502,7 +3502,7 @@
 				    Session session = (Session) e.nextElement();
 					if (on)
 						try {
-							session.setTraceOn(traceDirectory);
+							session.setTraceOn(traceDirectory,true);
 						} catch (Exception te ) {
 							consoleExceptionPrintTrace(te);
 							setTraceSuccessful = false;
@@ -3522,7 +3522,7 @@
 			{	
 				if (on)
 					try {                         
-						session.setTraceOn(traceDirectory);
+						session.setTraceOn(traceDirectory,true);
 					}catch (Exception te) {
 						consoleExceptionPrintTrace(te);
 						setTraceSuccessful = false;
@@ -3838,7 +3838,7 @@
 		// Note that we always re-fetch the tracing configuration because it
 		// may have changed (there are administrative commands which allow
 		// dynamic tracing reconfiguration).
-		Session session = new Session(connectionNumber, clientSocket,
+		Session session = new Session(this,connectionNumber, clientSocket,
 									  getTraceDirectory(), getTraceAll());
 
 		sessionTable.put(new Integer(connectionNumber), session);

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Session.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Session.java?rev=664124&r1=664123&r2=664124&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Session.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Session.java Fri Jun  6 14:04:31
2008
@@ -70,6 +70,9 @@
 										// client
 
 	private	Hashtable	dbtable;		// Table of databases accessed in this session
+	private NetworkServerControlImpl nsctrl;        // NetworkServerControlImpl needed for logging
+                                                        // message if tracing fails.
+                                                        
 
 	// constructor
 	/**
@@ -82,9 +85,10 @@
 	 *
 	 * @exception throws IOException
 	 */
-	Session (int connNum, Socket clientSocket, String traceDirectory,
-			boolean traceOn) throws IOException
+	Session (NetworkServerControlImpl nsctrl, int connNum, Socket clientSocket, String traceDirectory,
+			boolean traceOn) throws Exception
 	{
+                this.nsctrl = nsctrl;
 		this.connNum = connNum;
 		this.clientSocket = clientSocket;
 		this.traceOn = traceOn;
@@ -123,9 +127,11 @@
 	 * initialize a server trace for the DRDA protocol
 	 * 
 	 * @param traceDirectory - directory for trace file
+         * @param throwException - true if we should throw an exception if turning on tracing
fails.
+         *                         We do this for NetworkServerControl API commands.
 	 * @throws IOException 
 	 */
-	protected void initTrace(String traceDirectory) throws IOException 
+	protected void initTrace(String traceDirectory, boolean throwException)  throws Exception
 	{
 		if (traceDirectory != null)
 			traceFileName = traceDirectory + "/" + TRACENAME_PREFIX+
@@ -135,21 +141,31 @@
 		
 		if (dssTrace == null)
 			dssTrace = new DssTrace();
-		dssTrace.startComBufferTrace (traceFileName);
-		traceOn = true;
+                try {
+                    dssTrace.startComBufferTrace (traceFileName);
+                    traceOn = true;
+                } catch (Exception e)
+                {   
+                    if (throwException)
+                        throw e;
+                    // If there is an error starting tracing for the session,
+                    // log to the console and derby.log and do not turn tracing on.
+                    // let connection continue.
+                    nsctrl.consoleExceptionPrintTrace(e);
+                }              
 	}
 
 	/**
 	 * Set tracing on
 	 * 
 	 * @param traceDirectory 	directory for trace files
-	 * @throws IOException 
+	 * @throws Exception 
 	 */
-	protected void setTraceOn(String traceDirectory) throws IOException
+	protected void setTraceOn(String traceDirectory, boolean throwException) throws Exception
 	{
 		if (traceOn)
 			return;
-		initTrace(traceDirectory);    
+		initTrace(traceDirectory, throwException);    
 	}
 
 	/**
@@ -253,12 +269,12 @@
 	 * @param traceDirectory	- directory for trace files
 	 */
 	private void initialize(String traceDirectory)
-		throws IOException
+		throws Exception
 	{
 		sessionInput = clientSocket.getInputStream();
 		sessionOutput = clientSocket.getOutputStream();
 		if (traceOn)
-			initTrace(traceDirectory);
+			initTrace(traceDirectory,false);
 		state = INIT;
 	}
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/PrivilegedFileOps.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/PrivilegedFileOps.java?rev=664124&r1=664123&r2=664124&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/PrivilegedFileOps.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/util/PrivilegedFileOps.java Fri
Jun  6 14:04:31 2008
@@ -121,4 +121,29 @@
             throw (SecurityException)pae.getException();
         }
     }
+    
+    /**
+     * Creates the directory named by this abstract pathname and
+     * parent directories
+     * 
+     * @param file   directory to create
+     * @return  <code> true </true> if directory was created.
+     */
+    public static boolean mkdirs(final File file) {
+     
+        if (file == null) {
+            throw new IllegalArgumentException("file cannot be <null>");
+        }
+        try {
+            return ((Boolean) AccessController.doPrivileged(
+                        new PrivilegedExceptionAction() {
+                            public Object run() throws SecurityException {
+                                return new Boolean(file.mkdirs());
+                            }
+                        })).booleanValue();
+        } catch (PrivilegedActionException pae) {
+            throw (SecurityException)pae.getException();
+        }
+    }
+    
 } // End class PrivilegedFileOps

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java?rev=664124&r1=664123&r2=664124&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/NetworkServerControlApiTest.java
Fri Jun  6 14:04:31 2008
@@ -29,8 +29,10 @@
 import org.apache.derbyTesting.junit.NetworkServerTestSetup;
 import org.apache.derbyTesting.junit.SecurityManagerSetup;
 import org.apache.derbyTesting.junit.SupportFilesSetup;
+import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
 import org.apache.derbyTesting.junit.TestConfiguration;
 
+
 import java.io.File;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
@@ -38,7 +40,10 @@
 import java.security.Policy;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
+import java.sql.Connection;
+import java.sql.SQLException;
 import java.util.Enumeration;
+import java.util.Properties;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
@@ -127,7 +132,41 @@
     
                         
     }
-
+    
+    /**
+     * Test tracing with system properties if we have no permission
+     * to write to the trace directory. Make sure we can still 
+     * get a connection.  Trace directory set to "/" in test setup.
+     * 
+     */
+    public void xtestTraceSystemPropertiesNoPermission() throws SQLException{
+        // our connection should go through fine and there should be an
+        // exception in the derby.log.
+        //access denied (java.io.FilePermission \\ read). I verified 
+        // this manually when creating this fixture but do not know 
+        // how to check in the test.
+        assertEquals(getSystemProperty("derby.drda.traceAll"),"true");
+        assertEquals(getSystemProperty("derby.drda.traceDirectory"),"/");
+        Connection conn = getConnection();
+        assertFalse(conn.getMetaData().isReadOnly());
+    }
+    
+    /**
+     * Test tracing with system properties when we have permissions
+     * to write to the trace directory. 
+     * Check that the tracing file is there.
+     * 
+     */
+    public void xtestTraceSystemPropertiesHasPermission() throws SQLException{
+        String derbysystemhome = getSystemProperty("derby.system.home");
+        assertEquals(getSystemProperty("derby.drda.traceAll"),"true");
+        assertEquals(getSystemProperty("derby.drda.traceDirectory"),derbysystemhome + "/trace");
+        Connection conn = getConnection();
+        assertFalse(conn.getMetaData().isReadOnly());
+        assertTrue(fileExists(derbysystemhome+"/trace/Server1.trace"));
+    }
+    
+    
     /**
      * Test NetworkServerControl ping command.
      * @throws Exception
@@ -253,6 +292,7 @@
              new String[] { TARGET_POLICY_FILE_NAME}
              );
 
+       
         return test;
     }
     
@@ -264,7 +304,29 @@
         // Need derbynet.jar in the classpath!
         if (!Derby.hasServer())
             return suite;
+        suite.addTest(decorateTest());
+        
+        suite = decorateSystemPropertyTests(suite);
+                    
+        return suite;
+    }
+
+    private static TestSuite decorateSystemPropertyTests(TestSuite suite) {
+        Properties traceProps = new Properties();
+        traceProps.put("derby.drda.traceDirectory","/");
+        traceProps.put("derby.drda.traceAll","true");
+        suite.addTest(new SystemPropertyTestSetup(TestConfiguration.clientServerDecorator(
+                new NetworkServerControlApiTest("xtestTraceSystemPropertiesNoPermission")),
+                    traceProps));
+        
+        Properties traceProps2 = new Properties();
+        
+        traceProps2.put("derby.drda.traceDirectory",getSystemProperty("derby.system.home")
+ "/trace");
+        traceProps2.put("derby.drda.traceAll","true");
+        suite.addTest(new SystemPropertyTestSetup(TestConfiguration.clientServerDecorator(
+                new NetworkServerControlApiTest("xtestTraceSystemPropertiesHasPermission")),
+                    traceProps2));
         
-        return decorateTest();
+        return suite;
     }
 }



Mime
View raw message