Author: chetanm
Date: Fri Nov 27 11:37:57 2015
New Revision: 1716849

URL: http://svn.apache.org/viewvc?rev=1716849&view=rev
Log:
OAK-3687 - Oak standalone application example based on Spring Boot

Enable Mongo integration. The json config is made modular. If user selects 
mongo via "--mongo" command line arg then mongo related json would copied. 
Otherwise segment config file would be copied.

User can also specify complete mongouri via "--mongo=mongodb://server:port"

Added:
    
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/
    
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/mongomk-config.json
    
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/repository-config.json
      - copied, changed from r1716848, 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/repository-config.json
    
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/segmentmk-config.json
Removed:
    
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/repository-config.json
Modified:
    jackrabbit/oak/trunk/oak-examples/standalone/pom.xml
    
jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RepositoryInitializer.java
    
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/application.yml

Modified: jackrabbit/oak/trunk/oak-examples/standalone/pom.xml
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-examples/standalone/pom.xml?rev=1716849&r1=1716848&r2=1716849&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-examples/standalone/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-examples/standalone/pom.xml Fri Nov 27 11:37:57 
2015
@@ -91,6 +91,12 @@
       <version>3.1.0</version>
       <optional>true</optional>
     </dependency>
+    <dependency>
+      <groupId>org.mongodb</groupId>
+      <artifactId>mongo-java-driver</artifactId>
+      <version>2.13.3</version>
+      <optional>true</optional>
+    </dependency>
 
     <dependency>
       <groupId>javax.jcr</groupId>

Modified: 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RepositoryInitializer.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RepositoryInitializer.java?rev=1716849&r1=1716848&r2=1716849&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RepositoryInitializer.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/java/org/apache/jackrabbit/oak/standalone/RepositoryInitializer.java
 Fri Nov 27 11:37:57 2015
@@ -23,6 +23,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.List;
 import java.util.Map;
 
 import javax.annotation.PostConstruct;
@@ -31,6 +32,8 @@ import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.servlet.ServletContext;
 
