Example usage for java.util EventListener EventListener

List of usage examples for java.util EventListener EventListener

Introduction

In this page you can find the example usage for java.util EventListener EventListener.

Prototype

EventListener

Source Link

Usage

From source file:net.sourceforge.fullsync.cli.Main.java

public static void startup(String[] args, Launcher launcher) throws Exception {
    initOptions();/*ww w  . j  a  v  a2s .  c  o  m*/
    String configDir = getConfigDir();
    CommandLineParser parser = new DefaultParser();
    CommandLine line = null;

    try {
        line = parser.parse(options, args);
    } catch (ParseException ex) {
        System.err.println(ex.getMessage());
        printHelp();
        System.exit(1);
    }

    if (line.hasOption('V')) {
        System.out.println(String.format("FullSync version %s", Util.getFullSyncVersion())); //$NON-NLS-1$
        System.exit(0);
    }

    // Apply modifying options
    if (!line.hasOption("v")) { //$NON-NLS-1$
        System.setErr(new PrintStream(new FileOutputStream(getLogFileName())));
    }

    if (line.hasOption("h")) { //$NON-NLS-1$
        printHelp();
        System.exit(0);
    }

    upgradeLegacyPreferencesLocation(configDir);

    String profilesFile;
    if (line.hasOption("P")) { //$NON-NLS-1$
        profilesFile = line.getOptionValue("P"); //$NON-NLS-1$
    } else {
        profilesFile = configDir + FullSync.PROFILES_XML;
        upgradeLegacyProfilesXmlLocation(profilesFile);
    }
    final String prefrencesFile = configDir + FullSync.PREFERENCES_PROPERTIES;
    final Injector injector = Guice.createInjector(new FullSyncModule(line, prefrencesFile));
    final RuntimeConfiguration rtConfig = injector.getInstance(RuntimeConfiguration.class);
    injector.getInstance(ProfileManager.class).setProfilesFileName(profilesFile);
    final ScheduledExecutorService scheduledExecutorService = injector
            .getInstance(ScheduledExecutorService.class);
    final EventListener deadEventListener = new EventListener() {
        private final Logger logger = LoggerFactory.getLogger("DeadEventLogger"); //$NON-NLS-1$

        @Subscribe
        private void onDeadEvent(DeadEvent deadEvent) {
            if (!(deadEvent.getEvent() instanceof ShutdownEvent)) {
                logger.warn("Dead event triggered: {}", deadEvent); //$NON-NLS-1$
            }
        }
    };
    final EventBus eventBus = injector.getInstance(EventBus.class);
    eventBus.register(deadEventListener);

    final Semaphore sem = new Semaphore(0);
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        Logger logger = LoggerFactory.getLogger(Main.class);
        logger.debug("shutdown hook called, starting orderly shutdown"); //$NON-NLS-1$
        eventBus.post(new ShutdownEvent());
        scheduledExecutorService.shutdown();
        try {
            scheduledExecutorService.awaitTermination(5, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            // not relevant
        }
        logger.debug("shutdown hook finished, releaseing main thread semaphore"); //$NON-NLS-1$
        sem.release();
    }));
    if (rtConfig.isDaemon().orElse(false).booleanValue() || rtConfig.getProfileToRun().isPresent()) {
        finishStartup(injector);
        sem.acquireUninterruptibly();
        System.exit(0);
    } else {
        launcher.launchGui(injector);
        System.exit(0);
    }
}