Author: chetanm
Date: Fri Nov 11 14:22:05 2016
New Revision: 1769287

URL: http://svn.apache.org/viewvc?rev=1769287&view=rev
Log:
OAK-5074 - Configure Async Indexer via OSGi

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerService.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerServiceTest.java
   (with props)

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerService.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerService.java?rev=1769287&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerService.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerService.java
 Fri Nov 11 14:22:05 2016
@@ -0,0 +1,122 @@
+/*
+ * 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;
+
+import java.util.Arrays;
+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.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.jackrabbit.oak.commons.PropertiesUtil;
+import org.apache.jackrabbit.oak.osgi.OsgiWhiteboard;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
+import org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardIndexEditorProvider;
+import org.osgi.framework.BundleContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+@Component(
+        policy = ConfigurationPolicy.REQUIRE,
+        metatype = true,
+        label = "Apache Jackrabbit Oak Async Indexer Service",
+        description = "Configures the async indexer services which performs 
periodic indexing of repository content"
+)
+public class AsyncIndexerService {
+    @Property(
+            value = {
+                    "async:5"
+            },
+            cardinality = 1024,
+            label = "Async Indexer Configs",
+            description = "Async indexer configs in the form of 
<name>:<interval in secs> e.g. \"async:5\""
+    )
+    private static final String PROP_ASYNC_CONFIG = "asyncConfigs";
+    private static final char CONFIG_SEP = ':';
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final WhiteboardIndexEditorProvider indexEditorProvider = new 
WhiteboardIndexEditorProvider();
+    @Reference
+    private NodeStore nodeStore;
+    private IndexMBeanRegistration indexRegistration;
+
+    @Activate
+    public void activate(BundleContext bundleContext, Map<String, Object> 
config) {
+        List<AsyncConfig> asyncIndexerConfig = 
getAsyncConfig(PropertiesUtil.toStringArray(config.get
+                (PROP_ASYNC_CONFIG), new String[0]));
+        Whiteboard whiteboard = new OsgiWhiteboard(bundleContext);
+        indexRegistration = new IndexMBeanRegistration(whiteboard);
+        indexEditorProvider.start(whiteboard);
+
+        for (AsyncConfig c : asyncIndexerConfig) {
+            //TODO Pass name as part of scheduled job service property
+            AsyncIndexUpdate task = new AsyncIndexUpdate(c.name, nodeStore, 
indexEditorProvider);
+            indexRegistration.registerAsyncIndexer(task, c.timeIntervalInSecs);
+        }
+        log.info("Configured async indexers {} ", asyncIndexerConfig);
+    }
+
+    @Deactivate
+    public void deactivate() {
+        if (indexRegistration != null) {
+            indexRegistration.unregister();
+        }
+    }
+
+    //~-------------------------------------------< internal >
+
+    static List<AsyncConfig> getAsyncConfig(String[] configs) {
+        List<AsyncConfig> result = Lists.newArrayList();
+        for (String config : configs) {
+            int idOfEq = config.indexOf(CONFIG_SEP);
+            checkArgument(idOfEq > 0, "Invalid config provided [%s]", 
Arrays.toString(configs));
+
+            String name = config.substring(0, idOfEq).trim();
+            long interval = Long.parseLong(config.substring(idOfEq + 1));
+            result.add(new AsyncConfig(name, interval));
+        }
+        return result;
+    }
+
+    static class AsyncConfig {
+        final String name;
+        final long timeIntervalInSecs;
+
+        private AsyncConfig(String name, long timeIntervalInSecs) {
+            this.name = name;
+            this.timeIntervalInSecs = timeIntervalInSecs;
+        }
+
+        @Override
+        public String toString() {
+            return "AsyncConfig{" +
+                    "name='" + name + '\'' +
+                    ", timeIntervalInSecs=" + timeIntervalInSecs +
+                    '}';
+        }
+    }
+}

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

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerServiceTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerServiceTest.java?rev=1769287&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerServiceTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerServiceTest.java
 Fri Nov 11 14:22:05 2016
@@ -0,0 +1,73 @@
+/*
+ * 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;
+
+import java.util.List;
+import java.util.Map;
+
+import com.google.common.collect.ImmutableMap;
+import org.apache.jackrabbit.oak.plugins.index.AsyncIndexerService.AsyncConfig;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class AsyncIndexerServiceTest {
+    @Rule
+    public final OsgiContext context = new OsgiContext();
+
+    private AsyncIndexerService service = new AsyncIndexerService();
+
+    @Before
+    public void setUp() {
+        context.registerService(NodeStore.class, new MemoryNodeStore());
+        MockOsgi.injectServices(service, context.bundleContext());
+    }
+
+    @Test
+    public void asyncReg() throws Exception{
+        Map<String,Object> config = ImmutableMap.<String, Object>of(
+                "asyncConfigs", new String[] {"async:5"}
+        );
+        MockOsgi.activate(service, context.bundleContext(), config);
+        assertNotNull(context.getService(Runnable.class));
+
+        MockOsgi.deactivate(service);
+        assertNull(context.getService(Runnable.class));
+    }
+
+    @Test
+    public void configParsing() throws Exception{
+        List<AsyncConfig> configs = AsyncIndexerService.getAsyncConfig(new 
String[]{"async:15"});
+        assertEquals(1, configs.size());
+        assertEquals("async", configs.get(0).name);
+        assertEquals(15, configs.get(0).timeIntervalInSecs);
+
+        configs = AsyncIndexerService.getAsyncConfig(new String[]{"async:15", 
"foo:23"});
+        assertEquals(2, configs.size());
+        assertEquals("foo", configs.get(1).name);
+        assertEquals(23, configs.get(1).timeIntervalInSecs);
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/AsyncIndexerServiceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to