+import com.google.common.base.Joiner;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
@@ -44,12 +47,15 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.ApplicationArguments;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 
 @Configuration
 public class RepositoryInitializer {
+    public static final String ARG_MONGO = "mongo";
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     @Autowired
@@ -63,6 +69,15 @@ public class RepositoryInitializer {
     @Value("${repo.home}")
     private String repoHome;
 
+    @Value("${oak.mongo.db}")
+    private String mongoDbName;
+
+    @Value("${oak.mongo.uri}")
+    private String mongouri;
+
+    @Autowired
+    private ApplicationArguments args;
+
     @PostConstruct
     public void initialize() throws Exception {
         initRepository();
@@ -91,40 +106,67 @@ public class RepositoryInitializer {
         File repoHomeDir = new File(repoHome);
         FileUtils.forceMkdir(repoHomeDir);
 
-        File repoConfig = new File(repoHomeDir, "repository-config.json");
-        copyDefaultConfig(repoConfig, defaultRepoConfig);
-        repository = createRepository(repoConfig, repoHomeDir);
+        List<String> configFileNames = determineConfigFileNamesToCopy();
+        List<String> configFilePaths = copyConfigs(repoHomeDir, 
configFileNames);
+        repository = createRepository(configFilePaths, repoHomeDir);
     }
 
-    private Repository createRepository(File repoConfig, File repoHomeDir) 
throws RepositoryException {
+    private Repository createRepository(List<String> repoConfigs, File 
repoHomeDir) throws RepositoryException {
         Map<String,Object> config = Maps.newHashMap();
         config.put(OakOSGiRepositoryFactory.REPOSITORY_HOME, 
repoHomeDir.getAbsolutePath());
-        config.put(OakOSGiRepositoryFactory.REPOSITORY_CONFIG_FILE, 
repoConfig.getAbsolutePath());
+        config.put(OakOSGiRepositoryFactory.REPOSITORY_CONFIG_FILE, 
commaSepFilePaths(repoConfigs));
         config.put(OakOSGiRepositoryFactory.REPOSITORY_SHUTDOWN_ON_TIMEOUT, 
false);
         config.put(OakOSGiRepositoryFactory.REPOSITORY_ENV_SPRING_BOOT, true);
         config.put(OakOSGiRepositoryFactory.REPOSITORY_TIMEOUT_IN_SECS, 10);
 
         config.put("repo.home", repoHomeDir.getAbsolutePath());
+        config.put("oak.mongo.db", mongoDbName);
+        config.put("oak.mongo.uri", getMongoURI());
 
         configureActivator(config);
         return new OakOSGiRepositoryFactory().getRepository(config);
     }
 
-    private void configureActivator(Map<String, Object> config) {
-        config.put(BundleActivator.class.getName(), new BundleActivator() {
-            @Override
-            public void start(BundleContext bundleContext) throws Exception {
-                servletContext.setAttribute(BundleContext.class.getName(), 
bundleContext);
-            }
+    private String getMongoURI() {
+        List<String> mongoOpts = args.getOptionValues(ARG_MONGO);
+        if (mongoOpts != null && !mongoOpts.isEmpty()){
+            return mongoOpts.get(0);
+        }
+        return mongouri;
+    }
 
-            @Override
-            public void stop(BundleContext bundleContext) throws Exception {
-                servletContext.removeAttribute(BundleContext.class.getName());
-            }
-        });
+    private Object commaSepFilePaths(List<String> repoConfigs) {
+        return Joiner.on(",").join(repoConfigs);
+    }
 
+    private List<String> copyConfigs(File repoHomeDir, List<String> 
configFileNames)
+            throws IOException, RepositoryException {
+        List<String> filePaths = Lists.newArrayList();
+        for (String configName : configFileNames) {
+            File dest = new File(repoHomeDir, configName);
+            Resource source = new ClassPathResource("config-templates/" + 
configName);
+            copyDefaultConfig(dest, source);
+            filePaths.add(dest.getAbsolutePath());
+        }
+        return filePaths;
     }
 
+
+    private List<String> determineConfigFileNamesToCopy() {
+        List<String> configNames = Lists.newArrayList();
+        configNames.add("repository-config.json");
+
+        //Mongo mode can be selected via --mongo
+        if (args.containsOption(ARG_MONGO)) {
+            configNames.add("mongomk-config.json");
+            log.info("Using Mongo persistence");
+        } else {
+            configNames.add("segmentmk-config.json");
+        }
+        return configNames;
+    }
+
+
     private void copyDefaultConfig(File repoConfig, Resource defaultRepoConfig)
             throws IOException, RepositoryException {
         if (!repoConfig.exists()){
@@ -144,5 +186,18 @@ public class RepositoryInitializer {
         }
     }
 
+    private void configureActivator(Map<String, Object> config) {
+        config.put(BundleActivator.class.getName(), new BundleActivator() {
+            @Override
+            public void start(BundleContext bundleContext) throws Exception {
+                servletContext.setAttribute(BundleContext.class.getName(), 
bundleContext);
+            }
 
+            @Override
+            public void stop(BundleContext bundleContext) throws Exception {
+                servletContext.removeAttribute(BundleContext.class.getName());
+            }
+        });
+
+    }
 }

Modified: 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/application.yml
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/application.yml?rev=1716849&r1=1716848&r2=1716849&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/application.yml 
(original)
+++ 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/application.yml 
Fri Nov 27 11:37:57 2015
@@ -1,2 +1,6 @@
 repo:
-  home: repository
\ No newline at end of file
+  home: repository
+oak:
+  mongo:
+    db: oak
+    uri: mongodb://localhost:27017
\ No newline at end of file

Added: 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/mongomk-config.json
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/mongomk-config.json?rev=1716849&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/mongomk-config.json
 (added)
+++ 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/mongomk-config.json
 Fri Nov 27 11:37:57 2015
@@ -0,0 +1,7 @@
+{
+  "org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreService" : {
+    "customBlobStore" : true,
+    "db" : "${oak.mongo.db}",
+    "mongouri" : "${oak.mongo.uri}"
+  }
+}
\ No newline at end of file

Copied: 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/repository-config.json
 (from r1716848, 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/repository-config.json)
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/repository-config.json?p2=jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/repository-config.json&p1=jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/repository-config.json&r1=1716848&r2=1716849&rev=1716849&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/repository-config.json
 (original)
+++ 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/repository-config.json
 Fri Nov 27 11:37:57 2015
@@ -22,8 +22,6 @@
   "org.apache.jackrabbit.oak.jcr.osgi.RepositoryManager": {},
   "org.apache.jackrabbit.oak.plugins.blob.datastore.FileDataStore": {
     "minRecordLength" : 4096,
-    "path" : "${repo.home}/datastore"
-  },
-  "org.apache.jackrabbit.oak.plugins.segment.SegmentNodeStoreService" : {
+    "path" : "${repo.home}/repository/datastore"
   }
 }
\ No newline at end of file

Added: 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/segmentmk-config.json
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/segmentmk-config.json?rev=1716849&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/segmentmk-config.json
 (added)
+++ 
jackrabbit/oak/trunk/oak-examples/standalone/src/main/resources/config-templates/segmentmk-config.json
 Fri Nov 27 11:37:57 2015
@@ -0,0 +1,5 @@
+{
+  "org.apache.jackrabbit.oak.plugins.segment.SegmentNodeStoreService" : {
+    "customBlobStore" : true
+  }
+}
\ No newline at end of file


Reply via email to