1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
package net.sf.tlc.kernel; |
7 |
|
|
8 |
|
import java.util.Iterator; |
9 |
|
|
10 |
|
import net.sf.tlc.core.LifecycleAware; |
11 |
|
import net.sf.tlc.core.impl.DefaultServiceLocator; |
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
0 |
public final class TlcMicroKernel extends DefaultServiceLocator implements LifecycleAware { |
19 |
|
|
20 |
|
private static final int STATE_UNKNOWN = 0; |
21 |
|
|
22 |
|
private static final int STATE_INITIALIZING = 1; |
23 |
|
|
24 |
|
private static final int STATE_RUNNING = 2; |
25 |
|
|
26 |
|
private static final int STATE_CLEANUP = 3; |
27 |
|
|
28 |
|
private static final int STATE_SHUTDOWN = 4; |
29 |
|
|
30 |
0 |
private int state = STATE_UNKNOWN; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
private void setState(final int state) { |
37 |
0 |
this.state = state; |
38 |
0 |
} |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
private int getState() { |
44 |
0 |
return this.state; |
45 |
|
} |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
public void start() { |
53 |
0 |
if (TlcMicroKernel.STATE_UNKNOWN != getState()) { |
54 |
0 |
throw new IllegalStateException("Invalid state!"); |
55 |
|
} |
56 |
0 |
setState(TlcMicroKernel.STATE_INITIALIZING); |
57 |
0 |
startAllServices(); |
58 |
0 |
setState(TlcMicroKernel.STATE_RUNNING); |
59 |
0 |
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private void startAllServices() { |
65 |
0 |
final Iterator i = servicesSet().iterator(); |
66 |
0 |
while (i.hasNext()) { |
67 |
0 |
final Object o = i.next(); |
68 |
0 |
if (o instanceof Lclass="keyword">ifecycleAware) { |
69 |
0 |
((LifecycleAware) o).start(); |
70 |
|
} |
71 |
|
} |
72 |
0 |
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
public boolean isRunning() { |
80 |
0 |
return (TlcMicroKernel.STATE_RUNNING == getState()); |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
public void stop() { |
89 |
0 |
if (TlcMicroKernel.STATE_RUNNING != getState()) { |
90 |
0 |
throw new IllegalStateException("Invalid state!"); |
91 |
|
} |
92 |
0 |
setState(TlcMicroKernel.STATE_CLEANUP); |
93 |
0 |
stopAllServices(); |
94 |
0 |
waitForAllServices(); |
95 |
0 |
setState(TlcMicroKernel.STATE_SHUTDOWN); |
96 |
0 |
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
private void stopAllServices() { |
102 |
0 |
final Iterator i = servicesSet().iterator(); |
103 |
0 |
while (i.hasNext()) { |
104 |
0 |
final Object o = i.next(); |
105 |
0 |
if (o instanceof Lclass="keyword">ifecycleAware) { |
106 |
0 |
((LifecycleAware) o).stop(); |
107 |
|
} else { |
108 |
0 |
unregister(o); |
109 |
|
} |
110 |
|
} |
111 |
0 |
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
private void waitForAllServices() { |
117 |
0 |
boolean stillRunning = true; |
118 |
0 |
while (stillRunning) { |
119 |
0 |
stillRunning = false; |
120 |
0 |
final Iterator i = servicesSet().iterator(); |
121 |
0 |
while (i.hasNext()) { |
122 |
0 |
final Object o = i.next(); |
123 |
0 |
if (o instanceof Lclass="keyword">ifecycleAware) { |
124 |
0 |
stillRunning |= ((LifecycleAware) o).isRunning(); |
125 |
|
} |
126 |
|
} |
127 |
|
} |
128 |
0 |
} |
129 |
|
} |