1 /** ************************************************************************* */
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2016-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /** ************************************************************************* */
10 /// @file    Constants.java
11 /// @author  Maximiliano Bottazzi
12 /// @date    2016
13 /// @version $Id$
14 ///
15 //
16 /** ************************************************************************* */
17 package de.dlr.ts.lisum.gui;
18 
19 import de.dlr.ts.commons.logger.DLRLogger;
20 import de.dlr.ts.commons.tools.FileTools;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.Properties;
27 
28 /**
29  *
30  * @author @author <a href="mailto:maximiliano.bottazzi@dlr.de">Maximiliano
31  * Bottazzi</a>
32  */
33 public final class GlobalConfig {
34 
35     private static final GlobalConfig INSTANCE = new GlobalConfig();
36 
37     private static final String PERSISTENCE_FILE = "props";
38     private final int sumoPort = 9100;
39 
40     //private String sumoExec = "c:/Program Files (x86)/DLR/Sumo/bin/sumo-gui.exe";
41     private String sumoExec = "";
42     private final String sumoExec_linux_default = "/usr/bin/sumo-gui";
43     private String sumoExec_linux = sumoExec_linux_default;
44 
45     private final String defaultTextEditor = "notepad.exe";
46     private String textEditor = defaultTextEditor;
47     private final String defaultTextEditor_linux = "gedit";
48     private String textEditor_linux = defaultTextEditor_linux;
49 
50     private final String defaultFilesExplorer = "explorer.exe";
51     private String filesExplorer = defaultFilesExplorer;
52     private final String defaultFilesExplorer_linux = "nautilus";
53     private String filesExplorer_linux = defaultFilesExplorer_linux;
54 
55     private String workspace = "";
56     private final Properties props = new Properties();
57 
58     private final String defaultLisaRestFulServerDir = "OmlFgServer";
59     private String lisaRestFulServerDir = defaultLisaRestFulServerDir;
60 
61     private String lisaServerAddress = "localhost";
62     private int lisaServerPort = 9091;
63 
64     private String loggingLevel = "INFO";
65     private File persistenceFile;
66 
67     /**
68      * Keys
69      */
70     private static final String LOGGING_LEVEL_KEY = "logging_level";
71     private static final String RESTFUL_SERVER_DIR_KEY = "lisa_restFUL_server_dir";
72     private static final String TEXT_EDITOR_KEY = "text_editor";
73     private static final String TEXT_EDITOR_LINUX_KEY = "text_editor_linux";
74     private static final String SUMO_EXEC_KEY = "sumo_exec";
75     private static final String FILES_EXPLORER_KEY = "files_explorer";
76     private static final String SUMO_EXEC_LINUX_KEY = "sumo_exec_linux";
77     private static final String FILES_EXPLORER_LINUX_KEY = "files_explorer";
78 
79     /**
80      *
81      */
GlobalConfig()82     private GlobalConfig() {
83     }
84 
85     /**
86      *
87      */
update()88     private void update() {
89         setPersistenceFile();
90         readProps();
91         updateLisaServerParameters();
92         saveProps();
93     }
94 
95     /**
96      *
97      */
updateLisaServerParameters()98     private void updateLisaServerParameters() {
99         String fileName = lisaRestFulServerDir + File.separator + "OmlFgServer.ini";
100         try {
101             List<String> lines = FileTools.readSmallTextFile(fileName);
102             String _line = "";
103 
104             for (String line : lines) {
105                 if (line.contains("pdBase")) {
106                     _line = line;
107                 }
108             }
109 
110             _line = _line.replaceAll("pdBase=http://", "");
111             _line = _line.replaceAll("/", "");
112 
113             String[] split = _line.split(":");
114             lisaServerAddress = split[0];
115             lisaServerPort = Integer.valueOf(split[1]);
116         } catch (IOException ex) {
117             DLRLogger.severe(this, lisaRestFulServerDir + File.separator + "OmlFgServer.ini" + " file not found");
118         }
119     }
120 
121     /**
122      *
123      */
setPersistenceFile()124     private void setPersistenceFile() {
125         String tmpdir = workspace + File.separator + ".metadata" + File.separator;
126         persistenceFile = new File(tmpdir + PERSISTENCE_FILE);
127     }
128 
129     /**
130      *
131      */
readProps()132     private void readProps() {
133         try {
134             if (persistenceFile.exists()) {
135                 props.load(new FileInputStream(persistenceFile));
136 
137                 String logLevel = props.getProperty(LOGGING_LEVEL_KEY);
138                 if (logLevel != null) {
139                     this.loggingLevel = logLevel;
140                 }
141 
142                 String _lisaDir = props.getProperty(RESTFUL_SERVER_DIR_KEY);
143                 if (_lisaDir != null) {
144                     this.lisaRestFulServerDir = _lisaDir;
145                 }
146 
147                 String _textEditor = props.getProperty(TEXT_EDITOR_KEY);
148                 if (_textEditor != null) {
149                     this.textEditor = _textEditor;
150                 }
151 
152                 String _sumoExec = props.getProperty(SUMO_EXEC_KEY);
153                 if (_sumoExec != null) {
154                     this.sumoExec = _sumoExec;
155                 }
156 
157                 String files_ex = props.getProperty(FILES_EXPLORER_KEY);
158                 if (files_ex != null) {
159                     this.filesExplorer = files_ex;
160                 }
161 
162                 String __textEditor = props.getProperty(TEXT_EDITOR_LINUX_KEY);
163                 if (__textEditor != null) {
164                     this.textEditor_linux = __textEditor;
165                 }
166 
167                 String __sumoExec = props.getProperty(SUMO_EXEC_LINUX_KEY);
168                 if (__sumoExec != null) {
169                     this.sumoExec_linux = __sumoExec;
170                 }
171 
172                 String files__ex = props.getProperty(FILES_EXPLORER_LINUX_KEY);
173                 if (files__ex != null) {
174                     this.filesExplorer_linux = files__ex;
175                 }
176 
177             }
178         } catch (IOException ex) {
179             ex.printStackTrace();
180         }
181     }
182 
183     /**
184      *
185      * @return
186      */
getLisaRESTfulServerPath()187     public String getLisaRESTfulServerPath() {
188         return lisaRestFulServerDir;
189     }
190 
191     /**
192      *
193      */
saveProps()194     public void saveProps() {
195         props.put(TEXT_EDITOR_KEY, textEditor);
196         props.put(SUMO_EXEC_KEY, sumoExec);
197         props.put(FILES_EXPLORER_KEY, filesExplorer);
198         props.put(LOGGING_LEVEL_KEY, loggingLevel);
199         props.put(RESTFUL_SERVER_DIR_KEY, lisaRestFulServerDir);
200         props.put(TEXT_EDITOR_LINUX_KEY, textEditor_linux);
201         props.put(SUMO_EXEC_LINUX_KEY, sumoExec_linux);
202         props.put(FILES_EXPLORER_LINUX_KEY, filesExplorer_linux);
203 
204         DLRLogger.finest(this, "Saving preferences to " + persistenceFile);
205         DLRLogger.finest(this, props.toString());
206 
207         try {
208             props.store(new FileOutputStream(persistenceFile), "");
209         } catch (IOException ex) {
210             ex.printStackTrace();
211         }
212     }
213 
214     /**
215      *
216      * @return
217      */
getInstance()218     public static GlobalConfig getInstance() {
219         return INSTANCE;
220     }
221 
222     /**
223      *
224      * @return
225      */
getDefaultTextEditor()226     public String getDefaultTextEditor() {
227         if (System.getProperty("os.name").toUpperCase().contains("WIN")) {
228             return defaultTextEditor;
229         }
230 
231         return defaultTextEditor_linux;
232     }
233 
234     /**
235      *
236      * @return
237      */
getTextEditor()238     public String getTextEditor() {
239         if (System.getProperty("os.name").toUpperCase().contains("WIN")) {
240             return textEditor;
241         }
242 
243         return textEditor_linux;
244     }
245 
246     /**
247      *
248      * @param textEditor
249      */
setTextEditor(String textEditor)250     public void setTextEditor(String textEditor) {
251         if (System.getProperty("os.name").toUpperCase().contains("WIN")) {
252             this.textEditor = textEditor;
253         } else {
254             this.textEditor_linux = textEditor;
255         }
256     }
257 
258     /**
259      *
260      * @param sumoExec
261      */
setSumoExec(String sumoExec)262     public void setSumoExec(String sumoExec) {
263         if (System.getProperty("os.name").toUpperCase().contains("WIN")) {
264             this.sumoExec = sumoExec;
265         } else {
266             this.sumoExec_linux = sumoExec;
267         }
268     }
269 
270     /**
271      *
272      * @return
273      */
getWorkspace()274     public String getWorkspace() {
275         return workspace;
276     }
277 
278     /**
279      *
280      * @param workspace
281      */
setWorkspace(String workspace)282     public void setWorkspace(String workspace) {
283         if (!this.workspace.equals(workspace)) {
284             this.workspace = workspace;
285             update();
286         }
287     }
288 
289     /**
290      *
291      * @return
292      */
getSumoExec()293     public String getSumoExec() {
294         if (System.getProperty("os.name").toUpperCase().contains("WIN")) {
295             return sumoExec;
296         }
297 
298         return sumoExec_linux;
299     }
300 
301     /**
302      *
303      * @return
304      */
getSumoPort()305     public int getSumoPort() {
306         return sumoPort;
307     }
308 
getLisaServerPort()309     public int getLisaServerPort() {
310         return lisaServerPort;
311     }
312 
getLisaServerAddress()313     public String getLisaServerAddress() {
314         return lisaServerAddress;
315     }
316 
getLoggingLevel()317     public String getLoggingLevel() {
318         return loggingLevel;
319     }
320 
setLoggingLevel(String loggingLevel)321     public void setLoggingLevel(String loggingLevel) {
322         this.loggingLevel = loggingLevel;
323     }
324 
getDefaultLisaRestFulServerDir()325     public String getDefaultLisaRestFulServerDir() {
326         return defaultLisaRestFulServerDir;
327     }
328 
setLisaRestFulServerDir(String lisaRestFulServerDir)329     public void setLisaRestFulServerDir(String lisaRestFulServerDir) {
330         if (!lisaRestFulServerDir.equals(this.lisaRestFulServerDir)) {
331             this.lisaRestFulServerDir = lisaRestFulServerDir;
332             updateLisaServerParameters();
333         }
334     }
335 
getFilesExplorer()336     public String getFilesExplorer() {
337         if (System.getProperty("os.name").toUpperCase().contains("WIN")) {
338             return filesExplorer;
339         }
340 
341         return filesExplorer_linux;
342     }
343 
getDefaultFilesExplorer()344     public String getDefaultFilesExplorer() {
345         if (System.getProperty("os.name").toUpperCase().contains("WIN")) {
346             return defaultFilesExplorer;
347         }
348 
349         return defaultFilesExplorer_linux;
350     }
351 
setFilesExplorer(String filesExplorer)352     public void setFilesExplorer(String filesExplorer) {
353         if (System.getProperty("os.name").toUpperCase().contains("WIN")) {
354             this.filesExplorer = filesExplorer;
355         } else {
356             this.filesExplorer_linux = filesExplorer;
357         }
358     }
359 
360 }
361