Author: rwesten
Date: Wed Apr  6 13:56:29 2011
New Revision: 1089457

URL: http://svn.apache.org/viewvc?rev=1089457&view=rev
Log:
Somehow this files where only added, but not committed (STANBOL-141)

Added:
    
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallTask.java
   (with props)
    
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallerConstants.java
   (with props)
    
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexRemoveTask.java
   (with props)
    
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/SolrIndexInstaller.java
   (with props)

Added: 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallTask.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallTask.java?rev=1089457&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallTask.java
 (added)
+++ 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallTask.java
 Wed Apr  6 13:56:29 2011
@@ -0,0 +1,106 @@
+/*
+ * 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.entityhub.yard.solr.impl.install;
+
+import static 
org.apache.stanbol.entityhub.yard.solr.impl.install.IndexInstallerConstants.*;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.zip.GZIPInputStream;
+
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
+import 
org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
+import org.apache.commons.io.IOUtils;
+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.entityhub.yard.solr.SolrDirectoryManager;
+
+public class IndexInstallTask extends InstallTask {
+    /**
+     * use 19 because the config install uses 20 and the files MUST be 
installed
+     * before the config triggering the initialisation of the services. 
+     */
+    private static final String CONFIG_INSTALL_ORDER = "19-";
+
+    private final SolrDirectoryManager solrDirectoryManager;
+    public IndexInstallTask(TaskResourceGroup trg, SolrDirectoryManager 
solrDirectoryManager){
+        super(trg);
+        this.solrDirectoryManager = solrDirectoryManager;
+    }
+    @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));
+            setFinishedState(ResourceState.IGNORED);
+        } else { //this index does not exist
+            String archiveFormat = 
(String)getResource().getAttribute(PROPERTY_ARCHIVE_FORMAT);
+            InputStream is = null;
+            ArchiveInputStream ais = null;
+            try {
+                is = getResource().getInputStream();
+                ais = null;
+                if("zip".equals(archiveFormat)){
+                    ais = new ZipArchiveInputStream(is);
+                } else {
+                    if ("gz".equals(archiveFormat)) {
+                            is = new GZIPInputStream(is);
+                    } else if ("bz2".equals(archiveFormat)) {
+                            is = new BZip2CompressorInputStream(is);
+                    } else {
+                        throw new IllegalStateException("Unsupported 
compression format "+archiveFormat+" " +
+                                       "(implementation out of sync with 
Constants defined in "+IndexInstallerConstants.class.getName()+"). " +
+                                                       "Please report this to 
stanbol-dev mailing list!");
+                    }
+                    ais = new TarArchiveInputStream(is);
+                }
+                //now we can copy the core!
+                solrDirectoryManager.createSolrDirectory(indexName, ais);
+                //we are done ... set the state to installed!
+                setFinishedState(ResourceState.INSTALLED);
+            }catch (IOException e) {
+                ctx.log("Unable to open SolrIndexArchive for index name 
\"%s\"! (resource=%s, arviceFormat=%s)", 
+                    indexName,getResource().getURL(),archiveFormat);
+                setFinishedState(ResourceState.IGNORED);
+            } finally {
+                if(ais != null){ //close the top most stream
+                    IOUtils.closeQuietly(ais);
+                } else if(is != null){
+                    IOUtils.closeQuietly(is);
+                }
+            }
+            
+        }
+        
+    }
+
+    @Override
+    public String getSortKey() {
+        return 
CONFIG_INSTALL_ORDER+getResource().getPriority()+"-"+getResource().getEntityId();
+    }
+
+}

Propchange: 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallTask.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallerConstants.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallerConstants.java?rev=1089457&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallerConstants.java
 (added)
+++ 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallerConstants.java
 Wed Apr  6 13:56:29 2011
@@ -0,0 +1,64 @@
+/*
+ * 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.entityhub.yard.solr.impl.install;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Constants and static configuration used by the {@link SolrIndexInstaller}
+ * @author Rupert Westenthaler
+ *
+ */
+public final class IndexInstallerConstants {
+    private IndexInstallerConstants(){ /* do not create instances*/ }
+    
+    /**
+     * Supported archive types.
+     */
+    public static final Map<String,String> SUPPORTED_COMPRESSION_FORMAT;
+    static {
+        Map<String,String> cfm = new HashMap<String,String>();
+        cfm.put("SOLR_INDEX_ARCHIVE_EXTENSION", "zip"); //the default if not 
specified
+        cfm.put("gz", "gz");
+        cfm.put("bz2", "bz2");
+        cfm.put("zip", "zip");
+        cfm.put("jar", "zip");
+        SUPPORTED_COMPRESSION_FORMAT = Collections.unmodifiableMap(cfm);
+    }
+    /**
+     * Use &lt;indexName&gt;.solrindex[.&lt;archiveType&gt;] as file name
+     */
+    public static final String SOLR_INDEX_ARCHIVE_EXTENSION = "solrindex";
+    /**
+     * The schema used for transformed resources.
+     */
+    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 for the name of the index
+     */
+    public static final String PROPERTY_INDEX_NAME = 
PROPERTY_PREFIX+"index.name";
+    /**
+     * The key used for the type of the archive
+     */
+    public static final String PROPERTY_ARCHIVE_FORMAT = 
PROPERTY_PREFIX+"archive.format";
+
+}

