Author: chetanm
Date: Wed Jun 11 11:13:44 2014
New Revision: 1601853

URL: http://svn.apache.org/r1601853
Log:
OAK-1462 - Aggregate Index isn't usable in an osgi environment

Introducing LuceneIndexProviderService would register the LuceneIndexProvder. 
It would also track NodeAggregator and Analyzer and register it with LIP if 
found

Added:
    
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java

Modified: 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java?rev=1601853&r1=1601852&r2=1601853&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java
 Wed Jun 11 11:13:44 2014
@@ -40,12 +40,9 @@ public class LuceneIndexProvider impleme
 
     protected final IndexTracker tracker = new IndexTracker();
 
-    /**
-     * TODO how to inject this in an OSGi friendly way?
-     */
-    protected Analyzer analyzer = LuceneIndexConstants.ANALYZER;
+    protected volatile Analyzer analyzer = LuceneIndexConstants.ANALYZER;
 
-    protected NodeAggregator aggregator = null;
+    protected volatile NodeAggregator aggregator = null;
 
     public void close() {
         tracker.close();
@@ -94,5 +91,4 @@ public class LuceneIndexProvider impleme
         this.setAggregator(analyzer);
         return this;
     }
-
 }

Added: 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java?rev=1601853&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java
 Wed Jun 11 11:13:44 2014
@@ -0,0 +1,124 @@
+/*
+ * 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.jackrabbit.oak.plugins.index.lucene;
+
+import java.util.List;
+import java.util.Map;
+
+import com.google.common.collect.Lists;
+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.ReferencePolicyOption;
+import 
org.apache.jackrabbit.oak.plugins.index.aggregate.AggregateIndexProvider;
+import org.apache.jackrabbit.oak.plugins.index.aggregate.NodeAggregator;
+import org.apache.jackrabbit.oak.spi.commit.Observer;
+import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
+import org.apache.lucene.analysis.Analyzer;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@SuppressWarnings("UnusedDeclaration")
+@Component(immediate = true)
+public class LuceneIndexProviderService {
+
+    private LuceneIndexProvider indexProvider;
+
+    private final List<ServiceRegistration> regs = Lists.newArrayList();
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private final Analyzer defaultAnalyzer = LuceneIndexConstants.ANALYZER;
+
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
+            policyOption = ReferencePolicyOption.GREEDY,
+            policy = ReferencePolicy.DYNAMIC
+    )
+    private NodeAggregator nodeAggregator;
+
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
+            policyOption = ReferencePolicyOption.GREEDY,
+            policy = ReferencePolicy.DYNAMIC
+    )
+    protected Analyzer analyzer;
+
+    @Activate
+    private void activate(BundleContext bundleContext, Map<String, ?> config) {
+        indexProvider = new LuceneIndexProvider();
+        initialize();
+
+        QueryIndexProvider aggregate = 
AggregateIndexProvider.wrap(indexProvider);
+
+        
regs.add(bundleContext.registerService(QueryIndexProvider.class.getName(), 
aggregate, null));
+        regs.add(bundleContext.registerService(Observer.class.getName(), 
indexProvider, null));
+    }
+
+    @Deactivate
+    private void deactivate() {
+        for (ServiceRegistration reg : regs) {
+            reg.unregister();
+        }
+
+        if (indexProvider != null) {
+            indexProvider.close();
+            indexProvider = null;
+        }
+    }
+
+    private void initialize(){
+        if(indexProvider == null){
+            return;
+        }
+
+        if(nodeAggregator != null){
+            log.debug("Using NodeAggregator {}", nodeAggregator.getClass());
+        }
+
+        indexProvider.setAggregator(nodeAggregator);
+
+        Analyzer analyzer = this.analyzer != null ? this.analyzer : 
defaultAnalyzer;
+        indexProvider.setAnalyzer(analyzer);
+    }
+
+    protected void bindNodeAggregator(NodeAggregator aggregator) {
+        this.nodeAggregator = aggregator;
+        initialize();
+    }
+
+    protected void unbindNodeAggregator(NodeAggregator aggregator) {
+        this.nodeAggregator = null;
+        initialize();
+    }
+
+    protected void bindAnalyzer(Analyzer analyzer) {
+        this.analyzer = analyzer;
+        initialize();
+    }
+
+    protected void unbindAnalyzer(Analyzer analyzer) {
+        this.analyzer = null;
+        initialize();
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to