Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating objects with Guava from property files

In our applications we are using property files very much. Since a few months I have started to learn Guava and I liked it a lot actually.

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

The property file format is not strict. It can be changed if It can be expressed better with another format?

Sample property 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
like image 375
Cemo Avatar asked Nov 21 '11 10:11

Cemo


People also ask

How do I load a property file?

The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties. load() method of Properties class is convenient to load . properties file in the form of key-value pairs.

Where do I put properties file?

properties file inside your resource folder of the java project. The extension we use for the properties file is . properties.


2 Answers

The easiest thing is probably to use JSON rather than 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 just use a library such as Gson to convert that into 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 map:

Gson gson = new Gson();
Map<String, DataSource> dataSources = gson.fromJson(/* file or stream here */,
    DataSources.class).getDataSources();
like image 162
ColinD Avatar answered Sep 21 '22 01:09

ColinD


Properties 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 so you want to transform 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's no longer a view
Map<String, Datasource> finalMap = Maps.newHashMap(mSD);

There's probably an easier way, but this should work :)

Still you're better off with json or xml. You can also load properties of different datasources 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(....)
}
like image 21
soulcheck Avatar answered Sep 22 '22 01:09

soulcheck