public static boolean shouldExposeErrorsToHttp(ServiceProperties serviceProps) { String value = serviceProps.getProperty(FLAG_EXPOSE_ERRORS_HTTP); if (StringUtils.isNotEmpty(value) && Boolean.valueOf(value)) { return true; } else { return false; } }
public static boolean shouldDisableRpcInstanceRetry(ServiceProperties serviceProps) { String value = serviceProps.getProperty(DISABLE_RPC_INSTANCE_RETRY); if (StringUtils.isNotEmpty(value) && Boolean.valueOf(value)) { return true; } else { return false; } }
public int getIntegerProperty(String key, int defaultValue) { String value = getProperty(key); if (StringUtils.isNotBlank(value)) { try { return Integer.valueOf(value); } catch (Exception ex) { logger.info("Service property '{}' was not an integer value: '{}'", key, value); } } return defaultValue; }
private int parseSetting(ServiceProperties properties, String key, int defaultValue) { String value = properties.getProperty(key); if (StringUtils.isNotBlank(value)) { try { int retval = Integer.parseInt(value); if (retval < 0) { throw new IllegalArgumentException("Invalid " + key + " setting: " + value); } return retval; } catch (Exception ex) { logger.warn("Caught exception", ex); } } return defaultValue; }
public TestInjectionModule(String serviceName, ServiceProperties props) { this.serviceProperties = props; serviceProperties.setServiceName(serviceName); if (props.getProperty("registry") == null) { serviceProperties.addProperty("registry", "consul"); } if (props.getProperty("registryServer") == null) { serviceProperties.addProperty("registryServer", "localhost:8500"); } if (props.getProperty("kafkaServer") == null) { serviceProperties.addProperty("kafkaServer", "localhost:9092"); } }
private Object findPlugin(Injector injector) { Object retval = null; ServiceProperties serviceProperties = injector.getInstance(ServiceProperties.class); String pluginName = serviceProperties.getProperty("metricsReporter"); if (StringUtils.isBlank(pluginName)) { return null; } if (plugins == null) { // only scan if not already set plugins = new FastClasspathScanner().scan().getNamesOfClassesWithAnnotation(MetricsReporterPlugin.class); } boolean found = false; for (String plugin : plugins) { try { @SuppressWarnings("unchecked") Class<? extends MetricsReporterPlugin> pluginClass = (Class<? extends MetricsReporterPlugin>) Class.forName(plugin); MetricsReporterPlugin anno = pluginClass.getAnnotation(MetricsReporterPlugin.class); if (anno != null && pluginName.equals(anno.name())) { retval = injector.getInstance(pluginClass); found = true; break; } } catch (ClassNotFoundException e) { logger.error("MetricsReporterPlugin not found", e); } } if (! found) { logger.warn("Metrics reporting plugin '{}' was not found in the class path", pluginName); } return retval; }
private Object findConfigurationPlugin(Injector injector) { Object retval = null; String pluginName = serviceProperties.getProperty("configuration"); if (StringUtils.isBlank(pluginName)) { return null; } if (configurationPlugins == null) { // only scan if not already set configurationPlugins = new FastClasspathScanner().scan().getNamesOfClassesWithAnnotation(ConfigurationPlugin.class); } boolean found = false; for (String plugin : configurationPlugins) { try { @SuppressWarnings("unchecked") Class<? extends ConfigurationPlugin> pluginClass = (Class<? extends ConfigurationPlugin>) Class.forName(plugin); ConfigurationPlugin anno = pluginClass.getAnnotation(ConfigurationPlugin.class); if (anno != null && pluginName.equals(anno.name())) { retval = injector.getInstance(pluginClass); found = true; break; } } catch (ClassNotFoundException e) { logger.error("ConfigurationPlugin not found", e); } } if (! found) { logger.warn("Configuration plugin '{}' was not found in the class path", pluginName); } return retval; }
@SuppressWarnings("unchecked") private Object findTracingPlugin(Injector injector) { Object retval = null; String pluginName = serviceProperties.getProperty("tracing"); if (StringUtils.isBlank(pluginName)) { logger.debug("no tracing plugin set, defaulting to 'noop'");
private Object findServiceRegistryPlugin(Injector injector) { Object retval = null; String pluginName = serviceProperties.getProperty("registry"); if (StringUtils.isBlank(pluginName)) { return null; } if (serviceRegistryPlugins == null) { // only scan if not already set serviceRegistryPlugins = new FastClasspathScanner().scan().getNamesOfClassesWithAnnotation(ServiceRegistryPlugin.class); } boolean found = false; for (String plugin : serviceRegistryPlugins) { try { @SuppressWarnings("unchecked") Class<? extends ServiceRegistryPlugin> pluginClass = (Class<? extends ServiceRegistryPlugin>) Class.forName(plugin); ServiceRegistryPlugin anno = pluginClass.getAnnotation(ServiceRegistryPlugin.class); if (anno != null && pluginName.equals(anno.name())) { retval = injector.getInstance(pluginClass); found = true; break; } } catch (ClassNotFoundException e) { logger.error("ServiceRegistryPlugin not found", e); } } if (! found) { logger.warn("Registry plugin '{}' was not found in the class path", pluginName); } return retval; }
@Test public void propertiesNotSet() { props.addProperty("kafkaServer", "test.org:52"); assertThat(props.getKafkaServer()).isEqualTo("test.org:52"); assertThat(props.getProperty("blahblah")).isNull(); }
protected void migrateDatabase() { try { flyway.setTable(FLYWAY_SCHEMA_TABLE_NAME); String url = "jdbc:" + serviceProps.getDatabaseServer(); flyway.setDataSource(url, serviceProps.getDatabaseUsername(), serviceProps.getDatabasePassword()); //Use repair to fix the state if a migration failed and you corrected it. if (serviceProps.getProperty("repairDatabase") != null) { flyway.repair(); } flyway.setValidateOnMigrate(false); flyway.migrate(); } catch (Exception e) { String message = "Error migrating database schema: " + e.getMessage() + " Restart service with options '-repairDatabase true' after the problem" + " has been corrected to resume migration."; logger.error(message); healthCheck.updateStatus(new HealthCheck("migration_failure", HealthCheck.Status.FAIL, message)); try { flywayFailedSemaphore.acquire(); //sleep forever } catch (InterruptedException e1) { } } }