You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2013/01/02 15:26:09 UTC

svn commit: r1427798 - in /stanbol/trunk/commons/solr/core: pom.xml src/main/java/org/apache/stanbol/commons/solr/utils/DataFileResourceLoader.java src/main/java/org/apache/stanbol/commons/solr/utils/StanbolResourceLoader.java

Author: rwesten
Date: Wed Jan  2 14:26:08 2013
New Revision: 1427798

URL: http://svn.apache.org/viewvc?rev=1427798&view=rev
Log:
STANBOL-868: Added Solr ResourceLoader implementation that supports loading resources from the Stanbol DataFileProvider and an other that allows to explicitly parse the Classloader to be used for loading/instantiating Solr components. The later also supports setting a parent so that it can be used in combination with the first one. Currently those ResourceLoaders are not yet used by the commons.solr.core module as the Constructor that allows explicitly setting the ResourceLoader for Solr has package level visibility.

Added:
    stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/DataFileResourceLoader.java
    stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/StanbolResourceLoader.java
Modified:
    stanbol/trunk/commons/solr/core/pom.xml

Modified: stanbol/trunk/commons/solr/core/pom.xml
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/solr/core/pom.xml?rev=1427798&r1=1427797&r2=1427798&view=diff
==============================================================================
--- stanbol/trunk/commons/solr/core/pom.xml (original)
+++ stanbol/trunk/commons/solr/core/pom.xml Wed Jan  2 14:26:08 2013
@@ -90,7 +90,7 @@
               org.apache.solr.search.*;version=${solr-version},
               org.apache.solr.update.*;version=${solr-version},
               org.apache.solr.util.*;version=${solr-version},
-              org.apache.solr.analysis;version=${solr-version},
+              org.apache.solr.analysis;-split-package:=merge-last;version=${solr-version},
               org.apache.lucene.*;version=${solr-version}
             </_exportcontents>
             <!-- NOTE: 

Added: stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/DataFileResourceLoader.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/DataFileResourceLoader.java?rev=1427798&view=auto
==============================================================================
--- stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/DataFileResourceLoader.java (added)
+++ stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/DataFileResourceLoader.java Wed Jan  2 14:26:08 2013
@@ -0,0 +1,84 @@
+package org.apache.stanbol.commons.solr.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.collections.Predicate;
+import org.apache.commons.collections.iterators.FilterIterator;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.LineIterator;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.solr.common.ResourceLoader;
+import org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider;
+
+/**
+ * SolrResourceLoader that supports loading resources via the Apache Stanbol
+ * {@link DataFileProvider}<p>
+ * This does NOT implement the {@link #newInstance(String, String...)} method.
+ * Calls will throw an {@link UnsupportedOperationException}.
+ * Users that need to also load classes should combine this implementation with
+ * the {@link StanbolResourceLoader} that supports instantiation of classes via
+ * the parsed ClassLoader.
+ * @author Rupert Westenthaler
+ *
+ */
+@Component(immediate=true,policy=ConfigurationPolicy.OPTIONAL,metatype=true)
+@Service(value={ResourceLoader.class, DataFileResourceLoader.class})
+public class DataFileResourceLoader implements ResourceLoader {
+
+    @Reference
+    private DataFileProvider dfp;
+    
+    /**
+     * Default constructure used by OSGI
+     */
+    public DataFileResourceLoader(){}
+    
+    /**
+     * Constructs a {@link DataFileResourceLoader} using the parsed 
+     * {@link DataFileProvider}.
+     * @param dfp the {@link DataFileProvider}
+     * @throws IllegalArgumentException if the parsed {@link DataFileProvider} is
+     * <code>null</code>
+     */
+    public DataFileResourceLoader(DataFileProvider dfp){
+        if(dfp == null){
+            throw new IllegalArgumentException("The parsed DataFileProvider MUST NOT be NULL!");
+        }
+        this.dfp = dfp;
+    }
+    
+    @Override
+    public InputStream openResource(String resource) throws IOException {
+        return dfp.getInputStream(null, resource, null);
+    }
+
+    @Override
+    public List<String> getLines(String resource) throws IOException {
+        List<String> lines = new ArrayList<String>();
+        LineIterator it = IOUtils.lineIterator(openResource(resource), "UTF-8");
+        while(it.hasNext()){
+            String line = it.nextLine();
+            if(line != null && !line.isEmpty() && line.charAt(0) != '#'){
+                lines.add(line);
+            }
+        }
+        return lines;
+    }
+    /**
+     * Not implemented!
+     * @throws UnsupportedOperationException on every call to this mehtod
+     * @see StanbolResourceLoader#newInstance(String, String...)
+     */
+    @Override
+    public Object newInstance(String cname, String... subpackages) throws UnsupportedOperationException {
+        throw new UnsupportedOperationException("Loading of ClassFiles is not supported");
+    }
+
+}

