Skip to content
Snippets Groups Projects
Commit 1672ca48 authored by Jeffrey Phillips Freeman's avatar Jeffrey Phillips Freeman :boom:
Browse files

Added initialization script support.

parent dd497ed7
No related merge requests found
......@@ -240,6 +240,11 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
<reporting>
......
/**
* Copyright: (c) Syncleus, Inc.
*
* You may redistribute and modify this source code under the terms and
* conditions of the Open Source Community License - Type C version 1.0
* or any later version as published by Syncleus, Inc. at www.syncleus.com.
* There should be a copy of the license included with this file. If a copy
* of the license is not included you are granted no right to distribute or
* otherwise use this file except through a legal and valid license. You
* should also contact Syncleus, Inc. at the information below if you cannot
* find a license:
*
* Syncleus, Inc.
* 2604 South 12th Street
* Philadelphia, PA 19148
*/
package com.syncleus.maven.plugins.mongodb;
import java.io.File;
public class InitDataConfig {
private File[] scripts;
private String databaseName;
public InitDataConfig(File[] scripts, String databaseName) {
this.scripts = scripts;
this.databaseName = databaseName;
}
public File[] getScripts() {
return scripts;
}
public String getDatabaseName() {
return databaseName;
}
}
......@@ -16,6 +16,9 @@
*/
package com.syncleus.maven.plugins.mongodb;
import com.mongodb.CommandResult;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.syncleus.maven.plugins.mongodb.log.Loggers;
import com.syncleus.maven.plugins.mongodb.log.Loggers.LoggingStyle;
import de.flapdoodle.embed.mongo.*;
......@@ -45,13 +48,16 @@ import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import com.mongodb.DB;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import static java.util.Collections.singletonList;
......@@ -259,6 +265,8 @@ public class StartMongoMojo extends AbstractMongoMojo {
private Integer setPort = null;
private InitDataConfig[] initalizations;
@Override
@SuppressWarnings("unchecked")
public void start() throws MojoExecutionException, MojoFailureException {
......@@ -282,8 +290,8 @@ public class StartMongoMojo extends AbstractMongoMojo {
throw new MojoExecutionException("Failed to download MongoDB distribution: " + e.withDistribution(), e);
}
if(this.imports != null && imports.length > 0)
sendImportScript();
startImport();
startInitialization();
try {
final MongodProcess mongod = executable.start();
......@@ -312,7 +320,7 @@ public class StartMongoMojo extends AbstractMongoMojo {
try {
MongodConfigBuilder configBuilder = new MongodConfigBuilder()
.version(createVersion())
.net(new Net(bindIp, port, Network.localhostIsIPv6()))
.net(new Net(bindIp, getPort(), Network.localhostIsIPv6()))
.replication(new Storage(getDataDirectory(), replSet, oplogSize));
configBuilder = this.configureSyncDelay(configBuilder);
......@@ -472,7 +480,8 @@ public class StartMongoMojo extends AbstractMongoMojo {
for(final String featureString : this.features)
featuresSet.add(Feature.valueOf(featureString.toUpperCase()));
}
return (Feature[]) featuresSet.toArray();
final Feature[] retVal = new Feature[featuresSet.size()];
return featuresSet.toArray(retVal);
}
private int getPort() {
......@@ -483,7 +492,7 @@ public class StartMongoMojo extends AbstractMongoMojo {
setPort = PortUtils.allocateRandomPort();
else
setPort = Integer.valueOf(port);
project.getProperties().put("mongodb.port", setPort);
project.getProperties().put("mongodb.port", String.valueOf(setPort));
return setPort;
}
......@@ -495,14 +504,11 @@ public class StartMongoMojo extends AbstractMongoMojo {
}
}
private void sendImportScript() throws MojoExecutionException {
List<MongoImportProcess> pendingMongoProcess = new ArrayList<MongoImportProcess>();
if(imports == null || imports.length == 0) {
getLog().error("No imports found, check your configuration");
private void startImport() throws MojoExecutionException {
if(imports == null || imports.length == 0)
return;
}
List<MongoImportProcess> pendingMongoProcess = new ArrayList<MongoImportProcess>();
getLog().info("Default import database: " + defaultImportDatabase);
......@@ -579,4 +585,72 @@ public class StartMongoMojo extends AbstractMongoMojo {
"...");
}
private void startInitialization() throws MojoExecutionException, MojoFailureException {
if(initalizations == null || initalizations.length == 0)
return;
for(final InitDataConfig initConfig : this.initalizations ) {
DB db = connectToMongoAndGetDatabase(initConfig.getDatabaseName());
for(final File scriptFile : initConfig.getScripts()) {
if(scriptFile.isDirectory())
this.processScriptDirectory(db, scriptFile);
else
this.processScriptFile(db, scriptFile);
}
}
}
private DB connectToMongoAndGetDatabase(final String databaseName) throws MojoExecutionException {
if (databaseName == null || databaseName.trim().length() == 0) {
throw new MojoExecutionException("Database name is missing");
}
MongoClient mongoClient;
try {
mongoClient = new MongoClient("localhost", getPort());
} catch (UnknownHostException e) {
throw new MojoExecutionException("Unable to connect to mongo instance", e);
}
getLog().info("Connected to MongoDB");
return mongoClient.getDB(databaseName);
}
private void processScriptDirectory(final DB db, final File scriptDirectory) throws MojoExecutionException {
File[] files = scriptDirectory.listFiles();
getLog().info("Folder " + scriptDirectory.getAbsolutePath() + " contains " + files.length + " file(s):");
for (File file : files) {
this.processScriptFile(db, file);
}
getLog().info("Data initialized with success");
}
private void processScriptFile(final DB db, final File scriptFile) throws MojoExecutionException {
Scanner scanner = null;
StringBuilder instructions = new StringBuilder();
try {
scanner = new Scanner(scriptFile);
while (scanner.hasNextLine()) {
instructions.append(scanner.nextLine()).append("\n");
}
} catch (FileNotFoundException e) {
throw new MojoExecutionException("Unable to find file with name '" + scriptFile.getName() + "'", e);
} finally {
if (scanner != null) {
scanner.close();
}
}
CommandResult result;
try {
result = db.doEval("(function() {" + instructions.toString() + "})();", new Object[0]);
} catch (MongoException e) {
throw new MojoExecutionException("Unable to execute file with name '" + scriptFile.getName() + "'", e);
}
if (!result.ok()) {
getLog().error("- file " + scriptFile.getName() + " parsed with error: " + result.getErrorMessage());
throw new MojoExecutionException("Error while executing instructions from file '" + scriptFile.getName() + "': " + result.getErrorMessage(), result.getException());
}
getLog().info("- file " + scriptFile.getName() + " parsed successfully");
}
}
......@@ -80,7 +80,7 @@
</goals>
<configuration>
<systemPropertyVariables>
<mongo.port>${mongodb.port}</mongo.port>
<mongodb.port>${mongodb.port}</mongodb.port>
</systemPropertyVariables>
</configuration>
</execution>
......
......@@ -27,7 +27,7 @@ public class MongoIT {
@Test
public void testConnectMongo() throws Exception {
mongoSocket = new Socket("127.0.0.1", Integer.valueOf(System.getProperty("mongo.port")));
mongoSocket = new Socket("127.0.0.1", Integer.valueOf(System.getProperty("mongodb.port")));
}
@After
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment