[exo-jcr-commits] exo-jcr SVN: r448 - in ws/branches/2.2.x/rest/core/src: main/java/org/exoplatform/services/rest/impl/provider and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Oct 30 06:54:42 EDT 2009


Author: aparfonov
Date: 2009-10-30 06:54:42 -0400 (Fri, 30 Oct 2009)
New Revision: 448

Added:
   ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/FileCollector.java
Modified:
   ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java
   ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/DataSourceEntityProvider.java
   ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/FileEntityProvider.java
   ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/MultipartFormDataEntityProvider.java
   ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/BaseTest.java
   ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java
Log:
EXOJCR-185 : 

Added: ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/FileCollector.java
===================================================================
--- ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/FileCollector.java	                        (rev 0)
+++ ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/FileCollector.java	2009-10-30 10:54:42 UTC (rev 448)
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.rest.impl;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:andrey.parfonov at exoplatform.com">Andrey Parfonov</a>
+ * @version $Id$
+ */
+public final class FileCollector
+{
+
+   private static class FileCollectorHolder
+   {
+      private static final FileCollector INSTANCE =
+         new FileCollector(System.getProperty("java.io.tmpdir") + File.separator + "ws_jaxrs");
+   }
+
+   public static FileCollector getInstance()
+   {
+      return FileCollectorHolder.INSTANCE;
+   }
+
+   private final File store;
+
+   private FileCollector(String pathname)
+   {
+      store = new File(pathname);
+      if (!store.exists())
+         store.mkdirs();
+      Runtime.getRuntime().addShutdownHook(new Thread()
+      {
+         public void run()
+         {
+            clean();
+         }
+      });
+   }
+
+   /**
+    * Clean all files in storage.
+    */
+   public void clean()
+   {
+      for (File file : store.listFiles())
+         delete(file);
+   }
+
+   /**
+    * Create file with specified <code>fileName</code> in storage.
+    *  
+    * @param fileName file name
+    * @return newly created file
+    * @throws IOException if any i/o error occurs
+    */
+   public File createFile(String fileName) throws IOException
+   {
+      return new File(store, fileName);
+   }
+
+   /**
+    * Create new file with generated name in storage.
+    *  
+    * @param fileName file name
+    * @return newly created file
+    * @throws IOException if any i/o error occurs
+    */
+   public File createFile() throws IOException
+   {
+      return File.createTempFile("jaxrs", ".tmp", store);
+   }
+   
+   public File getStore()
+   {
+      return store;
+   }
+
+   private void delete(File file)
+   {
+      if (file.isDirectory())
+      {
+         File[] children = file.listFiles();
+         if (children.length > 0)
+         {
+            for (File ch : children)
+               delete(ch);
+         }
+      }
+      file.delete();
+   }
+
+}


Property changes on: ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/FileCollector.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java
===================================================================
--- ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java	2009-10-30 00:11:01 UTC (rev 447)
+++ ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/RequestHandlerImpl.java	2009-10-30 10:54:42 UTC (rev 448)
@@ -18,7 +18,6 @@
  */
 package org.exoplatform.services.rest.impl;
 
-import java.io.File;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.util.HashMap;
@@ -261,41 +260,4 @@
       }
    }
 
-   /**
-    * Startup initialization.
-    */
-   public void init()
-   {
-      // Directory for temporary files
-      final File tmpDir;
-      String tmpDirName = properties.get(WS_RS_TMP_DIR);
-      if (tmpDirName == null)
-      {
-         tmpDir = new File(System.getProperty("java.io.tmpdir") + File.separator + "ws_jaxrs");
-         properties.put(WS_RS_TMP_DIR, tmpDir.getPath());
-      }
-      else
-      {
-         tmpDir = new File(tmpDirName);
-      }
-
-      if (!tmpDir.exists())
-         tmpDir.mkdirs();
-
-      // Register Shutdown Hook for cleaning temporary files.
-      Runtime.getRuntime().addShutdownHook(new Thread()
-      {
-         public void run()
-         {
-            File[] files = tmpDir.listFiles();
-            for (File file : files)
-            {
-               if (file.exists())
-                  file.delete();
-            }
-         }
-      });
-
-   }
-
 }

