下面列出了com.google.common.collect.Maps#fromProperties ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Properties mergeConnectionProperties(URI uri, Properties driverProperties)
throws SQLException
{
Map<String, String> defaults = ConnectionProperties.getDefaults();
Map<String, String> urlProperties = parseParameters(uri.getQuery());
Map<String, String> suppliedProperties = Maps.fromProperties(driverProperties);
for (String key : urlProperties.keySet()) {
if (suppliedProperties.containsKey(key)) {
throw new SQLException(format("Connection property '%s' is both in the URL and an argument", key));
}
}
Properties result = new Properties();
setProperties(result, defaults);
setProperties(result, urlProperties);
setProperties(result, suppliedProperties);
return result;
}
private void fetchProperties() {
LOG.info("Fetching build properties from " + resourcePath);
InputStream in = ClassLoader.getSystemResourceAsStream(resourcePath);
if (in == null) {
LOG.warn("Failed to fetch build properties from " + resourcePath);
return;
}
try {
Properties buildProperties = new Properties();
buildProperties.load(in);
properties = Maps.fromProperties(buildProperties);
} catch (Exception e) {
LOG.warn("Failed to load properties file " + resourcePath, e);
}
}
/**
* Convert properties to map
*/
@Test
public void map_of_properties() {
Properties properties = new Properties();
properties.put("leveluplunch.java.examples",
"http://www.leveluplunch.com/java/examples/");
properties.put("leveluplunch.java.exercises",
"http://www.leveluplunch.com/java/exercises/");
properties.put("leveluplunch.java.tutorials",
"http://www.leveluplunch.com/java/tutorials/");
Map<String, String> mapOfProperties = Maps.fromProperties(properties);
logger.info(mapOfProperties);
assertThat(mapOfProperties, hasKey("leveluplunch.java.examples"));
}
@Test
public void create_map_from_properties_guava() {
Properties properties = new Properties();
properties.put("database.username", "yourname");
properties.put("database.password", "encrypted_password");
properties.put("database.driver", "com.mysql.jdbc.Driver");
properties.put("database.url",
"jdbc:mysql://localhost:3306/sakila?profileSQL=true");
Map<String, String> mapOfProperties = Maps.fromProperties(properties);
logger.info(mapOfProperties);
assertThat(
mapOfProperties.keySet(),
containsInAnyOrder("database.username", "database.password",
"database.driver", "database.url"));
}
/**
* @param includeResources
* @param excludeResources
* @param reload
*/
static void init(List<String> includeResources, List<String> excludeResources, boolean reload) {
ConfigKit.includeResources = includeResources;
ConfigKit.excludeResources = excludeResources;
ConfigKit.reload = reload;
for (final String resource : includeResources) {
LOG.debug("include :" + resource);
File[] propertiesFiles = new File(PathKit.getRootClassPath()).listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return Pattern.compile(resource).matcher(pathname.getName()).matches();
}
});
for (File file : propertiesFiles) {
String fileName = file.getName();
LOG.debug("fileName:" + fileName);
boolean excluded = false;
for (String exclude : excludeResources) {
if (Pattern.compile(exclude).matcher(file.getName()).matches()) {
excluded = true;
}
}
if (excluded) {
continue;
}
lastmodifies.put(fileName, new File(fileName).lastModified());
Map<String, String> mapData = Maps.fromProperties(new Prop(fileName).getProperties());
map.putAll(mapData);
}
}
LOG.debug("map" + map);
LOG.info("config init success!");
}
private Map<String, String> loadProperties(final File file)
throws Exception {
Preconditions.checkNotNull(file, "file is null");
final Properties properties = new Properties();
try (FileInputStream in = new FileInputStream(file)) {
properties.load(in);
}
return Maps.fromProperties(properties);
}
private static ImmutableMap<String, String> loadScopeDescriptions(String fileName) {
try {
Properties properties = new Properties();
URL resourceFile = Resources.getResource(DiscoveryGenerator.class, fileName);
InputStream inputStream = resourceFile.openStream();
properties.load(inputStream);
inputStream.close();
return Maps.fromProperties(properties);
} catch (IOException e) {
throw new IllegalStateException("Cannot load scope descriptions from " + fileName, e);
}
}
@Path("v1/versionInfo")
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
tags = "Service Configuration APIs",
value = "The build version information for deployed services")
public Map<String, String> getVersionInfo() {
if (versionInfo == null) {
// load version info created by maven build process if it is available
try {
final InputStream infoStream = getClass().getClassLoader().getResourceAsStream("git.properties");
if (infoStream != null) {
final Properties p = new Properties();
p.load(infoStream);
// add commit URL to make it easier to cut-and-paste into a browser
final String remoteOriginUrl = p.getProperty("git.remote.origin.url");
final String commitId = p.getProperty("git.commit.id");
if ((remoteOriginUrl != null) && (commitId != null)) {
p.setProperty("git.commit.url", String.format("%s/commit/%s", remoteOriginUrl, commitId));
}
versionInfo = Maps.fromProperties(p);
LOG.info("getVersionInfo: loaded version info");
}
} catch (final Throwable t) {
LOG.warn("getVersionInfo: failed to load version info", t);
}
}
return versionInfo;
}
private Map<String, String> loadProperties(String file) {
Properties properties = new Properties();
try {
properties.load(getClass().getResourceAsStream(file));
} catch (IOException e) {
e.printStackTrace();
}
return Maps.fromProperties(properties);
}
private static Map<String, String> readProperties(File file) {
Properties properties = new Properties();
try (InputStream is = new FileInputStream(file)) {
properties.load(is);
} catch (IOException e) {
// too bad
}
return Maps.fromProperties(properties);
}
public LegacyClientDelegate(String host, int port, Properties clientParams) {
super(new DummyServiceTracker(NetUtils.createSocketAddr(host, port)), null,
new KeyValueSet(clientParams == null ? new HashMap<String, String>() : Maps.fromProperties(clientParams)));
queryClient = new QueryClientImpl(this);
}
public LegacyClientDelegate(ServiceDiscovery discovery, Properties clientParams) {
super(new DelegateServiceTracker(discovery), null,
new KeyValueSet(clientParams == null ? new HashMap<String, String>() : Maps.fromProperties(clientParams)));
queryClient = new QueryClientImpl(this);
}
private Map<String, String> buildSystemPropertiesList() {
return Maps.fromProperties(System.getProperties());
}
TableDataInserter(String databaseName, String tableName, HiveConf conf) {
this.databaseName = databaseName;
this.tableName = tableName;
config = Maps.fromProperties(conf.getAllProperties());
}
/**
* Properties for the node. Template values (if set) become property defaults.
*
* @return Map<String,String>
*/
private Map<String, String> getProperties()
{
return Maps.fromProperties(properties);
}
/**
* Properties for the node. Template values (if set) become property defaults.
*
* @return Map<String,String>
*/
private Map<String, String> getProperties()
{
return Maps.fromProperties(properties);
}