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 2011/11/23 10:02:04 UTC

svn commit: r1205331 [2/7] - in /incubator/stanbol/trunk: commons/installer/bundleprovider/src/main/java/org/apache/stanbol/commons/installer/provider/bundle/impl/ commons/opennlp/src/test/java/org/apache/commons/opennlp/ commons/solr/ commons/solr/cor...

Copied: incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/impl/RegisteredSolrServerProvider.java (from r1195450, incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/impl/EmbeddedSolrPorovider.java)
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/impl/RegisteredSolrServerProvider.java?p2=incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/impl/RegisteredSolrServerProvider.java&p1=incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/impl/EmbeddedSolrPorovider.java&r1=1195450&r2=1205331&rev=1205331&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/impl/EmbeddedSolrPorovider.java (original)
+++ incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/impl/RegisteredSolrServerProvider.java Wed Nov 23 09:01:33 2011
@@ -16,216 +16,288 @@
  */
 package org.apache.stanbol.commons.solr.impl;
 
+import static org.apache.stanbol.commons.solr.SolrConstants.PROPERTY_CORE_DIR;
+import static org.apache.stanbol.commons.solr.SolrConstants.PROPERTY_CORE_NAME;
+import static org.apache.stanbol.commons.solr.SolrConstants.PROPERTY_CORE_SERVER_ID;
+import static org.apache.stanbol.commons.solr.SolrConstants.PROPERTY_SERVER_DIR;
+import static org.apache.stanbol.commons.solr.SolrConstants.PROPERTY_SERVER_NAME;
+import static org.osgi.framework.Constants.SERVICE_PID;
+
 import java.io.File;
-import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
-import java.util.Map;
+import java.util.List;
 import java.util.Set;
 
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.apache.commons.collections.map.ReferenceMap;
 import org.apache.commons.io.filefilter.NameFileFilter;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.apache.felix.scr.annotations.ReferencePolicy;
-import org.apache.felix.scr.annotations.ReferenceStrategy;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.solr.client.solrj.SolrServer;
 import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
 import org.apache.solr.core.CoreContainer;
-import org.apache.solr.core.CoreDescriptor;
 import org.apache.solr.core.SolrCore;
+import org.apache.stanbol.commons.solr.IndexReference;
+import org.apache.stanbol.commons.solr.SolrConstants;
+import org.apache.stanbol.commons.solr.SolrServerAdapter;
 import org.apache.stanbol.commons.solr.SolrServerProvider;
 import org.apache.stanbol.commons.solr.SolrServerTypeEnum;
-import org.apache.stanbol.commons.solr.SolrConstants;
 import org.apache.stanbol.commons.solr.utils.ConfigUtils;
+import org.apache.stanbol.commons.solr.utils.ServiceReferenceRankingComparator;
 import org.osgi.framework.Constants;
-import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.xml.sax.SAXException;
 
 /**
- * Support for the use of {@link EmbeddedSolrPorovider} in combination with the SolrYard implementation. This
- * implements the {@link SolrServerProvider} interface for the {@link SolrServerTypeEnum#EMBEDDED}.
- * <p>
- * 
- * TODO: add functionality to lookup the internally managed {@link CoreContainer}. Maybe this requires to add
- * a second service
- * 
+ * {@link SolrServerProvider} that supports all {@link CoreContainer}s and
+ * {@link SolrCore}s registered as OSGI services. <p>
+ * This implementation is intended to be used with {@link CoreContainer}s and
+ * {@link SolrCore}s as registered by the {@link SolrServerAdapter} however it
+ * can work with all {@link CoreContainer}s and {@link SolrCore}s if they provide
+ * the required meta data.
  * @author Rupert Westenthaler
  * 
  */
 @Component(immediate = true, metatype = true)
 @Service
