public static Config getInstance() { if (INSTANCE == null) { INSTANCE = new PropertiesConfigLoader().loadConfig(); } return INSTANCE; }
public static String getProperty(String property) { return CONFIG.getProperty(property); }
public Replacer(Properties replacements) { addReplacements(replacements); }
public Config loadConfig() { InputStream input = getConfigFromProperty(); if (input == null) { input = getConfigFromHome(); input = getDefaultConfig(); Replacer replacer = new Replacer(); replacer.addReplacements(getProperties()); replacer.addReplacements(properties); return new PropertiesConfig(properties, replacer);
public String replace(String property) { if (property == null) { return null; } StringBuilder buffer = new StringBuilder(property); int prefixStart = property.indexOf(placeholderPrefix); while (prefixStart != -1) { int replacementStart = prefixStart + placeholderPrefix.length(); int suffixStart = buffer.indexOf(placeholderSuffix, replacementStart); if (suffixStart != -1) { String placeholder = buffer.substring(replacementStart, suffixStart); String replacement = getReplacement(placeholder); if (replacement == null) { if (isIgnoreUnknownPlaceholders()) { prefixStart = buffer.indexOf(placeholderPrefix, suffixStart); } else { throw new IllegalArgumentException( format("Can't find replacement for %s placeholder", placeholder)); } } else { buffer.replace(prefixStart, suffixStart + placeholderSuffix.length(), replacement); prefixStart = buffer.indexOf(placeholderPrefix, prefixStart + replacement.length()); } } else { prefixStart = -1; } } return buffer.toString(); }
public void boot(String[] arguments) throws Exception { config = Config.getInstance(); Properties properties = config.getProperties(); if (logger.isDebugEnabled()) { logger.debug(format("Exposing bootstrap config as system properties %s", properties)); } for (Map.Entry<Object, Object> entry : properties.entrySet()) { setProperty((String) entry.getKey(), (String) entry.getValue()); } classLoader = createClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); Bootable bootable = createBootable(); bootable.boot(arguments); }
@Override public String getProperty(String property) { return replacer.replace(properties.getProperty(property)); }
private static InputStream getConfigFromHome() { File path = new File(new File(getHome(), CONFIG_DIR), DEFAULT_CONFIG); InputStream stream; try { stream = new FileInputStream(path); } catch (Throwable t) { stream = null; } if (stream != null) { if (logger.isDebugEnabled()) { logger.debug(format("Config found at %s", path)); } } else { if (logger.isInfoEnabled()) { logger.info(format("Config can't be found at %s", path)); } } return stream; }
public static String getProperty(String property, String defaultValue) { return CONFIG.getProperty(property, defaultValue); } }
@Override public String getProperty(String property, String defaultValue) { return replacer.replace(properties.getProperty(property, defaultValue)); }
protected Bootable createBootable() throws Exception { String bootableName = getConfig().getProperty(BOOTABLE_CLASS, DEFAULT_BOOTABLE_CLASS); if (logger.isDebugEnabled()) { logger.debug(format("Booting %s", bootableName)); } Class<? extends Bootable> bootableType = (Class<? extends Bootable>) getClassLoader().loadClass(bootableName); Constructor<? extends Bootable> constructor = bootableType.getConstructor(); return constructor.newInstance(); }
@Override public Properties getProperties() { Properties target = new Properties(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { target.setProperty((String) entry.getKey(), replacer.replace((String) entry.getValue())); } return target; } }
protected ClassLoader createClassLoader() throws IOException { if (logger.isDebugEnabled()) { logger.debug("Creating class loader"); } String classPath = getConfig().getProperty(CLASSPATH); if (classPath == null || classPath.equals("")) { return getClass().getClassLoader(); } return createClassPathLoader(classPath, currentThread().getContextClassLoader()); }