comparison src/org/eclipse/jetty/util/component/AggregateLifeCycle.java @ 912:1d0c304e12b5

simplify AggregateLifeCycle
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 08 Oct 2016 22:05:14 -0600
parents 9b65e8064f90
children
comparison
equal deleted inserted replaced
911:cab5830e1ab0 912:1d0c304e12b5
49 private final List<Bean> _beans=new CopyOnWriteArrayList<Bean>(); 49 private final List<Bean> _beans=new CopyOnWriteArrayList<Bean>();
50 private boolean _started=false; 50 private boolean _started=false;
51 51
52 private class Bean 52 private class Bean
53 { 53 {
54 Bean(Object b) 54 Bean(LifeCycle b,boolean managed)
55 { 55 {
56 _bean=b; 56 _bean = b;
57 } 57 _managed = managed;
58 final Object _bean; 58 }
59 volatile boolean _managed=true; 59 final LifeCycle _bean;
60 final boolean _managed;
60 61
61 public String toString() 62 public String toString()
62 { 63 {
63 return "{"+_bean+","+_managed+"}"; 64 return "{"+_bean+","+_managed+"}";
64 } 65 }
72 @Override 73 @Override
73 protected void doStart() throws Exception 74 protected void doStart() throws Exception
74 { 75 {
75 for (Bean b:_beans) 76 for (Bean b:_beans)
76 { 77 {
77 if (b._managed && b._bean instanceof LifeCycle) 78 if (b._managed)
78 { 79 {
79 LifeCycle l=(LifeCycle)b._bean; 80 LifeCycle l = b._bean;
80 if (!l.isRunning()) 81 if (!l.isRunning())
81 l.start(); 82 l.start();
82 } 83 }
83 } 84 }
84 // indicate that we are started, so that addBean will start other beans added. 85 // indicate that we are started, so that addBean will start other beans added.
85 _started=true; 86 _started = true;
86 super.doStart(); 87 super.doStart();
87 } 88 }
88 89
89 /* ------------------------------------------------------------ */ 90 /* ------------------------------------------------------------ */
90 /** 91 /**
92 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart() 93 * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart()
93 */ 94 */
94 @Override 95 @Override
95 protected void doStop() throws Exception 96 protected void doStop() throws Exception
96 { 97 {
97 _started=false; 98 _started = false;
98 super.doStop(); 99 super.doStop();
99 List<Bean> reverse = new ArrayList<Bean>(_beans); 100 List<Bean> reverse = new ArrayList<Bean>(_beans);
100 Collections.reverse(reverse); 101 Collections.reverse(reverse);
101 for (Bean b:reverse) 102 for (Bean b:reverse)
102 { 103 {
103 if (b._managed && b._bean instanceof LifeCycle) 104 if (b._managed)
104 { 105 {
105 LifeCycle l=(LifeCycle)b._bean; 106 LifeCycle l = b._bean;
106 if (l.isRunning()) 107 if (l.isRunning())
107 l.stop(); 108 l.stop();
108 } 109 }
109 } 110 }
110 } 111 }
121 Collections.reverse(reverse); 122 Collections.reverse(reverse);
122 for (Bean b:reverse) 123 for (Bean b:reverse)
123 { 124 {
124 if (b._bean instanceof Destroyable && b._managed) 125 if (b._bean instanceof Destroyable && b._managed)
125 { 126 {
126 Destroyable d=(Destroyable)b._bean; 127 Destroyable d = (Destroyable)b._bean;
127 d.destroy(); 128 d.destroy();
128 } 129 }
129 } 130 }
130 _beans.clear(); 131 _beans.clear();
131 } 132 }
134 /* ------------------------------------------------------------ */ 135 /* ------------------------------------------------------------ */
135 /** Is the bean contained in the aggregate. 136 /** Is the bean contained in the aggregate.
136 * @param bean 137 * @param bean
137 * @return True if the aggregate contains the bean 138 * @return True if the aggregate contains the bean
138 */ 139 */
139 private boolean contains(Object bean) 140 private boolean contains(LifeCycle bean)
140 { 141 {
141 for (Bean b:_beans) 142 for (Bean b:_beans)
142 if (b._bean==bean) 143 if (b._bean==bean)
143 return true; 144 return true;
144 return false; 145 return false;
152 * method should be used if this is not correct, or the {@link #manage(Object)} and {@link #unmanage(Object)} 153 * method should be used if this is not correct, or the {@link #manage(Object)} and {@link #unmanage(Object)}
153 * methods may be used after an add to change the status. 154 * methods may be used after an add to change the status.
154 * @param o the bean object to add 155 * @param o the bean object to add
155 * @return true if the bean was added or false if it has already been added. 156 * @return true if the bean was added or false if it has already been added.
156 */ 157 */
157 public boolean addBean(Object o) 158 public boolean addBean(LifeCycle o)
158 { 159 {
159 // beans are joined unless they are started lifecycles 160 // beans are joined unless they are started lifecycles
160 return addBean(o,!((o instanceof LifeCycle)&&((LifeCycle)o).isStarted())); 161 return addBean(o,!o.isStarted());
161 } 162 }
162 163
163 /* ------------------------------------------------------------ */ 164 /* ------------------------------------------------------------ */
164 /** Add an associated lifecycle. 165 /** Add an associated lifecycle.
165 * @param o The lifecycle to add 166 * @param o The lifecycle to add
166 * @param managed True if the LifeCycle is to be joined, otherwise it will be disjoint. 167 * @param managed True if the LifeCycle is to be joined, otherwise it will be disjoint.
167 * @return true if bean was added, false if already present. 168 * @return true if bean was added, false if already present.
168 */ 169 */
169 public boolean addBean(Object o, boolean managed) 170 public boolean addBean(LifeCycle o, boolean managed)
170 { 171 {
171 if (contains(o)) 172 if (contains(o))
172 return false; 173 return false;
173 174
174 Bean b = new Bean(o); 175 Bean b = new Bean(o,managed);
175 b._managed=managed;
176 _beans.add(b); 176 _beans.add(b);
177 177
178 if (o instanceof LifeCycle) 178 // Start the bean if we are started
179 { 179 if (managed && _started)
180 LifeCycle l=(LifeCycle)o; 180 {
181 181 try
182 // Start the bean if we are started 182 {
183 if (managed && _started) 183 o.start();
184 { 184 }
185 try 185 catch(Exception e)
186 { 186 {
187 l.start(); 187 throw new RuntimeException(e);
188 }
189 catch(Exception e)
190 {
191 throw new RuntimeException (e);
192 }
193 } 188 }
194 } 189 }
195 return true; 190 return true;
196 } 191 }
197 192
199 /** Get dependent beans 194 /** Get dependent beans
200 * @return List of beans. 195 * @return List of beans.
201 */ 196 */
202 public Collection<Object> getBeans() 197 public Collection<Object> getBeans()
203 { 198 {
204 List<Object> beans = new ArrayList<Object>(); 199 // return new ArrayList<Object>(_beans);
205 beans.addAll(_beans); 200 ArrayList<Object> beans = new ArrayList<Object>();
201 for (Bean b:_beans) {
202 beans.add(b._bean);
203 }
206 return beans; 204 return beans;
207 } 205 }
208 206
209 207
210 /* ------------------------------------------------------------ */ 208 /* ------------------------------------------------------------ */
211 /** Get dependent beans of a specific class.
212 * If more than one bean of the type exist, the first is returned.
213 * @see #addBean(Object)
214 * @param clazz
215 * @return bean or null
216 */
217 public <T> T getBean(Class<T> clazz)
218 {
219 for (Bean b:_beans)
220 {
221 if (clazz.isInstance(b._bean))
222 return (T)b._bean;
223 }
224
225 return null;
226 }
227
228 /* ------------------------------------------------------------ */
229 /** 209 /**
230 * Remove an associated bean. 210 * Remove an associated bean.
231 */ 211 */
232 public boolean removeBean (Object o) 212 public boolean removeBean (LifeCycle o)
233 { 213 {
234 Iterator<Bean> i = _beans.iterator(); 214 Iterator<Bean> i = _beans.iterator();
235 while(i.hasNext()) 215 while(i.hasNext())
236 { 216 {
237 Bean b=i.next(); 217 Bean b=i.next();
242 } 222 }
243 } 223 }
244 return false; 224 return false;
245 } 225 }
246 226
247 /* ------------------------------------------------------------ */
248 public void dumpStdErr()
249 {
250 try
251 {
252 dump(System.err,"");
253 }
254 catch (IOException e)
255 {
256 LOG.warn("",e);
257 }
258 }
259
260 /* ------------------------------------------------------------ */
261 public String dump() 227 public String dump()
262 { 228 {
263 return dump(this); 229 return dump(this);
264 } 230 }
265 231
266 /* ------------------------------------------------------------ */
267 public static String dump(Dumpable dumpable) 232 public static String dump(Dumpable dumpable)
268 { 233 {
269 StringBuilder b = new StringBuilder(); 234 StringBuilder b = new StringBuilder();
270 try 235 try
271 { 236 {
276 LOG.warn("",e); 241 LOG.warn("",e);
277 } 242 }
278 return b.toString(); 243 return b.toString();
279 } 244 }
280 245
281 /* ------------------------------------------------------------ */ 246 private void dump(Appendable out) throws IOException
282 public void dump(Appendable out) throws IOException
283 { 247 {
284 dump(out,""); 248 dump(out,"");
285 } 249 }
286 250
287 /* ------------------------------------------------------------ */ 251 private void dumpThis(Appendable out) throws IOException
288 protected void dumpThis(Appendable out) throws IOException
289 { 252 {
290 out.append(String.valueOf(this)).append(" - ").append(getState()).append("\n"); 253 out.append(String.valueOf(this)).append(" - ").append(getState()).append("\n");
291 } 254 }
292 255
293 /* ------------------------------------------------------------ */
294 public static void dumpObject(Appendable out,Object o) throws IOException 256 public static void dumpObject(Appendable out,Object o) throws IOException
295 { 257 {
296 try 258 try
297 { 259 {
298 if (o instanceof LifeCycle) 260 if (o instanceof LifeCycle)
304 { 266 {
305 out.append(" => ").append(th.toString()).append('\n'); 267 out.append(" => ").append(th.toString()).append('\n');
306 } 268 }
307 } 269 }
308 270
309 /* ------------------------------------------------------------ */
310 public void dump(Appendable out,String indent) throws IOException 271 public void dump(Appendable out,String indent) throws IOException
311 { 272 {
312 dumpThis(out); 273 dumpThis(out);
313 int size=_beans.size(); 274 int size=_beans.size();
314 if (size==0) 275 if (size==0)
332 293
333 if (i!=size) 294 if (i!=size)
334 out.append(indent).append(" |\n"); 295 out.append(indent).append(" |\n");
335 } 296 }
336 297
337 /* ------------------------------------------------------------ */
338 public static void dump(Appendable out,String indent,Collection<?>... collections) throws IOException 298 public static void dump(Appendable out,String indent,Collection<?>... collections) throws IOException
339 { 299 {
340 if (collections.length==0) 300 if (collections.length==0)
341 return; 301 return;
342 int size=0; 302 int size=0;