private static KeyGeneratorVerticle createKeyGenerator(TestContext context) { KeyGeneratorSettings settings = new KeyGeneratorSettings(); settings.setKeyGeneratorClass(MongoKeyGenerator.class); settings.getGeneratorProperties().put(MongoKeyGenerator.COLLECTTION_PROP, "keyGenSequence"); return new KeyGeneratorVerticle(settings); }
private void init(Future<Void> startFuture) { Objects.requireNonNull(settings.getKeyGeneratorClass(), "The keygenerator class must be set in the settings"); try { keyGenerator = settings.getKeyGeneratorClass().newInstance(); keyGenerator.init(settings, vertx, result -> { if (result.failed()) { startFuture.fail(result.cause()); } else { vertx.eventBus().consumer(SERVICE_NAME, message -> keyGenerator.generateKey(message)); startFuture.complete(); } }); } catch (Exception e) { startFuture.fail(e); } }
@Override protected void modifyKeyGeneratorVerticleSettings(TestContext context, KeyGeneratorSettings settings) { settings.setKeyGeneratorClass(FileKeyGenerator.class); super.modifyKeyGeneratorVerticleSettings(context, settings); settings.getGeneratorProperties().put(FileKeyGenerator.DESTINATION_DIRECTORY_PROP, "tmp"); settings.getGeneratorProperties().put(FileKeyGenerator.RESET_PROP, "true"); }
private static final KeyGeneratorSettings createDefaultSettings(Class<? extends IKeyGenerator> cls) throws Exception { KeyGeneratorSettings settings = new KeyGeneratorSettings(); settings.keyGeneratorClass = cls; IKeyGenerator gen = cls.newInstance(); settings.generatorProperties = gen.createDefaultProperties(); return settings; }
/** * Possibility to adapt the settings to the needs of the test * * @param context * @param settings */ protected void modifyKeyGeneratorVerticleSettings(TestContext context, KeyGeneratorSettings settings) { settings.setKeyGeneratorClass(DebugGenerator.class); }
@Override public void init(KeyGeneratorSettings settings, Handler<AsyncResult<Void>> handler) throws Exception { try { destinationDir = settings.getGeneratorProperties().getProperty(DESTINATION_DIRECTORY_PROP, KeyGeneratorSettings.LOCAL_USER_DIRECTORY); fileDestination = destinationDir + (destinationDir.endsWith("/") ? "" : "/") + FILENAME; fileSystem = getVertx().fileSystem(); reset = Boolean.valueOf(settings.getGeneratorProperties().getProperty(RESET_PROP, "false")); LOGGER.info("Storing file into " + fileDestination); loadKeyMap(); handler.handle(Future.succeededFuture()); } catch (Exception e) { handler.handle(Future.failedFuture(e)); } }
/** * Initialize the {@link KeyGeneratorSettings} which are used to init the current instance * * @return * @throws Exception */ protected KeyGeneratorSettings initSettings() { try { KeyGeneratorSettings s = KeyGeneratorSettings.loadSettings(vertx, MongoKeyGenerator.class, context); if (!s.isEdited()) { throw new InitException( "The settings are not yet edited. Change the value of property 'edited' to true inside the appropriate file"); } return s; } catch (Exception e) { LOGGER.error("", e); throw ExceptionUtil.createRuntimeException(e); } }
/** * Loads existing settings from the context, when the property {@link #SETTINGS_LOCATION_PROPERTY} is defined; * or loads or generates default settings and stores them in the local user directory, subdirectory .KeyGenerator * * @param vertx * the instance of {@link Vertx} to be used * @param defaultClass * if Settings are created new, this is the generator class to be used * @param context * the context, which could contain the property {@link #SETTINGS_LOCATION_PROPERTY}, where the location of * the settings file is defined * @return */ public static KeyGeneratorSettings loadSettings(Vertx vertx, Class<? extends IKeyGenerator> defaultClass, Context context) throws Exception { String path = context.config().getString(SETTINGS_LOCATION_PROPERTY); if (path != null) { return loadSettings(vertx, defaultClass, path); } else { vertx.fileSystem().mkdirsBlocking(LOCAL_USER_DIRECTORY); String localSettingsFileName = LOCAL_USER_DIRECTORY + "/" + KeyGeneratorVerticle.class.getName() + ".settings.json"; return loadSettings(vertx, defaultClass, localSettingsFileName); } }
private static KeyGeneratorSettings loadSettings(Vertx vertx, Class<? extends IKeyGenerator> defaultClass, String path) throws Exception { FileSystem fs = vertx.fileSystem(); if (fs.existsBlocking(path)) { LOGGER.info("going to load settings from " + path); Buffer buffer = fs.readFileBlocking(path); KeyGeneratorSettings settings = Json.decodeValue(buffer.toString(), KeyGeneratorSettings.class); LOGGER.info("settings successfully loaded from " + path); return settings; } else { LOGGER.info("creating default settings and store them in " + path); KeyGeneratorSettings settings = createDefaultSettings(defaultClass); fs.writeFileBlocking(path, Buffer.buffer(Json.encodePrettily(settings))); String message = String.format( "Settings file did not exist and was created new in path %s. NOTE: edit the file, set edited to true and restart the server", path); throw new InitException(message); } }
@Override protected void modifyKeyGeneratorVerticleSettings(TestContext context, KeyGeneratorSettings settings) { settings.setKeyGeneratorClass(MongoKeyGenerator.class); settings.getGeneratorProperties().put(MongoKeyGenerator.COLLECTTION_PROP, COLLECTION); settings.getGeneratorProperties().put(MongoKeyGenerator.START_MONGO_LOCAL_PROP, System.getProperty(MongoKeyGenerator.START_MONGO_LOCAL_PROP)); super.modifyKeyGeneratorVerticleSettings(context, settings); }
public KeyGeneratorVerticle createKeyGenerator(TestContext context) { KeyGeneratorSettings settings = new KeyGeneratorSettings(); modifyKeyGeneratorVerticleSettings(context, settings); return new KeyGeneratorVerticle(settings); }
@Override protected void modifyKeyGeneratorVerticleSettings(TestContext context, KeyGeneratorSettings settings) { settings.setKeyGeneratorClass(DebugGenerator.class); super.modifyKeyGeneratorVerticleSettings(context, settings); }
@Override public void init(KeyGeneratorSettings settings, Handler<AsyncResult<Void>> handler) throws Exception { try { LOGGER.info("init of MongoKeyGenerator"); Properties props = settings.getGeneratorProperties(); shared = Boolean.valueOf(props.getProperty(SHARED_PROP, "false")); startMongoLocal = Boolean.valueOf(props.getProperty(START_MONGO_LOCAL_PROP, "false")); localPort = Integer.parseInt(props.getProperty(LOCAL_PORT_PROP, String.valueOf(localPort))); if (startMongoLocal) { connectionString = "mongodb://localhost:" + localPort; } else { connectionString = props.getProperty(CONNECTION_STRING_PROPERTY, DEFAULT_CONNECTION); } dbName = props.getProperty(DBNAME_PROP, DEFAULT_DB_NAME); collectionName = props.getProperty(COLLECTTION_PROP, DEFAULT_COLLECTION_NAME); referenceName = props.getProperty(REFERENCE_NAME_PROP, DEFAULT_REFERENCE_NAME); referenceQuery = new JsonObject().put(REFERENCE_FIELD_NAME, referenceName); startMongoExe(startMongoLocal, localPort); initMongoClient(res -> { if (res.failed()) { handler.handle(res); } else { initCounterCollection(handler); } }); } catch (Exception e) { handler.handle(Future.failedFuture(e)); } }
public KeyGeneratorVerticle createKeyGenerator(TestContext context) { KeyGeneratorSettings settings = new KeyGeneratorSettings(); modifyKeyGeneratorVerticleSettings(context, settings); return new KeyGeneratorVerticle(settings); }