Author: tolusha
Date: 2010-06-11 03:49:26 -0400 (Fri, 11 Jun 2010)
New Revision: 2546
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/operations/ValueFileOperation.java
Log:
EXOJCR-756: doPrivileged TreeFileIOChannel & ValueFileOperation
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
07:38:35 UTC (rev 2545)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFileIOChannel.java 2010-06-11
07:49:26 UTC (rev 2546)
@@ -77,6 +77,10 @@
{
throw (IOException)cause;
}
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
else
{
throw new RuntimeException(cause);
@@ -87,14 +91,40 @@
@Override
protected File[] getFiles(final String propertyId) throws IOException
{
- 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++)
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
{
- files[i] = new TreeFile(dir.getAbsolutePath() + File.separator + fileNames[i],
cleaner, rootDir);
+ 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);
}
- return files;
+ 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);
+ }
+ }
}
protected String buildPath(String fileName)
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileOperation.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileOperation.java 2010-06-11
07:38:35 UTC (rev 2545)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileOperation.java 2010-06-11
07:49:26 UTC (rev 2546)
@@ -30,6 +30,9 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
/**
* Created by The eXo Platform SAS.
@@ -121,14 +124,40 @@
*/
public void lock() throws IOException
{
- // lock file in temp directory
- lockFile = new File(tempDir, targetFile.getName() + LOCK_FILE_EXTENSION);
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ // lock file in temp directory
+ lockFile = new File(tempDir, targetFile.getName() + LOCK_FILE_EXTENSION);
- FileOutputStream lout = new FileOutputStream(lockFile, true);
- lout.write(operationInfo.getBytes()); // TODO write info
- lout.getChannel().lock(); // wait for unlock (on Windows will wait for this JVM
too)
+ FileOutputStream lout = new FileOutputStream(lockFile, true);
+ lout.write(operationInfo.getBytes()); // TODO write info
+ lout.getChannel().lock(); // wait for unlock (on Windows will wait for
this JVM too)
- lockFileStream = lout;
+ return lout;
+ }
+ };
+ try
+ {
+ lockFileStream = (FileOutputStream)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);
+ }
+ }
}
/**
@@ -151,14 +180,43 @@
*/
public void unlock() throws IOException
{
- if (lockFileStream != null)
- lockFileStream.close();
- if (!lockFile.delete())
- { // TODO don't use FileCleaner, delete should be enough
- LOG.warn("Cannot delete lock file " + lockFile.getAbsolutePath() +
". Add to the FileCleaner");
- cleaner.addFile(lockFile);
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ if (lockFileStream != null)
+ lockFileStream.close();
+
+ if (!lockFile.delete())
+ { // TODO don't use FileCleaner, delete should be enough
+ LOG.warn("Cannot delete lock file " +
lockFile.getAbsolutePath() + ". Add to the FileCleaner");
+ cleaner.addFile(lockFile);
+ }
+
+ return null;
+ }
+ };
+ try
+ {
+ 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);
+ }
+ }
}
}