AbstractService.java
12.7 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package com.google.common.util.concurrent;
import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.Immutable;
import ro;
import ro.a;
@Beta
public abstract class AbstractService
implements Service
{
private static final ro.a<Service.Listener> a = new ro.a("starting()") {};
private static final ro.a<Service.Listener> b = new ro.a("running()") {};
private static final ro.a<Service.Listener> c = b(Service.State.STARTING);
private static final ro.a<Service.Listener> d = b(Service.State.RUNNING);
private static final ro.a<Service.Listener> e = a(Service.State.NEW);
private static final ro.a<Service.Listener> f = a(Service.State.RUNNING);
private static final ro.a<Service.Listener> g = a(Service.State.STOPPING);
private final Monitor h = new Monitor();
private final Monitor.Guard i = new Monitor.Guard(this.h)
{
public final boolean isSatisfied()
{
return AbstractService.this.state() == Service.State.NEW;
}
};
private final Monitor.Guard j = new Monitor.Guard(this.h)
{
public final boolean isSatisfied()
{
return AbstractService.this.state().compareTo(Service.State.RUNNING) <= 0;
}
};
private final Monitor.Guard k = new Monitor.Guard(this.h)
{
public final boolean isSatisfied()
{
return AbstractService.this.state().compareTo(Service.State.RUNNING) >= 0;
}
};
private final Monitor.Guard l = new Monitor.Guard(this.h)
{
public final boolean isSatisfied()
{
return AbstractService.this.state().a();
}
};
@GuardedBy("monitor")
private final List<ro<Service.Listener>> m = Collections.synchronizedList(new ArrayList());
@GuardedBy("monitor")
private volatile a n = new a(Service.State.NEW);
private static ro.a<Service.Listener> a(final Service.State paramState)
{
String str = String.valueOf(String.valueOf(paramState));
new ro.a(str.length() + 21 + "terminated({from = " + str + "})") {};
}
private void a()
{
if (!this.h.isOccupiedByCurrentThread())
{
int i1 = 0;
while (i1 < this.m.size())
{
((ro)this.m.get(i1)).a();
i1 += 1;
}
}
}
private static ro.a<Service.Listener> b(final Service.State paramState)
{
String str = String.valueOf(String.valueOf(paramState));
new ro.a(str.length() + 19 + "stopping({from = " + str + "})") {};
}
@GuardedBy("monitor")
private void c(Service.State paramState)
{
Object localObject = state();
if (localObject != paramState)
{
if (localObject == Service.State.FAILED)
{
paramState = String.valueOf(String.valueOf(paramState));
throw new IllegalStateException(paramState.length() + 55 + "Expected the service to be " + paramState + ", but the service has FAILED", failureCause());
}
paramState = String.valueOf(String.valueOf(paramState));
localObject = String.valueOf(String.valueOf(localObject));
throw new IllegalStateException(paramState.length() + 37 + ((String)localObject).length() + "Expected the service to be " + paramState + ", but was " + (String)localObject);
}
}
@GuardedBy("monitor")
private void d(Service.State paramState)
{
if (paramState == Service.State.STARTING)
{
c.a(this.m);
return;
}
if (paramState == Service.State.RUNNING)
{
d.a(this.m);
return;
}
throw new AssertionError();
}
@GuardedBy("monitor")
private void e(Service.State paramState)
{
switch (2.a[paramState.ordinal()])
{
case 2:
default:
throw new AssertionError();
case 1:
e.a(this.m);
return;
case 3:
f.a(this.m);
return;
}
g.a(this.m);
}
public final void addListener(Service.Listener paramListener, Executor paramExecutor)
{
Preconditions.checkNotNull(paramListener, "listener");
Preconditions.checkNotNull(paramExecutor, "executor");
this.h.enter();
try
{
if (!state().a()) {
this.m.add(new ro(paramListener, paramExecutor));
}
return;
}
finally
{
this.h.leave();
}
}
public final void awaitRunning()
{
this.h.enterWhenUninterruptibly(this.k);
try
{
c(Service.State.RUNNING);
return;
}
finally
{
this.h.leave();
}
}
public final void awaitRunning(long paramLong, TimeUnit paramTimeUnit)
throws TimeoutException
{
if (this.h.enterWhenUninterruptibly(this.k, paramLong, paramTimeUnit)) {
try
{
c(Service.State.RUNNING);
return;
}
finally
{
this.h.leave();
}
}
paramTimeUnit = String.valueOf(String.valueOf(this));
String str = String.valueOf(String.valueOf(state()));
throw new TimeoutException(paramTimeUnit.length() + 66 + str.length() + "Timed out waiting for " + paramTimeUnit + " to reach the RUNNING state. Current state: " + str);
}
public final void awaitTerminated()
{
this.h.enterWhenUninterruptibly(this.l);
try
{
c(Service.State.TERMINATED);
return;
}
finally
{
this.h.leave();
}
}
public final void awaitTerminated(long paramLong, TimeUnit paramTimeUnit)
throws TimeoutException
{
if (this.h.enterWhenUninterruptibly(this.l, paramLong, paramTimeUnit)) {
try
{
c(Service.State.TERMINATED);
return;
}
finally
{
this.h.leave();
}
}
paramTimeUnit = String.valueOf(String.valueOf(this));
String str = String.valueOf(String.valueOf(state()));
throw new TimeoutException(paramTimeUnit.length() + 65 + str.length() + "Timed out waiting for " + paramTimeUnit + " to reach a terminal state. Current state: " + str);
}
protected abstract void doStart();
protected abstract void doStop();
public final Throwable failureCause()
{
a locala = this.n;
if (locala.a == Service.State.FAILED) {}
for (boolean bool = true;; bool = false)
{
Preconditions.checkState(bool, "failureCause() is only valid if the service has failed, service is %s", new Object[] { locala.a });
return locala.c;
}
}
public final boolean isRunning()
{
return state() == Service.State.RUNNING;
}
protected final void notifyFailed(final Throwable paramThrowable)
{
Preconditions.checkNotNull(paramThrowable);
this.h.enter();
for (;;)
{
try
{
localObject = state();
switch (2.a[localObject.ordinal()])
{
case 1:
paramThrowable = String.valueOf(String.valueOf(localObject));
throw new AssertionError(paramThrowable.length() + 18 + "Unexpected state: " + paramThrowable);
}
}
finally
{
this.h.leave();
a();
}
final Object localObject = String.valueOf(String.valueOf(localObject));
throw new IllegalStateException(((String)localObject).length() + 22 + "Failed while in state:" + (String)localObject, paramThrowable);
this.n = new a(Service.State.FAILED, false, paramThrowable);
String str1 = String.valueOf(String.valueOf(localObject));
String str2 = String.valueOf(String.valueOf(paramThrowable));
new ro.a(str1.length() + 27 + str2.length() + "failed({from = " + str1 + ", cause = " + str2 + "})") {}.a(this.m);
this.h.leave();
a();
return;
}
}
protected final void notifyStarted()
{
this.h.enter();
try
{
if (this.n.a != Service.State.STARTING)
{
Object localObject1 = String.valueOf(String.valueOf(this.n.a));
localObject1 = new IllegalStateException(((String)localObject1).length() + 43 + "Cannot notifyStarted() when the service is " + (String)localObject1);
notifyFailed((Throwable)localObject1);
throw ((Throwable)localObject1);
}
}
finally
{
this.h.leave();
a();
}
if (this.n.b)
{
this.n = new a(Service.State.STOPPING);
doStop();
}
for (;;)
{
this.h.leave();
a();
return;
this.n = new a(Service.State.RUNNING);
b.a(this.m);
}
}
protected final void notifyStopped()
{
this.h.enter();
try
{
Object localObject = this.n.a;
if ((localObject != Service.State.STOPPING) && (localObject != Service.State.RUNNING))
{
localObject = String.valueOf(String.valueOf(localObject));
localObject = new IllegalStateException(((String)localObject).length() + 43 + "Cannot notifyStopped() when the service is " + (String)localObject);
notifyFailed((Throwable)localObject);
throw ((Throwable)localObject);
}
}
finally
{
this.h.leave();
a();
}
this.n = new a(Service.State.TERMINATED);
e(localState);
this.h.leave();
a();
}
public final Service startAsync()
{
if (this.h.enterIf(this.i)) {
try
{
this.n = new a(Service.State.STARTING);
a.a(this.m);
doStart();
return this;
}
catch (Throwable localThrowable)
{
notifyFailed(localThrowable);
return this;
}
finally
{
this.h.leave();
a();
}
}
String str = String.valueOf(String.valueOf(this));
throw new IllegalStateException(str.length() + 33 + "Service " + str + " has already been started");
}
public final Service.State state()
{
a locala = this.n;
if ((locala.b) && (locala.a == Service.State.STARTING)) {
return Service.State.STOPPING;
}
return locala.a;
}
public final Service stopAsync()
{
if (this.h.enterIf(this.j)) {}
for (;;)
{
try
{
Object localObject1 = state();
switch (2.a[localObject1.ordinal()])
{
case 1:
localObject1 = String.valueOf(String.valueOf(localObject1));
throw new AssertionError(((String)localObject1).length() + 18 + "Unexpected state: " + (String)localObject1);
}
}
catch (Throwable localThrowable)
{
notifyFailed(localThrowable);
return this;
this.n = new a(Service.State.TERMINATED);
e(Service.State.NEW);
return this;
this.n = new a(Service.State.STARTING, true, null);
d(Service.State.STARTING);
continue;
}
finally
{
this.h.leave();
a();
}
this.n = new a(Service.State.STOPPING);
d(Service.State.RUNNING);
doStop();
continue;
String str = String.valueOf(String.valueOf(localObject2));
throw new AssertionError(str.length() + 45 + "isStoppable is incorrectly implemented, saw: " + str);
}
}
public String toString()
{
String str1 = String.valueOf(String.valueOf(getClass().getSimpleName()));
String str2 = String.valueOf(String.valueOf(state()));
return str1.length() + 3 + str2.length() + str1 + " [" + str2 + "]";
}
@Immutable
static final class a
{
final Service.State a;
final boolean b;
@Nullable
final Throwable c;
a(Service.State paramState)
{
this(paramState, false, null);
}
a(Service.State paramState, boolean paramBoolean, @Nullable Throwable paramThrowable)
{
int i;
label40:
int j;
if ((!paramBoolean) || (paramState == Service.State.STARTING))
{
bool = true;
Preconditions.checkArgument(bool, "shudownWhenStartupFinishes can only be set if state is STARTING. Got %s instead.", new Object[] { paramState });
if (paramThrowable == null) {
break label102;
}
i = 1;
if (paramState != Service.State.FAILED) {
break label108;
}
j = 1;
label50:
if ((i ^ j) != 0) {
break label114;
}
}
label102:
label108:
label114:
for (boolean bool = true;; bool = false)
{
Preconditions.checkArgument(bool, "A failure cause should be set if and only if the state is failed. Got %s and %s instead.", new Object[] { paramState, paramThrowable });
this.a = paramState;
this.b = paramBoolean;
this.c = paramThrowable;
return;
bool = false;
break;
i = 0;
break label40;
j = 0;
break label50;
}
}
}
}
/* Location: /home/merong/decompile/hackery-dex2jar.jar!/com/google/common/util/concurrent/AbstractService.class
* Java compiler version: 6 (50.0)
* JD-Core Version: 0.7.1
*/