Author: nzamosenchuk
Date: 2010-06-11 09:41:42 -0400 (Fri, 11 Jun 2010)
New Revision: 2556
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/SecurityHelper.java
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/RedoLog.java
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/directory/FSDirectoryManager.java
Log:
EXOJCR-758: Implemented doPriviledged in FSDirectoryManager and RedoLog.
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/RedoLog.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/RedoLog.java 2010-06-11
13:06:51 UTC (rev 2555)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/RedoLog.java 2010-06-11
13:41:42 UTC (rev 2556)
@@ -19,6 +19,7 @@
import org.apache.lucene.store.Directory;
import org.exoplatform.services.jcr.impl.core.query.lucene.directory.IndexInputStream;
import org.exoplatform.services.jcr.impl.core.query.lucene.directory.IndexOutputStream;
+import org.exoplatform.services.jcr.impl.util.SecurityHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -30,6 +31,7 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
@@ -143,11 +145,18 @@
* @throws IOException if the node cannot be written to the redo
* log.
*/
- void append(MultiIndex.Action action) throws IOException
+ void append(final MultiIndex.Action action) throws IOException
{
- initOut();
- out.write(action.toString() + "\n");
- entryCount++;
+ SecurityHelper.doPriviledgedIOExceptionAction(new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ initOut();
+ out.write(action.toString() + "\n");
+ entryCount++;
+ return null;
+ }
+ });
}
/**
@@ -156,10 +165,17 @@
*/
void flush() throws IOException
{
- if (out != null)
+ SecurityHelper.doPriviledgedIOExceptionAction(new
PrivilegedExceptionAction<Object>()
{
- out.flush();
- }
+ public Object run() throws Exception
+ {
+ if (out != null)
+ {
+ out.flush();
+ }
+ return null;
+ }
+ });
}
/**
@@ -168,13 +184,20 @@
*/
void clear() throws IOException
{
- if (out != null)
+ SecurityHelper.doPriviledgedIOExceptionAction(new
PrivilegedExceptionAction<Object>()
{
- out.close();
- out = null;
- }
- dir.deleteFile(REDO_LOG);
- entryCount = 0;
+ public Object run() throws Exception
+ {
+ if (out != null)
+ {
+ out.close();
+ out = null;
+ }
+ dir.deleteFile(REDO_LOG);
+ entryCount = 0;
+ return null;
+ }
+ });
}
/**
@@ -184,11 +207,18 @@
*/
private void initOut() throws IOException
{
- if (out == null)
- {
- OutputStream os = new IndexOutputStream(dir.createOutput(REDO_LOG));
- out = new BufferedWriter(new OutputStreamWriter(os));
- }
+ SecurityHelper.doPriviledgedIOExceptionAction(new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ if (out == null)
+ {
+ OutputStream os = new IndexOutputStream(dir.createOutput(REDO_LOG));
+ out = new BufferedWriter(new OutputStreamWriter(os));
+ }
+ return null;
+ }
+ });
}
/**
@@ -198,43 +228,50 @@
* @throws IOException if an error occurs while reading from the
* log file.
*/
- private void read(ActionCollector collector) throws IOException
+ private void read(final ActionCollector collector) throws IOException
{
- if (!dir.fileExists(REDO_LOG))
- {
- return;
- }
- InputStream in = new IndexInputStream(dir.openInput(REDO_LOG));
- try
- {
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- String line;
- while ((line = reader.readLine()) != null)
+ SecurityHelper.doPriviledgedIOExceptionAction(new
PrivilegedExceptionAction<Object>()
{
- try
+ public Object run() throws Exception
{
- collector.collect(MultiIndex.Action.fromString(line));
- }
- catch (IllegalArgumentException e)
+ if (!dir.fileExists(REDO_LOG))
{
- log.warn("Malformed redo entry: " + e.getMessage());
+ return null;
}
- }
- }
- finally
- {
- if (in != null)
- {
+ InputStream in = new IndexInputStream(dir.openInput(REDO_LOG));
try
{
- in.close();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+ String line;
+ while ((line = reader.readLine()) != null)
+ {
+ try
+ {
+ collector.collect(MultiIndex.Action.fromString(line));
+ }
+ catch (IllegalArgumentException e)
+ {
+ log.warn("Malformed redo entry: " + e.getMessage());
+ }
+ }
}
- catch (IOException e)
+ finally
{
- log.warn("Exception while closing redo log: " + e.toString());
+ if (in != null)
+ {
+ try
+ {
+ in.close();
+ }
+ catch (IOException e)
+ {
+ log.warn("Exception while closing redo log: " +
e.toString());
+ }
+ }
}
- }
- }
+ return null;
+ }
+ });
}
//-----------------------< internal >---------------------------------------
Modified:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/directory/FSDirectoryManager.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/directory/FSDirectoryManager.java 2010-06-11
13:06:51 UTC (rev 2555)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/directory/FSDirectoryManager.java 2010-06-11
13:41:42 UTC (rev 2556)
@@ -16,110 +16,172 @@
*/
package org.exoplatform.services.jcr.impl.core.query.lucene.directory;
-import java.io.File;
-import java.io.FileFilter;
-import java.io.IOException;
-
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.NativeFSLockFactory;
import org.exoplatform.services.jcr.impl.core.query.lucene.SearchIndex;
+import org.exoplatform.services.jcr.impl.util.SecurityHelper;
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.security.PrivilegedExceptionAction;
+
/**
* <code>FSDirectoryManager</code> implements a directory manager for
* {@link FSDirectory} instances.
*/
-public class FSDirectoryManager implements DirectoryManager {
+public class FSDirectoryManager implements DirectoryManager
+{
- /**
- * The base directory.
- */
- private File baseDir;
+ /**
+ * The base directory.
+ */
+ private File baseDir;
- /**
- * {@inheritDoc}
- */
- public void init(SearchIndex handler) throws IOException {
- baseDir = new File(handler.getPath());
- }
+ /**
+ * {@inheritDoc}
+ */
+ public void init(final SearchIndex handler) throws IOException
+ {
+ SecurityHelper.doPriviledgedIOExceptionAction(new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ baseDir = new File(handler.getPath());
+ return null;
+ }
+ });
+ }
- /**
- * {@inheritDoc}
- */
- public boolean hasDirectory(String name) throws IOException {
- return new File(baseDir, name).exists();
- }
+ /**
+ * {@inheritDoc}
+ */
+ public boolean hasDirectory(final String name) throws IOException
+ {
+ return SecurityHelper.doPriviledgedIOExceptionAction(new
PrivilegedExceptionAction<Boolean>()
+ {
+ public Boolean run() throws Exception
+ {
+ return new File(baseDir, name).exists();
- /**
- * {@inheritDoc}
- */
- public Directory getDirectory(String name)
- throws IOException {
- File dir;
- if (name.equals(".")) {
- dir = baseDir;
- } else {
- dir = new File(baseDir, name);
- }
- return FSDirectory.getDirectory(dir, new NativeFSLockFactory(dir));
- }
+ }
+ });
+ }
- /**
- * {@inheritDoc}
- */
- public String[] getDirectoryNames() throws IOException {
- File[] dirs = baseDir.listFiles(new FileFilter() {
- public boolean accept(File pathname) {
- return pathname.isDirectory();
+ /**
+ * {@inheritDoc}
+ */
+ public Directory getDirectory(final String name) throws IOException
+ {
+ return SecurityHelper.doPriviledgedIOExceptionAction(new
PrivilegedExceptionAction<Directory>()
+ {
+ public Directory run() throws Exception
+ {
+ File dir;
+ if (name.equals("."))
+ {
+ dir = baseDir;
}
- });
- if (dirs != null) {
- String[] names = new String[dirs.length];
- for (int i = 0; i < dirs.length; i++) {
- names[i] = dirs[i].getName();
+ else
+ {
+ dir = new File(baseDir, name);
}
- return names;
- } else {
- throw new IOException("listFiles for " + baseDir.getPath() + "
returned null");
- }
- }
+ return FSDirectory.getDirectory(dir, new NativeFSLockFactory(dir));
+ }
+ });
+ }
- /**
- * {@inheritDoc}
- */
- public boolean delete(String name) {
- File directory = new File(baseDir, name);
- // trivial if it does not exist anymore
- if (!directory.exists()) {
- return true;
- }
- // delete files first
- File[] files = directory.listFiles();
- if (files != null) {
- for (int i = 0; i < files.length; i++) {
- if (!files[i].delete()) {
- return false;
- }
+ /**
+ * {@inheritDoc}
+ */
+ public String[] getDirectoryNames() throws IOException
+ {
+ return SecurityHelper.doPriviledgedIOExceptionAction(new
PrivilegedExceptionAction<String[]>()
+ {
+ public String[] run() throws Exception
+ {
+ File[] dirs = baseDir.listFiles(new FileFilter()
+ {
+ public boolean accept(File pathname)
+ {
+ return pathname.isDirectory();
+ }
+ });
+ if (dirs != null)
+ {
+ String[] names = new String[dirs.length];
+ for (int i = 0; i < dirs.length; i++)
+ {
+ names[i] = dirs[i].getName();
+ }
+ return names;
}
- } else {
- return false;
- }
- // now delete directory itself
- return directory.delete();
- }
+ else
+ {
+ throw new IOException("listFiles for " + baseDir.getPath() +
" returned null");
+ }
+ }
+ });
+ }
- /**
- * {@inheritDoc}
- */
- public boolean rename(String from, String to) {
- File src = new File(baseDir, from);
- File dest = new File(baseDir, to);
- return src.renameTo(dest);
- }
+ /**
+ * {@inheritDoc}
+ */
+ public boolean delete(final String name)
+ {
+ return SecurityHelper.doPriviledgedAction(new
PrivilegedExceptionAction<Boolean>()
+ {
+ public Boolean run() throws Exception
+ {
+ File directory = new File(baseDir, name);
+ // trivial if it does not exist anymore
+ if (!directory.exists())
+ {
+ return true;
+ }
+ // delete files first
+ File[] files = directory.listFiles();
+ if (files != null)
+ {
+ for (int i = 0; i < files.length; i++)
+ {
+ if (!files[i].delete())
+ {
+ return false;
+ }
+ }
+ }
+ else
+ {
+ return false;
+ }
+ // now delete directory itself
+ return directory.delete();
+ }
+ });
+ }
- /**
- * {@inheritDoc}
- */
- public void dispose() {
- }
+ /**
+ * {@inheritDoc}
+ */
+ public boolean rename(final String from, final String to)
+ {
+ return SecurityHelper.doPriviledgedAction(new
PrivilegedExceptionAction<Boolean>()
+ {
+ public Boolean run() throws Exception
+ {
+ File src = new File(baseDir, from);
+ File dest = new File(baseDir, to);
+ return src.renameTo(dest);
+ }
+ });
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void dispose()
+ {
+ }
}
Added:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/SecurityHelper.java
===================================================================
---
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/SecurityHelper.java
(rev 0)
+++
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/SecurityHelper.java 2010-06-11
13:41:42 UTC (rev 2556)
@@ -0,0 +1,95 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+
+/**
+ * Helps running code in privileged
+ *
+ * @author <a href="mailto:nikolazius@gmail.com">Nikolay
Zamosenchuk</a>
+ * @version $Id: SecurityHelper.java 34360 2009-07-22 23:58:59Z nzamosenchuk $
+ *
+ */
+public class SecurityHelper
+{
+
+ /**
+ * Launches action in privileged mode. Can throw only IO exception.
+ *
+ * @param <E>
+ * @param action
+ * @return
+ * @throws IOException
+ */
+ public static <E> E
doPriviledgedIOExceptionAction(PrivilegedExceptionAction<E> action) throws
IOException
+ {
+ try
+ {
+ return 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);
+ }
+ }
+ }
+
+ /**
+ * Launches action in privileged mode. Can throw only runtime exceptions.
+ *
+ * @param <E>
+ * @param action
+ * @return
+ * @throws IOException
+ */
+ public static <E> E doPriviledgedAction(PrivilegedExceptionAction<E>
action)
+ {
+ try
+ {
+ return AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+}
Property changes on:
jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/SecurityHelper.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain