List of usage examples for java.util Properties entrySet
@Override
public Set<Map.Entry<Object, Object>> entrySet()
From source file:com.alibaba.wasp.zookeeper.ZKConfig.java
/** * Parse ZooKeeper's zoo.cfg, injecting Wasp Configuration variables in. * This method is used for testing so we can pass our own InputStream. * @param conf WaspConfiguration to use for injecting variables. * @param inputStream InputStream to read from. * @return Properties parsed from config stream with variables substituted. * @throws java.io.IOException if anything goes wrong parsing config *//*from w w w . j a v a2 s. co m*/ public static Properties parseZooCfg(Configuration conf, InputStream inputStream) throws IOException { Properties properties = new Properties(); try { properties.load(inputStream); } catch (IOException e) { final String msg = "fail to read properties from " + FConstants.ZOOKEEPER_CONFIG_NAME; LOG.fatal(msg); throw new IOException(msg, e); } for (Entry<Object, Object> entry : properties.entrySet()) { String value = entry.getValue().toString().trim(); String key = entry.getKey().toString().trim(); StringBuilder newValue = new StringBuilder(); int varStart = value.indexOf(VARIABLE_START); int varEnd = 0; while (varStart != -1) { varEnd = value.indexOf(VARIABLE_END, varStart); if (varEnd == -1) { String msg = "variable at " + varStart + " has no end marker"; LOG.fatal(msg); throw new IOException(msg); } String variable = value.substring(varStart + VARIABLE_START_LENGTH, varEnd); String substituteValue = System.getProperty(variable); if (substituteValue == null) { substituteValue = conf.get(variable); } if (substituteValue == null) { String msg = "variable " + variable + " not set in system property " + "or wasp configs"; LOG.fatal(msg); throw new IOException(msg); } newValue.append(substituteValue); varEnd += VARIABLE_END_LENGTH; varStart = value.indexOf(VARIABLE_START, varEnd); } // Special case for 'wasp.cluster.distributed' property being 'true' if (key.startsWith("server.")) { boolean mode = conf.getBoolean(FConstants.CLUSTER_DISTRIBUTED, FConstants.DEFAULT_CLUSTER_DISTRIBUTED); if (mode == FConstants.CLUSTER_IS_DISTRIBUTED && value.startsWith(FConstants.LOCALHOST)) { String msg = "The server in zoo.cfg cannot be set to localhost " + "in a fully-distributed setup because it won't be reachable. " + "See \"Getting Started\" for more information."; LOG.fatal(msg); throw new IOException(msg); } } newValue.append(value.substring(varEnd)); properties.setProperty(key, newValue.toString()); } return properties; }
From source file:de.alpharogroup.resourcebundle.properties.PropertiesFileExtensions.java
/** * Gets the redundant keys in properties files from the given directory. The search is recursive * and finds all properties files. The result is a map with key the properties file and the * found redundant keys as a map.//w w w .j a v a 2 s.c o m * * @param dirToSearch * the dir to search * @return the redundant keys * @throws IOException * Signals that an I/O exception has occurred. */ public static Map<File, Map<String, List<String>>> getRedundantKeys(final File dirToSearch) throws IOException { final List<File> foundFiles = FileSearchExtensions.findAllFiles(dirToSearch, FileSearchExtensions.getSearchFilePattern("properties")); final Map<String, List<String>> linesMap = new LinkedHashMap<>(); final Map<File, Map<String, List<String>>> fileMap = new LinkedHashMap<>(); for (final File file : foundFiles) { final List<String> lines = PropertiesFileExtensions.removeComments(file); final Properties p = PropertiesFileExtensions.loadProperties(file); for (final Map.Entry<Object, Object> entry : p.entrySet()) { final String key = ((String) entry.getKey()).trim(); for (final String line : lines) { if (line.startsWith(key)) { final char nextChar = line.charAt(key.length()); // char[] anc = {nextChar}; // String nc = new String(anc); if (nextChar == '.') { continue; } else if (nextChar == '=' || nextChar == ':' || nextChar == ' ') { if (!linesMap.containsKey(key)) { final List<String> dl = new ArrayList<>(); dl.add(line); linesMap.put(key, dl); } else { final List<String> dl = linesMap.get(key); dl.add(line); linesMap.put(key, dl); } } else { throw new RuntimeException("nextChar is '" + nextChar + "'"); } } } } final Map<String, List<String>> duplicateKeys = new LinkedHashMap<>(); for (final Map.Entry<String, List<String>> entry : linesMap.entrySet()) { if (1 < entry.getValue().size()) { duplicateKeys.put(entry.getKey(), entry.getValue()); } } if (0 < duplicateKeys.size()) { fileMap.put(file, duplicateKeys); } } return fileMap; }
From source file:com.alibaba.wasp.zookeeper.ZKConfig.java
/** * Return the ZK Quorum servers string given zk properties returned by * makeZKProps// w w w . ja v a2s . co m * @param properties * @return Quorum servers String */ public static String getZKQuorumServersString(Properties properties) { String clientPort = null; List<String> servers = new ArrayList<String>(); // The clientPort option may come after the server.X hosts, so we need to // grab everything and then create the final host:port comma separated list. boolean anyValid = false; for (Entry<Object, Object> property : properties.entrySet()) { String key = property.getKey().toString().trim(); String value = property.getValue().toString().trim(); if (key.equals("clientPort")) { clientPort = value; } else if (key.startsWith("server.")) { String host = value.substring(0, value.indexOf(':')); servers.add(host); try { //noinspection ResultOfMethodCallIgnored InetAddress.getByName(host); anyValid = true; } catch (UnknownHostException e) { LOG.warn(StringUtils.stringifyException(e)); } } } if (!anyValid) { LOG.error("no valid quorum servers found in " + FConstants.ZOOKEEPER_CONFIG_NAME); return null; } if (clientPort == null) { LOG.error("no clientPort found in " + FConstants.ZOOKEEPER_CONFIG_NAME); return null; } if (servers.isEmpty()) { LOG.fatal("No server.X lines found in conf/zoo.cfg. Wasp must have a " + "ZooKeeper cluster configured for its operation."); return null; } StringBuilder hostPortBuilder = new StringBuilder(); for (int i = 0; i < servers.size(); ++i) { String host = servers.get(i); if (i > 0) { hostPortBuilder.append(','); } hostPortBuilder.append(host); hostPortBuilder.append(':'); hostPortBuilder.append(clientPort); } return hostPortBuilder.toString(); }
From source file:com.espertech.esper.core.EPServicesContextFactoryDefault.java
private static Map<String, Object> createPropertyTypes(Properties properties) { Map<String, Object> propertyTypes = new HashMap<String, Object>(); for (Map.Entry entry : properties.entrySet()) { String property = (String) entry.getKey(); String className = (String) entry.getValue(); Class clazz = JavaClassHelper.getClassForSimpleName(className); if (clazz == null) { throw new ConfigurationException("The type '" + className + "' is not a recognized type"); }/*from w w w . j a v a2 s.co m*/ propertyTypes.put(property, clazz); } return propertyTypes; }
From source file:org.apache.falcon.lifecycle.engine.oozie.utils.OozieBuilderUtils.java
public static CONFIGURATION getCoordinatorConfig(Properties props) { CONFIGURATION conf = new CONFIGURATION(); for (Map.Entry<Object, Object> prop : props.entrySet()) { CONFIGURATION.Property confProp = new CONFIGURATION.Property(); confProp.setName((String) prop.getKey()); confProp.setValue((String) prop.getValue()); conf.getProperty().add(confProp); }//w w w .j ava 2s .c o m return conf; }
From source file:org.apache.hadoop.hbase.zookeeper.ZKConfig.java
/** * Parse ZooKeeper's zoo.cfg, injecting HBase Configuration variables in. * This method is used for testing so we can pass our own InputStream. * @param conf HBaseConfiguration to use for injecting variables. * @param inputStream InputStream to read from. * @return Properties parsed from config stream with variables substituted. * @throws IOException if anything goes wrong parsing config * @deprecated in 0.96 onwards. HBase will no longer rely on zoo.cfg * availability./*from ww w . j a v a2 s .co m*/ */ @Deprecated public static Properties parseZooCfg(Configuration conf, InputStream inputStream) throws IOException { Properties properties = new Properties(); try { properties.load(inputStream); } catch (IOException e) { final String msg = "fail to read properties from " + HConstants.ZOOKEEPER_CONFIG_NAME; LOG.fatal(msg); throw new IOException(msg, e); } for (Entry<Object, Object> entry : properties.entrySet()) { String value = entry.getValue().toString().trim(); String key = entry.getKey().toString().trim(); StringBuilder newValue = new StringBuilder(); int varStart = value.indexOf(VARIABLE_START); int varEnd = 0; while (varStart != -1) { varEnd = value.indexOf(VARIABLE_END, varStart); if (varEnd == -1) { String msg = "variable at " + varStart + " has no end marker"; LOG.fatal(msg); throw new IOException(msg); } String variable = value.substring(varStart + VARIABLE_START_LENGTH, varEnd); String substituteValue = System.getProperty(variable); if (substituteValue == null) { substituteValue = conf.get(variable); } if (substituteValue == null) { String msg = "variable " + variable + " not set in system property " + "or hbase configs"; LOG.fatal(msg); throw new IOException(msg); } newValue.append(substituteValue); varEnd += VARIABLE_END_LENGTH; varStart = value.indexOf(VARIABLE_START, varEnd); } // Special case for 'hbase.cluster.distributed' property being 'true' if (key.startsWith("server.")) { boolean mode = conf.getBoolean(HConstants.CLUSTER_DISTRIBUTED, HConstants.DEFAULT_CLUSTER_DISTRIBUTED); if (mode == HConstants.CLUSTER_IS_DISTRIBUTED && value.startsWith(HConstants.LOCALHOST)) { String msg = "The server in zoo.cfg cannot be set to localhost " + "in a fully-distributed setup because it won't be reachable. " + "See \"Getting Started\" for more information."; LOG.fatal(msg); throw new IOException(msg); } } newValue.append(value.substring(varEnd)); properties.setProperty(key, newValue.toString()); } return properties; }
From source file:org.apache.hadoop.hbase.zookeeper.ZKConfig.java
/** * Return the ZK Quorum servers string given zk properties returned by * makeZKProps/* w w w . j a v a 2 s.c o m*/ * @param properties * @return Quorum servers String */ public static String getZKQuorumServersString(Properties properties) { String clientPort = null; List<String> servers = new ArrayList<String>(); // The clientPort option may come after the server.X hosts, so we need to // grab everything and then create the final host:port comma separated list. boolean anyValid = false; for (Entry<Object, Object> property : properties.entrySet()) { String key = property.getKey().toString().trim(); String value = property.getValue().toString().trim(); if (key.equals("clientPort")) { clientPort = value; } else if (key.startsWith("server.")) { String host = value.substring(0, value.indexOf(':')); servers.add(host); try { //noinspection ResultOfMethodCallIgnored InetAddress.getByName(host); anyValid = true; } catch (UnknownHostException e) { LOG.warn(StringUtils.stringifyException(e)); } } } if (!anyValid) { LOG.error("no valid quorum servers found in " + HConstants.ZOOKEEPER_CONFIG_NAME); return null; } if (clientPort == null) { LOG.error("no clientPort found in " + HConstants.ZOOKEEPER_CONFIG_NAME); return null; } if (servers.isEmpty()) { LOG.fatal("No servers were found in provided ZooKeeper configuration. " + "HBase must have a ZooKeeper cluster configured for its " + "operation. Ensure that you've configured '" + HConstants.ZOOKEEPER_QUORUM + "' properly."); return null; } StringBuilder hostPortBuilder = new StringBuilder(); for (int i = 0; i < servers.size(); ++i) { String host = servers.get(i); if (i > 0) { hostPortBuilder.append(','); } hostPortBuilder.append(host); hostPortBuilder.append(':'); hostPortBuilder.append(clientPort); } return hostPortBuilder.toString(); }
From source file:gobblin.util.ConfigUtils.java
/** * Finds a list of properties whose keys are complete prefix of other keys. This function is * meant to be used during conversion from Properties to typesafe Config as the latter does not * support this scenario./*from w w w . j a v a2 s .c o m*/ * @param properties the Properties collection to inspect * @param keyPrefix an optional key prefix which limits which properties are inspected. * */ public static Set<String> findFullPrefixKeys(Properties properties, Optional<String> keyPrefix) { TreeSet<String> propNames = new TreeSet<>(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { String entryKey = entry.getKey().toString(); if (StringUtils.startsWith(entryKey, keyPrefix.or(StringUtils.EMPTY))) { propNames.add(entryKey); } } Set<String> result = new HashSet<>(); String lastKey = null; Iterator<String> sortedKeysIter = propNames.iterator(); while (sortedKeysIter.hasNext()) { String propName = sortedKeysIter.next(); if (null != lastKey && propName.startsWith(lastKey + ".")) { result.add(lastKey); } lastKey = propName; } return result; }
From source file:org.apache.metron.integration.components.FluxTopologyComponent.java
private static TopologyDef loadYaml(String topologyName, File yamlFile, File templateFile, Properties properties) throws IOException { File tmpFile = File.createTempFile(topologyName, "props"); tmpFile.deleteOnExit();//from www . ja v a 2 s . c o m if (templateFile != null) { try (FileWriter propWriter = new FileWriter(tmpFile)) { String templateContents = FileUtils.readFileToString(templateFile); for (Map.Entry prop : properties.entrySet()) { String replacePattern = String.format("{{%s}}", prop.getKey()); templateContents = templateContents.replaceAll(Pattern.quote(replacePattern), (String) prop.getValue()); } propWriter.write(templateContents); propWriter.flush(); return FluxParser.parseFile(yamlFile.getAbsolutePath(), false, true, tmpFile.getAbsolutePath(), false); } } else { try (FileWriter propWriter = new FileWriter(tmpFile)) { properties.store(propWriter, topologyName + " properties"); return FluxParser.parseFile(yamlFile.getAbsolutePath(), false, true, tmpFile.getAbsolutePath(), false); } } }
From source file:org.apache.hadoop.hbase.zookeeper.HQuorumPeer.java
/** * Parse ZooKeeper's zoo.cfg, injecting HBase Configuration variables in. * This method is used for testing so we can pass our own InputStream. * @param conf HBaseConfiguration to use for injecting variables. * @param inputStream InputStream to read from. * @return Properties parsed from config stream with variables substituted. * @throws IOException if anything goes wrong parsing config *///from www. j a va 2 s . c o m public static Properties parseZooCfg(Configuration conf, InputStream inputStream) throws IOException { Properties properties = new Properties(); try { properties.load(inputStream); } catch (IOException e) { final String msg = "fail to read properties from " + HConstants.ZOOKEEPER_CONFIG_NAME; LOG.fatal(msg); throw new IOException(msg, e); } for (Entry<Object, Object> entry : properties.entrySet()) { String value = entry.getValue().toString().trim(); String key = entry.getKey().toString().trim(); StringBuilder newValue = new StringBuilder(); int varStart = value.indexOf(VARIABLE_START); int varEnd = 0; while (varStart != -1) { varEnd = value.indexOf(VARIABLE_END, varStart); if (varEnd == -1) { String msg = "variable at " + varStart + " has no end marker"; LOG.fatal(msg); throw new IOException(msg); } String variable = value.substring(varStart + VARIABLE_START_LENGTH, varEnd); String substituteValue = System.getProperty(variable); if (substituteValue == null) { substituteValue = conf.get(variable); } if (substituteValue == null) { String msg = "variable " + variable + " not set in system property " + "or hbase configs"; LOG.fatal(msg); throw new IOException(msg); } newValue.append(substituteValue); varEnd += VARIABLE_END_LENGTH; varStart = value.indexOf(VARIABLE_START, varEnd); } // Special case for 'hbase.cluster.distributed' property being 'true' if (key.startsWith("server.")) { if (conf.get(HConstants.CLUSTER_DISTRIBUTED).equals(HConstants.CLUSTER_IS_DISTRIBUTED) && value.startsWith("localhost")) { String msg = "The server in zoo.cfg cannot be set to localhost " + "in a fully-distributed setup because it won't be reachable. " + "See \"Getting Started\" for more information."; LOG.fatal(msg); throw new IOException(msg); } } newValue.append(value.substring(varEnd)); properties.setProperty(key, newValue.toString()); } return properties; }