KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > util > ComponentStateValidator


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.util;
9
10 import org.apache.avalon.framework.activity.Disposable;
11 import org.apache.avalon.framework.activity.Initializable;
12 import org.apache.avalon.framework.activity.Startable;
13 import org.apache.avalon.framework.activity.Suspendable;
14 import org.apache.avalon.framework.component.Composable;
15 import org.apache.avalon.framework.configuration.Configurable;
16 import org.apache.avalon.framework.context.Contextualizable;
17 import org.apache.avalon.framework.logger.LogEnabled;
18 import org.apache.avalon.framework.logger.Loggable;
19 import org.apache.avalon.framework.parameters.Parameterizable;
20
21 /**
22  * This class provides basic facilities for enforcing Avalon's contracts
23  * within your own code.
24  *
25  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
26  * @author <a HREF="mailto:mike@hihat.net">Michael McKibben</a>
27  * @version CVS $Revision: 1.7 $ $Date: 2002/01/17 15:02:01 $
28  */

29 public final class ComponentStateValidator
30 {
31     private static final String JavaDoc OOO_FAIL = "Initialization out of order";
32     private static final String JavaDoc LOG_FAIL = OOO_FAIL + ": LogEnabled";
33     private static final String JavaDoc CONTEXT_FAIL = OOO_FAIL + ": Contextualizable";
34     private static final String JavaDoc PARAMETER_FAIL = OOO_FAIL + ": Parameterizable";
35     private static final String JavaDoc CONFIGURE_FAIL = OOO_FAIL + ": Configurable";
36     private static final String JavaDoc COMPOSE_FAIL = OOO_FAIL + ": Composable";
37     private static final String JavaDoc INIT_FAIL = OOO_FAIL + ": Initializable";
38     private static final String JavaDoc START_FAIL = OOO_FAIL + ": Startable.start()";
39     private static final String JavaDoc STOP_FAIL = OOO_FAIL + ": Startable.stop()";
40     private static final String JavaDoc SUSPEND_FAIL = OOO_FAIL + ": Suspendable.suspend()";
41     private static final String JavaDoc RESUME_FAIL = OOO_FAIL + ": Suspendable.resume()";
42     private static final String JavaDoc DISPOSE_FAIL = OOO_FAIL + ": Disposable";
43     private static final String JavaDoc ACTIVE_FAIL = "Component is not Active";
44     private static final String JavaDoc WRITE_FAIL = "Value is already bound";
45
46     private static final long LOG_ENABLED = 0x00000001;
47     private static final long CONTEXTUALIZED = 0x00000002;
48     private static final long COMPOSED = 0x00000004;
49     private static final long CONFIGURED = 0x00000008;
50     private static final long PARAMETERIZED = 0x00000010;
51     private static final long INITIALIZED = 0x00000020;
52     private static final long STARTED = 0x00000040;
53     private static final long ACTIVE = 0x10000000;
54     private static final long SUSPENDED = 0x01000000;
55     private static final long STOPPED = 0x00000080;
56     private static final long DISPOSED = 0x00000100;
57
58     private static final long LOG_MASK = LOG_ENABLED;
59     private static final long CONTEXT_MASK = LOG_MASK | CONTEXTUALIZED;
60     private static final long COMPOSE_MASK = CONTEXT_MASK | COMPOSED;
61     private static final long CONFIGURE_MASK = COMPOSE_MASK | CONFIGURED;
62     private static final long PARAMETER_MASK = CONFIGURE_MASK | PARAMETERIZED;
63     private static final long INIT_MASK = PARAMETER_MASK | INITIALIZED;
64     private static final long START_MASK = INIT_MASK | STARTED;
65     private static final long STOP_MASK = START_MASK | STOPPED;
66     private static final long DISPOSE_MASK = STOP_MASK | DISPOSED;
67
68     private final long m_mask;
69     private long m_state;
70
71     /**
72      * Create state validator from object (this can be used for more than just
73      * components).
74      */

75     public ComponentStateValidator( final Object JavaDoc object )
76     {
77         int mask = 0;
78
79         if ( object instanceof LogEnabled ||
80              object instanceof Loggable )
81         {
82             mask |= LOG_ENABLED;
83         }
84
85         if ( object instanceof Contextualizable )
86         {
87             mask |= CONTEXTUALIZED;
88         }
89
90         if ( object instanceof Parameterizable )
91         {
92             mask |= PARAMETERIZED;
93         }
94
95         if ( object instanceof Configurable )
96         {
97             mask |= CONFIGURED;
98         }
99
100         if ( object instanceof Composable )
101         {
102             mask |= COMPOSED;
103         }
104
105         if ( object instanceof Initializable )
106         {
107             mask |= INITIALIZED;
108         }
109
110         if ( object instanceof Disposable )
111         {
112             mask |= DISPOSED;
113         }
114
115         if ( object instanceof Startable )
116         {
117             mask |= STARTED | STOPPED;
118         }
119
120         if ( object instanceof Suspendable )
121         {
122             mask |= SUSPENDED;
123         }
124
125         m_mask = mask & ~ACTIVE;
126     }
127
128     /**
129      * Throw an exception if the initialization is out of order. It tests to see
130      * if the LOG_ENABLED state has already been set, if the component implements
131      * LogEnabled or Logger, and if the state has progressed beyond the Logger
132      * stage.
133      *
134      * @throws IllegalStateException if the state is manage out of order
135      */

136     public void checkLogEnabled()
137     {
138         checkLogEnabled( LOG_FAIL );
139     }
140
141     /**
142      * Throw an exception if the initialization is out of order. It tests to see
143      * if the LOG_ENABLED state has already been set, if the component implements
144      * LogEnabled or Logger, and if the state has progressed beyond the Logger
145      * stage.
146      *
147      * @param message the message to include in the thrown exception
148      * @throws IllegalStateException if the state is manage out of order
149      */

150     public void checkLogEnabled( final String JavaDoc message )
151     {
152         boolean isValid = false;
153         isValid = (m_state & m_mask & LOG_ENABLED) == 0;
154         isValid = isValid && ((m_mask & LOG_MASK) | LOG_ENABLED) ==
155                              ((m_state & DISPOSE_MASK) | LOG_ENABLED);
156
157         if ( ! isValid )
158         {
159             throw new IllegalStateException JavaDoc( message );
160         }
161
162         m_state |= LOG_ENABLED;
163         if ( (m_state & START_MASK) == (m_mask & START_MASK) )
164         {
165             m_state |= ACTIVE;
166         }
167     }
168
169     /**
170      * Throw an exception if the initialization is out of order. It tests to see
171      * if the CONTEXTUALIZED state has already been set, if the component implements
172      * Contextualizable, and if the state has progressed beyond the Context stage.
173      *
174      * @throws IllegalStateException if the state is manage out of order
175      */

176     public void checkContextualized()
177     {
178         checkContextualized(CONTEXT_FAIL);
179     }
180
181     /**
182      * Throw an exception if the initialization is out of order. It tests to see
183      * if the CONTEXTUALIZED state has already been set, if the component implements
184      * Contextualizable, and if the state has progressed beyond the Context stage.
185      *
186      * @param message the message to include in the thrown exception
187      * @throws IllegalStateException if the state is manage out of order
188      */

189     public void checkContextualized( final String JavaDoc message )
190     {
191         boolean isValid = false;
192         isValid = (m_state & m_mask & CONTEXTUALIZED) == 0;
193         isValid = isValid && ((m_mask & CONTEXT_MASK) | CONTEXTUALIZED) ==
194                              ((m_state & DISPOSE_MASK) | CONTEXTUALIZED);
195
196         if ( ! isValid )
197         {
198             throw new IllegalStateException JavaDoc( message );
199         }
200
201         m_state |= CONTEXTUALIZED;
202         if ( (m_state & START_MASK) == (m_mask & START_MASK) )
203         {
204             m_state |= ACTIVE;
205         }
206     }
207
208     /**
209      * Throw an exception if the initialization is out of order. It tests to see
210      * if the PARAMETERIZED state has already been set, if the component implements
211      * Parameterizable, and if the state has progressed beyond the Parameters stage.
212      *
213      * @throws IllegalStateException if the state is manage out of order
214      */

215     public void checkParameterized()
216     {
217         checkParameterized( PARAMETER_FAIL );
218     }
219
220     /**
221      * Throw an exception if the initialization is out of order. It tests to see
222      * if the PARAMETERIZED state has already been set, if the component implements
223      * Parameterizable, and if the state has progressed beyond the Parameters stage.
224      *
225      * @param message the message to include in the thrown exception
226      * @throws IllegalStateException if the state is manage out of order
227      */

228     public void checkParameterized( final String JavaDoc message )
229     {
230         boolean isValid = false;
231         isValid = (m_state & m_mask & PARAMETERIZED) == 0;
232         isValid = isValid && ((m_mask & PARAMETER_MASK) | PARAMETERIZED) ==
233                              ((m_state & DISPOSE_MASK) | PARAMETERIZED);
234
235         if ( ! isValid )
236         {
237             throw new IllegalStateException JavaDoc( message );
238         }
239
240         m_state |= PARAMETERIZED;
241         if ( (m_state & START_MASK) == (m_mask & START_MASK) )
242         {
243             m_state |= ACTIVE;
244         }
245     }
246
247     /**
248      * Throw an exception if the initialization is out of order. It tests to see
249      * if the CONFIGURED state has already been set, if the component implements
250      * Configurable, and if the state has progressed beyond the Configuration stage.
251      *
252      * @throws IllegalStateException if the state is manage out of order
253      */

254     public void checkConfigured()
255     {
256         checkConfigured( CONFIGURE_FAIL );
257     }
258
259     /**
260      * Throw an exception if the initialization is out of order. It tests to see
261      * if the CONFIGURED state has already been set, if the component implements
262      * Configurable, and if the state has progressed beyond the Configuration stage.
263      *
264      * @param message the message to include in the thrown exception
265      * @throws IllegalStateException if the state is manage out of order
266      */

267     public void checkConfigured( final String JavaDoc message )
268     {
269         boolean isValid = false;
270         isValid = (m_state & m_mask & CONFIGURED) == 0;
271         isValid = isValid && ((m_mask & CONFIGURE_MASK) | CONFIGURED) ==
272                              ((m_state & DISPOSE_MASK) | CONFIGURED);
273
274         if ( ! isValid )
275         {
276             throw new IllegalStateException JavaDoc( message );
277         }
278
279         m_state |= CONFIGURED;
280         if ( (m_state & START_MASK) == (m_mask & START_MASK) )
281         {
282             m_state |= ACTIVE;
283         }
284     }
285
286     /**
287      * Throw an exception if the initialization is out of order. It tests to see
288      * if the COMPOSED state has already been set, if the component implements
289      * Composable, and if the state has progressed beyond the Configuration stage.
290      *
291      * @throws IllegalStateException if the state is manage out of order
292      */

293     public void checkComposed()
294     {
295         checkComposed( COMPOSE_FAIL );
296     }
297
298     /**
299      * Throw an exception if the initialization is out of order. It tests to see
300      * if the COMPOSED state has already been set, if the component implements
301      * Composable, and if the state has progressed beyond the Configuration stage.
302      *
303      * @param message the message to include in the thrown exception
304      * @throws IllegalStateException if the state is manage out of order
305      */

306     public void checkComposed( final String JavaDoc message )
307     {
308         boolean isValid = false;
309         isValid = (m_state & m_mask & COMPOSED) == 0;
310         isValid = isValid && ((m_mask & COMPOSE_MASK) | COMPOSED) ==
311                              ((m_state & DISPOSE_MASK) | COMPOSED);
312
313         if ( ! isValid )
314         {
315             throw new IllegalStateException JavaDoc( message );
316         }
317
318         m_state |= COMPOSED;
319         if ( (m_state & START_MASK) == (m_mask & START_MASK) )
320         {
321             m_state |= ACTIVE;
322         }
323     }
324
325     /**
326      * Throw an exception if the initialization is out of order. It tests to see
327      * if the INITIALIZED state has already been set, if the component implements
328      * Initializable, and if the state has progressed beyond the <code>initialize</code> stage.
329      *
330      * @throws IllegalStateException if the state is manage out of order
331      */

332     public void checkInitialized()
333     {
334         checkInitialized( INIT_FAIL );
335     }
336
337     /**
338      * Throw an exception if the initialization is out of order. It tests to see
339      * if the INITIALIZED state has already been set, if the component implements
340      * Initializable, and if the state has progressed beyond the <code>initialize</code> stage.
341      *
342      * @param message the message to include in the thrown exception
343      * @throws IllegalStateException if the state is manage out of order
344      */

345     public void checkInitialized( final String JavaDoc message )
346     {
347         boolean isValid = false;
348         isValid = (m_state & m_mask & INITIALIZED) == 0;
349         isValid = isValid && ((m_mask & INIT_MASK) | INITIALIZED) ==
350                              ((m_state & DISPOSE_MASK) | INITIALIZED);
351
352         if ( ! isValid )
353         {
354             throw new IllegalStateException JavaDoc( message );
355         }
356
357         m_state |= INITIALIZED;
358         if ( (m_state & START_MASK) == (m_mask & START_MASK) )
359         {
360             m_state |= ACTIVE;
361         }
362     }
363
364     /**
365      * Throw an exception if the initialization is out of order. It tests to see
366      * if the STARTED state has already been set, if the component implements
367      * Startable, and if the state has progressed beyond the <code>start</code> stage.
368      *
369      * @throws IllegalStateException if the state is manage out of order
370      */

371     public void checkStarted()
372     {
373         checkStarted( START_FAIL );
374     }
375
376     /**
377      * Throw an exception if the initialization is out of order. It tests to see
378      * if the STARTED state has already been set, if the component implements
379      * Startable, and if the state has progressed beyond the <code>start</code> stage.
380      *
381      * @param message the message to include in the thrown exception
382      * @throws IllegalStateException if the state is manage out of order
383      */

384     public void checkStarted( final String JavaDoc message )
385     {
386         boolean isValid = false;
387         isValid = (m_state & m_mask & STARTED) == 0;
388         isValid = isValid && ((m_mask & START_MASK) | STARTED) ==
389                              ((m_state & DISPOSE_MASK) | STARTED);
390
391         if ( ! isValid )
392         {
393             throw new IllegalStateException JavaDoc( message );
394         }
395
396         m_state |= STARTED;
397         if ( (m_state & START_MASK) == (m_mask & START_MASK) )
398         {
399             m_state |= ACTIVE;
400         }
401     }
402
403     /**
404      * Throw an exception if the initialization is out of order. It tests to see
405      * if the SUSPENDED state has already been set, if the component implements
406      * Suspendable, and if the Component is active.
407      *
408      * @throws IllegalStateException if the state is manage out of order
409      */

410     public void checkSuspended()
411     {
412         checkSuspended( SUSPEND_FAIL );
413     }
414
415     /**
416      * Throw an exception if the initialization is out of order. It tests to see
417      * if the SUSPENDED state has already been set, if the component implements
418      * Suspendable, and if the Component is active.
419      *
420      * @param message the message to include in the thrown exception
421      * @throws IllegalStateException if the state is manage out of order
422      */

423     public void checkSuspended( final String JavaDoc message )
424     {
425         checkActive( message );
426         boolean isValid = false;
427         isValid = (m_state & m_mask & SUSPENDED) == 0;
428         isValid = isValid && ((m_mask & START_MASK) & ~SUSPENDED) ==
429                              ((m_state & DISPOSE_MASK) & ~SUSPENDED);
430
431         if ( ! isValid )
432         {
433             throw new IllegalStateException JavaDoc( message );
434         }
435
436         m_state |= SUSPENDED;
437     }
438
439     /**
440      * Throw an exception if the initialization is out of order. It tests to see
441      * if the SUSPENDED state has not been set, if the component implements
442      * Suspendable, and if the Component is active.
443      *
444      * @throws IllegalStateException if the state is manage out of order
445      */

446     public void checkResumed()
447     {
448         checkResumed( RESUME_FAIL );
449     }
450
451     /**
452      * Throw an exception if the initialization is out of order. It tests to see
453      * if the SUSPENDED state has not been set, if the component implements
454      * Suspendable, and if the Component is active.
455      *
456      * @param message the message to include in the thrown exception
457      * @throws IllegalStateException if the state is manage out of order
458      */

459     public void checkResumed( final String JavaDoc message )
460     {
461         checkActive( message );
462         boolean isValid = false;
463         isValid = (m_state & m_mask & SUSPENDED) > 0;
464         isValid = isValid && ((m_mask & START_MASK) | SUSPENDED) ==
465                              ((m_state & DISPOSE_MASK) | SUSPENDED);
466
467         if ( ! isValid )
468         {
469             throw new IllegalStateException JavaDoc( message );
470         }
471
472         m_state &= ~SUSPENDED;
473     }
474
475     /**
476      * Throw an exception if the initialization is out of order. It tests to see
477      * if the STOPPED state has not been set, if the component implements
478      * Startable, and if the Component is active.
479      *
480      * @throws IllegalStateException if the state is manage out of order
481      */

482     public void checkStopped()
483     {
484         checkStopped( STOP_FAIL );
485     }
486
487     /**
488      * Throw an exception if the initialization is out of order. It tests to see
489      * if the STOPPED state has not been set, if the component implements
490      * Startable, and if the Component is active.
491      *
492      * @param message the message to include in the thrown exception
493      * @throws IllegalStateException if the state is manage out of order
494      */

495     public void checkStopped( final String JavaDoc message )
496     {
497         boolean isValid = false;
498         isValid = (m_state & m_mask & STOPPED) == 0;
499         isValid = isValid && ((m_mask & STOP_MASK) | STOPPED) ==
500                              ((m_state & DISPOSE_MASK) | STOPPED);
501
502         if ( ! isValid )
503         {
504             throw new IllegalStateException JavaDoc( message );
505         }
506
507         m_state &= ~ACTIVE;
508         m_state |= STOPPED;
509     }
510
511     /**
512      * Throw an exception if the initialization is out of order. It tests to see
513      * if the DISPOSED state has not been set, if the component implements
514      * Disposable.
515      *
516      * @throws IllegalStateException if the state is manage out of order
517      */

518     public void checkDisposed()
519     {
520         checkDisposed( DISPOSE_FAIL );
521     }
522
523     /**
524      * Throw an exception if the initialization is out of order. It tests to see
525      * if the DISPOSED state has not been set, if the component implements
526      * Disposable.
527      *
528      * @param message the message to include in the thrown exception
529      * @throws IllegalStateException if the state is manage out of order
530      */

531     public void checkDisposed( final String JavaDoc message )
532     {
533         boolean isValid = false;
534         isValid = (m_state & m_mask & DISPOSED) == 0;
535         isValid = isValid && ((m_mask & DISPOSE_MASK) | DISPOSED) ==
536                              ((m_state & DISPOSE_MASK) | DISPOSED);
537
538         if ( ! isValid )
539         {
540             throw new IllegalStateException JavaDoc( message );
541         }
542
543         m_state &= ~ACTIVE;
544         m_state |= DISPOSED;
545     }
546
547     /**
548      * Checks to see if the state is active.
549      *
550      * @throws IllegalStateException if the component is not active
551      */

552     public void checkActive()
553     {
554         checkActive( ACTIVE_FAIL );
555     }
556
557     /**
558      * Checks to see if the state is active.
559      *
560      * @param message the message to include in the thrown exception
561      * @throws IllegalStateException if the component is not active
562      */

563     public void checkActive( final String JavaDoc message )
564     {
565         if ( isActive() )
566         {
567             return;
568         }
569
570         throw new IllegalStateException JavaDoc( message );
571     }
572
573     /**
574      * Checks to see if the state is active, and returns true or false.
575      *
576      * @returns <code>true</code> if active, <code>false</code> if not
577      */

578     public boolean isActive()
579     {
580         if ( (ACTIVE & m_state) > 0 )
581         {
582             return true;
583         }
584
585         return false;
586     }
587
588     /**
589      * Make sure object has not been assigned yet.
590      *
591      * @param object to test
592      * @throws IllegalStateException if the state is manage out of order
593      */

594     public void checkNotAssigned( final Object JavaDoc object )
595     {
596         checkNotAssigned( object, WRITE_FAIL );
597     }
598
599     /**
600      * Make sure object has not been assigned yet.
601      *
602      * @param object to test
603      * @param message the message to include in the thrown exception
604      * @throws IllegalStateException if the state is manage out of order
605      */

606     public void checkNotAssigned( final Object JavaDoc object, final String JavaDoc message )
607     {
608         if ( null != object )
609         {
610             throw new IllegalStateException JavaDoc( message );
611         }
612     }
613 }
614
615
Popular Tags