blob: 240ee605174b5768c8b2ab4bd38f7562b45a25f0 (
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
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;
import com.google.inject.Inject;
import com.yahoo.jdisc.service.CurrentContainer;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* <p>This class is a convenient parent class for {@link Application} developers that require simple access to the most
* commonly used jDISC APIs.</p>
*
* <p>A simple hello world application could be implemented like this:</p>
* <pre>
* class HelloApplication extends AbstractApplication {
*
* @Inject
* public HelloApplication(BundleInstaller bundleInstaller, ContainerActivator activator,
* CurrentContainer container) {
* super(bundleInstaller, activator, container);
* }
*
* @Override
* public void start() {
* ContainerBuilder builder = newContainerBuilder();
* ServerProvider myServer = new MyHttpServer();
* builder.serverProviders().install(myServer);
* builder.serverBindings().bind("http://*/*", new MyHelloWorldHandler());
*
* activateContainer(builder);
* myServer.start();
* myServer.release();
* }
* }
* </pre>
*
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public abstract class AbstractApplication implements Application {
private final CountDownLatch destroyed = new CountDownLatch(1);
private final BundleInstaller bundleInstaller;
private final ContainerActivator activator;
private final CurrentContainer container;
@Inject
protected AbstractApplication(BundleInstaller bundleInstaller, ContainerActivator activator,
CurrentContainer container) {
this.bundleInstaller = bundleInstaller;
this.activator = activator;
this.container = container;
}
@Override
public void stop() {
}
@Override
public final void destroy() {
destroyed.countDown();
}
public final List<Bundle> installAndStartBundle(String... locations) throws BundleException {
return installAndStartBundle(Arrays.asList(locations));
}
public final List<Bundle> installAndStartBundle(Iterable<String> locations) throws BundleException {
return bundleInstaller.installAndStart(locations);
}
public final void stopAndUninstallBundle(Bundle... bundles) throws BundleException {
stopAndUninstallBundle(Arrays.asList(bundles));
}
public final void stopAndUninstallBundle(Iterable<Bundle> bundles) throws BundleException {
bundleInstaller.stopAndUninstall(bundles);
}
public final ContainerBuilder newContainerBuilder() {
return activator.newContainerBuilder();
}
public final DeactivatedContainer activateContainer(ContainerBuilder builder) {
return activator.activateContainer(builder);
}
public final CurrentContainer container() {
return container;
}
public final boolean isTerminated() {
return destroyed.getCount() == 0;
}
public final boolean awaitTermination(int timeout, TimeUnit unit) throws InterruptedException {
return destroyed.await(timeout, unit);
}
public final void awaitTermination() throws InterruptedException {
destroyed.await();
}
}
|