1+ package org .freenetproject .mobile ;
2+
3+ import org .apache .commons .lang3 .*;
4+
5+ import java .io .*;
6+ import java .nio .file .*;
7+ import java .util .*;
8+
9+ /**
10+ * Small abstraction over java.util.Properties class.
11+ */
12+ class Config {
13+ private final String properties = "freenet.ini" ;
14+ private Properties config = new Properties ();
15+
16+ /**
17+ * Set a given value under key.
18+ *
19+ * @param key Key name.
20+ * @param val Value.
21+ */
22+ public void set (String key , String val ) {
23+ config .setProperty (key , val );
24+ }
25+
26+ /**
27+ * Gets the value for configuration key, defaults to ""
28+ * if the configuration key is not present.
29+ *
30+ * @param key Configuration key.
31+ * @return Value.
32+ */
33+ public String get (String key ) {
34+ return config .getProperty (key , "" );
35+ }
36+
37+ /**
38+ * Gets the value for configuration key, defaults to defaultValue
39+ * if the configuration key is not present.
40+ *
41+ * @param key Configuration key.
42+ * @return Value.
43+ */
44+ public String get (String key , String defaultValue ) {
45+ return config .getProperty (key , defaultValue );
46+ }
47+
48+ /**
49+ * Loads node configuration or creates one under path with default values.
50+ *
51+ * @param path Path where the node is located.
52+ */
53+ public void loadOrDefault (Path path ) throws IOException {
54+ Path config = Paths .get (path .toString (), properties );
55+
56+ if (Files .exists (config )) {
57+ InputStream is = new FileInputStream (config .toFile ());
58+ this .config = new Properties ();
59+ this .config .load (is );
60+ return ;
61+ }
62+
63+ this .config = getDefaultConfig (path );
64+ persist ();
65+ }
66+
67+ /**
68+ * Stores the current configuration to filesystem.
69+ *
70+ * This method saves the configuration options without using the Properties#store method.
71+ * Thus avoiding the escaping of : (used for IPv6 for example) and other especial characters
72+ * that freenet configuration contains.
73+ */
74+ public void persist () throws IOException {
75+ File dest = Paths .get (this .config .getProperty ("node.install.cfgDir" ), properties ).toFile ();
76+ FileWriter writer = new FileWriter (dest );
77+ this .config .forEach ((k , v ) -> {
78+ try {
79+ writer .write (String .format ("%s=%s%n" , k , v ));
80+ } catch (IOException e ) {
81+ e .printStackTrace ();
82+ }
83+ });
84+ writer .write (String .format ("%s%n" , "End" ));
85+ writer .close ();
86+ }
87+
88+ /**
89+ * Gets the default configuration and update the paths before returning it.
90+ *
91+ * @param path Node path.
92+ * @return Configured node.
93+ */
94+ private Properties getDefaultConfig (Path path ) throws IOException {
95+ InputStream defaultConfig = getClass ()
96+ .getClassLoader ()
97+ .getResourceAsStream (
98+ Paths .get ("defaults" , properties ).toString ()
99+ );
100+
101+ Properties config = new Properties ();
102+ config .load (defaultConfig );
103+ String dir = path .toString ();
104+ config .setProperty ("node.install.cfgDir" , dir );
105+ config .setProperty ("node.install.userDir" , dir );
106+ config .setProperty ("node.install.nodeDir" , dir );
107+ config .setProperty ("node.install.runDir" , dir );
108+ config .setProperty ("node.install.storeDir" , dir + "/pathstore" );
109+ config .setProperty ("node.install.tempDir" , dir + "/temp" );
110+ config .setProperty ("node.install.pluginDir" , dir + "/plugins" );
111+ config .setProperty ("node.install.pluginStoresDir" , dir + "/plugin-path" );
112+ config .setProperty ("node.install.persistentTempDir" , dir + "/persistent-temp" );
113+
114+ config .setProperty ("node.masterKeyFile" , dir + "/master.keys" );
115+ config .setProperty ("node.downloadsDir" , dir + "/downloads" );
116+
117+ config .setProperty ("ssl.sslKeyStorePass" , RandomStringUtils .randomAscii (64 ));
118+ config .setProperty ("ssl.sslKeyPass" , RandomStringUtils .randomAscii (64 ));
119+
120+ config .setProperty ("logger.dirname" , dir + "/logs" );
121+
122+ return config ;
123+ }
124+ }
0 commit comments