Propchange: 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexInstallerConstants.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexRemoveTask.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexRemoveTask.java?rev=1089457&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexRemoveTask.java
 (added)
+++ 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexRemoveTask.java
 Wed Apr  6 13:56:29 2011
@@ -0,0 +1,64 @@
+/*
+ * 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.entityhub.yard.solr.impl.install;
+
+import org.apache.sling.installer.api.tasks.InstallTask;
+import org.apache.sling.installer.api.tasks.InstallationContext;
+import org.apache.sling.installer.api.tasks.TaskResourceGroup;
+import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
+import org.apache.solr.core.CoreContainer;
+import org.apache.stanbol.entityhub.yard.solr.SolrDirectoryManager;
+
+/**
+ * TODO:
+ * To remove a SolrIndex one would need first to close the SolrCore or
+ * shutdown the SolrContainer. This is currently not possible be cause the 
current
+ * architecture was not intended to support that.<p>
+ * To implement this one would need access to the CoreContainer with the core
+ * running on top of the Core to remove. Than one would need to call
+ * {@link CoreContainer#remove(String)} with the core and 
+ * {@link CoreContainer#persist()} to remove the core also from the solr.xml.
+ * After that one can remove the files from the disk.<p>
+ * This would still have the problem that other components using an 
+ * {@link EmbeddedSolrServer} that is based on this core would not be notified
+ * about such a change.
+ * @author Rupert Westenthaler
+ *
+ */
+public class IndexRemoveTask extends InstallTask {
+    /**
+     * Use 11 because the RemoveConfiguration uses 10 and we need to ensure 
that
+     * the files are removed after the services are shut down.
+     */
+    private static final String CONFIG_INSTALL_ORDER = "11-";
+
+    private final SolrDirectoryManager solrDirectoryManager;
+    public IndexRemoveTask(TaskResourceGroup trg,SolrDirectoryManager 
solrDirectoryManager){
+        super(trg);
+        this.solrDirectoryManager = solrDirectoryManager;
+    }
+    @Override
+    public void execute(InstallationContext ctx) {
+        throw new UnsupportedOperationException("TODO: Not yet implemented 
:(");
+    }
+
+    @Override
+    public String getSortKey() {
+        return CONFIG_INSTALL_ORDER+getResource().getEntityId();
+    }
+
+}

Propchange: 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/IndexRemoveTask.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/SolrIndexInstaller.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/SolrIndexInstaller.java?rev=1089457&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/SolrIndexInstaller.java
 (added)
+++ 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/SolrIndexInstaller.java
 Wed Apr  6 13:56:29 2011
@@ -0,0 +1,170 @@
+/*
+ * 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.entityhub.yard.solr.impl.install;
+
+import static 
org.apache.stanbol.entityhub.yard.solr.impl.install.IndexInstallerConstants.PROPERTY_ARCHIVE_FORMAT;
+import static 
org.apache.stanbol.entityhub.yard.solr.impl.install.IndexInstallerConstants.PROPERTY_INDEX_NAME;
+import static 
org.apache.stanbol.entityhub.yard.solr.impl.install.IndexInstallerConstants.SOLR_INDEX_ARCHIVE_EXTENSION;
+import static 
org.apache.stanbol.entityhub.yard.solr.impl.install.IndexInstallerConstants.SOLR_INDEX_ARCHIVE_RESOURCE_TYPE;
+import static 
org.apache.stanbol.entityhub.yard.solr.impl.install.IndexInstallerConstants.SUPPORTED_COMPRESSION_FORMAT;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.felix.scr.annotations.Component;
+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;
+import org.apache.sling.installer.api.tasks.InstallTask;
+import org.apache.sling.installer.api.tasks.InstallTaskFactory;
+import org.apache.sling.installer.api.tasks.RegisteredResource;
+import org.apache.sling.installer.api.tasks.ResourceState;
+import org.apache.sling.installer.api.tasks.ResourceTransformer;
+import org.apache.sling.installer.api.tasks.TaskResource;
+import org.apache.sling.installer.api.tasks.TaskResourceGroup;
+import org.apache.sling.installer.api.tasks.TransformationResult;
+import org.apache.solr.client.solrj.SolrServer;
+import org.apache.stanbol.entityhub.yard.solr.SolrDirectoryManager;
+import org.apache.stanbol.entityhub.yard.solr.SolrServerProviderManager;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+/**
+ * The main class implementing the two core interfaces of the Apache Sling
+ * Installer framework.<p>
+ * The {@link ResourceTransformer} is needed to check if installed files 
+ * actually SolrIndexes. Currently this check is done by checking if 
+ * <code>'.'+{@link 
IndexInstallerConstants#SOLR_INDEX_ARCHIVE_EXTENSION}</code>
+ * is contained in the filename. In addition it is checked of the archive type
+ * is hinted by the Filename. If not than ".zip" (works also for ".jar") is
+ * assumed. Note also that for ".gz" and ".bz2" it is assumed that ".tar" is
+ * used. File names such as "&lt;indexName&gt;.
+ * {@value IndexInstallerConstants#SOLR_INDEX_ARCHIVE_EXTENSION}
+ * [.&lt;archiveType&gt;]" are used by this implementation<p>
+ * The {@link InstallTaskFactory} interface is needed to create the actual
+ * install and remove task for transformed resources. Based on the requested
+ * activity instances of {@link IndexInstallTask} or {@link IndexRemoveTask} 
are
+ * created.<p>
+ * This implementation uses a {@link Constants#SERVICE_RANKING} of 100. This
+ * ensures that the this implementation is called before any 
+ * {@link ResourceTransformer} directly part of the Apache Sling Installer
+ * 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
+ *
+ */
+@Component(immediate=true)
+@Services(value={
+        @Service(value=InstallTaskFactory.class),
+        @Service(value=ResourceTransformer.class)
+})
+@Property(name=Constants.SERVICE_RANKING,intValue=100) //we need to be in 
front of the Sling Components
+public class SolrIndexInstaller implements InstallTaskFactory, 
ResourceTransformer{
+
+    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;
+    
+    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);
+            } else {
+                return new 
IndexInstallTask(taskResourceGroup,solrDirectoryManager);
+            }
+        } else {
+            return null;
+        }
+    }
+
+    @Override
+    public TransformationResult[] transform(RegisteredResource 
registeredResource) {
+        if(InstallableResource.TYPE_FILE.equals(registeredResource.getType())){
+            return checkIndex(registeredResource);
+        } else { //this processes only files
+            return null;
+        }
+    }
+    /**
+     * Checks if the installed resource is an Solr Index Archive
+     * @param registeredResource the registered resource parsed by the Apache
+     * Sling installer framework
+     * @return the transformed resource or <code>null</code> if the parsed
+     * resource is not an Solr Index Archive.
+     */
+    private TransformationResult[] checkIndex(RegisteredResource 
registeredResource){
+        //the URL is <schema>:<filePath>
+        // where the schema is the provider that registered the resource
+        Map<String,Object> properties = new HashMap<String,Object>();
+        String filePath = 
registeredResource.getURL().substring(registeredResource.getURL().lastIndexOf(':')+1);
+        //get the name of the index
+        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);
+        //now convert to lover case to ease the tests for file endings
+        filePath = filePath.toLowerCase();
+        if(!filePath.contains('.'+SOLR_INDEX_ARCHIVE_EXTENSION)){
+            //not an solr index archive 
+            return null; // -> can not transform
+        }
+        String extension = FilenameUtils.getExtension(filePath);
+        String archiveFormat = SUPPORTED_COMPRESSION_FORMAT.get(extension);
+        if(archiveFormat == null){
+            log.error("Unable to process Solr Index Archive from Resource 
"+registeredResource.getURL()+
+                "because of unsupported archive format \""+extension+"\" 
(supported are "+SUPPORTED_COMPRESSION_FORMAT.keySet()+")");
+            return null;
+        } else {
+            properties.put(PROPERTY_ARCHIVE_FORMAT, archiveFormat);
+        }
+        
+        TransformationResult tr = new TransformationResult();
+//        try {
+//            tr.setInputStream(registeredResource.getInputStream());
+//        } catch (IOException e) {
+//            log.error(String.format("Unable to transform RegisteredResource 
%s with type %s and scheme %s",
+//                registeredResource.getURL(), registeredResource.getType(), 
registeredResource.getScheme()),e);
+//            return null;
+//        }
+        tr.setId(indexName+'.'+SOLR_INDEX_ARCHIVE_EXTENSION+'.'+archiveFormat);
+        tr.setAttributes(properties);
+        tr.setResourceType(SOLR_INDEX_ARCHIVE_RESOURCE_TYPE);
+        return new TransformationResult[]{tr};
+    }
+}

Propchange: 
incubator/stanbol/trunk/entityhub/yard/solr/src/main/java/org/apache/stanbol/entityhub/yard/solr/impl/install/SolrIndexInstaller.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to