aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/client/AbstractClientApplication.java
blob: 78377a0942bcf7656b746c24711f753c83027a6b (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.client;

import com.google.inject.Inject;
import com.yahoo.jdisc.application.AbstractApplication;
import com.yahoo.jdisc.application.BundleInstaller;
import com.yahoo.jdisc.application.ContainerActivator;
import com.yahoo.jdisc.service.CurrentContainer;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * <p>This is a convenient parent class for {@link ClientApplication} developers. It extends {@link AbstractApplication}
 * and implements {@link Runnable} to wait for {@link #shutdown()} to be called. When using this class, you implement
 * {@link #start()} (and optionally {@link #stop()}), and provide a reference to it to whatever component is responsible
 * for signaling shutdown.</p>
 *
 * @author Simon Thoresen Hult
 */
public abstract class AbstractClientApplication extends AbstractApplication implements ClientApplication {

    private final CountDownLatch done = new CountDownLatch(1);

    @Inject
    public AbstractClientApplication(BundleInstaller bundleInstaller, ContainerActivator activator,
                                     CurrentContainer container) {
        super(bundleInstaller, activator, container);
    }

    @Override
    public final void run() {
        try {
            done.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    public final void shutdown() {
        done.countDown();
    }

    public final boolean isShutdown() {
        return done.getCount() == 0;
    }

    public final boolean awaitShutdown(int timeout, TimeUnit unit) throws InterruptedException {
        return done.await(timeout, unit);
    }

    public final void awaitShutdown() throws InterruptedException {
        done.await();
    }
}