manifoldcf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1459562 - in /manifoldcf/branches/CONNECTORS-664: connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LLSERVER.java framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCF.java
Date Thu, 21 Mar 2013 22:35:24 GMT
Author: kwright
Date: Thu Mar 21 22:35:23 2013
New Revision: 1459562

URL: http://svn.apache.org/r1459562
Log:
Better handling of temp directories etc.

Modified:
    manifoldcf/branches/CONNECTORS-664/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LLSERVER.java
    manifoldcf/branches/CONNECTORS-664/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCF.java

Modified: manifoldcf/branches/CONNECTORS-664/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LLSERVER.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-664/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LLSERVER.java?rev=1459562&r1=1459561&r2=1459562&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-664/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LLSERVER.java
(original)
+++ manifoldcf/branches/CONNECTORS-664/connectors/livelink/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/livelink/LLSERVER.java
Thu Mar 21 22:35:23 2013
@@ -130,35 +130,11 @@ public class LLSERVER
             {
               java.security.cert.Certificate cert = keystore.getCertificate(alias);
               byte[] certData = cert.getEncoded();
-              File fileName = new File(certFolder,alias + ".cer");
+              File fileName = new File(certFolder,ManifoldCF.safeFileName(alias) + ".cer");
               FileOutputStream fos = new FileOutputStream(fileName);
               try
               {
-                OutputStreamWriter osw = new OutputStreamWriter(fos,"ASCII");
-                try
-                {
-                  osw.write("-----BEGIN CERTIFICATE-----\n");
-                  String certBase64 = new Base64().encodeByteArray(certData);
-                  int offset = 0;
-                  while (offset < certBase64.length())
-                  {
-                    int remainder = certBase64.length() - offset;
-                    if (remainder < 64)
-                    {
-                      osw.write(certBase64,offset,remainder);
-                      osw.write("\n");
-                      break;
-                    }
-                    osw.write(certBase64,offset,64);
-                    offset += 64;
-                    osw.write("\n");
-                  }
-                  osw.write("-----END CERTIFICATE-----\n");
-                }
-                finally
-                {
-                  osw.flush();
-                }
+                fos.write(certData);
               }
               finally
               {
@@ -217,33 +193,8 @@ public class LLSERVER
   protected void createCertFolder()
     throws ManifoldCFException
   {
-    String tempDirLocation = System.getProperty("java.io.tmpdir");
-    if (tempDirLocation == null)
-      throw new ManifoldCFException("Can't find temporary directory!");
-    File tempDir = new File(tempDirLocation);
-    // Start with current timestamp, and generate a hash, then look for collision
-    long currentFileID = System.currentTimeMillis();
-    long currentFileHash = (currentFileID << 5) ^ (currentFileID >> 3);
-    int raceConditionRepeat = 0;
-    while (raceConditionRepeat < 1000)
-    {
-      File tempCertDir = new File(tempDir,"llcrt_"+currentFileID+".d");
-      if (tempCertDir.mkdir())
-      {
-        certFolder = tempCertDir;
-        return;
-      }
-      if (tempCertDir.exists())
-      {
-        currentFileID++;
-        continue;
-      }
-      // Doesn't exist but couldn't create either.  COULD be a race condition; we'll only
know if we retry
-      // lots and nothing changes.
-      raceConditionRepeat++;
-      Thread.yield();
-    }
-    throw new ManifoldCFException("Temporary directory appears to be unwritable");
+    certFolder = ManifoldCF.createTempDir("llcrt_",".d");
+    ManifoldCF.addFile(certFolder);
   }
   
   /** Release temporary session-bound cert directory.
@@ -252,32 +203,11 @@ public class LLSERVER
   {
     if (certFolder != null)
     {
-      recursiveDelete(certFolder);
+      ManifoldCF.deleteFile(certFolder);
       certFolder = null;
     }
   }
 
-  /** Recursive delete: for cleaning up company folder.
-  *@param directoryPath is the File describing the directory to be removed.
-  */
-  protected static void recursiveDelete(File directoryPath)
-  {
-    File[] children = directoryPath.listFiles();
-    if (children != null)
-    {
-      int i = 0;
-      while (i < children.length)
-      {
-        File x = children[i++];
-        if (x.isDirectory())
-          recursiveDelete(x);
-        else
-          x.delete();
-      }
-    }
-    directoryPath.delete();
-  }
-
   /**
   * Returns the server name where the Livelink
   * Server has been installed on

Modified: manifoldcf/branches/CONNECTORS-664/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCF.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-664/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCF.java?rev=1459562&r1=1459561&r2=1459562&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-664/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCF.java
(original)
+++ manifoldcf/branches/CONNECTORS-664/framework/core/src/main/java/org/apache/manifoldcf/core/system/ManifoldCF.java
Thu Mar 21 22:35:23 2013
@@ -397,21 +397,23 @@ public class ManifoldCF
   }
 
   /** Recursive delete: for cleaning up company folder.
-  *@param directoryPath is the File describing the directory to be removed.
+  *@param directoryPath is the File describing the directory or file to be removed.
   */
-  protected static void recursiveDelete(File directoryPath)
+  public static void recursiveDelete(File directoryPath)
   {
-    File[] children = directoryPath.listFiles();
-    if (children != null)
+    if (!directoryPath.exists())
+      return;
+    if (directoryPath.isDirectory())
     {
-      int i = 0;
-      while (i < children.length)
+      File[] children = directoryPath.listFiles();
+      if (children != null)
       {
-        File x = children[i++];
-        if (x.isDirectory())
+        int i = 0;
+        while (i < children.length)
+        {
+          File x = children[i++];
           recursiveDelete(x);
-        else
-          x.delete();
+        }
       }
     }
     directoryPath.delete();
@@ -718,6 +720,39 @@ public class ManifoldCF
     master.dropUserAndDatabase(masterUsername,masterPassword,null);
   }
 
+  /** Create temporary directory.
+  */
+  public static File createTempDir(String prefix, String suffix)
+    throws ManifoldCFException
+  {
+    String tempDirLocation = System.getProperty("java.io.tmpdir");
+    if (tempDirLocation == null)
+      throw new ManifoldCFException("Can't find temporary directory!");
+    File tempDir = new File(tempDirLocation);
+    // Start with current timestamp, and generate a hash, then look for collision
+    long currentFileID = System.currentTimeMillis();
+    long currentFileHash = (currentFileID << 5) ^ (currentFileID >> 3);
+    int raceConditionRepeat = 0;
+    while (raceConditionRepeat < 1000)
+    {
+      File tempCertDir = new File(tempDir,prefix+currentFileHash+suffix);
+      if (tempCertDir.mkdir())
+      {
+        return tempCertDir;
+      }
+      if (tempCertDir.exists())
+      {
+        currentFileHash++;
+        continue;
+      }
+      // Doesn't exist but couldn't create either.  COULD be a race condition; we'll only
know if we retry
+      // lots and nothing changes.
+      raceConditionRepeat++;
+      Thread.yield();
+    }
+    throw new ManifoldCFException("Temporary directory appears to be unwritable");
+  }
+
   /** Add a file to the tracking system. */
   public static void addFile(File f)
   {
@@ -1194,8 +1229,8 @@ public class ManifoldCF
   /** Class that tracks files that need to be cleaned up on exit */
   protected static class FileTrack implements IShutdownHook
   {
-    /** Key and value are both File objects */
-    protected Map<File,File> filesToDelete = new HashMap<File,File>();
+    /** Set of File objects */
+    protected Set<File> filesToDelete = new HashSet<File>();
 
     /** Constructor */
     public FileTrack()
@@ -1207,7 +1242,7 @@ public class ManifoldCF
     {
       synchronized (this)
       {
-        filesToDelete.put(f,f);
+        filesToDelete.add(f);
       }
     }
 
@@ -1216,12 +1251,12 @@ public class ManifoldCF
     {
       // Because we never reuse file names, it is OK to delete twice.
       // So the delete() can be outside the synchronizer.
-      f.delete();
+      recursiveDelete(f);
       synchronized (this)
       {
         filesToDelete.remove(f);
       }
-  }
+    }
 
     /** Delete all remaining files */
     public void doCleanup()
@@ -1229,10 +1264,10 @@ public class ManifoldCF
     {
       synchronized (this)
       {
-	Iterator iter = filesToDelete.keySet().iterator();
+	Iterator<File> iter = filesToDelete.iterator();
 	while (iter.hasNext())
 	{
-	  File f = (File)iter.next();
+	  File f = iter.next();
 	  f.delete();
 	}
 	filesToDelete.clear();



Mime
View raw message