jtulach@90
|
1 |
package org.apidesign.aserverinfo;
|
jtulach@90
|
2 |
|
jtulach@90
|
3 |
import java.net.URL;
|
jtulach@90
|
4 |
import java.util.concurrent.Callable;
|
jtulach@90
|
5 |
import org.openide.util.Exceptions;
|
jtulach@90
|
6 |
import org.openide.util.Lookup;
|
jtulach@90
|
7 |
|
jtulach@90
|
8 |
public final class AServerInfo {
|
jtulach@90
|
9 |
private final Callable<String> name;
|
jtulach@90
|
10 |
private final Callable<URL> url;
|
jtulach@90
|
11 |
private final Callable<Void> reset;
|
jtulach@90
|
12 |
private final Callable<Void> shutdown;
|
jtulach@90
|
13 |
|
jtulach@90
|
14 |
public AServerInfo(Callable<String> name, Callable<URL> url, Callable<Void> reset, Callable<Void> s) {
|
jtulach@90
|
15 |
this.name = name;
|
jtulach@90
|
16 |
this.url = url;
|
jtulach@90
|
17 |
this.reset = reset;
|
jtulach@90
|
18 |
this.shutdown = s;
|
jtulach@90
|
19 |
}
|
jtulach@90
|
20 |
|
jtulach@90
|
21 |
public String getName() {
|
jtulach@90
|
22 |
return call(name, "noname");
|
jtulach@90
|
23 |
}
|
jtulach@90
|
24 |
|
jtulach@90
|
25 |
public URL getURL() {
|
jtulach@90
|
26 |
return call(url, null);
|
jtulach@90
|
27 |
}
|
jtulach@90
|
28 |
|
jtulach@90
|
29 |
public void reset() throws Exception {
|
jtulach@90
|
30 |
reset.call();
|
jtulach@90
|
31 |
}
|
jtulach@90
|
32 |
|
jtulach@90
|
33 |
public void shutdown() throws Exception {
|
jtulach@90
|
34 |
shutdown.call();
|
jtulach@90
|
35 |
}
|
jtulach@90
|
36 |
|
jtulach@90
|
37 |
|
jtulach@90
|
38 |
|
jtulach@90
|
39 |
private static <T> T call(Callable<T> name, T defValue) {
|
jtulach@90
|
40 |
try {
|
jtulach@90
|
41 |
return name.call();
|
jtulach@90
|
42 |
} catch (Exception ex) {
|
jtulach@90
|
43 |
return defValue;
|
jtulach@90
|
44 |
}
|
jtulach@90
|
45 |
}
|
jtulach@90
|
46 |
|
jtulach@90
|
47 |
// BEGIN: aserverinfo.create
|
jtulach@90
|
48 |
public interface NameProvider {
|
jtulach@90
|
49 |
public String getName();
|
jtulach@90
|
50 |
}
|
jtulach@90
|
51 |
public interface URLProvider {
|
jtulach@90
|
52 |
public URL getURL();
|
jtulach@90
|
53 |
}
|
jtulach@90
|
54 |
public interface ResetHandler {
|
jtulach@90
|
55 |
public void reset();
|
jtulach@90
|
56 |
}
|
jtulach@90
|
57 |
|
jtulach@90
|
58 |
public static AServerInfo create(final Lookup interfaces)
|
jtulach@90
|
59 |
// END: aserverinfo.create
|
jtulach@90
|
60 |
{
|
jtulach@90
|
61 |
Callable<String> nameP = new Callable<String>() {
|
jtulach@90
|
62 |
public String call() throws Exception {
|
jtulach@90
|
63 |
NameProvider p = interfaces.lookup(NameProvider.class);
|
jtulach@90
|
64 |
return p == null ? "noname" : p.getName();
|
jtulach@90
|
65 |
}
|
jtulach@90
|
66 |
};
|
jtulach@90
|
67 |
Callable<URL> urlP = new Callable<URL>() {
|
jtulach@90
|
68 |
public URL call() throws Exception {
|
jtulach@90
|
69 |
URLProvider p = interfaces.lookup(URLProvider.class);
|
jtulach@90
|
70 |
return p == null ? null : p.getURL();
|
jtulach@90
|
71 |
}
|
jtulach@90
|
72 |
};
|
jtulach@90
|
73 |
Callable<Void> resetP = new Callable<Void>() {
|
jtulach@90
|
74 |
public Void call() throws Exception {
|
jtulach@90
|
75 |
ResetHandler h = interfaces.lookup(ResetHandler.class);
|
jtulach@90
|
76 |
if (h != null) {
|
jtulach@90
|
77 |
h.reset();
|
jtulach@90
|
78 |
}
|
jtulach@90
|
79 |
return null;
|
jtulach@90
|
80 |
}
|
jtulach@90
|
81 |
};
|
jtulach@90
|
82 |
|
jtulach@90
|
83 |
return new AServerInfo(nameP, urlP, resetP, null);
|
jtulach@90
|
84 |
}
|
jtulach@90
|
85 |
|
jtulach@90
|
86 |
// BEGIN: aserverinfo.regularcreate
|
jtulach@90
|
87 |
public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset)
|
jtulach@90
|
88 |
// END: aserverinfo.regularcreate
|
jtulach@90
|
89 |
{
|
jtulach@90
|
90 |
return create(nameProvider, urlProvider, reset, null);
|
jtulach@90
|
91 |
}
|
jtulach@90
|
92 |
|
jtulach@90
|
93 |
// BEGIN: aserverinfo.regularcreate.withshutdown
|
jtulach@90
|
94 |
/** @since 2.0 */
|
jtulach@90
|
95 |
public interface ShutdownHandler {
|
jtulach@90
|
96 |
public void shutdown();
|
jtulach@90
|
97 |
}
|
jtulach@90
|
98 |
|
jtulach@90
|
99 |
/** @since 2.0 */
|
jtulach@90
|
100 |
public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset, ShutdownHandler shutdown)
|
jtulach@90
|
101 |
// END: aserverinfo.regularcreate.withshutdown
|
jtulach@90
|
102 |
{
|
jtulach@90
|
103 |
final NameProvider np = nameProvider;
|
jtulach@90
|
104 |
final URLProvider up = urlProvider;
|
jtulach@90
|
105 |
final ResetHandler h = reset;
|
jtulach@90
|
106 |
final ShutdownHandler s = shutdown;
|
jtulach@90
|
107 |
|
jtulach@90
|
108 |
Callable<String> nameP = new Callable<String>() {
|
jtulach@90
|
109 |
public String call() throws Exception {
|
jtulach@90
|
110 |
return np == null ? "noname" : np.getName();
|
jtulach@90
|
111 |
}
|
jtulach@90
|
112 |
};
|
jtulach@90
|
113 |
Callable<URL> urlP = new Callable<URL>() {
|
jtulach@90
|
114 |
public URL call() throws Exception {
|
jtulach@90
|
115 |
return up == null ? null : up.getURL();
|
jtulach@90
|
116 |
}
|
jtulach@90
|
117 |
};
|
jtulach@90
|
118 |
Callable<Void> resetP = new Callable<Void>() {
|
jtulach@90
|
119 |
public Void call() throws Exception {
|
jtulach@90
|
120 |
if (h != null) {
|
jtulach@90
|
121 |
h.reset();
|
jtulach@90
|
122 |
}
|
jtulach@90
|
123 |
return null;
|
jtulach@90
|
124 |
}
|
jtulach@90
|
125 |
};
|
jtulach@90
|
126 |
Callable<Void> shutP = new Callable<Void>() {
|
jtulach@90
|
127 |
public Void call() throws Exception {
|
jtulach@90
|
128 |
if (s != null) {
|
jtulach@90
|
129 |
s.shutdown();
|
jtulach@90
|
130 |
}
|
jtulach@90
|
131 |
return null;
|
jtulach@90
|
132 |
}
|
jtulach@90
|
133 |
};
|
jtulach@90
|
134 |
|
jtulach@90
|
135 |
return new AServerInfo(nameP, urlP, resetP, shutP);
|
jtulach@90
|
136 |
}
|
jtulach@90
|
137 |
|
jtulach@90
|
138 |
|
jtulach@90
|
139 |
|
jtulach@90
|
140 |
}
|