Author: suat
Date: Tue Jul 31 12:20:18 2012
New Revision: 1367514

URL: http://svn.apache.org/viewvc?rev=1367514&view=rev
Log:
STANBOL-471: -Adapter retrieval of Store service assuming that there can be 
other Store instances. Contenthub specific components retrieves the FileStore 
according to its hardcoded name.

Modified:
    
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/index/src/main/java/org/apache/stanbol/contenthub/index/ldpath/LDPathSemanticIndex.java
    
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java
    
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java
    
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java

Modified: 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/index/src/main/java/org/apache/stanbol/contenthub/index/ldpath/LDPathSemanticIndex.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/index/src/main/java/org/apache/stanbol/contenthub/index/ldpath/LDPathSemanticIndex.java?rev=1367514&r1=1367513&r2=1367514&view=diff
==============================================================================
--- 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/index/src/main/java/org/apache/stanbol/contenthub/index/ldpath/LDPathSemanticIndex.java
 (original)
+++ 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/index/src/main/java/org/apache/stanbol/contenthub/index/ldpath/LDPathSemanticIndex.java
 Tue Jul 31 12:20:18 2012
@@ -41,6 +41,9 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
 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.SolrServerException;
@@ -54,6 +57,7 @@ import org.apache.stanbol.commons.semant
 import org.apache.stanbol.commons.semanticindex.index.SemanticIndex;
 import org.apache.stanbol.commons.semanticindex.store.ChangeSet;
 import org.apache.stanbol.commons.semanticindex.store.IndexingSource;
+import org.apache.stanbol.commons.semanticindex.store.Store;
 import org.apache.stanbol.commons.semanticindex.store.StoreException;
 import org.apache.stanbol.commons.solr.IndexReference;
 import org.apache.stanbol.commons.solr.RegisteredSolrServerTracker;
@@ -143,10 +147,7 @@ public class LDPathSemanticIndex impleme
     @Reference(target = "(org.apache.solr.core.CoreContainer.name=contenthub)")
     private ManagedSolrServer managedSolrServer;
 
-    // TODO: this assumes that only a single Store instance is active!
-    // Semantic Indexes should be connected to stores based on the
-    // name of the Store!
-    @Reference
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, 
referenceInterface = IndexingSource.class, policy = ReferencePolicy.DYNAMIC, 
bind = "bindIndexingSource", unbind = "unbindIndexingSource", strategy = 
ReferenceStrategy.EVENT)
     private IndexingSource<ContentItem> store;
 
     @Reference
@@ -766,6 +767,16 @@ public class LDPathSemanticIndex impleme
         pollingThread.start();
     }
 
+    protected void bindIndexingSource(IndexingSource<ContentItem> 
indexingSource) {
+        if (indexingSource.getName().equals("contenthubFileStore")) {
+            this.store = indexingSource;
+        }
+    }
+
+    protected void unbindIndexingSource(IndexingSource<ContentItem> 
indexingSource) {
+        this.store = null;
+    }
+
     @Deactivate
     protected void deactivate(ComponentContext context) throws 