Added: stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/StanbolResourceLoader.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/StanbolResourceLoader.java?rev=1427798&view=auto
==============================================================================
--- stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/StanbolResourceLoader.java (added)
+++ stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/StanbolResourceLoader.java Wed Jan  2 14:26:08 2013
@@ -0,0 +1,153 @@
+package org.apache.stanbol.commons.solr.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.LineIterator;
+import org.apache.solr.common.ResourceLoader;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.core.SolrResourceLoader;
+import org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Solr {@link ResourceLoader} implementation that supports adding an parent as
+ * well as parsing the classloader used for 
+ * {@link #newInstance(String, String...)}.<p>
+ * This implementation can be used in combination with the 
+ * {@link DataFileResourceLoader} to allow providing resources via the
+ * Stanbol {@link DataFileProvider} infrastructure.<p>
+ * The {@link #newInstance(String, String...)} method uses the same algorithm as
+ * the {@link SolrResourceLoader#newInstance(String, String...)} method to 
+ * build candidate class names. It also supports the default packages if
+ * <code>null</code> or an empty array is parsed as second parameter.
+ * @author Rupert Westenthaler
+ *
+ */
+public class StanbolResourceLoader implements ResourceLoader {
+
+    private Logger log = LoggerFactory.getLogger(StanbolResourceLoader.class);
+    
+    static final String project = "solr";
+    static final String base = "org.apache" + "." + project;
+    static final String[] packages = {"","analysis.","schema.","handler.","search.","update.","core.","response.","request.","update.processor.","util.", "spelling.", "handler.component.", "handler.dataimport." };
+    
+    protected final ClassLoader classloader;
+    protected final ResourceLoader parent;
+    
+    public StanbolResourceLoader(){
+        this(null,null);
+    }
+    
+    public StanbolResourceLoader(ClassLoader classloader){
+        this(classloader,null);
+    }
+    
+    public StanbolResourceLoader(ResourceLoader parent){
+        this(null,parent);
+    }
+    
+    public StanbolResourceLoader(ClassLoader classloader, ResourceLoader parent){
+        this.classloader = classloader == null ? StanbolResourceLoader.class.getClassLoader() : classloader;
+        this.parent = parent;
+    }
+    
+    
+    @Override
+    public InputStream openResource(String resource) throws IOException {
+        InputStream in;
+        String parentMessage = null;
+        if(parent != null){
+            try {
+                in = parent.openResource(resource);
+            } catch (IOException e) {
+                in = null;
+                parentMessage = e.getMessage();
+            } catch (SecurityException e) { //do not catch security related exceptions
+                throw e;
+            } catch (RuntimeException e) {
+                in = null;
+                parentMessage = e.getMessage();
+            }
+        } else {
+            in = null;
+        }
+        if(in == null){
+            in = classloader.getResourceAsStream(resource);
+        }
+        if(in == null){
+            throw new IOException("Unable to load Resource '"+resource+"' from "
+                + (parent != null ? ("parent (message: "+parentMessage+") and from") : "")
+                + "classpath!");
+        }
+        return in;
+    }
+
+    @Override
+    public List<String> getLines(String resource) throws IOException {
+        List<String> lines = new ArrayList<String>();
+        LineIterator it = IOUtils.lineIterator(openResource(resource), "UTF-8");
+        while(it.hasNext()){
+            String line = it.nextLine();
+            if(line != null && !line.isEmpty() && line.charAt(0) != '#'){
+                lines.add(line);
+            }
+        }
+        return lines;
+    }
+
+    @Override
+    public Object newInstance(String cname, String... subpackages) {
+        String parentMessage = null;
+        if(parent != null){
+            try {
+                return parent.newInstance(cname, subpackages);
+            } catch (SecurityException e) { //do not catch security related exceptions
+                throw e;
+            } catch (RuntimeException e) {
+                parentMessage = e.getMessage();
+            }
+        }
+        if (subpackages == null || subpackages.length == 0 || subpackages == packages) {
+            subpackages = packages;
+        }
+        Class clazz = null;
+        // first try cname == full name
+        try {
+            clazz = classloader.loadClass(cname);
+        } catch (ClassNotFoundException e) {
+            String newName = cname;
+            if (newName.startsWith(project)) {
+                newName = cname.substring(project.length() + 1);
+            }
+            for (String subpackage : subpackages) {
+                try {
+                    String name = base + '.' + subpackage + newName;
+                    log.trace("Trying class name " + name);
+                    clazz = classloader.loadClass(name);
+                    break;
+                } catch (ClassNotFoundException e1) {
+                    // ignore... assume first exception is best.
+                }
+            }
+        }
+        if(clazz == null){
+            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, 
+                "Error loading class '" + cname + "' " + (parent != null ? 
+                        ("from parent (message: "+parentMessage+") and ") : "")
+                        + "via Classloader "+classloader);
+
+        }
+        try {
+          return clazz.newInstance();
+        } catch (Exception e) {
+            throw new SolrException( SolrException.ErrorCode.SERVER_ERROR,
+                "Error instantiating class: '" + clazz.getName()+"'", e);
+        }
+
+    }
+}