2020import java .util .function .Consumer ;
2121
2222import org .apache .commons .lang3 .SystemUtils ;
23+ import org .cryptomator .ui .util .ApplicationVersion ;
2324import org .cryptomator .ui .util .SingleInstanceManager ;
2425import org .cryptomator .ui .util .SingleInstanceManager .RemoteInstance ;
2526import org .eclipse .jetty .util .ConcurrentHashSet ;
@@ -33,60 +34,59 @@ public class Cryptomator {
3334 public static final CompletableFuture <Consumer <File >> OPEN_FILE_HANDLER = new CompletableFuture <>();
3435 private static final Logger LOG = LoggerFactory .getLogger (Cryptomator .class );
3536 private static final Set <Runnable > SHUTDOWN_TASKS = new ConcurrentHashSet <>();
36- private static final CleanShutdownPerformer CLEAN_SHUTDOWN_PERFORMER = new CleanShutdownPerformer ();
3737
3838 public static void main (String [] args ) {
39- String cryptomatorVersion = Optional . ofNullable ( Cryptomator . class . getPackage (). getImplementationVersion ()). orElse ("SNAPSHOT" );
40- LOG . info ( "Starting Cryptomator {} on {} {} ({})" , cryptomatorVersion , SystemUtils . OS_NAME , SystemUtils . OS_VERSION , SystemUtils . OS_ARCH );
39+ LOG . info ( "Starting Cryptomator {} on {} {} ({})" , ApplicationVersion . orElse ("SNAPSHOT" ), SystemUtils . OS_NAME , SystemUtils . OS_VERSION , SystemUtils . OS_ARCH );
40+
4141 if (SystemUtils .IS_OS_MAC_OSX ) {
42- /*
43- * On OSX we're in an awkward position. We need to register a handler in the main thread of this application. However, we can't
44- * even pass objects to the application, so we're forced to use a static CompletableFuture for the handler, which actually opens
45- * the file in the application.
46- *
47- * Code taken from https://github.com/axet/desktop/blob/master/src/main/java/com/github/axet/desktop/os/mac/AppleHandlers.java
48- */
49- try {
50- final Class <?> applicationClass = Class .forName ("com.apple.eawt.Application" );
51- final Class <?> openFilesHandlerClass = Class .forName ("com.apple.eawt.OpenFilesHandler" );
52- final Method getApplication = applicationClass .getMethod ("getApplication" );
53- final Object application = getApplication .invoke (null );
54- final Method setOpenFileHandler = applicationClass .getMethod ("setOpenFileHandler" , openFilesHandlerClass );
55-
56- final ClassLoader openFilesHandlerClassLoader = openFilesHandlerClass .getClassLoader ();
57- final OpenFilesHandlerClassHandler openFilesHandlerHandler = new OpenFilesHandlerClassHandler ();
58- final Object openFilesHandlerObject = Proxy .newProxyInstance (openFilesHandlerClassLoader , new Class <?>[] {openFilesHandlerClass }, openFilesHandlerHandler );
59-
60- setOpenFileHandler .invoke (application , openFilesHandlerObject );
61- } catch (ReflectiveOperationException | RuntimeException e ) {
62- // Since we're trying to call OS-specific code, we'll just have
63- // to hope for the best.
64- LOG .error ("exception adding OSX file open handler" , e );
65- }
42+ addOsxFileOpenHandler ();
6643 }
6744
68- /*
69- * Perform certain things on VM termination.
70- */
71- Runtime .getRuntime ().addShutdownHook (CLEAN_SHUTDOWN_PERFORMER );
45+ new CleanShutdownPerformer ().registerShutdownHook ();
46+
47+ final Optional <RemoteInstance > runningInstance = SingleInstanceManager .getRemoteInstance (MainApplication .APPLICATION_KEY );
48+ if (runningInstance .isPresent ()) {
49+ sendArgsToRunningInstance (args , runningInstance );
50+ } else {
51+ Application .launch (MainApplication .class , args );
52+ }
53+ }
7254
55+ private static void addOsxFileOpenHandler () {
7356 /*
74- * Before starting the application, we check if there is already an instance running on this computer. If so, we send our command
75- * line arguments to that instance and quit.
57+ * On OSX we're in an awkward position. We need to register a handler in the main thread of this application. However, we can't
58+ * even pass objects to the application, so we're forced to use a static CompletableFuture for the handler, which actually opens
59+ * the file in the application.
60+ *
61+ * Code taken from https://github.com/axet/desktop/blob/master/src/main/java/com/github/axet/desktop/os/mac/AppleHandlers.java
7662 */
77- final Optional <RemoteInstance > remoteInstance = SingleInstanceManager .getRemoteInstance (MainApplication .APPLICATION_KEY );
63+ try {
64+ final Class <?> applicationClass = Class .forName ("com.apple.eawt.Application" );
65+ final Class <?> openFilesHandlerClass = Class .forName ("com.apple.eawt.OpenFilesHandler" );
66+ final Method getApplication = applicationClass .getMethod ("getApplication" );
67+ final Object application = getApplication .invoke (null );
68+ final Method setOpenFileHandler = applicationClass .getMethod ("setOpenFileHandler" , openFilesHandlerClass );
69+
70+ final ClassLoader openFilesHandlerClassLoader = openFilesHandlerClass .getClassLoader ();
71+ final OpenFilesHandlerClassHandler openFilesHandlerHandler = new OpenFilesHandlerClassHandler ();
72+ final Object openFilesHandlerObject = Proxy .newProxyInstance (openFilesHandlerClassLoader , new Class <?>[] {openFilesHandlerClass }, openFilesHandlerHandler );
73+
74+ setOpenFileHandler .invoke (application , openFilesHandlerObject );
75+ } catch (ReflectiveOperationException | RuntimeException e ) {
76+ // Since we're trying to call OS-specific code, we'll just have
77+ // to hope for the best.
78+ LOG .error ("exception adding OSX file open handler" , e );
79+ }
80+ }
7881
79- if (remoteInstance .isPresent ()) {
80- try (RemoteInstance instance = remoteInstance .get ()) {
81- LOG .info ("An instance of Cryptomator is already running at {}." , instance .getRemotePort ());
82- for (int i = 0 ; i < args .length ; i ++) {
83- remoteInstance .get ().sendMessage (args [i ], 100 );
84- }
85- } catch (Exception e ) {
86- LOG .error ("Error forwarding arguments to remote instance" , e );
82+ private static void sendArgsToRunningInstance (String [] args , final Optional <RemoteInstance > remoteInstance ) {
83+ try (RemoteInstance instance = remoteInstance .get ()) {
84+ LOG .info ("An instance of Cryptomator is already running at {}." , instance .getRemotePort ());
85+ for (int i = 0 ; i < args .length ; i ++) {
86+ remoteInstance .get ().sendMessage (args [i ], 100 );
8787 }
88- } else {
89- Application . launch ( MainApplication . class , args );
88+ } catch ( Exception e ) {
89+ LOG . error ( "Error forwarding arguments to remote instance" , e );
9090 }
9191 }
9292
@@ -111,6 +111,10 @@ public void run() {
111111 });
112112 SHUTDOWN_TASKS .clear ();
113113 }
114+
115+ public void registerShutdownHook () {
116+ Runtime .getRuntime ().addShutdownHook (this );
117+ }
114118 }
115119
116120 private static void handleOpenFileRequest (File file ) {
0 commit comments