private Set<String> stringPropertyNames(Properties properties) { //jdk9以下版本Properties#enumerateStringProperties方法存在性能问题,keys() + get(k) 重复迭代, jdk9之后改为entrySet遍历. Map<String, String> h = new HashMap<>(); for (Map.Entry<Object, Object> e : properties.entrySet()) { Object k = e.getKey(); Object v = e.getValue(); if (k instanceof String && v instanceof String) { h.put((String) k, (String) v); } } return h.keySet(); }
public Props(File... files) throws FileNotFoundException, IOException { this.props = new HashMap<String, String>(); for(int i = files.length - 1; i >= 0; i--) { Properties properties = new Properties(); InputStream input = new BufferedInputStream(new FileInputStream(files[i].getAbsolutePath())); properties.load(input); for(Entry<Object, Object> e: properties.entrySet()) this.props.put((String) e.getKey(), (String) e.getValue()); input.close(); } }
protected synchronized Properties getProps() { if (properties == null) { properties = new Properties(); Map<String, String[]> backup = new ConcurrentHashMap<String, String[]>(updatingResource); loadResources(properties, resources, quietmode); if (overlay != null) { properties.putAll(overlay); for (Entry<Object,Object> item: overlay.entrySet()) { String key = (String)item.getKey(); String[] source = backup.get(key); if(source != null) { updatingResource.put(key, source); } } } } return properties; }
/** * Loads a {@link Map} from an {@link InputStream} assuming strings as keys * and values. * * @param stream the {@link InputStream} containing a * {@link Properties}-like layout of keys and values. * @return the read data as a {@link Map}. * @throws IOException if the {@code stream} throws {@link IOException}. */ public static Map<String,String> load( InputStream stream ) throws IOException { Properties props = new Properties(); props.load( stream ); HashMap<String,String> result = new HashMap<>(); for ( Map.Entry<Object,Object> entry : props.entrySet() ) { // Properties does not trim whitespace from the right side of values result.put( (String) entry.getKey(), ( (String) entry.getValue() ).trim() ); } return result; }
protected Map<String, String> getAttributes(final TarArchiveInputStream stream) throws IOException { final Properties props = new Properties(); props.loadFromXML(new NonCloseableInputStream(stream)); final Map<String, String> result = new HashMap<>(); for (final Entry<Object, Object> entry : props.entrySet()) { final Object keyObject = entry.getKey(); final Object valueObject = entry.getValue(); if (!(keyObject instanceof String)) { throw new IOException("Flow file attributes object contains key of type " + keyObject.getClass().getCanonicalName() + " but expected java.lang.String"); } else if (!(keyObject instanceof String)) { throw new IOException("Flow file attributes object contains value of type " + keyObject.getClass().getCanonicalName() + " but expected java.lang.String"); } final String key = (String) keyObject; final String value = (String) valueObject; result.put(key, value); } return result; }
private static Properties cloneProperties(Properties properties) { Properties clone = new Properties(); for (Map.Entry<Object, Object> entry : properties.entrySet()) { clone.put(entry.getKey(), entry.getValue()); } return clone; }
public static void init(final File mimeFile) { if (mimeTable.isEmpty()) { // load the mime table BufferedInputStream mimeTableInputStream = null; try { mimeTableInputStream = new BufferedInputStream(new FileInputStream(mimeFile)); mimeTable.load(mimeTableInputStream); } catch (final Exception e) { } finally { if (mimeTableInputStream != null) try { mimeTableInputStream.close(); } catch (final Exception e1) {} } } for (Entry<Object, Object> entry: mimeTable.entrySet()) { String ext = (String) entry.getKey(); String mime = (String) entry.getValue(); if (mime.startsWith("text/")) textExtSet.add(ext.toLowerCase()); if (mime.startsWith("audio/")) audioExtSet.add(ext.toLowerCase()); if (mime.startsWith("video/")) videoExtSet.add(ext.toLowerCase()); if (mime.startsWith("application/")) appsExtSet.add(ext.toLowerCase()); } }
private void init(InputStream is) { if (is == null) return; Properties props = new Properties(); try { props.load(is); } catch (IOException e) { logger.error("", e); } finally { IOUtils.closeQuietly(is); } for (Entry<Object, Object> kv : props.entrySet()) { String key = (String) kv.getKey(); String value = (String) kv.getValue(); if (key.equals(value)) continue; // no change if (value.contains(key)) throw new IllegalStateException("New key '" + value + "' contains old key '" + key + "' causes trouble to repeated find & replace"); if (value.endsWith(".")) old2newPrefix.put(key, value); else old2new.put(key, value); } }
/** * Converts a Properties object to a Map<String, String>, calling {@link #toString} to ensure all keys and values * are Strings. */ public static Map<String, String> propsToStringMap(Properties props) { Map<String, String> result = new HashMap<>(); for (Map.Entry<Object, Object> entry : props.entrySet()) result.put(entry.getKey().toString(), entry.getValue().toString()); return result; }
protected synchronized Properties getProps() { if (properties == null) { properties = new Properties(); Map<String, String[]> backup = updatingResource != null ? new ConcurrentHashMap<String, String[]>(updatingResource) : null; loadResources(properties, resources, quietmode); if (overlay != null) { properties.putAll(overlay); if (backup != null) { for (Map.Entry<Object, Object> item : overlay.entrySet()) { String key = (String) item.getKey(); String[] source = backup.get(key); if (source != null) { updatingResource.put(key, source); } } } } } return properties; }
@Override public void write(Writer out) throws IOException { Properties cfg = new Properties(); cfg.load( new StringReader( qv.toString())); List<String> servers = new ArrayList<String>(); for (Entry<Object, Object> entry : cfg.entrySet()) { String key = entry.getKey().toString().trim(); if ( !needKeepVersion && key.startsWith("version")) continue; String value = entry.getValue().toString().trim(); servers.add(key .concat("=") .concat(value)); } Collections.sort(servers); out.write(StringUtils.joinStrings(servers, "\n")); } });
/** * Removes the instance prefix in the properties. This is to make the configuration * parsing logic backward compatible with old configuration format. */ private void removeInstancePrefix() { Properties newProperties = new Properties(); for (Map.Entry<Object, Object> entry : mProperties.entrySet()) { String key = entry.getKey().toString(); if (key.startsWith("*") || key.startsWith("worker") || key.startsWith("master")) { String newKey = key.substring(key.indexOf('.') + 1); newProperties.put(newKey, entry.getValue()); } else { newProperties.put(key, entry.getValue()); } } mProperties = newProperties; }
public static Map<String, String> readConfig(Path data) throws IOException { File conf_dir = new File("conf"); Properties prop = new Properties(); prop.load(new FileInputStream(new File(conf_dir, "config.properties"))); Map<String, String> config = new HashMap<>(); for (Map.Entry<Object, Object> entry: prop.entrySet()) config.put((String) entry.getKey(), (String) entry.getValue()); Path settings_dir = data.resolve("settings"); settings_dir.toFile().mkdirs(); OS.protectPath(settings_dir); File customized_config = new File(settings_dir.toFile(), "customized_config.properties"); if (!customized_config.exists()) { BufferedWriter w = new BufferedWriter(new FileWriter(customized_config)); w.write("# This file can be used to customize the configuration file conf/config.properties\n"); w.close(); } Properties customized_config_props = new Properties(); customized_config_props.load(new FileInputStream(customized_config)); for (Map.Entry<Object, Object> entry: customized_config_props.entrySet()) config.put((String) entry.getKey(), (String) entry.getValue()); return config; }
public Map<String, ?> get () { Map<String, Object> map = new HashMap<String, Object>(); for (Entry<Object, Object> val : properties.entrySet()) { if (val.getValue() instanceof Boolean) map.put((String)val.getKey(), (Boolean)Boolean.parseBoolean((String)val.getValue())); if (val.getValue() instanceof Integer) map.put((String)val.getKey(), (Integer)Integer.parseInt((String)val.getValue())); if (val.getValue() instanceof Long) map.put((String)val.getKey(), (Long)Long.parseLong((String)val.getValue())); if (val.getValue() instanceof String) map.put((String)val.getKey(), (String)val.getValue()); if (val.getValue() instanceof Float) map.put((String)val.getKey(), (Float)Float.parseFloat((String)val.getValue())); } return map; }
/** * Uses regex to parse every original property key to a prefix and a suffix. Creates sub * properties that are grouped by the prefix. * * @param prop the original properties * @param regex prefix and suffix pattern * @return a {@code Map} from the prefix to its properties */ public static Map<String, Properties> subProperties(Properties prop, String regex) { Map<String, Properties> subProperties = new HashMap<>(); Pattern pattern = Pattern.compile(regex); for (Map.Entry<Object, Object> entry : prop.entrySet()) { Matcher m = pattern.matcher(entry.getKey().toString()); if (m.find()) { String prefix = m.group(1); String suffix = m.group(2); if (!subProperties.containsKey(prefix)) { subProperties.put(prefix, new Properties()); } subProperties.get(prefix).put(suffix, entry.getValue()); } } return subProperties; }
private static void setSystemProperties(final File sysprops) throws IOException { try (InputStream in = new FileInputStream(sysprops)) { Properties properties = new Properties(); properties.load(in); for (Entry<Object, Object> prop : properties.entrySet()) { String name = prop.getKey().toString(); String value = prop.getValue().toString(); String existing = System.getProperty(name); if (!value.equals(existing)) { // set property System.setProperty(name, value); } } } }
private static Properties constructDefaultKafkaConsumerProperties(String brokers, String consumerGroup, Properties properties) { Properties props = new Properties(); if (properties != null) { for (Map.Entry entry : properties.entrySet()) { props.put(entry.getKey(), entry.getValue()); } } props.put("bootstrap.servers", brokers); props.put("key.deserializer", StringDeserializer.class.getName()); props.put("value.deserializer", StringDeserializer.class.getName()); props.put("group.id", consumerGroup); props.put("enable.auto.commit", "false"); return props; }