@Override public <T> T getConfig(ConfigKey<T> key) { return getConfig(key, null); }
public static BrooklynProperties filterFor(BrooklynProperties properties, Predicate<? super String> filter) { BrooklynProperties result = BrooklynProperties.Factory.newEmpty(); for (String k: (Collection<String>)properties.keySet()) { if (filter.apply(k)) { result.put(k, properties.get(k)); } } return result; }
public BrooklynProperties build() { BrooklynProperties properties = new BrooklynProperties(); // TODO Could also read from http://brooklyn.io, for up-to-date values? // But might that make unit tests run very badly when developer is offline? addPropertiesFromUrl(properties, defaultLocationMetadataUrl, false); addPropertiesFromFile(properties, globalLocationMetadataFile); addPropertiesFromFile(properties, globalPropertiesFile); if (localPropertiesFile != null) addPropertiesFromFile(properties, localPropertiesFile); properties.addEnvironmentVars(); properties.addSystemProperties(); return properties; } }
/** inserts the value under the given key, if it was not present */ public boolean putIfAbsent(String key, Object value) { if (containsKey(key)) return false; put(key, value); return true; }
/** expects a property already set in scope, whose value is acceptable to {@link #addFromUrl(String)}; * if property not set, does nothing */ public BrooklynProperties addFromUrlProperty(String urlProperty) { String url = (String) get(urlProperty); if (url==null) addFromUrl(url); return this; }
/** * Gets all properties that start with the given {@code fullPrefix}, stripping off * the prefix in the returned map. */ protected Map<String, Object> getMatchingProperties(String fullPrefix, Map<String, ?> properties) { return ConfigUtils.filterForPrefixAndStrip(properties, fullPrefix).asMapWithStringKeys(); }
@Override public synchronized Object put(Object key, Object value) { // ugly way to load them in order // (Properties is a hashtable so loses order otherwise) return BrooklynProperties.this.put(key, value); } };
@Override public BrooklynProperties submap(Predicate<ConfigKey<?>> filter) { BrooklynProperties result = Factory.newEmpty(); for (Object entry: entrySet()) { ConfigKey<?> k = new BasicConfigKey<Object>(Object.class, ""+((Map.Entry)entry).getKey()); if (filter.apply(k)) result.put(((Map.Entry)entry).getKey(), ((Map.Entry)entry).getValue()); } return result; }
public BrooklynProperties addFrom(URL u) { try { return addFrom(u.openStream()); } catch (IOException e) { throw new RuntimeException("Error reading properties from "+u+": "+e, e); } } /**
public static BrooklynProperties newEmpty() { return new BrooklynProperties(); }
public static ManagementContext newManagementContext(Map<?,?> props) { return new LocalManagementContext( BrooklynProperties.Factory.newEmpty().addFromMap(props)); }
/** * Gets all properties that start with {@code "brooklyn.location."+scopeSuffix+"."}, stripping off * the prefix in the returned map. */ protected Map<String, Object> getScopedLocationProperties(String scopeSuffix, Map<String, ?> properties) { checkArgument(!scopeSuffix.startsWith("."), "scopeSuffix \"%s\" should not start with \".\"", scopeSuffix); checkArgument(!scopeSuffix.endsWith("."), "scopeSuffix \"%s\" should not end with \".\"", scopeSuffix); String prefix = String.format("brooklyn.location.%s.", scopeSuffix); return ConfigUtils.filterForPrefixAndStrip(properties, prefix).asMapWithStringKeys(); }
/** like normal map.putAll, except config keys are dereferenced on the way in */ @Override public void putAll(Map vals) { for (Map.Entry<?,?> entry : ((Map<?,?>)vals).entrySet()) { put(entry.getKey(), entry.getValue()); } }
public BrooklynProperties addFrom(File f) { if (!f.exists()) { LOG.warn("Unable to find file '"+f.getAbsolutePath()+"' when loading properties; ignoring"); return this; } else { try { return addFrom(new FileInputStream(f)); } catch (FileNotFoundException e) { throw Throwables.propagate(e); } } } public BrooklynProperties addFrom(URL u) {
@Override public <T> T getConfig(HasConfigKey<T> key) { return getConfig(key.getConfigKey(), null); }
public static BrooklynProperties filterForPrefix(BrooklynProperties properties, String prefix) { BrooklynProperties result = BrooklynProperties.Factory.newEmpty(); for (String k: (Collection<String>)properties.keySet()) { if (k.startsWith(prefix)) { result.put(k, properties.get(k)); } } return result; }
protected Map<String, Object> getNamedLocationProperties(String locationName, Map<String, ?> properties) { checkArgument(!Strings.isNullOrEmpty(locationName), "locationName should not be blank"); String prefix = String.format("brooklyn.location.named.%s.", locationName); return ConfigUtils.filterForPrefixAndStrip(properties, prefix).asMapWithStringKeys(); }
/** prefix generally ends with a full stop */ public static BrooklynProperties filterForPrefixAndStrip(Map<String,?> properties, String prefix) { BrooklynProperties result = BrooklynProperties.Factory.newEmpty(); for (String k: properties.keySet()) { if (k.startsWith(prefix)) { result.put(k.substring(prefix.length()), properties.get(k)); } } return result; }
public static BrooklynProperties loadFromFile(String file) { BrooklynProperties result = BrooklynProperties.Factory.newEmpty(); if (file!=null) result.addFrom(new File(file)); return result; }
@Override public <T> T getConfig(HasConfigKey<T> key, T defaultValue) { return getConfig(key.getConfigKey(), defaultValue); }