Author: tolusha
Date: 2010-06-11 04:35:52 -0400 (Fri, 11 Jun 2010)
New Revision: 2547
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFile.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileIOHelper.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/WriteValue.java
Log:
EXOJCR-756: doPrivileged TreeFile, ValueFileIOHelper, WriteValue
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFile.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFile.java 2010-06-11
07:49:26 UTC (rev 2546)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/TreeFile.java 2010-06-11
08:35:52 UTC (rev 2547)
@@ -23,6 +23,8 @@
import org.exoplatform.services.log.Log;
import java.io.File;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
/**
* Created by The eXo Platform SAS
@@ -54,11 +56,18 @@
@Override
public boolean delete()
{
- boolean res = super.delete();
- if (res)
- deleteParent(new File(getParent()));
+ PrivilegedAction<Object> action = new PrivilegedAction<Object>()
+ {
+ public Object run()
+ {
+ boolean res = deleteFromSuper();
+ if (res)
+ deleteParent(new File(getParent()));
- return res;
+ return res;
+ }
+ };
+ return (Boolean)AccessController.doPrivileged(action);
}
protected boolean deleteParent(File fp)
@@ -87,4 +96,9 @@
fLog.warn("Parent can not be a file but found " +
fp.getAbsolutePath());
return res;
}
+
+ private boolean deleteFromSuper()
+ {
+ return super.delete();
+ }
}
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileIOHelper.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileIOHelper.java 2010-06-11
07:49:26 UTC (rev 2546)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileIOHelper.java 2010-06-11
08:35:52 UTC (rev 2547)
@@ -36,6 +36,9 @@
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
/**
* Created by The eXo Platform SAS.
@@ -160,52 +163,81 @@
* @throws IOException
* if error occurs
*/
- protected void writeStreamedValue(File file, ValueData value) throws IOException
+ protected void writeStreamedValue(final File file, final ValueData value) throws
IOException
{
- // stream Value
- if (value instanceof StreamPersistedValueData)
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
{
- StreamPersistedValueData streamed = (StreamPersistedValueData)value;
-
- if (streamed.isPersisted())
+ public Object run() throws Exception
{
- // already persisted in another Value, copy it to this Value
- copyClose(streamed.getAsStream(), new FileOutputStream(file));
- }
- else
- {
- // the Value not yet persisted, i.e. or in client stream or spooled to a temp
file
- File tempFile;
- if ((tempFile = streamed.getTempFile()) != null)
+ // do what you want
+ // stream Value
+ if (value instanceof StreamPersistedValueData)
{
- // it's spooled Value, try move its file to VS
- if (!tempFile.renameTo(file))
+ StreamPersistedValueData streamed = (StreamPersistedValueData)value;
+
+ if (streamed.isPersisted())
{
- // not succeeded - copy bytes, temp file will be deleted by transient
ValueData
- if (LOG.isDebugEnabled())
+ // already persisted in another Value, copy it to this Value
+ copyClose(streamed.getAsStream(), new FileOutputStream(file));
+ }
+ else
+ {
+ // the Value not yet persisted, i.e. or in client stream or spooled to
a temp file
+ File tempFile;
+ if ((tempFile = streamed.getTempFile()) != null)
{
- LOG
- .debug("Value spool file move (rename) to Values Storage is
not succeeded. Trying bytes copy. Spool file: "
- + tempFile.getAbsolutePath() + ". Destination: " +
file.getAbsolutePath());
+ // it's spooled Value, try move its file to VS
+ if (!tempFile.renameTo(file))
+ {
+ // not succeeded - copy bytes, temp file will be deleted by
transient ValueData
+ if (LOG.isDebugEnabled())
+ {
+ LOG
+ .debug("Value spool file move (rename) to Values
Storage is not succeeded. Trying bytes copy. Spool file: "
+ + tempFile.getAbsolutePath() + ". Destination:
" + file.getAbsolutePath());
+ }
+
+ copyClose(new FileInputStream(tempFile), new
FileOutputStream(file));
+ }
}
+ else
+ {
+ // not spooled, use client InputStream
+ copyClose(streamed.getStream(), new FileOutputStream(file));
+ }
- copyClose(new FileInputStream(tempFile), new FileOutputStream(file));
+ // link this Value to file in VS
+ streamed.setPersistedFile(file);
}
}
else
{
- // not spooled, use client InputStream
- copyClose(streamed.getStream(), new FileOutputStream(file));
+ // copy from Value stream to the file, e.g. from FilePersistedValueData to
this Value
+ copyClose(value.getAsStream(), new FileOutputStream(file));
}
- // link this Value to file in VS
- streamed.setPersistedFile(file);
+ return null;
}
+ };
+ try
+ {
+ AccessController.doPrivileged(action);
}
- else
+ catch (PrivilegedActionException pae)
{
- // copy from Value stream to the file, e.g. from FilePersistedValueData to this
Value
- copyClose(value.getAsStream(), new FileOutputStream(file));
+ Throwable cause = pae.getCause();
+ if (cause instanceof IOException)
+ {
+ throw (IOException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
}
}
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/WriteValue.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/WriteValue.java 2010-06-11
07:49:26 UTC (rev 2546)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/WriteValue.java 2010-06-11
08:35:52 UTC (rev 2547)
@@ -24,6 +24,9 @@
import java.io.File;
import java.io.IOException;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
/**
* Created by The eXo Platform SAS.
@@ -97,9 +100,37 @@
if (fileLock != null)
try
{
- // be sure the destination dir exists (case for Tree-style storage)
- file.getParentFile().mkdirs();
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ // be sure the destination dir exists (case for Tree-style storage)
+ file.getParentFile().mkdirs();
+ 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);
+ }
+ }
+
// write value to the file
writeValue(file, value);
}