|
26 | 26 | import java.util.Properties; |
27 | 27 |
|
28 | 28 | /** |
29 | | - * Mongo connection properties |
| 29 | + * Mongo connection properties loader |
30 | 30 | */ |
31 | | -public class MongoConnectionProperties { |
| 31 | +public class MongoConnectionPropertiesLoader { |
32 | 32 |
|
33 | 33 | private static final String DEFAULT_HOST = "localhost"; |
34 | 34 | private static final int DEFAULT_PORT = 27017; |
35 | 35 |
|
36 | | - private String host; |
37 | | - private int port; |
38 | | - |
39 | | - /** |
40 | | - * Constructor |
41 | | - */ |
42 | | - public MongoConnectionProperties() { |
43 | | - this.host = DEFAULT_HOST; |
44 | | - this.port = DEFAULT_PORT; |
45 | | - } |
46 | | - |
47 | | - /** |
48 | | - * @return host name |
49 | | - */ |
50 | | - public String getHost() { |
51 | | - return host; |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * @return port number |
56 | | - */ |
57 | | - public int getPort() { |
58 | | - return port; |
59 | | - } |
60 | | - |
61 | 36 | /** |
62 | 37 | * Try to load connection properties from file. |
63 | 38 | * Fall back to default connection properties. |
64 | 39 | */ |
65 | | - public MongoConnectionProperties load() { |
| 40 | + public static void load() { |
| 41 | + String host = DEFAULT_HOST; |
| 42 | + int port = DEFAULT_PORT; |
66 | 43 | String path = System.getProperty("hexagonal.properties.path"); |
67 | 44 | Properties properties = new Properties(); |
68 | 45 | if (path != null) { |
69 | 46 | try (FileInputStream fin = new FileInputStream(path)) { |
70 | 47 | properties.load(fin); |
71 | | - this.host = properties.getProperty("host"); |
72 | | - this.port = Integer.parseInt(properties.getProperty("port")); |
| 48 | + host = properties.getProperty("mongo-host"); |
| 49 | + port = Integer.parseInt(properties.getProperty("mongo-port")); |
73 | 50 | } catch (Exception e) { |
74 | 51 | // error occurred, use default properties |
75 | 52 | e.printStackTrace(); |
76 | 53 | } |
77 | 54 | } |
78 | | - return this; |
| 55 | + System.setProperty("mongo-host", host); |
| 56 | + System.setProperty("mongo-port", String.format("%d", port)); |
79 | 57 | } |
80 | 58 | } |
0 commit comments