Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package dy.fi.maja.mesdbservice;
  7.  
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.nio.file.Path;
  15. import java.util.Properties;
  16.  
  17. /**
  18. *
  19. * @author Jarno
  20. */
  21. public class Settings
  22. {
  23. private String databasePath;
  24. private String mqttUrl;
  25. private String mqttUsername;
  26. private String mqttPassword;
  27. private int mqttPort;
  28. private String serverUrl;
  29. private int serverPort;
  30. private ConnectionType connectionType;
  31.  
  32. private static final String filename = "config.properties";
  33.  
  34. public static enum ConnectionType
  35. {
  36. MQTT,
  37. HttpServer
  38. }
  39.  
  40.  
  41. public static Settings getSettings()
  42. {
  43. InputStream input = null;
  44. Properties prop = new Properties();
  45.  
  46. try
  47. {
  48. input = new FileInputStream(filename);
  49. if(input != null)
  50. {
  51. prop.load(input);
  52.  
  53. Settings s = new Settings();
  54. s.setDatabasePath(prop.getProperty("DatabasePath"));
  55. s.setMqttUrl(prop.getProperty("MQTTUrl"));
  56. s.setMqttUsername(prop.getProperty("MQTTUsername"));
  57. s.setMqttPassword(prop.getProperty("MQTTPassword"));
  58. s.setMqttPort(Integer.parseInt(prop.getProperty("MQTTPort")));
  59. s.setServerUrl(prop.getProperty("ServerUrl"));
  60. s.setServerPort(Integer.parseInt(prop.getProperty("ServerPort")));
  61. s.setConnectionType(Enum.valueOf(ConnectionType.class, prop.getProperty("ConnectionType")));
  62. return s;
  63. }
  64. }
  65. catch(Exception e)
  66. {
  67. e.printStackTrace();
  68. }
  69. finally
  70. {
  71. if(input != null)
  72. {
  73. try
  74. {
  75. input.close();
  76. }
  77. catch(IOException e){ e.printStackTrace();}
  78. }
  79. }
  80.  
  81. Settings s = new Settings();
  82. s.setDatabasePath("");
  83. s.setMqttUrl("");
  84. s.setMqttUsername("");
  85. s.setMqttPassword("");
  86. s.setMqttPort(1883);
  87. s.setServerUrl("");
  88. s.setServerPort(80);
  89. s.setConnectionType(ConnectionType.MQTT);
  90.  
  91. writeSettings(s);
  92.  
  93. return s;
  94. }
  95. public static void writeSettings(Settings s)
  96. {
  97. Properties prop = new Properties();
  98. OutputStream output = null;
  99.  
  100. try
  101. {
  102. output = new FileOutputStream(filename);
  103.  
  104. prop.setProperty("DatabasePath", s.getDatabasePath());
  105. prop.setProperty("MQTTUrl", s.getMqttUrl());
  106. prop.setProperty("MQTTUsername", s.getMqttUsername());
  107. prop.setProperty("MQTTPassword", s.getMqttPassword());
  108. prop.setProperty("MQTTPort", String.valueOf(s.getMqttPort()));
  109. prop.setProperty("ServerUrl", s.getServerUrl());
  110. prop.setProperty("ServerPort", String.valueOf(s.getServerPort()));
  111. prop.setProperty("ConnectionType", String.valueOf(s.getConnectionType()));
  112. prop.store(output, null);
  113. }
  114. catch(IOException e)
  115. {
  116. e.printStackTrace();
  117. }
  118. finally
  119. {
  120. if(output != null)
  121. {
  122. try
  123. {
  124. output.close();
  125. }
  126. catch(IOException e) {e.printStackTrace();}
  127. }
  128. }
  129. }
  130.  
  131. public String getDatabaseFileName()
  132. {
  133. String tempString = this.databasePath.replace('\\', '/');
  134. int li = tempString.lastIndexOf("/");
  135. return tempString.substring(li+1);
  136. }
  137.  
  138. public String getDatabaseFolderPath()
  139. {
  140. String tempString = this.databasePath.replace('\\', '/');
  141. int li = tempString.lastIndexOf("/");
  142. return tempString.substring(0,li+1);
  143. }
  144.  
  145. public String getDatabasePath()
  146. {
  147. return databasePath.replace("\\", "/");
  148. }
  149.  
  150. public void setDatabasePath(String databasePath)
  151. {
  152. this.databasePath = databasePath;
  153. }
  154.  
  155. public ConnectionType getConnectionType()
  156. {
  157. return connectionType;
  158. }
  159.  
  160. public void setConnectionType(ConnectionType connectionType)
  161. {
  162. this.connectionType = connectionType;
  163. }
  164.  
  165. public String getMqttUrl()
  166. {
  167. return mqttUrl;
  168. }
  169.  
  170. public void setMqttUrl(String mqttUrl)
  171. {
  172. this.mqttUrl = mqttUrl;
  173. }
  174.  
  175. public String getMqttUsername()
  176. {
  177. return mqttUsername;
  178. }
  179.  
  180. public void setMqttUsername(String mqttUsername)
  181. {
  182. this.mqttUsername = mqttUsername;
  183. }
  184.  
  185. public String getMqttPassword()
  186. {
  187. return mqttPassword;
  188. }
  189.  
  190. public void setMqttPassword(String mqttPassword)
  191. {
  192. this.mqttPassword = mqttPassword;
  193. }
  194.  
  195. public int getMqttPort()
  196. {
  197. return mqttPort;
  198. }
  199.  
  200. public void setMqttPort(int mqttPort)
  201. {
  202. this.mqttPort = mqttPort;
  203. }
  204.  
  205. public String getServerUrl()
  206. {
  207. return serverUrl;
  208. }
  209.  
  210. public void setServerUrl(String serverUrl)
  211. {
  212. this.serverUrl = serverUrl;
  213. }
  214.  
  215. public int getServerPort()
  216. {
  217. return serverPort;
  218. }
  219.  
  220. public void setServerPort(int serverPort)
  221. {
  222. this.serverPort = serverPort;
  223. }
  224.  
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement