Author: rwesten
Date: Wed Oct 16 06:42:30 2013
New Revision: 1532653

URL: http://svn.apache.org/r1532653
Log:
STANBOL-1177: creation, writing and reading of FST corpora is now done within 
AccessController.doPrivileged(..) blocks

Modified:
    
stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusCreationTask.java
    
stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusInfo.java

Modified: 
stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusCreationTask.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusCreationTask.java?rev=1532653&r1=1532652&r2=1532653&view=diff
==============================================================================
--- 
stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusCreationTask.java
 (original)
+++ 
stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusCreationTask.java
 Wed Oct 16 06:42:30 2013
@@ -17,6 +17,9 @@
 package org.apache.stanbol.enhancer.engines.lucenefstlinking;
 
 import java.io.IOException;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
 
@@ -67,37 +70,56 @@ public class CorpusCreationTask implemen
             log.warn("Unable to build {} becuase SolrCore {} is 
closed!",fstInfo,core.getName());
             return;
         }
-        TaggerFstCorpus corpus = null;
+        final TaggerFstCorpus corpus;
         RefCounted<SolrIndexSearcher> searcherRef = core.getSearcher();
-        try {
-            SolrIndexSearcher searcher = searcherRef.get();
+        try { //STANBOL-1177: create FST models in 
AccessController.doPrivileged(..)
+            final SolrIndexSearcher searcher = searcherRef.get();
             //we do get the AtomicReader, because TaggerFstCorpus will need it
             //anyways. This prevents to create another 
SlowCompositeReaderWrapper.
-            IndexReader reader = searcher.getAtomicReader();
+            final IndexReader reader = searcher.getAtomicReader();
             log.info(" ... build FST corpus for {}",fstInfo);
-            corpus = new TaggerFstCorpus(reader, 
searcher.getIndexReader().getVersion(),
-                null, fstInfo.indexedField, fstInfo.storedField, 
fstInfo.analyzer,
-                fstInfo.partialMatches,1,100);
-        } catch (IOException e) {
-            throw new IllegalStateException("Unable to read Information to 
build "
-                    + fstInfo + " from SolrIndex '" + core.getName() + "'!", 
e);
+            corpus = AccessController.doPrivileged(new 
PrivilegedExceptionAction<TaggerFstCorpus>() {
+                public TaggerFstCorpus run() throws IOException {
+                    return new TaggerFstCorpus(reader, 
searcher.getIndexReader().getVersion(),
+                        null, fstInfo.indexedField, fstInfo.storedField, 
fstInfo.analyzer,
+                        fstInfo.partialMatches,1,100);
+                }
+            });
+        } catch (PrivilegedActionException pae) {
+            Exception e = pae.getException();
+            if(e instanceof IOException){ //IO Exception while loading the file
+                throw new IllegalStateException("Unable to read Information to 
build "
+                        + fstInfo + " from SolrIndex '" + core.getName() + 
"'!", e);
+            } else { //Runtime exception
+                throw RuntimeException.class.cast(e);
+            }
         } finally {
             searcherRef.decref(); //ensure that we dereference the searcher
         }
         if(indexConfig.isActive()){
-            if(fstInfo.fst.exists()){
-                if(!FileUtils.deleteQuietly(fstInfo.fst)){
-                    log.warn("Unable to delete existing FST fiel for 
{}",fstInfo);
-                }
-            }
-            try {
-                corpus.save(fstInfo.fst);
-            } catch (IOException e) {
-                log.warn("Unable to store FST corpus " + fstInfo + " to "
-                        + fstInfo.fst.getAbsolutePath() + "!", e);
-            }
             //set the created corpus to the FST Info
             fstInfo.setCorpus(enqueued, corpus);
+            try { //STANBOL-1177: save FST models in 
AccessController.doPrivileged(..)
+                AccessController.doPrivileged(new 
PrivilegedExceptionAction<Object>() {
+                    public Object run() throws IOException {
+                        if(fstInfo.fst.exists()){
+                            if(!FileUtils.deleteQuietly(fstInfo.fst)){
+                                log.warn("Unable to delete existing FST file 
for {}", fstInfo);
+                            }
+                        }
+                        corpus.save(fstInfo.fst);
+                        return null; //not used
+                    }
+                });
+            } catch (PrivilegedActionException pae) {
+                Exception e = pae.getException();
+                if(e instanceof IOException){ //IO Exception while loading the 
file
+                    log.warn("Unable to store FST corpus " + fstInfo + " to "
+                            + fstInfo.fst.getAbsolutePath() + "!", e);
+                } else { //Runtime exception
+                    throw RuntimeException.class.cast(e);
+                }
+            }
         } //else index configuration no longer active ... ignore the built FST
     }
     

Modified: 
stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusInfo.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusInfo.java?rev=1532653&r1=1532652&r2=1532653&view=diff
==============================================================================
--- 
stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusInfo.java
 (original)
+++ 
stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/CorpusInfo.java
 Wed Oct 16 06:42:30 2013
@@ -21,10 +21,10 @@ import java.io.IOException;
 import java.lang.ref.Reference;
 import java.lang.ref.SoftReference;
 import java.lang.ref.WeakReference;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.Date;
-import java.util.concurrent.Future;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.ObjectUtils;
@@ -182,23 +182,36 @@ public class CorpusInfo {
         } else if(taggerCorpusRef != null){
             taggerCorpusRef = null; //reset to null as the reference was taken
         }
-        //if we do not have a corpus try to load from file
-        if(corpus == null && fst.exists() && //if the file exists
-                //AND the file was not yet failing to load OR the file is newer
-                //as the last version failing to load
-                (!fstFileError || FileUtils.isFileNewer(fst, fstDate))){
-            try {
-                corpus = TaggerFstCorpus.load(fst);
+        if(corpus == null) {
+            try { //STANBOL-1177: load FST models in 
AccessController.doPrivileged(..)
+                corpus = AccessController.doPrivileged(new 
PrivilegedExceptionAction<TaggerFstCorpus>() {
+                    public TaggerFstCorpus run() throws IOException {
+                        if(fst.exists() && //if the file exists AND the file 
was not yet failing to load 
+                                //OR the file is newer as the last version 
failing to load
+                                (!fstFileError || FileUtils.isFileNewer(fst, 
fstDate))){
+                            return TaggerFstCorpus.load(fst);
+                        } else {
+                            return null;
+                        }
+                    }
+                });
+            } catch (PrivilegedActionException pae) {
+                Exception e = pae.getException();
+                if(e instanceof IOException){ //IO Exception while loading the 
file
+                    this.errorMessage = new StringBuilder("Unable to load FST 
corpus from "
+                            + "FST file: '").append(fst.getAbsolutePath())
+                            .append("' (Message: 
").append(e.getMessage()).append(")!").toString();
+                        log.warn(errorMessage,e);
+                        fstFileError = true;
+                } else { //Runtime exception
+                    throw RuntimeException.class.cast(e);
+                }
+            }
+            if(corpus != null){
                 fstFileError = false;
                 fstDate = new Date(fst.lastModified());
                 taggerCorpusRef = new SoftReference<TaggerFstCorpus>(corpus);
-            } catch (IOException e) {
-                this.errorMessage = new StringBuilder("Unable to load FST 
corpus from "
-                    + "FST file: '").append(fst.getAbsolutePath())
-                    .append("' (Message: 
").append(e.getMessage()).append(")!").toString();
-                log.warn(errorMessage,e);
-                fstFileError = true;
-            }
+            } //else not loaded from file
         }
         return corpus;
     }


Reply via email to