IndexManagementException {
         // close store check thread and solr core tracker

Modified: 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java?rev=1367514&r1=1367513&r2=1367514&view=diff
==============================================================================
--- 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java
 (original)
+++ 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/store/file/src/main/java/org/apache/stanbol/contenthub/store/file/FileStore.java
 Tue Jul 31 12:20:18 2012
@@ -68,7 +68,6 @@ import org.apache.stanbol.contenthub.sto
 import org.apache.stanbol.enhancer.servicesapi.Blob;
 import org.apache.stanbol.enhancer.servicesapi.ContentItem;
 import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
-import org.apache.stanbol.enhancer.servicesapi.EnhancementException;
 import org.apache.stanbol.enhancer.servicesapi.EnhancementJobManager;
 import org.apache.stanbol.enhancer.servicesapi.NoSuchPartException;
 import org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper;
@@ -381,12 +380,12 @@ public class FileStore implements Store<
 
     @Override
     public String put(ContentItem ci) throws StoreException {
-        try {
-            jobManager.enhanceContent(ci);
-        } catch (EnhancementException e) {
-            throw new StoreException(String.format("Failed to enhance given 
content item with URI: %s",
-                ci.getUri()), e);
-        }
+        // try {
+        // jobManager.enhanceContent(ci);
+        // } catch (EnhancementException e) {
+        // throw new StoreException(String.format("Failed to enhance given 
content item with URI: %s",
+        // ci.getUri()), e);
+        // }
 
         long enhancementCount = getEnhancementCount(ci);
         JSONObject htmlMetadata = getHTMLMetadata(ci);

Modified: 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java?rev=1367514&r1=1367513&r2=1367514&view=diff
==============================================================================
--- 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java
 (original)
+++ 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/test/src/main/java/org/apache/stanbol/contenthub/test/index/ldpath/LDPathSemanticIndexTest.java
 Tue Jul 31 12:20:18 2012
@@ -93,6 +93,7 @@ public class LDPathSemanticIndexTest {
     private static String pid;
     private static int counter = 0;
 
+    @SuppressWarnings("unchecked")
     @Before
     public void before() throws IndexManagementException, IndexException, 
InterruptedException, IOException {
         String name = "test_index_name";
@@ -356,6 +357,7 @@ public class LDPathSemanticIndexTest {
                     .getServiceReference(ConfigurationAdmin.class.getName());
             ConfigurationAdmin configAdmin = (ConfigurationAdmin) 
bundleContext.getService(reference);
             Configuration config = configAdmin.getConfiguration(pid);
+            @SuppressWarnings("unchecked")
             Dictionary<String,String> properties = config.getProperties();
             properties.put(LDPathSemanticIndex.PROP_LD_PATH_PROGRAM, 
newProgram);
             properties.put(LDPathSemanticIndex.PROP_DESCRIPTION, "reindexing");
@@ -370,7 +372,7 @@ public class LDPathSemanticIndexTest {
                 timeoutCount++;
             }
             // index ci to new semantic index
-            while(semanticIndex.getState() != IndexState.ACTIVE) {
+            while (semanticIndex.getState() != IndexState.ACTIVE) {
                 Thread.sleep(500);
             }
             semanticIndex.index(ci);

Modified: 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java?rev=1367514&r1=1367513&r2=1367514&view=diff
==============================================================================
--- 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java
 (original)
+++ 
incubator/stanbol/branches/contenthub-two-layered-structure/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/StoreResource.java
 Tue Jul 31 12:20:18 2012
@@ -43,6 +43,8 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -55,7 +57,6 @@ import java.util.List;
 import java.util.Set;
 import java.util.TreeSet;
 
-import javax.net.ssl.SSLEngineResult.Status;
 import javax.servlet.ServletContext;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
@@ -90,12 +91,15 @@ import org.apache.stanbol.contenthub.sto
 import org.apache.stanbol.enhancer.servicesapi.ContentItem;
 import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
 import org.apache.stanbol.enhancer.servicesapi.EngineException;
+import org.apache.stanbol.enhancer.servicesapi.EnhancementJobManager;
 import org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper;
 import org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource;
 import org.apache.stanbol.enhancer.servicesapi.impl.StringSource;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -122,9 +126,10 @@ public class StoreResource extends BaseS
 
     private static final Logger log = 
LoggerFactory.getLogger(StoreResource.class);
 
+    private EnhancementJobManager enhancementJobManager;
+
     private TcManager tcManager;
 
-    //TODO: make shure that the store is of typeContentItem
     private Store<ContentItem> store;
 
     private ContentItemFactory cif;
@@ -185,25 +190,52 @@ public class StoreResource extends BaseS
      * @param uriInfo
      */
     public StoreResource(@Context ServletContext context, @Context UriInfo 
uriInfo) {
-
-        this.tcManager = ContextHelper.getServiceFromContext(TcManager.class, 
context);
-        //TODO: this has the assumption that there is only a single Store 
instance.
-        //      The store should be referenced by a name
-        this.store = ContextHelper.getServiceFromContext(Store.class, context);
-        if (this.store == null) {
-            log.error("Missing Store Service");
+        BundleContext bundleContext = ContextHelper.getBundleContext(context);
+        if (bundleContext == null) {
+            log.error("Missing BundleContext");
             throw new WebApplicationException(404);
         }
-        if(ContentItem.class.isAssignableFrom(store.getClass())){
-               throw new 
WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
-                               .entity("Store instance is not compatible with 
ContentItem!").build());
-        }
+        this.tcManager = ContextHelper.getServiceFromContext(TcManager.class, 
context);
         this.cif = 
ContextHelper.getServiceFromContext(ContentItemFactory.class, context);
         this.serializer = 
ContextHelper.getServiceFromContext(Serializer.class, context);
+        this.enhancementJobManager = 
ContextHelper.getServiceFromContext(EnhancementJobManager.class, context);
+        this.store = getStoreFromBundleContext(bundleContext);
+        if (this.store == null || this.cif == null || 
this.enhancementJobManager == null
+            || this.serializer == null || this.tcManager == null) {
+            log.error("Missing dependency");
+            throw new WebApplicationException(404);
+        }
         this.uriInfo = uriInfo;
 
     }
 
+    @SuppressWarnings("unchecked")
+    private Store<ContentItem> getStoreFromBundleContext(BundleContext 
bundleContext) {
+        Store<ContentItem> contentHubStore = null;
+        try {
+            ServiceReference[] stores = 
bundleContext.getServiceReferences(Store.class.getName(), null);
+            for (ServiceReference serviceReference : stores) {
+                Object store = bundleContext.getService(serviceReference);
+                Type[] genericInterfaces = 
store.getClass().getGenericInterfaces();
+                if (genericInterfaces.length == 1 && genericInterfaces[0] 
instanceof ParameterizedType) {
+                    Type[] types = ((ParameterizedType) 
genericInterfaces[0]).getActualTypeArguments();
+                    try {
+                        @SuppressWarnings("unused")
+                        Class<ContentItem> contentItemClass = 
(Class<ContentItem>) types[0];
+                        if (((Store<ContentItem>) 
store).getName().equals("contenthubFileStore")) {
+                            contentHubStore = (Store<ContentItem>) store;
+                        }
+                    } catch (ClassCastException e) {
+                        // ignore
+                    }
+                }
+            }
+        } catch (InvalidSyntaxException e) {
+            // ignore as there is no filter
+        }
+        return contentHubStore;
+    }
+
     @OPTIONS
     public Response handleCorsPreflight(@Context HttpHeaders headers) {
         ResponseBuilder res = Response.ok();
@@ -644,7 +676,7 @@ public class StoreResource extends BaseS
     @Path("/{uri:.+}")
     public Response deleteContentItem(@PathParam(value = "uri") String 
contentURI,
                                       @Context HttpHeaders headers) throws 
StoreException {
-        if(store.get(contentURI) == null){
+        if (store.get(contentURI) == null) {
             throw new WebApplicationException(404);
         }
         store.remove(contentURI);


Reply via email to