Author: tolusha
Date: 2010-06-11 11:05:59 -0400 (Fri, 11 Jun 2010)
New Revision: 2558
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/FilePersistedValueData.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/StreamPersistedValueData.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFileIOChannel.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/PrivilegedFileHelper.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/SpoolFile.java
Log:
EXOJCR-756: small refactoring, using PrivilegedFileHelper
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/FilePersistedValueData.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/FilePersistedValueData.java 2010-06-11
14:42:21 UTC (rev 2557)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/FilePersistedValueData.java 2010-06-11
15:05:59 UTC (rev 2558)
@@ -21,6 +21,7 @@
import org.exoplatform.services.jcr.datamodel.ValueData;
import org.exoplatform.services.jcr.impl.dataflow.AbstractPersistedValueData;
import org.exoplatform.services.jcr.impl.dataflow.TransientValueData;
+import org.exoplatform.services.jcr.impl.util.io.PrivilegedFileHelper;
import java.io.Externalizable;
import java.io.File;
@@ -36,9 +37,6 @@
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
import javax.jcr.RepositoryException;
@@ -89,33 +87,7 @@
*/
public InputStream getAsStream() throws IOException
{
- PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
- {
- public Object run() throws Exception
- {
- return new FileInputStream(file);
- }
- };
- try
- {
- return (InputStream)AccessController.doPrivileged(action);
- }
- catch (PrivilegedActionException pae)
- {
- Throwable cause = pae.getCause();
- if (cause instanceof IOException)
- {
- throw (IOException)cause;
- }
- else if (cause instanceof RuntimeException)
- {
- throw (RuntimeException)cause;
- }
- else
- {
- throw new RuntimeException(cause);
- }
- }
+ return PrivilegedFileHelper.fileInputStream(file);
}
/**
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/StreamPersistedValueData.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/StreamPersistedValueData.java 2010-06-11
14:42:21 UTC (rev 2557)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/persistent/StreamPersistedValueData.java 2010-06-11
15:05:59 UTC (rev 2558)
@@ -19,6 +19,7 @@
package org.exoplatform.services.jcr.impl.dataflow.persistent;
import org.exoplatform.services.jcr.impl.dataflow.TransientValueData;
+import org.exoplatform.services.jcr.impl.util.io.PrivilegedFileHelper;
import org.exoplatform.services.jcr.impl.util.io.SpoolFile;
import org.exoplatform.services.jcr.impl.util.io.SwapFile;
@@ -27,8 +28,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
import javax.jcr.RepositoryException;
@@ -182,43 +181,36 @@
@Override
public long getLength()
{
- PrivilegedAction<Object> action = new PrivilegedAction<Object>()
+ if (file != null)
{
- public Object run()
+ return PrivilegedFileHelper.length(file);
+ }
+ else if (tempFile != null)
+ {
+ return PrivilegedFileHelper.length(tempFile);
+ }
+ else if (stream instanceof FileInputStream)
+ {
+ try
{
- if (file != null)
- {
- return new Long(file.length());
- }
- else if (tempFile != null)
- {
- return new Long(tempFile.length());
- }
- else if (stream instanceof FileInputStream)
- {
- try
- {
- return new Long(((FileInputStream)stream).getChannel().size());
- }
- catch (IOException e)
- {
- return new Long(-1);
- }
- }
- else
- {
- try
- {
- return new Long(stream.available());
- }
- catch (IOException e)
- {
- return new Long(-1);
- }
- }
+ return ((FileInputStream)stream).getChannel().size();
}
- };
- return (Long)AccessController.doPrivileged(action);
+ catch (IOException e)
+ {
+ return -1;
+ }
+ }
+ else
+ {
+ try
+ {
+ return stream.available();
+ }
+ catch (IOException e)
+ {
+ return -1;
+ }
+ }
}
/**
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFileIOChannel.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFileIOChannel.java 2010-06-11
14:42:21 UTC (rev 2557)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFileIOChannel.java 2010-06-11
15:05:59 UTC (rev 2558)
@@ -20,12 +20,10 @@
import org.exoplatform.services.jcr.impl.storage.value.ValueDataResourceHolder;
import org.exoplatform.services.jcr.impl.util.io.FileCleaner;
+import org.exoplatform.services.jcr.impl.util.io.PrivilegedFileHelper;
import java.io.File;
import java.io.IOException;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
import java.util.concurrent.Semaphore;
/**
@@ -55,76 +53,27 @@
@Override
protected File getFile(final String propertyId, final int orderNumber) throws
IOException
{
- PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
- {
- public Object run() throws Exception
- {
- final TreeFile tfile =
- new TreeFile(rootDir.getAbsolutePath() + makeFilePath(propertyId,
orderNumber), cleaner, rootDir);
- mkdirs(tfile.getParentFile()); // make dirs on path
- return tfile;
- }
- };
- try
- {
- return (File)AccessController.doPrivileged(action);
- }
- catch (PrivilegedActionException pae)
- {
- Throwable cause = pae.getCause();
- if (cause instanceof IOException)
- {
- throw (IOException)cause;
- }
- else if (cause instanceof RuntimeException)
- {
- throw (RuntimeException)cause;
- }
- else
- {
- throw new RuntimeException(cause);
- }
- }
+ final TreeFile tfile =
+ new TreeFile(rootDir.getAbsolutePath() + makeFilePath(propertyId, orderNumber),
cleaner, rootDir);
+
+ PrivilegedFileHelper.mkdirs(tfile.getParentFile());
+
+ return tfile;
}
@Override
protected File[] getFiles(final String propertyId) throws IOException
{
- PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ final File dir = PrivilegedFileHelper.file(rootDir.getAbsolutePath() +
buildPath(propertyId));
+ String[] fileNames = PrivilegedFileHelper.list(dir);
+ File[] files = new File[fileNames.length];
+ for (int i = 0; i < fileNames.length; i++)
{
- public Object run() throws Exception
- {
- final File dir = new File(rootDir.getAbsolutePath() +
buildPath(propertyId));
- String[] fileNames = dir.list();
- File[] files = new File[fileNames.length];
- for (int i = 0; i < fileNames.length; i++)
- {
- files[i] = new TreeFile(dir.getAbsolutePath() + File.separator +
fileNames[i], cleaner, rootDir);
- }
- return files;
- }
- };
- try
- {
- return (File[])AccessController.doPrivileged(action);
+ files[i] =
+ new TreeFile(PrivilegedFileHelper.getAbsolutePath(dir) + File.separator +
fileNames[i], cleaner, rootDir);
}
- catch (PrivilegedActionException pae)
- {
- Throwable cause = pae.getCause();
- if (cause instanceof IOException)
- {
- throw (IOException)cause;
- }
- else if (cause instanceof RuntimeException)
- {
- throw (RuntimeException)cause;
- }
- else
- {
- throw new RuntimeException(cause);
- }
- }
+ return files;
}
protected String buildPath(String fileName)
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/PrivilegedFileHelper.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/PrivilegedFileHelper.java 2010-06-11
14:42:21 UTC (rev 2557)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/PrivilegedFileHelper.java 2010-06-11
15:05:59 UTC (rev 2558)
@@ -22,6 +22,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
@@ -98,6 +99,42 @@
}
}
+ public static File createTempFile(final String prefix, final String suffix, final File
directory)
+ throws IllegalArgumentException, IOException
+ {
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ return File.createTempFile(prefix, suffix, directory);
+ }
+ };
+ try
+ {
+ return (File)AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof IllegalArgumentException)
+ {
+ throw (IllegalArgumentException)cause;
+ }
+ else if (cause instanceof IOException)
+ {
+ throw (IOException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+
public static long length(final File file)
{
PrivilegedAction<Object> action = new PrivilegedAction<Object>()
@@ -170,4 +207,28 @@
};
return (Boolean)AccessController.doPrivileged(action);
}
+
+ public static File file(final String pathname)
+ {
+ PrivilegedAction<Object> action = new PrivilegedAction<Object>()
+ {
+ public Object run()
+ {
+ return new File(pathname);
+ }
+ };
+ return (File)AccessController.doPrivileged(action);
+ }
+
+ public static String[] list(final File file)
+ {
+ PrivilegedAction<Object> action = new PrivilegedAction<Object>()
+ {
+ public Object run()
+ {
+ return file.list();
+ }
+ };
+ return (String[])AccessController.doPrivileged(action);
+ }
}
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/SpoolFile.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/SpoolFile.java 2010-06-11
14:42:21 UTC (rev 2557)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/SpoolFile.java 2010-06-11
15:05:59 UTC (rev 2558)
@@ -24,9 +24,6 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
import java.util.Map;
import java.util.WeakHashMap;
@@ -71,33 +68,8 @@
public static SpoolFile createTempFile(final String prefix, final String suffix, final
File directory)
throws IOException
{
- PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
- {
- public Object run() throws Exception
- {
- return new SpoolFile(File.createTempFile(prefix, suffix,
directory).getAbsolutePath());
- }
- };
- try
- {
- return (SpoolFile)AccessController.doPrivileged(action);
- }
- catch (PrivilegedActionException pae)
- {
- Throwable cause = pae.getCause();
- if (cause instanceof IOException)
- {
- throw (IOException)cause;
- }
- else if (cause instanceof RuntimeException)
- {
- throw (RuntimeException)cause;
- }
- else
- {
- throw new RuntimeException(cause);
- }
- }
+ return new
SpoolFile(PrivilegedFileHelper.getAbsolutePath(PrivilegedFileHelper.createTempFile(prefix,
suffix,
+ directory)));
}
public synchronized void acquire(Object holder) throws FileNotFoundException