{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathApplication.java
More file actions
404 lines (340 loc) · 13.4 KB
/
Copy pathApplication.java
File metadata and controls
404 lines (340 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Syncany, www.syncany.org
* Copyright (C) 2011 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.stacksync.desktop;
import com.stacksync.desktop.chunker.TTTD.RollingChecksum;
import com.stacksync.desktop.config.Config;
import com.stacksync.desktop.config.ConnectionController;
import com.stacksync.desktop.config.ConnectionTester;
import com.stacksync.desktop.config.profile.Profile;
import com.stacksync.desktop.exceptions.ConfigException;
import com.stacksync.desktop.exceptions.InitializationException;
import com.stacksync.desktop.exceptions.StorageConnectException;
import com.stacksync.desktop.gui.server.Desktop;
import com.stacksync.desktop.gui.settings.SettingsDialog;
import com.stacksync.desktop.gui.tray.Tray;
import com.stacksync.desktop.gui.tray.TrayEventListenerImpl;
import com.stacksync.desktop.gui.wizard.WizardDialog;
import com.stacksync.desktop.index.Indexer;
import com.stacksync.desktop.logging.RemoteLogs;
import com.stacksync.desktop.periodic.CacheCleaner;
import com.stacksync.desktop.periodic.TreeSearch;
import com.stacksync.desktop.repository.Uploader;
import com.stacksync.desktop.watch.local.LocalWatcher;
import com.stacksync.desktop.watch.remote.ChangeManager;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
/**
* Represents the application.
*
* <ul>
* <li>{@link Watcher}: Listens to changes of the file system in the given local
* sync folder. Passes changes to the indexer.
* <li>{@link Indexer}: Reads local files and compares them to the versions in
* local database. If necessary, it creates DB versions of new or altered files
* and passes them to the storage manager for upload.
* <li>{@link Uploader}: Uploads and downloads remote files from the shared
* storage. Receives upload requests by the {@link Indexer}, and download
* requests by the {@link PeriodicStorageMonitor}.
* <li>{@link PeriodicStorageMonitor}: Checks the online storage for changes in
* regular intervals, then downloads changes and notifies the
* {@link ChangeManager}.
* </ul>
*
* <p>General application To-Do list: Focus: <b>GET IT TO WORK!</b>
* <ul>
* <li>TODO [high] adjust separator for Win/Linux platforms: e.g. transform "\"
* to "/" EVERYWHERE!
* </ul>
*
* <p>Medium priority To-Do list:
* <ul>
* <li>TODO [medium] Connectivity management: Handle broken connections in every
* single class
* <li>TODO [medium] Make checksum long value instead of int, cp.
* {@link RollingChecksum}
* </ul>
*
* <p>Low priority To-Do list:
* <ul>
* <li>TODO [low] make platform specific file manager integration (windows
* explorer, mac finder, ...)
* <li>TODO [low] cache: implement a cache-cleaning functionality for the local
* and online storage.
* <li>TODO [low] cache: implement a cache-size parameter for the local cache.
* </ul>
*
* <p>Wish list:
* <ul>
* <li>TODO [wish] strategy for down/uploading : FIFO, larget first, ...
* </ul>
*
* @author Philipp C. Heckel <philipp.heckel@gmail.com>
*/
public class Application implements ConnectionController, ApplicationController {
private final Logger logger = Logger.getLogger(Application.class.getName());
private final ResourceBundle resourceBundle = Config.getInstance().getResourceBundle();
private Config config;
private Desktop desktop;
private Indexer indexer;
private LocalWatcher localWatcher;
private Tray tray;
private TreeSearch periodic;
private CacheCleaner cache;
private SettingsDialog settingsDialog;
private ConnectionTester connectionTester;
private Profile profile;
public Application() {
this.connectionTester = new ConnectionTester(this);
}
public void start() throws InitializationException {
logger.info("Starting Application.");
// Do NOT change the order of these method calls!
// They strongly depend on each other.
initDependencies();
boolean deamonMode = config.isDaemonMode();
if (!deamonMode) {
logger.info("Init UI...");
initUI();
}
tray.setStartDemonOnly(deamonMode);
boolean success = loadProfile();
if (success) {
startThreads();
} else {
this.tray.setStatusText("StackSync", resourceBundle.getString("tray_no_internet"));
// Start process to check internet connection
this.connectionTester.start();
}
}
private void initDependencies() {
logger.info("Instantiating dependencies ...");
config = Config.getInstance();
new Thread(new Runnable() {
@Override
public void run() {
config.getDatabase().getEntityManager(); // creates de database to solve some time after.
}
}, "InitDatabase").start();
desktop = Desktop.getInstance();
indexer = Indexer.getInstance();
localWatcher = LocalWatcher.getInstance();
periodic = new TreeSearch();
cache = new CacheCleaner();
tray = Tray.getInstance();
profile = config.getProfile();
tray.registerProcess("StackSync");
}
private void startThreads() {
// Start the rest
// Desktop integration
desktop.start(config.isDaemonMode()); // must be started before indexer!
indexer.start();
localWatcher.start();
periodic.start();
cache.start();
RemoteLogs.getInstance().setActive(config.isRemoteLogs());
}
private void doShutdown() {
logger.info("Shutting down ...");
tray.destroy();
indexer.stop();
localWatcher.stop();
periodic.stop();
cache.stop();
desktop.stop(config.isDaemonMode());
if (config.getProfile() != null) {
config.getProfile().stop();
}
System.exit(0);
}
private void initUI() throws InitializationException {
/* THIS IS NOT NECESSARY SINCE THE SETTINGS DIALOG IS NOT ENABLED
* LEAVE THE CODE HERE FOR A FUTURE USE!
*/
// Settings Dialog
/* try {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
settingsDialog = new SettingsDialog();
for (Profile p : config.getProfiles().list()) {
settingsDialog.addProfileToTree(p, false);
}
}
});
} catch (Exception ex) {
//checkthis
logger.error("Unable to init SettingsDialog: ", ex);
RemoteLogs.getInstance().sendLog(ex);
throw new InitializationException(ex);
}*/
// Tray
tray.init(resourceBundle.getString("tray_uptodate"));
tray.addTrayEventListener(new TrayEventListenerImpl(this));
tray.updateUI();
/*
// Desktop integration
desktop.start(); // must be started before indexer!
*/
}
private boolean loadProfile() throws InitializationException {
boolean success = true;
Profile profile = config.getProfile();
// a. Launch first time wizard
if (!profile.isInitialized()) {
if (!config.isDaemonMode()) {
profile = initFirstTimeWizard();
} else {
logger.error("Daemon needs config.xml have minimum one profile to start the application.");
}
if (profile == null) {
doShutdown();
}
} else { // b. Activate profiles (Index files, then start local/remote watcher)
try {
initProfile();
} catch (StorageConnectException ex) {
logger.error(ex);
success = false;
}
}
return success;
}
private void initProfile() throws InitializationException, StorageConnectException {
tray.setStatusIcon("StackSync", Tray.StatusIcon.UPDATING);
tray.updateUI(); // This is only necessary in Linux...
tray.setStatusText("StackSync", resourceBundle.getString("tray_initializing"));
if (profile == null) {
throw new InitializationException("No profile found!");
}
if (!profile.isEnabled()) {
return;
}
profile.savePathToRegistry();
try {
profile.setActive(true);
} catch (InitializationException ex) {
logger.error("Can't load the profile.", ex);
RemoteLogs.getInstance().sendLog(ex);
throw ex;
}
tray.setStatusIcon("StackSync", Tray.StatusIcon.UPTODATE);
tray.updateUI(); // This is only necessary in Linux...
tray.setStatusText("StackSync", "");
}
private Profile initFirstTimeWizard() {
Profile newProfile = WizardDialog.showWizardOpenJdk(true);
// Ok clicked
if (newProfile != null) {
config.setProfile(newProfile);
this.profile = newProfile;
//settingsDialog.addProfileToTree(profile, false);
tray.updateUI();
newProfile.savePathToRegistry();
try {
config.save();
newProfile.setActive(true);
} catch (ConfigException ex) {
logger.error("Could not save profile from first-start wizard. EXITING.", ex);
RemoteLogs.getInstance().sendLog(ex);
throw new RuntimeException("Could not save profile from first-start wizard. EXITING.", ex);
} catch (InitializationException ex) {
logger.error("Could not save profile from first-start wizard. EXITING.", ex);
RemoteLogs.getInstance().sendLog(ex);
throw new RuntimeException("Could not save profile from first-start wizard. EXITING.", ex);
} catch (StorageConnectException ex) {
// TODO is this possible???
logger.error("Could not save profile from first-start wizard. EXITING.", ex);
RemoteLogs.getInstance().sendLog(ex);
throw new RuntimeException("Could not save profile from first-start wizard. EXITING.", ex);
}
}
return newProfile;
}
@Override
public void connectionEstablished() {
logger.info("Connection established!!");
this.connectionTester.stop();
tray.setStatusText("StackSync", "");
boolean success = false;
try {
success = loadProfile();
} catch (InitializationException ex) {
logger.error(ex);
System.exit(2);
}
if (success) {
startThreads();
} else {
this.tray.setStatusText("StackSync", resourceBundle.getString("tray_no_internet"));
// Start process to check internet connection
this.connectionTester.start();
}
}
@Override
public void pauseSync() {
logger.info("Pausing syncing.");
if (profile == null) {
return;
}
try {
profile.setActive(false);
} catch (Exception ex) {
logger.error("Could not pause synchronization: ", ex);
RemoteLogs.getInstance().sendLog(ex);
return;
}
indexer.stop();
localWatcher.stop();
periodic.stop();
cache.stop();
desktop.stop(config.isDaemonMode());
tray.setStatusIcon("StackSync", Tray.StatusIcon.DISCONNECTED);
tray.updateUI(); // This is only necessary in Linux...
tray.setStatusText("StackSync", resourceBundle.getString("tray_paused_sync"));
}
@Override
public void resumeSync() {
logger.info("Resume syncing.");
tray.setStatusIcon("StackSync", Tray.StatusIcon.UPDATING);
tray.updateUI(); // This is only necessary in Linux...
tray.setStatusText("StackSync", resourceBundle.getString("tray_initializing"));
try {
initProfile();
} catch (InitializationException ex) {
// Error logged in initProfiles function.
return;
} catch (StorageConnectException ex) {
logger.error("Could not pause synchronization: ", ex);
RemoteLogs.getInstance().sendLog(ex);
return;
}
tray.setStatusIcon("StackSync", Tray.StatusIcon.UPTODATE);
tray.updateUI();
tray.setStatusText("StackSync", "");
desktop.start(config.isDaemonMode()); // must be started before indexer!
indexer.start();
localWatcher.start();
periodic.start();
cache.start();
}
@Override
public void doShutdownTray() {
doShutdown();
}
}
You can’t perform that action at this time.