Modified: ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/DataSourceEntityProvider.java
===================================================================
--- ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/DataSourceEntityProvider.java	2009-10-30 00:11:01 UTC (rev 447)
+++ ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/DataSourceEntityProvider.java	2009-10-30 10:54:42 UTC (rev 448)
@@ -21,6 +21,7 @@
 import org.exoplatform.services.rest.ApplicationContext;
 import org.exoplatform.services.rest.RequestHandler;
 import org.exoplatform.services.rest.impl.ApplicationContextImpl;
+import org.exoplatform.services.rest.impl.FileCollector;
 import org.exoplatform.services.rest.provider.EntityProvider;
 
 import java.io.ByteArrayOutputStream;
@@ -138,7 +139,7 @@
          return new ByteArrayDataSource(bout.toByteArray(), mimeType);
 
       // large data, use file
-      final File file = File.createTempFile("datasource", "tmp");
+      final File file = FileCollector.getInstance().createFile();
       OutputStream fout = new FileOutputStream(file);
 
       // copy data from byte array in file

Modified: ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/FileEntityProvider.java
===================================================================
--- ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/FileEntityProvider.java	2009-10-30 00:11:01 UTC (rev 447)
+++ ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/FileEntityProvider.java	2009-10-30 10:54:42 UTC (rev 448)
@@ -18,6 +18,7 @@
  */
 package org.exoplatform.services.rest.impl.provider;
 
+import org.exoplatform.services.rest.impl.FileCollector;
 import org.exoplatform.services.rest.provider.EntityProvider;
 
 import java.io.File;
@@ -55,7 +56,7 @@
    public File readFrom(Class<File> type, Type genericType, Annotation[] annotations, MediaType mediaType,
       MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException
    {
-      File f = File.createTempFile("ws_rs", "tmp");
+      File f = FileCollector.getInstance().createFile();
       OutputStream out = new FileOutputStream(f);
       try
       {

Modified: ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/MultipartFormDataEntityProvider.java
===================================================================
--- ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/MultipartFormDataEntityProvider.java	2009-10-30 00:11:01 UTC (rev 447)
+++ ws/branches/2.2.x/rest/core/src/main/java/org/exoplatform/services/rest/impl/provider/MultipartFormDataEntityProvider.java	2009-10-30 10:54:42 UTC (rev 448)
@@ -25,6 +25,7 @@
 import org.exoplatform.services.rest.ApplicationContext;
 import org.exoplatform.services.rest.RequestHandler;
 import org.exoplatform.services.rest.impl.ApplicationContextImpl;
+import org.exoplatform.services.rest.impl.FileCollector;
 import org.exoplatform.services.rest.provider.EntityProvider;
 
 import java.io.File;
@@ -100,9 +101,8 @@
             context.getProperties().get(RequestHandler.WS_RS_BUFFER_SIZE) == null
                ? RequestHandler.WS_RS_BUFFER_SIZE_VALUE : Integer.parseInt((String)context.getProperties().get(
                   RequestHandler.WS_RS_BUFFER_SIZE));
-         File repo = new File((String)context.getProperties().get(RequestHandler.WS_RS_TMP_DIR));
-
-         DefaultFileItemFactory factory = new DefaultFileItemFactory(bufferSize, repo);
+         
+         DefaultFileItemFactory factory = new DefaultFileItemFactory(bufferSize, FileCollector.getInstance().getStore());
          FileUpload upload = new FileUpload(factory);
          return upload.parseRequest(httpRequest).iterator();
       }

Modified: ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/BaseTest.java
===================================================================
--- ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/BaseTest.java	2009-10-30 00:11:01 UTC (rev 447)
+++ ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/BaseTest.java	2009-10-30 10:54:42 UTC (rev 448)
@@ -47,7 +47,6 @@
    {
       resources = new ResourceBinderImpl();
       requestHandler = new RequestHandlerImpl(resources, new SimpleDependencySupplier());
-      requestHandler.init();
 
       // reset providers to be sure it is clean
       ProviderBinder.setInstance(new ProviderBinder());

Modified: ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java
===================================================================
--- ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java	2009-10-30 00:11:01 UTC (rev 447)
+++ ws/branches/2.2.x/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java	2009-10-30 10:54:42 UTC (rev 448)
@@ -50,7 +50,6 @@
       depInjector.put(InjectableComponent2.class, new InjectableComponent2());
 
       requestHandler = new RequestHandlerImpl(resources, depInjector);
-      requestHandler.init();
       
       // reset providers to be sure it is clean
       ProviderBinder.setInstance(new ProviderBinder());



More information about the exo-jcr-commits mailing list