-public class EmbeddedSolrPorovider implements SolrServerProvider {
-    private final Logger log = LoggerFactory.getLogger(EmbeddedSolrPorovider.class);
+public class RegisteredSolrServerProvider implements SolrServerProvider {
+    private final Logger log = LoggerFactory.getLogger(RegisteredSolrServerProvider.class);
     // define the default values here because they are not accessible via the Solr API
-    public static final String SOLR_XML_NAME = "solr.xml";
-    public static final String SOLR_CONFIG_NAME = "solrconfig.xml";
-    public static final String SOLR_SCHEMA_NAME = "schema.xml";
-
-    /**
-     * internally used to keep track of active {@link CoreContainer}s for requested paths.
-     */
-    @SuppressWarnings("unchecked")
-    private Map<String,CoreContainer> coreContainers = new ReferenceMap();
-    /**
-     * The {@link ComponentContext} as set in the {@link #activate(ComponentContext)}
-     * method
-     */
-    private ComponentContext context;
-    private ServiceTracker defaultSolrServerTracker;
 
-    private static String filter = "("+SolrConstants.PROPERTY_SERVER_NAME+"=embedded)";
+    private ServiceTracker coreTracker;
+    private ServiceTracker serverTracker;
+    
+   
 
-    public EmbeddedSolrPorovider() {}
-
-//    protected void bindSolrDirectoryManager(SolrDirectoryManager solrDirectoryManager){
-//        this.solrDirectoryManager = solrDirectoryManager;
-//    }
-//    protected void unbindSolrDirectoryManager(SolrDirectoryManager solrDirectoryManager) {
-//        this.solrDirectoryManager = null;
-//    }
-//    protected SolrDirectoryManager getSolrDirectoryManager(){
-//        return this.solrDirectoryManager;
-//    }
+    public RegisteredSolrServerProvider() {}
 
+    /**
+     * This supports the following uriOrPath values to lookup internally
+     * managed {@link SolrCore}s and to return an {@link EmbeddedSolrServer}
+     * instance:<ol>
+     * <li> file URLs to the Core directory
+     * <li> file paths to the Core directory
+     * <li> [{server-name}:]{core-name} where both the server-name and the
+     *      core-name MUST NOT contain '/' and '\' (on windows) chars. If the 
+     *      server-name is not specified the server with the highest 
+     *      {@link Constants#SERVICE_RANKING} is assumed.
+     * </ol><p>
+     * If the server-name is not known (the case for the first two options),
+     * than <code>null</code> is returned if the referenced Core is not present.
+     * In case of the third option an {@link EmbeddedSolrServer} instance is
+     * also returned if the referenced SolrServer does currently not define
+     * a core with the specified name. Therefore users are encouraged to test
+     * by calling {@link SolrServer#ping()} whether the core is ready to be used
+     * or not. This behaviour was chosen to avoid the need to wait for the 
+     * completion of the initialisation of a Core. In addition typically users
+     * might not know if a used SolrCore is local (embedded) or accessed via
+     * the RESTful interface. Therefore code for dealing with temporarily
+     * unavailable services is typically needed anyway.
+     */
     @Override
-    public SolrServer getSolrServer(SolrServerTypeEnum type, String uriOrPath, String... additional) throws NullPointerException,
-                                                                                      IllegalArgumentException {
-        log.debug(String.format("getSolrServer Request for %s and path %s", type, uriOrPath));
+    public SolrServer getSolrServer(SolrServerTypeEnum type, String uriOrPath, String... additional) {
+        log.debug("getSolrServer Request for {} and path {}", type, uriOrPath);
         if (uriOrPath == null) {
-            throw new IllegalArgumentException("The Path to the Index MUST NOT be NULL!");
+            throw new IllegalArgumentException("The name of requested SolrCore MUST NOT be NULL!");
         }
-        log.info("parsed solr server location " + uriOrPath);
-        // first try as file (but keep in mind it could also be an URI)
-        File index = ConfigUtils.toFile(uriOrPath);
-        if(!index.isAbsolute()){
-            CoreContainer defaultServer = defaultSolrServerTracker == null ? null : (CoreContainer)defaultSolrServerTracker.getService();
-            if(defaultServer != null){
-                if(defaultServer.getCoreNames().contains(index.getName())){
-                    //create an EmbeddedSolrServer
-                    return new EmbeddedSolrServer(defaultServer, index.getName());
-                } else {
-                    log.info("Internally Managed SolrServer does not know a SolrCore with the name '{}'",uriOrPath);
-                }
-            } else if(context != null){
-                log.warn("Internally Managed SolrServer not available: Unable to lookup SolrCore '{}'!",uriOrPath);
-            }
-        } 
-        if (!index.exists()) {
-            throw new IllegalArgumentException(String.format("The parsed Index Path '%s' does not " +
-            		"refer to an internally managed SolrServer nor to a Directory on the FileSystem",
-                uriOrPath));
+        if(type != SolrServerTypeEnum.EMBEDDED){
+            throw new IllegalStateException("Parsed SolrServer type '"+type+
+                "' is not supported by this Implementation (suppoted: '"+
+                SolrServerTypeEnum.EMBEDDED+"')!");
         }
-        //TODO: refactor that so that also external SolrServer are initialised
-        //      using ManagedSsolrServerImpl
-        log.info("get solr server for location " + index);
-        File coreDir = null;
-        if (index.isDirectory()) {
-            File solr = getFile(index, SOLR_XML_NAME);
-            String coreName;
-            if (solr != null) {
-                // in that case we assume that this is a single core installation
-                coreName = "";
+        //(1) parse the pathOrUir -> serverName & coreName
+//        String serverName;
+        String coreName;
+        IndexReference indexReference = IndexReference.parse(uriOrPath);
+        ServiceReference serverRef;
+        if(indexReference.getServer() != null){
+//            if(indexReference.getServer().isEmpty()){
+//                serverRef = getDefaultServerReference();
+//            } else {
+                serverRef = getServerReference(indexReference.getServer());
+//            }
+            if(serverRef == null){
+                log.info(" > {} SolrServer {} is currently not present",
+                    indexReference.getServer().isEmpty()?"Default":"Parsed",indexReference.getServer());
+                coreName = null;
             } else {
-                solr = getFile(index.getParentFile(), SOLR_XML_NAME);
-                if (solr != null) {
-                    // assume this is a multi core
-                    coreName = index.getName();
-                    coreDir = index;
-                    index = index.getParentFile(); // set the index dir to the parent
+                coreName = indexReference.getIndex();
+            }
+        } else {
+            ServiceReference coreRef = getCoreReference(indexReference.getIndex());
+            if(coreRef == null){ 
+                log.info(" > Unable to locate Core '{}' on any active Solr Server",
+                    indexReference.getIndex());
+                serverRef = null;
+                coreName = null;
+            } else {
+                serverRef = getServerReference(coreRef);
+                if(serverRef == null){
+                    log.info(" > SolrServer for Core {} no longer available ",
+                        indexReference.getIndex());
+                    coreName = null;
                 } else {
-                    throw new IllegalArgumentException(String.format(
-                        "The parsed Index Path %s is not an Solr "
-                                + "Index nor a Core of an Multi Core Configuration " + "(no \""
-                                + SOLR_XML_NAME + "\" was found in this nor the parent directory!)",
-                        uriOrPath));
+                    coreName = (String)coreRef.getProperty(PROPERTY_CORE_NAME);
                 }
             }
-            // now init the EmbeddedSolrServer
-            log.info("Create EmbeddedSolrServer for index {} and core {}",
-                index.getAbsolutePath(), coreName);
-            CoreContainer coreContainer = getCoreContainer(index.getAbsolutePath(), solr);
-            // if we have a multi core environment and the core is not yet registered
-            if (!coreName.isEmpty() && !coreContainer.getCoreNames().contains(coreName)) {
-                // register this core first
-                /*
-                 * NOTE: We need to reset the ContextClassLoader to the one used for this Bundle, because Solr
-                 * uses this ClassLoader to load all the plugins configured in the SOLR_XML_NAME and
-                 * schema.xml. The finally block resets the context class loader to the previous value.
-                 * (Rupert Westenthaler 20010209)
-                 */
-                ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-                Thread.currentThread().setContextClassLoader(EmbeddedSolrPorovider.class.getClassLoader());
-                try {
-                    // SolrResourceLoader solrLoader = new SolrResourceLoader(coreDir.getAbsolutePath());
-                    CoreDescriptor coreDescriptor = new CoreDescriptor(coreContainer, coreName,
-                            coreDir.getAbsolutePath());
-                    SolrCore core;
-                    try {
-                        core = coreContainer.create(coreDescriptor);
-                    } catch (Exception e) {
-                        throw new IllegalStateException(String.format("Unable to load/register Solr Core %s "
-                                                                      + "to SolrServer %s!", coreName,
-                            index.getAbsoluteFile()), e);
+        }
+        if(serverRef != null){
+        //we need not to check for the core -> create anyway!
+            return new EmbeddedSolrServer(
+                (CoreContainer)serverTracker.getService(serverRef), coreName);
+        } else {
+            return null;
+        }
+    }
+
+
+
+    /**
+     * Getter for the reference to the tracked {@link CoreContainer} instance 
+     * with the highest {@link Constants#SERVICE_RANKING} - the default 
+     * Server
+     */
+    private ServiceReference getDefaultServerReference() {
+        ServiceReference[] refs = serverTracker.getServiceReferences();
+        if(refs == null){
+            return null;
+        } else {
+            if (refs.length > 1){
+                Arrays.sort(refs,ServiceReferenceRankingComparator.INSTANCE);
+            }
+            return refs[0];
+        }
+    }
+    /**
+     * Getter for the Reference to the {@link CoreContainer} for the {@link SolrCore}
+     * referenced bythe parsed {@link ServiceReference}.<p>
+     * <b>IMPLEMENTATION NOTE:</b> <br>
+     * This method Iterates over all tracked {@link ServiceReference} and
+     * can therefore be assumed as low-performance. However the assumptions are that
+     * first there are only a few tracked services and this method is not called
+     * frequently. If one of this assumptions is not true one should consider to
+     * re-implement this by building an in-memory model of tracked 
+     * {@link CoreContainer}s and {@link SolrCore}s with the required indexes
+     * required to implement lookups like that.
+     * @param name the name (or path) of the {@link SolrCore}
+     * @return the reference to the {@link CoreContainer} or <code>null</code>
+     * if not found
+     */
+    private ServiceReference getServerReference(ServiceReference coreRef){
+        if(coreRef != null){
+            Long serverId = (Long)coreRef.getProperty(PROPERTY_CORE_SERVER_ID);
+            if(serverId != null){
+                ServiceReference[] serverRefs = serverTracker.getServiceReferences();
+                if(serverRefs != null){
+                    for(ServiceReference serverRef : serverRefs){
+                        if(serverId.equals(serverRef.getProperty(Constants.SERVICE_ID))){
+                            return serverRef;
+                        }
                     }
-                    coreContainer.register(coreName, core, false);
-                    // persist the new core to have it available on the next start
-                    coreContainer.persist();
-                } finally {
-                    Thread.currentThread().setContextClassLoader(classLoader);
+                }
+                return null;
+            } else { //search based on name
+                String serverName = (String) coreRef.getProperty(PROPERTY_SERVER_NAME);
+                if(serverName != null){
+                    ServiceReference[] serverRefs = serverTracker.getServiceReferences();
+                    if(serverRefs != null){
+                        for(ServiceReference serverRef : serverRefs){
+                            if(serverName.equals(serverRef.getProperty(PROPERTY_SERVER_NAME))){
+                                //we need to get the name, because maybe the path was parsed
+                                String coreName = (String)coreRef.getProperty(PROPERTY_CORE_NAME);
+                                Collection<?> cores = (Collection<?>)serverRef.getProperty(SolrConstants.PROPERTY_SERVER_CORES);
+                                if(cores.contains(coreName)){
+                                    return serverRef;
+                                } //else the wrong server
+                            } //else  server with wrong server name
+                        }
+                    }
+                    return null; //not found
+                } else { 
+                    //no PROPERTY_CORE_SERVER_PID and PROPERTY_SERVER_NAME
+                    //property for this core
+                    return null;
                 }
             }
-            return (SolrServer)new EmbeddedSolrServer(coreContainer, coreName);
-        } else {
-            throw new IllegalArgumentException(String.format("The parsed Index Path %s is no Directory",
-                uriOrPath));
+        } else {//the requested core was not found
+            return null;
         }
     }
-
-    protected final CoreContainer getCoreContainer(File solrDir) throws IllegalArgumentException,
-                                                                IllegalStateException {
-        return getCoreContainer(solrDir.getAbsolutePath(), new File(solrDir, SOLR_XML_NAME));
+    /**
+     * Getter for the {@link ServiceReference} with the highest Ranking for
+     * the requested Core name<p>
+     * <b>IMPLEMENTATION NOTE:</b> <br>
+     * This method Iterates over all tracked {@link ServiceReference} and
+     * can therefore be assumed as low-performance. However the assumptions are that
+     * first there are only a few tracked services and this method is not called
+     * frequently. If one of this assumptions is not true one should consider to
+     * re-implement this by building an in-memory model of tracked 
+     * {@link CoreContainer}s and {@link SolrCore}s with the required indexes
+     * required to implement lookups like that.     
+     * @param name the name (or path) of the core
+     * @return the reference or <code>null</code> if not found
+     */
+    private ServiceReference getCoreReference(String name){
+        List<ServiceReference> matches;
+        ServiceReference[] refs = coreTracker.getServiceReferences();
+        if(refs != null){
+            matches = new ArrayList<ServiceReference>();
+            for(ServiceReference ref : refs){
+                if(name.equals(ref.getProperty(PROPERTY_CORE_NAME)) ||
+                        name.equals(ref.getProperty(PROPERTY_CORE_DIR))){
+                    matches.add(ref);
+                }
+            }
+            if(matches.size()>1){
+                Collections.sort(matches, ServiceReferenceRankingComparator.INSTANCE);
+            }
+        } else {
+            matches = null;
+        }
+        return matches == null || matches.isEmpty() ? null : matches.get(0);
+        
     }
-
-    protected final CoreContainer getCoreContainer(String solrDir, File solrConf) throws IllegalArgumentException,
-                                                                                         IllegalStateException {
-        CoreContainer container = coreContainers.get(solrDir);
-        if (container == null) {
-            container = new CoreContainer(solrDir);
-            /*
-             * NOTE: We need to reset the ContextClassLoader to the one used for this Bundle, because Solr
-             * uses this ClassLoader to load all the plugins configured in the SOLR_XML_NAME and schema.xml.
-             * The finally block resets the context class loader to the previous value. (Rupert Westenthaler
-             * 20010209)
-             */
-            ClassLoader loader = Thread.currentThread().getContextClassLoader();
-            Thread.currentThread().setContextClassLoader(EmbeddedSolrPorovider.class.getClassLoader());
-            try {
-                container.load(solrDir, solrConf);
-            } catch (ParserConfigurationException e) {
-                throw new IllegalStateException("Unable to parse Solr Configuration", e);
-            } catch (IOException e) {
-                throw new IllegalStateException("Unable to access Solr Configuration", e);
-            } catch (SAXException e) {
-                throw new IllegalStateException("Unable to parse Solr Configuration", e);
-            } finally {
-                Thread.currentThread().setContextClassLoader(loader);
+    /**
+     * Getter for the {@link ServiceReference} to the {@link CoreContainer} for
+     * the parsed name (or path) with the highest service rank<p>
+     * <b>IMPLEMENTATION NOTE:</b> <br>
+     * This method Iterates over all tracked {@link ServiceReference} and
+     * can therefore be assumed as low-performance. However the assumptions are that
+     * first there are only a few tracked services and this method is not called
+     * frequently. If one of this assumptions is not true one should consider to
+     * re-implement this by building an in-memory model of tracked 
+     * {@link CoreContainer}s and {@link SolrCore}s with the required indexes
+     * required to implement lookups like that.
+     * @param name The name (or path) of the server
+     * @return The reference or <code>null</code> if not found
+     */
+    private ServiceReference getServerReference(String name){
+        List<ServiceReference> matches;
+        ServiceReference[] refs = serverTracker.getServiceReferences();
+        if(refs != null){
+            matches = new ArrayList<ServiceReference>();
+            for(ServiceReference ref : refs){
+                if(name.equals(ref.getProperty(PROPERTY_SERVER_NAME)) ||
+                        name.equals(ref.getProperty(PROPERTY_SERVER_DIR))){
+                    matches.add(ref);
+                }
+            }
+            if(matches.size()>1){
+                Collections.sort(matches, ServiceReferenceRankingComparator.INSTANCE);
             }
-            coreContainers.put(solrDir, container);
+        } else {
+            matches = null;
         }
-        return container;
+        return matches == null || matches.isEmpty() ? null : matches.get(0);
     }
+    
 
     @Override
     public Set<SolrServerTypeEnum> supportedTypes() {
@@ -234,29 +306,26 @@ public class EmbeddedSolrPorovider imple
 
     @Activate
     protected void activate(ComponentContext context) throws InvalidSyntaxException {
-        log.debug("activating" + EmbeddedSolrPorovider.class.getSimpleName());
-        this.context = context;
-        String filterString = String.format("(&(%s=%s)(%s=%s))",
-            Constants.OBJECTCLASS,CoreContainer.class.getName(),
-            SolrConstants.PROPERTY_SERVER_NAME,"default");
-
-        Filter filter = context.getBundleContext().createFilter(filterString);
-        defaultSolrServerTracker = new ServiceTracker(
-            context.getBundleContext(), filter, null);
-        defaultSolrServerTracker.open();
-
+        log.debug("activating" + RegisteredSolrServerProvider.class.getSimpleName());
+        coreTracker = new ServiceTracker(context.getBundleContext(), 
+            SolrCore.class.getName(), null);
+        serverTracker = new ServiceTracker(context.getBundleContext(),
+            CoreContainer.class.getName(), null);
+        coreTracker.open();
+        serverTracker.open();
     }
 
     @Deactivate
     protected void deactivate(ComponentContext context) {
-        log.debug("deactivating" + EmbeddedSolrPorovider.class.getSimpleName());
-        defaultSolrServerTracker.close();
-        defaultSolrServerTracker = null;
-        //shutdown externally managed CoreContainers
-        for(CoreContainer coreContainer : coreContainers.values()){
-            coreContainer.shutdown();
+        log.debug("deactivating" + RegisteredSolrServerProvider.class.getSimpleName());
+        if(coreTracker != null){
+            coreTracker.close();
+            coreTracker = null;
+        }
+        if(serverTracker != null){
+            serverTracker.close();
+            serverTracker = null;
         }
-        this.context = null;
     }
 
     // Keeping for now because this might be useful when checking for required files
@@ -300,4 +369,8 @@ public class EmbeddedSolrPorovider imple
         String[] files = dir.list(new NameFileFilter(name));
         return files.length > 0 ? new File(dir, files[0]) : null;
     }
+
+    public static void main(String[] args) {
+      System.out.println(Integer.MAX_VALUE);
+    }
 }

Propchange: incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/impl/RegisteredSolrServerProvider.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/ConfigUtils.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/ConfigUtils.java?rev=1205331&r1=1205330&r2=1205331&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/ConfigUtils.java (original)
+++ incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/ConfigUtils.java Wed Nov 23 09:01:33 2011
@@ -21,6 +21,7 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -42,15 +43,16 @@ import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
-import org.apache.stanbol.commons.solr.impl.EmbeddedSolrPorovider;
+import org.apache.stanbol.commons.solr.impl.RegisteredSolrServerProvider;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * This Utility provides Methods that copy a configuration from a {@link Bundle} and copy it to a directory.
  * <p>
- * This is currently used by the {@link EmbeddedSolrPorovider} to initialise the internally managed
+ * This is currently used by the {@link RegisteredSolrServerProvider} to initialise the internally managed
  * {@link EmbeddedSolrServer} and/or to add additional cores. There are always two variants of the methods.
  * The one taking a bundle as parameter is supposed to be used when running within an OSGI environment. The
  * variant taking a Class object works outside of an OSGI environment.
@@ -637,5 +639,71 @@ public final class ConfigUtils {
         }
         return file;
     }
-
+    /**
+     * Parses the SolrServer name and the Core name form the parsed reference.<p>
+     * The following values are supported:<ol>
+     * <li> file URLs to the Core directory
+     * <li> file paths to the Core directory
+     * <li> [{server-name}:]{core-name} where both the server-name and the
+     *      core-name MUST NOT contain '/' and '\' (on windows) chars. If the 
+     *      server-name is not specified the server with the highest 
+     *      {@link Constants#SERVICE_RANKING} is assumed.
+     * </ol><p>
+     * @param uriOrPath the reference to the core
+     * @return the name of the server (or <code>null</code>) at index [0] and 
+     * the name of the core (or <code>null</code> if not parsed) at index [1].
+     */
+//    public static String[] parseSolrServerReference(String uriOrPath) {
+//        String[] referencedCore = new String[2];
+//        if(uriOrPath.startsWith("file:")){ //file
+//            File file = null; 
+//            try {
+//                file = FileUtils.toFile(new URL(uriOrPath));
+//            }catch (MalformedURLException e) {
+//                log.error("Unable to parse file URL '"+uriOrPath+"'!",e);
+//                file = null;
+//            }
+//            referencedCore[0] = null; //no server name for such values
+//            if(file != null){
+//                file = file.getAbsoluteFile(); 
+//                try {
+//                    referencedCore[1] = file.getCanonicalPath();
+//                } catch (IOException e) {
+//                    log.warn("Unable to create canonical path for the SolrCore reference '"+
+//                        file+"' -> will use this value instead!");
+//                    referencedCore[1] = file.getAbsolutePath();
+//                }
+//            } else {
+//                referencedCore[1] = null;
+//            }
+//        } else if(uriOrPath.indexOf(File.pathSeparatorChar) >=0 ||
+//                uriOrPath.indexOf('/') >=0){ //also support UNIX style on Windows
+//            //we assume a File Reference
+//            File file = new File(FilenameUtils.separatorsToSystem(uriOrPath));
+//            referencedCore[0] = null;
+//            file = file.getAbsoluteFile(); 
+//            try {
+//                referencedCore[1] = file.getCanonicalPath();
+//            } catch (IOException e) {
+//                log.warn("Unable to create canonical path for the SolrCore reference '"+
+//                    file+"' -> will use this value instead!");
+//                referencedCore[1] = file.getAbsolutePath();
+//            }
+//        } else { //reference in the style [{server-name}:]{core-name}
+//            int index = uriOrPath.indexOf(':');
+//            if(index < 0){
+//                referencedCore[0] = "";
+//                referencedCore[1] = uriOrPath;
+//            } else {
+//                referencedCore[0] = uriOrPath.substring(0,index);
+//                if(index+2 >= uriOrPath.length()){
+//                    throw new IllegalArgumentException("The parsed SolrCore name '"+
+//                        uriOrPath+"' MUST NOT end with ':'" +
+//                            "used as separator between the SolrServer and the CoreName!");
+//                }
+//                referencedCore[1] = uriOrPath.substring(index+1);
+//            }
+//        }
+//        return referencedCore;
+//    }
 }

Copied: incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/ServiceReferenceRankingComparator.java (from r1196547, incubator/stanbol/trunk/commons/solr/web/src/main/java/org/apache/stanbol/commons/solr/web/utils/ServiceReferenceRankingComparator.java)
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/ServiceReferenceRankingComparator.java?p2=incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/ServiceReferenceRankingComparator.java&p1=incubator/stanbol/trunk/commons/solr/web/src/main/java/org/apache/stanbol/commons/solr/web/utils/ServiceReferenceRankingComparator.java&r1=1196547&r2=1205331&rev=1205331&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/solr/web/src/main/java/org/apache/stanbol/commons/solr/web/utils/ServiceReferenceRankingComparator.java (original)
+++ incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/ServiceReferenceRankingComparator.java Wed Nov 23 09:01:33 2011
@@ -14,7 +14,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-package org.apache.stanbol.commons.solr.web.utils;
+package org.apache.stanbol.commons.solr.utils;
 
 import java.util.Comparator;
 
@@ -32,13 +32,23 @@ public class ServiceReferenceRankingComp
      */
     public static ServiceReferenceRankingComparator INSTANCE = new ServiceReferenceRankingComparator();
     @Override
-    public int compare(ServiceReference r1, ServiceReference r2) {
-        int ranking1,ranking2;
-        Integer tmp = (Integer)r1.getProperty(Constants.SERVICE_RANKING);
-        ranking1 = tmp != null ? tmp : 0;
-        tmp = (Integer)r2.getProperty(Constants.SERVICE_RANKING);
-        ranking2 = tmp != null ? tmp : 0;
-        return ranking2-ranking1; //highest rank first
+    public int compare(ServiceReference ref1, ServiceReference ref2) {
+        int r1,r2;
+        Object tmp = ref1.getProperty(Constants.SERVICE_RANKING);
+        r1 = tmp != null ? ((Integer)tmp).intValue() : 0;
+        tmp = (Integer)ref2.getProperty(Constants.SERVICE_RANKING);
+        r2 = tmp != null ? ((Integer)tmp).intValue() : 0;
+        if(r1 == r2){
+            tmp = (Long)ref1.getProperty(Constants.SERVICE_ID);
+            long id1 = tmp != null ? ((Long)tmp).longValue() : Long.MAX_VALUE;
+            tmp = (Long)ref2.getProperty(Constants.SERVICE_ID);
+            long id2 = tmp != null ? ((Long)tmp).longValue() : Long.MAX_VALUE;
+            //the lowest id must be first -> id1 < id2 -> [id1,id2] -> return -1
+            return id1 < id2 ? -1 : id2 == id1 ? 0 : 1; 
+        } else {
+            //the highest ranking MUST BE first -> r1 < r2 -> [r2,r1] -> return 1
+            return r1 < r2 ? 1:-1;
+        }
     }
 
 }

Propchange: incubator/stanbol/trunk/commons/solr/core/src/main/java/org/apache/stanbol/commons/solr/utils/ServiceReferenceRankingComparator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/stanbol/trunk/commons/solr/core/src/main/resources/META-INF/services/org.apache.stanbol.commons.solr.SolrServerProvider
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/core/src/main/resources/META-INF/services/org.apache.stanbol.commons.solr.SolrServerProvider?rev=1205331&r1=1205330&r2=1205331&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/solr/core/src/main/resources/META-INF/services/org.apache.stanbol.commons.solr.SolrServerProvider (original)
+++ incubator/stanbol/trunk/commons/solr/core/src/main/resources/META-INF/services/org.apache.stanbol.commons.solr.SolrServerProvider Wed Nov 23 09:01:33 2011
@@ -13,5 +13,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-org.apache.stanbol.commons.solr.impl.DefaultSolrServerProvider
-org.apache.stanbol.commons.solr.impl.EmbeddedSolrPorovider
+org.apache.stanbol.commons.solr.impl.RESTfulSolrServerProvider
+
+# TODO: adding here the StandaloneEmbeddedSolrServerProvider of the 
+#       solr.managed bundle as workaround for the maven assembly plugin
+#       overriding multiple files with the same name
+org.apache.stanbol.commons.solr.managed.standalone.StandaloneEmbeddedSolrServerProvider
+

Modified: incubator/stanbol/trunk/commons/solr/install/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/install/pom.xml?rev=1205331&r1=1205330&r2=1205331&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/solr/install/pom.xml (original)
+++ incubator/stanbol/trunk/commons/solr/install/pom.xml Wed Nov 23 09:01:33 2011
@@ -83,6 +83,10 @@
       <groupId>org.apache.stanbol</groupId>
       <artifactId>org.apache.stanbol.commons.solr.core</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.stanbol</groupId>
+      <artifactId>org.apache.stanbol.commons.solr.managed</artifactId>
+    </dependency>
     <!-- for the Solr Index Installer -->
     <dependency>
       <groupId>org.apache.sling</groupId>

Modified: incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/IndexInstallerConstants.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/IndexInstallerConstants.java?rev=1205331&r1=1205330&r2=1205331&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/IndexInstallerConstants.java (original)
+++ incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/IndexInstallerConstants.java Wed Nov 23 09:01:33 2011
@@ -32,17 +32,7 @@ public final class IndexInstallerConstan
      */
     public static final String SOLR_INDEX_ARCHIVE_RESOURCE_TYPE = "solrarchive";
 
-    private static final String PROPERTY_PREFIX = "org.apache.stanbol.yard.solr.installer.";
-    /**
-     * The key used to configure the name of the Index-Archive The default name is
-     * \"&lt;indexName&gt;.solrarchive\".
-     */
-    public static final String PROPERTY_INDEX_ARCHIVE = "Index-Archive";
-
-    /**
-     * The key used for the name of the index
-     */
-    public static final String PROPERTY_INDEX_NAME = PROPERTY_PREFIX + "index.name";
+    private static final String PROPERTY_PREFIX = "org.apache.stanbol.commons.solr.install.";
     /**
      * The key used for the type of the archive
      */

Modified: incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/IndexInstallTask.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/IndexInstallTask.java?rev=1205331&r1=1205330&r2=1205331&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/IndexInstallTask.java (original)
+++ incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/IndexInstallTask.java Wed Nov 23 09:01:33 2011
@@ -17,10 +17,9 @@
 package org.apache.stanbol.commons.solr.install.impl;
 
 import static org.apache.stanbol.commons.solr.IndexInstallerConstants.PROPERTY_ARCHIVE_FORMAT;
-import static org.apache.stanbol.commons.solr.IndexInstallerConstants.PROPERTY_INDEX_ARCHIVE;
-import static org.apache.stanbol.commons.solr.IndexInstallerConstants.PROPERTY_INDEX_NAME;
+import static org.apache.stanbol.commons.solr.managed.ManagedIndexConstants.INDEX_ARCHIVES;
+import static org.apache.stanbol.commons.solr.managed.ManagedIndexConstants.INDEX_NAME;
 
-import java.io.File;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.Map;
@@ -32,7 +31,8 @@ import org.apache.sling.installer.api.ta
 import org.apache.sling.installer.api.tasks.InstallationContext;
 import org.apache.sling.installer.api.tasks.ResourceState;
 import org.apache.sling.installer.api.tasks.TaskResourceGroup;
-import org.apache.stanbol.commons.solr.SolrDirectoryManager;
+import org.apache.stanbol.commons.solr.managed.ManagedIndexConstants;
+import org.apache.stanbol.commons.solr.managed.ManagedSolrServer;
 import org.apache.stanbol.commons.solr.utils.ConfigUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -46,71 +46,86 @@ public class IndexInstallTask extends In
      */
     private static final String CONFIG_INSTALL_ORDER = "19-";
 
-    private final SolrDirectoryManager solrDirectoryManager;
+    /**
+     * Mapping for the managed servers. The default server uses the <code>null</code>
+     * key!
+     */
+    private final Map<String,ManagedSolrServer> managedServers;
 
-    public IndexInstallTask(TaskResourceGroup trg, SolrDirectoryManager solrDirectoryManager) {
+    public IndexInstallTask(TaskResourceGroup trg, Map<String,ManagedSolrServer> managedServers) {
         super(trg);
-        this.solrDirectoryManager = solrDirectoryManager;
+        if(managedServers == null){
+            throw new IllegalArgumentException("The parsed map with the ManagedSolrServer MUST NOT be NULL!");
+        }
+        this.managedServers = managedServers;
     }
 
     @Override
     public void execute(InstallationContext ctx) {
-        String indexName = (String) getResource().getAttribute(PROPERTY_INDEX_NAME);
-        Map<String,File> existingIndexes = solrDirectoryManager.getManagedIndices();
-        if (existingIndexes.containsKey(indexName)) {
-            // an Index with that name already exists -> ignore
-            ctx.log(
-                "Unable to install the Index with the name \"%s\" becuase an index with that name is already managed by the the SolrYard "
-                        + "(resource %s | location of the existing index %s)!", indexName, getResource()
-                        .getURL(), existingIndexes.get(indexName));
+        String indexName = (String) getResource().getAttribute(INDEX_NAME);
+        if(indexName == null){
+            log.error("Unable to remove Managed Index because the required Property '{}'" +
+                    "used to define the name of the Index is missing",INDEX_NAME);
             setFinishedState(ResourceState.IGNORED);
-        } else { // this index does not exist
-            String archiveFormat = (String) getResource().getAttribute(PROPERTY_ARCHIVE_FORMAT);
-            InputStream is = null;
-            try {
-                is = getResource().getInputStream();
-                if ("properties".equals(archiveFormat)) {
-                    InputStreamReader reader = new InputStreamReader(is, "UTF-8");
-                    Properties props = new Properties();
-                    try {
-                        props.load(reader);
-                    } finally {
-                        IOUtils.closeQuietly(reader);
-                    }
-                    String indexPath = props.getProperty(PROPERTY_INDEX_ARCHIVE);
-                    if (indexPath == null) {
-                        indexPath = indexName + '.' + ConfigUtils.SOLR_INDEX_ARCHIVE_EXTENSION;
-                        log.info("Property \""
-                                 + PROPERTY_INDEX_ARCHIVE
-                                 + "\" not present within the SolrIndex references file. Will use the default name \""
-                                 + indexPath + "\"");
-                    }
-                    solrDirectoryManager.createSolrDirectory(indexName, indexPath, props);
-                    setFinishedState(ResourceState.INSTALLED);
-                } else {
-                    ArchiveInputStream ais = null;
-                    try {
-                        ais = ConfigUtils.getArchiveInputStream(archiveFormat, is);
-                        solrDirectoryManager.createSolrIndex(indexName, ais);
-                        // we are done ... set the state to installed!
+        } else {
+            String serverName = (String) getResource().getAttribute(ManagedIndexConstants.SERVER_NAME);
+            ManagedSolrServer server = managedServers.get(serverName);
+            if(server == null){
+                log.warn("Unable to remove Managed Solr Index {} because the {} " +
+                        "Server {} is currently not active!", 
+                        new Object[]{indexName,serverName == null ? "default" : "",
+                                serverName != null ? serverName : ""});
+                setFinishedState(ResourceState.IGNORED);
+            } else {
+                //we have an index name and a server to in stall it ... 
+                //  ... let's do the work
+                String archiveFormat = (String) getResource().getAttribute(PROPERTY_ARCHIVE_FORMAT);
+                InputStream is = null;
+                try {
+                    is = getResource().getInputStream();
+                    if ("properties".equals(archiveFormat)) {
+                        InputStreamReader reader = new InputStreamReader(is, "UTF-8");
+                        Properties props = new Properties();
+                        try {
+                            props.load(reader);
+                        } finally {
+                            IOUtils.closeQuietly(reader);
+                        }
+                        //TODO install to the right server!
+                        String indexPath = props.getProperty(INDEX_ARCHIVES);
+                        if (indexPath == null) {
+                            indexPath = indexName + '.' + ConfigUtils.SOLR_INDEX_ARCHIVE_EXTENSION;
+                            log.info("Property \""
+                                     + INDEX_ARCHIVES
+                                     + "\" not present within the SolrIndex references file. Will use the default name \""
+                                     + indexPath + "\"");
+                        }
+                        server.updateIndex(indexName, indexPath, props);
                         setFinishedState(ResourceState.INSTALLED);
-                    } finally {
-                        IOUtils.closeQuietly(ais);
+                    } else {
+                        ArchiveInputStream ais = null;
+                        try {
+                            ais = ConfigUtils.getArchiveInputStream(archiveFormat, is);
+                            server.updateIndex(indexName, ais);
+                            // we are done ... set the state to installed!
+                            setFinishedState(ResourceState.INSTALLED);
+                        } finally {
+                            IOUtils.closeQuietly(ais);
+                        }
                     }
+                    // now we can copy the core!
+                } catch (Exception e) {
+                    String message = String.format(
+                        "Unable to install SolrIndexArchive for index name '%s'!" +
+                        " (resource=%s, arviceFormat=%s)",
+                        indexName, getResource().getURL(), archiveFormat);
+                    log.error(message, e);
+                    ctx.log("%s! Reason: %s", message, e.getMessage());
+                    setFinishedState(ResourceState.IGNORED);
+                } finally {
+                    IOUtils.closeQuietly(is);
                 }
-                // now we can copy the core!
-            } catch (Exception e) {
-                String message = String
-                        .format(
-                            "Unable to install SolrIndexArchive for index name \"%s\"! (resource=%s, arviceFormat=%s)",
-                            indexName, getResource().getURL(), archiveFormat);
-                log.error(message, e);
-                ctx.log("%s! Reason: %s", message, e.getMessage());
-                setFinishedState(ResourceState.IGNORED);
-            } finally {
-                IOUtils.closeQuietly(is);
             }
-
         }
 
     }

Modified: incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/IndexRemoveTask.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/IndexRemoveTask.java?rev=1205331&r1=1205330&r2=1205331&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/IndexRemoveTask.java (original)
+++ incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/IndexRemoveTask.java Wed Nov 23 09:01:33 2011
@@ -16,15 +16,16 @@
  */
 package org.apache.stanbol.commons.solr.install.impl;
 
-import static org.apache.stanbol.commons.solr.IndexInstallerConstants.PROPERTY_INDEX_NAME;
+import static org.apache.stanbol.commons.solr.managed.ManagedIndexConstants.INDEX_NAME;
 
-import java.io.File;
+import java.util.Map;
 
 import org.apache.sling.installer.api.tasks.InstallTask;
 import org.apache.sling.installer.api.tasks.InstallationContext;
 import org.apache.sling.installer.api.tasks.ResourceState;
 import org.apache.sling.installer.api.tasks.TaskResourceGroup;
-import org.apache.stanbol.commons.solr.SolrDirectoryManager;
+import org.apache.stanbol.commons.solr.managed.ManagedIndexConstants;
+import org.apache.stanbol.commons.solr.managed.ManagedSolrServer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,29 +53,40 @@ public class IndexRemoveTask extends Ins
      */
     private static final String CONFIG_INSTALL_ORDER = "11-";
     
-    private final SolrDirectoryManager solrDirectoryManager;
+    /**
+     * Mapping for the managed servers. The default server uses the <code>null</code>
+     * key!
+     */
+    private final Map<String,ManagedSolrServer> managedServers;
 
-    public IndexRemoveTask(TaskResourceGroup trg, SolrDirectoryManager solrDirectoryManager) {
+    public IndexRemoveTask(TaskResourceGroup trg, Map<String,ManagedSolrServer> managedServers) {
         super(trg);
-        if(solrDirectoryManager == null){
-            throw new IllegalArgumentException("The parsed SolrDirectoryManager MUST NOT be NULL");
+        if(managedServers == null){
+            throw new IllegalArgumentException("The parsed map with the ManagedSolrServer MUST NOT be NULL!");
         }
-        this.solrDirectoryManager = solrDirectoryManager;
+        this.managedServers = managedServers;
     }
 
     @Override
     public void execute(InstallationContext ctx) {
-        String indexName = (String) getResource().getAttribute(PROPERTY_INDEX_NAME);
-        File solrIndexDir = solrDirectoryManager.getSolrIndexDirectory(indexName);
-        if (solrIndexDir == null) {
-            // no index with that name installed -> nothing to do
-            ctx.log(String.format("SolrIndex '%s' not installed. Nothing to uninstall",
-                indexName));
+        String indexName = (String) getResource().getAttribute(INDEX_NAME);
+        if(indexName == null){
+            log.error("Unable to remove Managed Index because the required Property '{}'" +
+            		"used to define the name of the Index is missing",INDEX_NAME);
             setFinishedState(ResourceState.IGNORED);
-        } else { // this index does not exist
-            //solrDirectoryManager.removeSolrIndex()
-            log.warn("Uninstalling of SolrIndexes not yet Implemented -> marking as uninstalled (see STANBOL-287)");
-            setFinishedState(ResourceState.UNINSTALLED);
+        } else {
+            String serverName = (String) getResource().getAttribute(ManagedIndexConstants.SERVER_NAME);
+            ManagedSolrServer server = managedServers.get(serverName);
+            if(server == null){
+                log.warn("Unable to remove Managed Solr Index {} because the {} " +
+                		"Server {} is currently not active!", 
+                		new Object[]{indexName,serverName == null ? "default" : "",
+                		        serverName != null ? serverName : ""});
+                setFinishedState(ResourceState.IGNORED);
+            } else {
+                server.removeIndex(indexName, true);
+                setFinishedState(ResourceState.UNINSTALLED);
+            }
         }
     }
 

Modified: incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/SolrIndexInstaller.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/SolrIndexInstaller.java?rev=1205331&r1=1205330&r2=1205331&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/SolrIndexInstaller.java (original)
+++ incubator/stanbol/trunk/commons/solr/install/src/main/java/org/apache/stanbol/commons/solr/install/impl/SolrIndexInstaller.java Wed Nov 23 09:01:33 2011
@@ -17,18 +17,21 @@
 package org.apache.stanbol.commons.solr.install.impl;
 
 import static org.apache.stanbol.commons.solr.IndexInstallerConstants.PROPERTY_ARCHIVE_FORMAT;
-import static org.apache.stanbol.commons.solr.IndexInstallerConstants.PROPERTY_INDEX_NAME;
 import static org.apache.stanbol.commons.solr.IndexInstallerConstants.SOLR_INDEX_ARCHIVE_RESOURCE_TYPE;
+import static org.apache.stanbol.commons.solr.managed.ManagedIndexConstants.INDEX_NAME;
 import static org.apache.stanbol.commons.solr.utils.ConfigUtils.SOLR_INDEX_ARCHIVE_EXTENSION;
 import static org.apache.stanbol.commons.solr.utils.ConfigUtils.SUPPORTED_SOLR_ARCHIVE_FORMAT;
 
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.commons.io.FilenameUtils;
+import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.felix.scr.annotations.Services;
 import org.apache.sling.installer.api.InstallableResource;
@@ -41,9 +44,12 @@ import org.apache.sling.installer.api.ta
 import org.apache.sling.installer.api.tasks.TaskResourceGroup;
 import org.apache.sling.installer.api.tasks.TransformationResult;
 import org.apache.stanbol.commons.solr.IndexInstallerConstants;
-import org.apache.stanbol.commons.solr.SolrDirectoryManager;
-import org.apache.stanbol.commons.solr.SolrServerProviderManager;
+import org.apache.stanbol.commons.solr.managed.ManagedSolrServer;
+import org.apache.stanbol.commons.solr.utils.ServiceReferenceRankingComparator;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -67,14 +73,6 @@ import org.slf4j.LoggerFactory;
  * framework. If {@link #transform(RegisteredResource)} returns <code>null</code> the Sling Installer
  * framework will call the next registered {@link ResourceTransformer} instance. By returning a
  * {@link TransformationResult} no further {@link ResourceTransformer} will be called.
- * <p>
- * 
- * TODO: This package should move to an own bundle supporting Sling Install capabilities for Solr. Even the
- * current version only on the {@link SolrDirectoryManager}. The reason why it is still inside the SolrYard
- * Bundle is that the remove functionality would also need to stop currently running SolrServers. this is
- * currently not possible with the current architecture because {@link SolrServer} instances returned by the
- * {@link SolrServerProviderManager} are no OSGI components.
- * 
  * 
  * @author Rupert Westenthaler
  * 
@@ -87,20 +85,59 @@ public class SolrIndexInstaller implemen
 
     private static final Logger log = LoggerFactory.getLogger(SolrIndexInstaller.class);
 
-    /**
-     * This service manages the SolrIndex Directory of the SolrYard. It is needed by the
-     * {@link IndexInstallTask} and {@link IndexRemoveTask} to do there work.
-     */
-    @Reference
-    private SolrDirectoryManager solrDirectoryManager;
 
+    
+    private ServiceTracker serverTracker;
+    
+    @Activate
+    public void activate(ComponentContext context){
+        
+        serverTracker = new ServiceTracker(context.getBundleContext(), 
+            ManagedSolrServer.class.getName(), null);
+        serverTracker.open();
+    }
+    @Deactivate
+    public void deactivate(ComponentContext context){
+        if(serverTracker != null){
+            serverTracker.close();
+            serverTracker = null;
+        }
+    }
+    
+    private Map<String,ManagedSolrServer> getActiveServers(){
+        Map<String,ManagedSolrServer> map;
+        ServiceReference[] serverRefs = serverTracker.getServiceReferences();
+        if(serverRefs == null){
+            map = Collections.emptyMap();
+        } else {
+            map = new HashMap<String,ManagedSolrServer>();
+            if(serverRefs.length > 1){
+                Arrays.sort(serverRefs,ServiceReferenceRankingComparator.INSTANCE);
+            }
+            ManagedSolrServer defaultServer = null;
+            for(ServiceReference ref : serverRefs){
+                ManagedSolrServer server = (ManagedSolrServer)serverTracker.getService(ref);
+                if(server != null){ //may become inactive in the meantime
+                    map.put(server.getServerName(), server);
+                    if(defaultServer == null){
+                        defaultServer = server;
+                    }
+                }
+            }
+            if(defaultServer != null){
+                map.put(null, defaultServer);
+            }
+        }
+        return map;
+    }
+    
     public InstallTask createTask(TaskResourceGroup taskResourceGroup) {
         TaskResource toActivate = taskResourceGroup.getActiveResource();
         if (SOLR_INDEX_ARCHIVE_RESOURCE_TYPE.equals(toActivate.getType())) {
             if (toActivate.getState() == ResourceState.UNINSTALL) {
-                return new IndexRemoveTask(taskResourceGroup, solrDirectoryManager);
+                return new IndexRemoveTask(taskResourceGroup, getActiveServers());
             } else {
-                return new IndexInstallTask(taskResourceGroup, solrDirectoryManager);
+                return new IndexInstallTask(taskResourceGroup, getActiveServers());
             }
         } else {
             return null;
@@ -134,7 +171,7 @@ public class SolrIndexInstaller implemen
         String indexName = FilenameUtils.getBaseName(filePath);
         // only the String until the first '.' -> multiple endings (e.g. slrindex.zip) expected
         indexName = indexName.indexOf('.') > 0 ? indexName.substring(0, indexName.indexOf('.')) : indexName;
-        properties.put(PROPERTY_INDEX_NAME, indexName);
+        properties.put(INDEX_NAME, indexName);
         // now convert to lover case to ease the tests for file endings
         filePath = filePath.toLowerCase();
         if (!filePath.contains('.' + SOLR_INDEX_ARCHIVE_EXTENSION)) {

Propchange: incubator/stanbol/trunk/commons/solr/managed/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Nov 23 09:01:33 2011
@@ -0,0 +1,7 @@
+.settings
+
+target
+
+.classpath
+
+.project

Added: incubator/stanbol/trunk/commons/solr/managed/src/main/java/org/apache/stanbol/commons/solr/managed/impl/ClassPathSolrIndexConfigProvider.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/solr/managed/src/main/java/org/apache/stanbol/commons/solr/managed/impl/ClassPathSolrIndexConfigProvider.java?rev=1205331&view=auto
==============================================================================
--- incubator/stanbol/trunk/commons/solr/managed/src/main/java/org/apache/stanbol/commons/solr/managed/impl/ClassPathSolrIndexConfigProvider.java (added)
+++ incubator/stanbol/trunk/commons/solr/managed/src/main/java/org/apache/stanbol/commons/solr/managed/impl/ClassPathSolrIndexConfigProvider.java Wed Nov 23 09:01:33 2011
@@ -0,0 +1,84 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.commons.solr.managed.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Map;
+
+import org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ClassPathSolrIndexConfigProvider implements DataFileProvider {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    /**
+     * Solr Core configuration are loaded form "solr/core/{core-name}
+     */
+    public static final String INDEX_BASE_PATH = "solr/core/";
+    
+    private final String symbolicName;
+    /**
+     * Creates a DataFileProvider that loads SolrIndexConfigurations via the
+     * classpath relative to {@value #INDEX_BASE_PATH}.
+     * @param bundleSymbolicName the symbolic name of the bundle to accept
+     * requests from or <code>null</code> to accept any request.
+     */
+    public ClassPathSolrIndexConfigProvider(String bundleSymbolicName) {
+        symbolicName = bundleSymbolicName;
+    }
+    
+    @Override
+    public InputStream getInputStream(String bundleSymbolicName,
+            String filename, Map<String, String> comments) 
+    throws IOException {
+        final URL dataFile = getDataFile(bundleSymbolicName, filename);
+        
+        // Returning null is fine - if we don't have the data file, another
+        // provider might supply it
+        return dataFile != null ? dataFile.openStream() : null;
+    }
+    
+    @Override
+    public boolean isAvailable(String bundleSymbolicName, String filename, Map<String,String> comments) {
+        return getDataFile(bundleSymbolicName, filename) != null;
+    }
+
+    /**
+     * @param bundleSymbolicName
+     * @param filename
+     * @return
+     */
+    private URL getDataFile(String bundleSymbolicName, String filename) {
+        //if the parsed bundleSymbolicName is null accept any request
+        //if not, than check if the request is from the correct bundle.
+        if(bundleSymbolicName != null && !bundleSymbolicName.equals(bundleSymbolicName)) {
+            log.debug("Requested bundleSymbolicName {} does not match mine ({}), request ignored",
+                    bundleSymbolicName, symbolicName);
+            return null;
+        }
+        
+        // load default OpenNLP models from classpath (embedded in the defaultdata bundle)
+        final String resourcePath = INDEX_BASE_PATH + filename;
+        final URL dataFile = getClass().getClassLoader().getResource(resourcePath);
+        //log.debug("Resource {} found: {}", (in == null ? "NOT" : ""), resourcePath);
+        return dataFile;
+    }
+
+}

Propchange: incubator/stanbol/trunk/commons/solr/managed/src/main/java/org/apache/stanbol/commons/solr/managed/impl/ClassPathSolrIndexConfigProvider.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain