Creating Objects Using Guava from Property Files

In our applications, we use a lot of property files. From a few months I began to study Guava, and I really liked it.

What is the best way to create a Map<String, Datasource> ?

The properties file format is not strict. Can it be changed if it can be better expressed in a different format?

Sample Properties File:

 datasource1.url=jdbc:mysql://192.168.11.46/db1 datasource1.password=password datasource1.user=root datasource2.url=jdbc:mysql://192.168.11.45/db2 datasource2.password=password datasource2.user=root 
+6
source share
3 answers

The property class is a subclass of HashTable, which in turn implements Map.

You load it as usual with

 Properties properties = new Properties(); try { properties.load(new FileInputStream("filename.properties")); } catch (IOException e) { } 

edit: Ok to convert it to Map <String, Datasource>;)

 //First convert properties to Map<String, String> Map<String, String> m = Maps.fromProperties(properties); //Sort them so that password < url < user for each datasource and dataSource1.* < dataSource2.*. In your case default string ordering is ok so we can take a normal treemap Map<String, String> sorted = Maps.newTreeMap(); sorted.putAll(m); //Create Multimap<String, List<String>> mapping datasourcename->[password,url, user ] Function<Map.Entry<String, String>, String> propToList = new Function<String, Integer>() { @Override public String apply(Map.Entry<String, String> entry) { return entry.getKey().split("\\.")[0]; } }; Multimap<Integer, String> nameToParamMap = Multimaps.index(m.entrySet(), propToList); //Convert it to map Map<String, Collection<String>> mm = nameToParamMap.asMap(); //Transform it to Map<String, Datasource> Map<String, Datasource> mSD = Maps.transformEntries(mm, new EntryTransformer<String, Collection<String>, DataSource>() { public DataSource transformEntry(String key, Collection<String> value) { // Create your datasource. You know by now that Collection<String> is actually a list so you can assume elements are in order: [password, url, user] return new Datasource(.....) } }; //Copy transformed map so it no longer a view Map<String, Datasource> finalMap = Maps.newHashMap(mSD); 

This is probably an easier way, but this should work :)

However, you're better off with json or xml. You can also load properties of different data sources from different files.

edit2: with less guava, more java:

 //Sort them so that password < url < user for each datasource and dataSource1.* < dataSource2.*. In your case default string ordering is ok so we can take a normal SortedSet SortedSet <String> sorted = new SortedSet<String>(); sorted.putAll(m.keySet); //Divide keys into lists of 3 Iterable<List<String>> keyLists = Iterables.partition(sorted.keySet(), 3); Map<String, Datasource> m = new HashMap<String, Datasource>(); for (keyList : keyLists) { //Contains datasourcex.password, datasroucex.url, datasourcex.user String[] params = keyList.toArray(new String[keyList.size()]); String password = properties.get(params[0]); String url = properties.get(params[1]); String user = properties.get(params[2]); m.put(params[0].split("\\.")[0], new DataSource(....) } 
+5
source

The easiest way is probably to use JSON and not a properties file for this:

  {
   "datasources": [
     {
       "name": "datasource1",
       "url": "jdbc: mysql: //192.168.11.46/db1",
       "user": "root",
       "password": "password"
     },
     {
       "name": "datasource2",
       "url": "jdbc: mysql: //192.168.11.46/db2",
       "user": "root",
       "password": "password"
     }  
   ]
 } 

Then you can simply use a library such as Gson to convert it to objects:

 public class DataSources { private List<DataSourceInfo> dataSources; public Map<String, DataSource> getDataSources() { // create the map } } public class DataSourceInfo { private String name; private String url; private String user; private String password; // constructor, getters } 

Then to get the card:

 Gson gson = new Gson(); Map<String, DataSource> dataSources = gson.fromJson(/* file or stream here */, DataSources.class).getDataSources(); 
+6
source

If the file you use for configuration is not strict, you can use the XML file to store the patch.

Definition example:

 <resources> <configuration> <datasrouce> <connection name="" url="" password="" user=""/> <connection name="" url="" password="" user=""/> </datasource> </configuration> </resources> 

Using the connection manager class, you can simply read the XML for connection information and instantiate the connections and bind them.

0
source

Source: https://habr.com/ru/post/901983/


All Articles