|
1 | 1 | package eu.mihosoft.vrl.v3d.parametrics; |
2 | 2 |
|
3 | 3 | import java.io.File; |
4 | | -import java.io.IOException; |
5 | | -import java.io.InputStream; |
6 | | -import java.io.OutputStream; |
7 | | -import java.lang.reflect.Type; |
8 | | -import java.util.concurrent.ConcurrentHashMap; |
9 | 4 | import java.util.concurrent.CopyOnWriteArrayList; |
10 | 5 |
|
11 | | -import org.apache.commons.io.FileUtils; |
12 | | -import org.apache.commons.io.IOUtils; |
13 | | -import com.google.gson.Gson; |
14 | | -import com.google.gson.GsonBuilder; |
15 | | -import com.google.gson.reflect.TypeToken; |
16 | | - |
17 | 6 | public class CSGDatabase { |
18 | | - |
19 | | - private static ConcurrentHashMap<String,Parameter> database=null; |
20 | | - private static File dbFile=new File("CSGdatabase.json"); |
21 | | - private static final Type TT_mapStringString = new TypeToken<ConcurrentHashMap<String,Parameter>>(){}.getType(); |
22 | | - private static final Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); |
23 | | - //private static final HashMap<String,ArrayList<IParameterChanged>> parameterListeners=new HashMap<>(); |
24 | | - private static final ConcurrentHashMap<String, CopyOnWriteArrayList<IParameterChanged>> parameterListeners = new ConcurrentHashMap<>(); |
25 | | - public static void set(String key, Parameter value){ |
26 | | - getDatabase(); |
27 | | - //synchronized(database){ |
28 | | - getDatabase().put(key, value); |
29 | | - //} |
30 | | - } |
31 | | - public static Parameter get(String key){ |
32 | | - Parameter ret =null; |
33 | | - getDatabase();// load database before synchronization |
34 | | - //synchronized(database){ |
35 | | - ret=getDatabase().get(key); |
36 | | - //} |
37 | | - return ret; |
38 | | - } |
39 | | - |
40 | | - public static void clear(){ |
41 | | - |
42 | | - getDatabase(); |
43 | | - //synchronized(database){ |
44 | | - database.clear(); |
45 | | - //} |
46 | | - parameterListeners.clear(); |
47 | | - saveDatabase(); |
48 | | - } |
49 | | - public static void addParameterListener(String key, IParameterChanged l){ |
50 | | - CopyOnWriteArrayList<IParameterChanged> list = getParamListeners(key); |
51 | | - if(!list.contains(l)){ |
52 | | - list.add(l); |
53 | | - } |
54 | | - } |
55 | | - public static void clearParameterListeners(String key){ |
56 | | - synchronized (parameterListeners) { |
57 | | - CopyOnWriteArrayList<IParameterChanged> back = parameterListeners.get(key); |
58 | | - if(back==null){ |
59 | | - back = new CopyOnWriteArrayList<>(); |
60 | | - parameterListeners.put(key, back); |
61 | | - } |
62 | | - back.clear(); |
63 | | - } |
64 | | - } |
65 | | - public static void removeParameterListener(String key, IParameterChanged l){ |
66 | | - if(parameterListeners.get(key)==null){ |
67 | | - return; |
68 | | - } |
69 | | - CopyOnWriteArrayList<IParameterChanged> list = parameterListeners.get(key); |
70 | | - if(list.contains(l)){ |
71 | | - list.remove(l); |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - public static CopyOnWriteArrayList<IParameterChanged> getParamListeners(String key){ |
76 | | - synchronized (parameterListeners) { |
77 | | - CopyOnWriteArrayList<IParameterChanged> back = parameterListeners.get(key); |
78 | | - if(back==null){ |
79 | | - back = new CopyOnWriteArrayList<>(); |
80 | | - parameterListeners.put(key, back); |
81 | | - } |
82 | | - return back; |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - |
87 | | - public static void delete(String key){ |
88 | | - //synchronized(database){ |
89 | | - getDatabase().remove(key); |
90 | | - //} |
91 | | - } |
92 | | - private static ConcurrentHashMap<String,Parameter> getDatabase() { |
93 | | - if(database==null){ |
94 | | - new Thread(){ |
95 | | - public void run(){ |
96 | | - String jsonString; |
97 | | - try { |
98 | | - |
99 | | - if(!getDbFile().exists()){ |
100 | | - setDatabase(new ConcurrentHashMap<String,Parameter>()); |
101 | | - } |
102 | | - else{ |
103 | | - InputStream in = null; |
104 | | - try { |
105 | | - in = FileUtils.openInputStream(getDbFile()); |
106 | | - jsonString= IOUtils.toString(in); |
107 | | - } finally { |
108 | | - IOUtils.closeQuietly(in); |
109 | | - } |
110 | | - ConcurrentHashMap<String,Parameter> tm=gson.fromJson(jsonString, TT_mapStringString); |
111 | | - |
112 | | - |
113 | | - if(tm!=null){ |
114 | | -// ////System.out.println("Hash Map loaded from "+jsonString); |
115 | | -// for(String k:tm.keySet()){ |
116 | | -// ////System.out.println("Key: "+k+" vlaue= "+tm.get(k)); |
117 | | -// } |
118 | | - setDatabase(tm); |
119 | | - } |
120 | | - } |
121 | | - } catch (Exception e) { |
122 | | - e.printStackTrace(); |
123 | | - //System.out.println(dbFile.getAbsolutePath()); |
124 | | - setDatabase(new ConcurrentHashMap<String,Parameter>()); |
125 | | - } |
126 | | - Runtime.getRuntime().addShutdownHook(new Thread() { |
127 | | - @Override |
128 | | - public void run() { |
129 | | - saveDatabase(); |
130 | | - } |
131 | | - }); |
132 | | - } |
133 | | - }.start(); |
134 | | - long start = System.currentTimeMillis(); |
135 | | - while(database==null){ |
136 | | - try { |
137 | | - Thread.sleep(10); |
138 | | - } catch (InterruptedException e) { |
139 | | - // Auto-generated catch block |
140 | | - e.printStackTrace(); |
141 | | - } |
142 | | - if((System.currentTimeMillis()-start)>500){ |
143 | | - setDatabase(new ConcurrentHashMap<String,Parameter>()); |
144 | | - } |
145 | | - } |
146 | | - } |
147 | | - return database; |
148 | | - } |
149 | | - |
150 | | - public static void loadDatabaseFromFile(File f){ |
151 | | - InputStream in = null; |
152 | | - String jsonString; |
153 | | - try { |
154 | | - try { |
155 | | - in = FileUtils.openInputStream(f); |
156 | | - jsonString= IOUtils.toString(in); |
157 | | - ConcurrentHashMap<String,Parameter> tm=gson.fromJson(jsonString, TT_mapStringString); |
158 | | - if(tm !=null) |
159 | | - for(String k:tm.keySet()){ |
160 | | - set(k,tm.get(k)); |
161 | | - } |
162 | | - saveDatabase(); |
163 | | - } catch (Exception e) { |
164 | | - //System.out.println(f.getAbsolutePath()); |
165 | | - e.printStackTrace(); |
166 | | - } |
167 | | - |
168 | | - } finally { |
169 | | - IOUtils.closeQuietly(in); |
170 | | - } |
171 | | - } |
172 | | - |
173 | | - public static String getDataBaseString(){ |
174 | | - String writeOut=null; |
175 | | - getDatabase(); |
176 | | - //synchronized(database){ |
177 | | - writeOut =gson.toJson(database, TT_mapStringString); |
178 | | - //} |
179 | | - return writeOut; |
180 | | - } |
181 | | - |
182 | | - public static void saveDatabase(){ |
183 | | - String writeOut=getDataBaseString(); |
184 | | - try { |
185 | | - if(!getDbFile().exists()){ |
186 | | - getDbFile().createNewFile(); |
187 | | - } |
188 | | - OutputStream out = null; |
189 | | - try { |
190 | | - out = FileUtils.openOutputStream(getDbFile(), false); |
191 | | - IOUtils.write(writeOut, out); |
192 | | - out.flush(); |
193 | | - out.close(); // don't swallow close Exception if copy completes normally |
194 | | - } finally { |
195 | | - IOUtils.closeQuietly(out); |
196 | | - } |
197 | | - } catch (IOException e) { |
198 | | - // Auto-generated catch block |
199 | | - e.printStackTrace(); |
200 | | - } |
201 | | - } |
202 | | - private static void setDatabase(ConcurrentHashMap<String,Parameter> database) { |
203 | | - if(CSGDatabase.database!=null){ |
204 | | - return; |
205 | | - } |
206 | | - CSGDatabase.database = database; |
| 7 | + private static CSGDatabaseInstance instance = new CSGDatabaseInstance(new File("CSGdatabase.json")); |
| 8 | + |
| 9 | + public static void set(String key, Parameter value) { |
| 10 | + getInstance().set(key, value); |
| 11 | + } |
| 12 | + |
| 13 | + public static Parameter get(String key) { |
| 14 | + return getInstance().get(key); |
| 15 | + } |
| 16 | + |
| 17 | + public static void clear() { |
| 18 | + getInstance().clear(); |
| 19 | + } |
| 20 | + |
| 21 | + public static void addParameterListener(String key, IParameterChanged l) { |
| 22 | + getInstance().addParameterListener(key, l); |
| 23 | + } |
| 24 | + |
| 25 | + public static void clearParameterListeners(String key) { |
| 26 | + getInstance().clearParameterListeners(key); |
| 27 | + } |
| 28 | + |
| 29 | + public static void removeParameterListener(String key, IParameterChanged l) { |
| 30 | + getInstance().removeParameterListener(key, l); |
| 31 | + } |
| 32 | + |
| 33 | + public static CopyOnWriteArrayList<IParameterChanged> getParamListeners(String key) { |
| 34 | + return getInstance().getParamListeners(key); |
| 35 | + } |
| 36 | + |
| 37 | + public static void delete(String key) { |
| 38 | + getInstance().delete(key); |
| 39 | + } |
| 40 | + |
| 41 | + public static void loadDatabaseFromFile(File f) { |
| 42 | + getInstance().loadDatabaseFromFile(f); |
| 43 | + } |
| 44 | + |
| 45 | + public static String getDataBaseString() { |
| 46 | + return getInstance().getDataBaseString(); |
| 47 | + } |
| 48 | + |
| 49 | + public static void saveDatabase() { |
| 50 | + getInstance().saveDatabase(); |
207 | 51 | } |
| 52 | + |
| 53 | + |
208 | 54 | public static File getDbFile() { |
209 | | - return dbFile; |
| 55 | + return getInstance().getDbFile(); |
210 | 56 | } |
| 57 | + |
211 | 58 | public static void setDbFile(File dbFile) { |
212 | | - if(!dbFile.exists()) |
213 | | - try { |
214 | | - dbFile.createNewFile(); |
215 | | - } catch (IOException e) { |
216 | | - // Auto-generated catch block |
217 | | - e.printStackTrace(); |
218 | | - } |
219 | | - CSGDatabase.dbFile = dbFile; |
220 | | - loadDatabaseFromFile(dbFile); |
| 59 | + getInstance().setDbFile(dbFile); |
221 | 60 | } |
| 61 | + |
222 | 62 | public static void reLoadDbFile() { |
223 | | - setDbFile(dbFile); |
| 63 | + getInstance().reLoadDbFile(); |
| 64 | + } |
| 65 | + |
| 66 | + public static CSGDatabaseInstance getInstance() { |
| 67 | + return instance; |
| 68 | + } |
| 69 | + |
| 70 | + public static void setInstance(CSGDatabaseInstance instance) { |
| 71 | + CSGDatabase.instance = instance; |
224 | 72 | } |
225 | 73 | } |
0 commit comments