Author: tolusha
Date: 2010-06-15 05:42:31 -0400 (Tue, 15 Jun 2010)
New Revision: 2597
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/PrivilegedSystemHelper.java
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/dataflow/serialization/SerializationConstants.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/SessionFactory.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/value/ValueFactoryImpl.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/serialization/PersistedValueDataReader.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/ValueFileOperation.java
Log:
EXOJCR-756: small refactoring, add PrivilegedSystemHelper
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/dataflow/serialization/SerializationConstants.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/dataflow/serialization/SerializationConstants.java 2010-06-15
09:34:19 UTC (rev 2596)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/dataflow/serialization/SerializationConstants.java 2010-06-15
09:42:31 UTC (rev 2597)
@@ -18,6 +18,8 @@
*/
package org.exoplatform.services.jcr.dataflow.serialization;
+import org.exoplatform.services.jcr.impl.util.io.PrivilegedSystemHelper;
+
import java.io.File;
/**
@@ -33,7 +35,8 @@
/**
* Serialization temp dir.
*/
- public static final String TEMP_DIR = System.getProperty("java.io.tmpdir") +
File.separator + "_jcrser.tmp";
+ public static final String TEMP_DIR =
+ PrivilegedSystemHelper.getProperty("java.io.tmpdir") + File.separator +
"_jcrser.tmp";
/**
* TransientValueData class.
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/SessionFactory.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/SessionFactory.java 2010-06-15
09:34:19 UTC (rev 2596)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/SessionFactory.java 2010-06-15
09:42:31 UTC (rev 2597)
@@ -22,14 +22,12 @@
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.services.jcr.config.WorkspaceEntry;
import
org.exoplatform.services.jcr.impl.dataflow.session.TransactionableResourceManager;
+import org.exoplatform.services.jcr.impl.util.io.PrivilegedSystemHelper;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.security.ConversationState;
import org.exoplatform.services.transaction.TransactionService;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-
import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
@@ -67,47 +65,40 @@
this.tService = tService;
this.txResourceManager = new TransactionableResourceManager();
- PrivilegedAction<Object> action = new PrivilegedAction<Object>()
+ boolean tracking =
+
"true".equalsIgnoreCase(PrivilegedSystemHelper.getProperty("exo.jcr.session.tracking.active",
"false"));
+
+ if (tracking)
{
- public Object run()
+ long maxAgeMillis = 0;
+
+ String maxagevalue =
PrivilegedSystemHelper.getProperty("exo.jcr.jcr.session.tracking.maxage");
+ if (maxagevalue != null)
{
- boolean tracking =
"true".equalsIgnoreCase(System.getProperty("exo.jcr.session.tracking.active",
"false"));
- if (tracking)
+ try
{
- long maxAgeMillis = 0;
-
- String maxagevalue =
System.getProperty("exo.jcr.jcr.session.tracking.maxage");
- if (maxagevalue != null)
- {
- try
- {
- maxAgeMillis = Long.parseLong(maxagevalue) * 1000;
- }
- catch (NumberFormatException e)
- {
- //
- }
- }
- if (maxAgeMillis <= 0)
- {
- maxAgeMillis = 1000 * 60 * 2; // 2 mns
- }
-
+ maxAgeMillis = Long.parseLong(maxagevalue) * 1000;
+ }
+ catch (NumberFormatException e)
+ {
//
- try
- {
- SessionReference.start(maxAgeMillis);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
}
+ }
+ if (maxAgeMillis <= 0)
+ {
+ maxAgeMillis = 1000 * 60 * 2; // 2 mns
+ }
- return null;
+ //
+ try
+ {
+ SessionReference.start(maxAgeMillis);
}
- };
- AccessController.doPrivileged(action);
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
}
/**
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/value/ValueFactoryImpl.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/value/ValueFactoryImpl.java 2010-06-15
09:34:19 UTC (rev 2596)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/value/ValueFactoryImpl.java 2010-06-15
09:42:31 UTC (rev 2597)
@@ -30,6 +30,7 @@
import org.exoplatform.services.jcr.impl.dataflow.TransientValueData;
import org.exoplatform.services.jcr.impl.util.JCRDateFormat;
import org.exoplatform.services.jcr.impl.util.io.FileCleaner;
+import org.exoplatform.services.jcr.impl.util.io.PrivilegedSystemHelper;
import org.exoplatform.services.jcr.impl.util.io.WorkspaceFileCleanerHolder;
import org.exoplatform.services.jcr.storage.WorkspaceDataContainer;
import org.exoplatform.services.log.ExoLogger;
@@ -40,8 +41,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
import java.util.Calendar;
import javax.jcr.Node;
@@ -78,17 +77,8 @@
this.locationFactory = locationFactory;
this.fileCleaner = cleanerHolder.getFileCleaner();
+ this.tempDirectory = new
File(PrivilegedSystemHelper.getProperty("java.io.tmpdir"));
- PrivilegedAction<Object> action = new PrivilegedAction<Object>()
- {
- public Object run()
- {
- tempDirectory = new File(System.getProperty("java.io.tmpdir"));
- return null;
- }
- };
- AccessController.doPrivileged(action);
-
// TODO we use WorkspaceDataContainer constants but is it ok?
this.maxBufferSize =
workspaceConfig.getContainer().getParameterInteger(WorkspaceDataContainer.MAXBUFFERSIZE_PROP,
@@ -99,16 +89,7 @@
{
this.locationFactory = locationFactory;
this.maxBufferSize = WorkspaceDataContainer.DEF_MAXBUFFERSIZE;
-
- PrivilegedAction<Object> action = new PrivilegedAction<Object>()
- {
- public Object run()
- {
- tempDirectory = new File(System.getProperty("java.io.tmpdir"));
- return null;
- }
- };
- AccessController.doPrivileged(action);
+ this.tempDirectory = new
File(PrivilegedSystemHelper.getProperty("java.io.tmpdir"));
}
/**
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/serialization/PersistedValueDataReader.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/serialization/PersistedValueDataReader.java 2010-06-15
09:34:19 UTC (rev 2596)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/dataflow/serialization/PersistedValueDataReader.java 2010-06-15
09:42:31 UTC (rev 2597)
@@ -31,8 +31,6 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
/**
* Created by The eXo Platform SAS. <br/>
@@ -89,14 +87,7 @@
*/
public AbstractPersistedValueData read(ObjectReader in) throws
UnknownClassIdException, IOException
{
- PrivilegedAction<String> action = new PrivilegedAction<String>()
- {
- public String run()
- {
- return SerializationConstants.TEMP_DIR;
- }
- };
- File tempDirectory = new File(AccessController.doPrivileged(action));
+ File tempDirectory = new File(SerializationConstants.TEMP_DIR);
PrivilegedFileHelper.mkdirs(tempDirectory);
// read id
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-15
09:34:19 UTC (rev 2596)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileIOHelper.java 2010-06-15
09:42:31 UTC (rev 2597)
@@ -37,9 +37,6 @@
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.
@@ -78,59 +75,33 @@
*/
protected ValueData readValue(final File file, final int orderNum, final int
maxBufferSize) throws IOException
{
- PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
- {
- public Object run() throws Exception
- {
- long fileSize = PrivilegedFileHelper.length(file);
+ long fileSize = PrivilegedFileHelper.length(file);
- if (fileSize > maxBufferSize)
- {
- return new FilePersistedValueData(orderNum, file);
- }
- else
- {
- FileInputStream is = PrivilegedFileHelper.fileInputStream(file);
- try
- {
- int buffSize = (int)fileSize;
- byte[] res = new byte[buffSize];
- int rpos = 0;
- int r = -1;
- byte[] buff = new byte[IOBUFFER_SIZE > buffSize ? IOBUFFER_SIZE :
buffSize];
- while ((r = is.read(buff)) >= 0)
- {
- System.arraycopy(buff, 0, res, rpos, r);
- rpos += r;
- }
- return new ByteArrayPersistedValueData(orderNum, res);
- }
- finally
- {
- is.close();
- }
- }
- }
- };
- try
+ if (fileSize > maxBufferSize)
{
- return (ValueData)AccessController.doPrivileged(action);
+ return new FilePersistedValueData(orderNum, file);
}
- catch (PrivilegedActionException pae)
+ else
{
- Throwable cause = pae.getCause();
- if (cause instanceof IOException)
+ FileInputStream is = PrivilegedFileHelper.fileInputStream(file);
+ try
{
- throw (IOException)cause;
+ int buffSize = (int)fileSize;
+ byte[] res = new byte[buffSize];
+ int rpos = 0;
+ int r = -1;
+ byte[] buff = new byte[IOBUFFER_SIZE > buffSize ? IOBUFFER_SIZE :
buffSize];
+ while ((r = is.read(buff)) >= 0)
+ {
+ System.arraycopy(buff, 0, res, rpos, r);
+ rpos += r;
+ }
+ return new ByteArrayPersistedValueData(orderNum, res);
}
- else if (cause instanceof RuntimeException)
+ finally
{
- throw (RuntimeException)cause;
+ is.close();
}
- else
- {
- throw new RuntimeException(cause);
- }
}
}
@@ -168,42 +139,14 @@
*/
protected void writeByteArrayValue(final File file, final ValueData value) throws
IOException
{
- PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
- {
- public Object run() throws Exception
- {
- OutputStream out = PrivilegedFileHelper.fileOutputStream(file);
- try
- {
- out.write(value.getAsByteArray());
- }
- finally
- {
- out.close();
- }
-
- return null;
- }
- };
+ OutputStream out = PrivilegedFileHelper.fileOutputStream(file);
try
{
- AccessController.doPrivileged(action);
+ out.write(value.getAsByteArray());
}
- catch (PrivilegedActionException pae)
+ finally
{
- Throwable cause = pae.getCause();
- if (cause instanceof IOException)
- {
- throw (IOException)cause;
- }
- else if (cause instanceof RuntimeException)
- {
- throw (RuntimeException)cause;
- }
- else
- {
- throw new RuntimeException(cause);
- }
+ out.close();
}
}
@@ -219,82 +162,53 @@
*/
protected void writeStreamedValue(final File file, final ValueData value) throws
IOException
{
- PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ // do what you want
+ // stream Value
+ if (value instanceof StreamPersistedValueData)
{
- public Object run() throws Exception
+ StreamPersistedValueData streamed = (StreamPersistedValueData)value;
+
+ if (streamed.isPersisted())
{
- // do what you want
- // stream Value
- if (value instanceof StreamPersistedValueData)
+ // already persisted in another Value, copy it to this Value
+ copyClose(streamed.getAsStream(),
PrivilegedFileHelper.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)
{
- StreamPersistedValueData streamed = (StreamPersistedValueData)value;
-
- if (streamed.isPersisted())
+ // it's spooled Value, try move its file to VS
+ if (!PrivilegedFileHelper.renameTo(tempFile, file))
{
- // already persisted in another Value, copy it to this Value
- copyClose(streamed.getAsStream(),
PrivilegedFileHelper.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)
+ // not succeeded - copy bytes, temp file will be deleted by transient
ValueData
+ if (LOG.isDebugEnabled())
{
- // it's spooled Value, try move its file to VS
- if (!PrivilegedFileHelper.renameTo(tempFile, 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: "
- + PrivilegedFileHelper.getAbsolutePath(tempFile)
- + ". Destination: "
- + PrivilegedFileHelper.getAbsolutePath(file));
- }
-
- copyClose(PrivilegedFileHelper.fileInputStream(tempFile),
PrivilegedFileHelper
- .fileOutputStream(file));
- }
+ LOG
+ .debug("Value spool file move (rename) to Values Storage is
not succeeded. Trying bytes copy. Spool file: "
+ + PrivilegedFileHelper.getAbsolutePath(tempFile)
+ + ". Destination: "
+ + PrivilegedFileHelper.getAbsolutePath(file));
}
- else
- {
- // not spooled, use client InputStream
- copyClose(streamed.getStream(),
PrivilegedFileHelper.fileOutputStream(file));
- }
- // link this Value to file in VS
- streamed.setPersistedFile(file);
+ copyClose(PrivilegedFileHelper.fileInputStream(tempFile),
PrivilegedFileHelper.fileOutputStream(file));
}
}
else
{
- // copy from Value stream to the file, e.g. from FilePersistedValueData to
this Value
- copyClose(value.getAsStream(),
PrivilegedFileHelper.fileOutputStream(file));
+ // not spooled, use client InputStream
+ copyClose(streamed.getStream(),
PrivilegedFileHelper.fileOutputStream(file));
}
- return null;
+ // link this Value to file in VS
+ streamed.setPersistedFile(file);
}
- };
- try
- {
- AccessController.doPrivileged(action);
}
- catch (PrivilegedActionException pae)
+ else
{
- Throwable cause = pae.getCause();
- if (cause instanceof IOException)
- {
- throw (IOException)cause;
- }
- else if (cause instanceof RuntimeException)
- {
- throw (RuntimeException)cause;
- }
- else
- {
- throw new RuntimeException(cause);
- }
+ // copy from Value stream to the file, e.g. from FilePersistedValueData to this
Value
+ copyClose(value.getAsStream(), PrivilegedFileHelper.fileOutputStream(file));
}
}
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-15
09:34:19 UTC (rev 2596)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileOperation.java 2010-06-15
09:42:31 UTC (rev 2597)
@@ -125,40 +125,14 @@
*/
public void lock() throws IOException
{
- 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);
+ // lock file in temp directory
+ lockFile = new File(tempDir, targetFile.getName() + LOCK_FILE_EXTENSION);
- FileOutputStream lout = PrivilegedFileHelper.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 = PrivilegedFileHelper.fileOutputStream(lockFile, true);
+ lout.write(operationInfo.getBytes()); // TODO write info
+ lout.getChannel().lock(); // wait for unlock (on Windows will wait for this JVM
too)
- 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);
- }
- }
+ lockFileStream = lout;
}
/**
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/PrivilegedSystemHelper.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/PrivilegedSystemHelper.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/PrivilegedSystemHelper.java 2010-06-15
09:42:31 UTC (rev 2597)
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2010 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.jcr.impl.util.io;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * @author <a href="anatoliy.bazko(a)exoplatform.org">Anatoliy
Bazko</a>
+ * @version $Id: PrivilegedSystemHelper.java 111 2010-11-11 11:11:11Z tolusha $
+ *
+ */
+public class PrivilegedSystemHelper
+{
+
+ public static String getProperty(final String name)
+ {
+ PrivilegedAction<String> action = new PrivilegedAction<String>()
+ {
+ public String run()
+ {
+ return System.getProperty(name);
+ }
+ };
+ return AccessController.doPrivileged(action);
+ }
+
+ public static String getProperty(final String name, final String def)
+ {
+ PrivilegedAction<String> action = new PrivilegedAction<String>()
+ {
+ public String run()
+ {
+ return System.getProperty(name, def);
+ }
+ };
+ return AccessController.doPrivileged(action);
+ }
+}