diff --git a/pom.xml b/pom.xml index 5a0fb1726e9b76fb4cf40bdbcfd339e0481434bc..fef1b64bdb16b8c4d86d6c171d0059e0bef103b6 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ <groupId>com.syncleus.maven.plugins</groupId> <artifactId>maven-mongodb-plugin</artifactId> - <version>1.0.2-SNAPSHOT</version> + <version>1.1.0-SNAPSHOT</version> <packaging>maven-plugin</packaging> <name>maven-mongodb-plugin</name> @@ -337,7 +337,7 @@ <dependency> <groupId>de.flapdoodle.embed</groupId> <artifactId>de.flapdoodle.embed.mongo</artifactId> - <version>1.47.3</version> + <version>1.48.0</version> </dependency> <dependency> <groupId>junit</groupId> @@ -348,7 +348,7 @@ <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> - <version>2.13.0</version> + <version>3.0.2</version> </dependency> <dependency> <groupId>org.mockito</groupId> diff --git a/src/main/java/com/syncleus/maven/plugins/mongodb/ReplSetInitiateConfig.java b/src/main/java/com/syncleus/maven/plugins/mongodb/ReplSetInitiateConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..56ca33d8e1dde08dbc161a3aa4c7332da69c0aa8 --- /dev/null +++ b/src/main/java/com/syncleus/maven/plugins/mongodb/ReplSetInitiateConfig.java @@ -0,0 +1,232 @@ +/** + * 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 com.mongodb.BasicDBList; +import com.mongodb.BasicDBObject; +import org.apache.maven.plugins.annotations.Parameter; +import java.util.HashMap; + +public class ReplSetInitiateConfig { + private String _id; + private Integer version; + private MembersConfig[] members; + private SettingsConfig settings; + + public ReplSetInitiateConfig() { + } + + public ReplSetInitiateConfig(String _id, Integer version, MembersConfig[] members, SettingsConfig settings) { + this._id = _id; + this.version = version; + this.members = members; + this.settings = settings; + } + + public String get_id() { + return _id; + } + + public Integer getVersion() { + return version; + } + + public MembersConfig[] getMembers() { + return members; + } + + public SettingsConfig getSettings() { + return settings; + } + + public BasicDBObject makeCommand() { + final BasicDBObject cmd = new BasicDBObject(); + if( _id != null && !_id.isEmpty()) + cmd.put("_id", _id); + if(version != null) + cmd.put("version", version); + if( members != null && members.length > 0) { + final BasicDBList dbMembers = new BasicDBList(); + for(final MembersConfig member : members ) + dbMembers.add(member.makeCommand()); + cmd.put("members", dbMembers); + } + if(settings != null) + cmd.put("settings", settings.makeCommand()); + return cmd; + } + + public static class MembersConfig { + private Integer _id; + private String host; + private Boolean arbiterOnly; + private Boolean buildIndexes; + private Boolean hidden; + private Integer priority; + private HashMap<String, String> tags; + private Integer slaveDelay; + private Integer votes; + + public MembersConfig() { + } + + public MembersConfig(Integer _id, String host, Boolean arbiterOnly, Boolean buildIndexes, Boolean hidden, Integer priority, HashMap<String, String> tags, Integer slaveDelay, Integer votes) { + this._id = _id; + this.host = host; + this.arbiterOnly = arbiterOnly; + this.buildIndexes = buildIndexes; + this.hidden = hidden; + this.priority = priority; + this.tags = tags; + this.slaveDelay = slaveDelay; + this.votes = votes; + } + + public Integer get_id() { + return _id; + } + + public String getHost() { + return host; + } + + public Boolean getArbiterOnly() { + return arbiterOnly; + } + + public Boolean getBuildIndexes() { + return buildIndexes; + } + + public Boolean getHidden() { + return hidden; + } + + public Integer getPriority() { + return priority; + } + + public HashMap<String, String> getTags() { + return tags; + } + + public Integer getSlaveDelay() { + return slaveDelay; + } + + public Integer getVotes() { + return votes; + } + + public BasicDBObject makeCommand() { + final BasicDBObject cmd = new BasicDBObject(); + if(_id != null) + cmd.put("_id", _id); + if(host != null && !host.isEmpty()) + cmd.put("host", host); + if(arbiterOnly != null) + cmd.put("arbiterOnly", arbiterOnly); + if(hidden != null) + cmd.put("hidden", arbiterOnly); + if(priority != null) + cmd.put("priority", priority); + if( tags != null && tags.size() > 0) + cmd.put("tags", new BasicDBObject(tags)); + if(slaveDelay != null) + cmd.put("slaveDelay", slaveDelay); + if(votes != null) + cmd.put("votes", votes); + return cmd; + } + } + + public static class SettingsConfig { + private Boolean chainingAllowed; + private Integer heartbeatTimeoutSecs; + private GetLastErrorModesConfig[] getLastErrorModes; + + //TODO: implement getLastErrorDefaults + + public SettingsConfig() { + } + + public SettingsConfig(Boolean chainingAllowed, Integer heartbeatTimeoutSecs, GetLastErrorModesConfig[] getLastErrorModes) { + this.chainingAllowed = chainingAllowed; + this.heartbeatTimeoutSecs = heartbeatTimeoutSecs; + this.getLastErrorModes = getLastErrorModes; + } + + public Boolean getChainingAllowed() { + return chainingAllowed; + } + + public Integer getHeartbeatTimeoutSecs() { + return heartbeatTimeoutSecs; + } + + public GetLastErrorModesConfig[] getGetLastErrorModes() { + return getLastErrorModes; + } + + public BasicDBObject makeCommand() { + final BasicDBObject cmd = new BasicDBObject(); + if(chainingAllowed != null) + cmd.put("chainingAllowed", chainingAllowed); + if(heartbeatTimeoutSecs != null) + cmd.put("heartbeatTimeoutSecs", heartbeatTimeoutSecs); + if(getLastErrorModes != null && getLastErrorModes.length > 0) { + final BasicDBObject dbGetLastErrorModes = new BasicDBObject(); + for( final GetLastErrorModesConfig getLastErrorMode : getLastErrorModes) { + getLastErrorMode.makeCommand(dbGetLastErrorModes); + } + cmd.put("getLastErrorModes", dbGetLastErrorModes); + } + return cmd; + } + + public static class GetLastErrorModesConfig { + private String writeConcern; + private HashMap<String, String> tags; + + public GetLastErrorModesConfig() { + } + + public GetLastErrorModesConfig(String writeConcern, HashMap<String, String> tags) { + this.writeConcern = writeConcern; + this.tags = tags; + } + + public String getWriteConcern() { + return writeConcern; + } + + public HashMap<String, String> getTags() { + return tags; + } + + public void makeCommand(final BasicDBObject cmd) { + if( tags != null && !tags.isEmpty()) { + final BasicDBObject dbTags = new BasicDBObject(); + dbTags.putAll(tags); + cmd.put(writeConcern, dbTags); + } + else + cmd.put(writeConcern, new BasicDBObject()); + } + } + } +} diff --git a/src/main/java/com/syncleus/maven/plugins/mongodb/StartMongoMojo.java b/src/main/java/com/syncleus/maven/plugins/mongodb/StartMongoMojo.java index f0cbce3e1d75cd4becf00b307472282561459728..e5f877d15b3522fc3a3bf7545fc10a852dc842ee 100644 --- a/src/main/java/com/syncleus/maven/plugins/mongodb/StartMongoMojo.java +++ b/src/main/java/com/syncleus/maven/plugins/mongodb/StartMongoMojo.java @@ -21,10 +21,7 @@ */ package com.syncleus.maven.plugins.mongodb; -import com.mongodb.CommandResult; -import com.mongodb.DB; -import com.mongodb.MongoClient; -import com.mongodb.MongoException; +import com.mongodb.*; import com.syncleus.maven.plugins.mongodb.log.Loggers; import com.syncleus.maven.plugins.mongodb.log.Loggers.LoggingStyle; import de.flapdoodle.embed.mongo.*; @@ -285,6 +282,9 @@ public class StartMongoMojo extends AbstractMongoMojo { @Parameter private InitializerConfig[] initalizations; + @Parameter + private ReplSetInitiateConfig replSetInitiate; + /** * Not a mojo configuration parameter, this is used internally. */ @@ -387,6 +387,7 @@ public class StartMongoMojo extends AbstractMongoMojo { throw new MojoExecutionException("Unable to start the mongod", e); } + startReplSetInitiate(); startImport(); startInitialization(); @@ -679,12 +680,13 @@ public class StartMongoMojo extends AbstractMongoMojo { } + private void startInitialization() throws MojoExecutionException, MojoFailureException { if (initalizations == null || initalizations.length == 0) return; for (final InitializerConfig initConfig : this.initalizations) { - final DB db = connectToMongoAndGetDatabase(initConfig.getDatabaseName()); + final DB db = connectToMongoAndGetDB(initConfig.getDatabaseName()); for (final File scriptFile : initConfig.getScripts()) { if (scriptFile.isDirectory()) @@ -695,21 +697,18 @@ public class StartMongoMojo extends AbstractMongoMojo { } } - DB connectToMongoAndGetDatabase(final String databaseName) throws MojoExecutionException { + @Deprecated + DB connectToMongoAndGetDB(final String databaseName) throws MojoExecutionException { if (databaseName == null || databaseName.trim().length() == 0) { throw new MojoExecutionException("Database name is missing"); } - final MongoClient mongoClient; - try { - mongoClient = new MongoClient("localhost", getPort()); - } catch (final UnknownHostException e) { - throw new MojoExecutionException("Unable to connect to mongo instance", e); - } + final MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", getPort())); getLog().info("Connected to MongoDB"); return mongoClient.getDB(databaseName); } + @Deprecated private void processScriptDirectory(final DB db, final File scriptDirectory) throws MojoExecutionException { final File[] files = scriptDirectory.listFiles(); getLog().info("Folder " + scriptDirectory.getAbsolutePath() + " contains " + files.length + " file(s):"); @@ -719,6 +718,7 @@ public class StartMongoMojo extends AbstractMongoMojo { getLog().info("Data initialized with success"); } + @Deprecated private void processScriptFile(final DB db, final File scriptFile) throws MojoExecutionException { Scanner scanner = null; final StringBuilder instructions = new StringBuilder(); @@ -747,4 +747,16 @@ public class StartMongoMojo extends AbstractMongoMojo { } getLog().info("- file " + scriptFile.getName() + " parsed successfully"); } + + private void startReplSetInitiate() throws MojoExecutionException, MojoFailureException { + if(replSetInitiate == null) + return; + + final MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", getPort())); + getLog().info("Connected to MongoDB"); + final DB db = mongoClient.getDB("admin"); + + getLog().info("would have initated: " + replSetInitiate.makeCommand().toString()); + db.command(new BasicDBObject("replSetInitiate", replSetInitiate.makeCommand())); + } } diff --git a/src/test/java/com/mongodb/EmbedMongoDB.java b/src/test/java/com/mongodb/EmbedMongoDB.java index a58f7ea2349aec5add17114d5b26c4d6c6824f7e..bf3cfda19bedffea5137c063ba1b417f5922e518 100644 --- a/src/test/java/com/mongodb/EmbedMongoDB.java +++ b/src/test/java/com/mongodb/EmbedMongoDB.java @@ -16,6 +16,8 @@ */ package com.mongodb; +import org.bson.BsonDocument; + import java.net.UnknownHostException; import java.util.Set; @@ -26,44 +28,20 @@ public class EmbedMongoDB extends DB { } public CommandResult notOkErrorResult(String message) { - try { - CommandResult commandResult = new CommandResult(new ServerAddress("localhost")); - commandResult.put("errmsg", message); - commandResult.put("ok", 0); - return commandResult; - } catch (UnknownHostException e) { - return null; - } + CommandResult commandResult = new CommandResult(new BsonDocument(), new ServerAddress("localhost")); + commandResult.put("errmsg", message); + commandResult.put("ok", 0); + return commandResult; } @Override public CommandResult doEval(String code, Object... args) { - CommandResult commandResult; - try { - commandResult = new CommandResult(new ServerAddress("localhost")); - commandResult.put("ok", 1.0); - commandResult.put("retval", "null"); - } catch (UnknownHostException e) { - return notOkErrorResult(e.getMessage()); - } + CommandResult commandResult = new CommandResult(new BsonDocument(), new ServerAddress("localhost")); + commandResult.put("ok", 1.0); + commandResult.put("retval", "null"); return commandResult; } - @Override - public void requestStart() { - - } - - @Override - public void requestDone() { - - } - - @Override - public void requestEnsureConnection() { - - } - @Override protected DBCollection doGetCollection(String name) { return null; @@ -73,14 +51,4 @@ public class EmbedMongoDB extends DB { public Set<String> getCollectionNames() { return null; } - - @Override - CommandResult doAuthenticate(MongoCredential credentials) { - return null; - } - - @Override - public void cleanCursors(boolean force) { - - } } diff --git a/src/test/java/com/syncleus/maven/plugins/mongodb/StartMongoMojoTest.java b/src/test/java/com/syncleus/maven/plugins/mongodb/StartMongoMojoTest.java index 88c5518d4359d5b14883b894354c6d31107e02af..71ca02f32aaa2c70530776928c31997be0299b28 100644 --- a/src/test/java/com/syncleus/maven/plugins/mongodb/StartMongoMojoTest.java +++ b/src/test/java/com/syncleus/maven/plugins/mongodb/StartMongoMojoTest.java @@ -131,7 +131,7 @@ public class StartMongoMojoTest { } @Override - DB connectToMongoAndGetDatabase(String databaseName) throws MojoExecutionException { + DB connectToMongoAndGetDB(String databaseName) throws MojoExecutionException { return database; } }