KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.excalibur.testcase;
18
19 import org.apache.avalon.framework.activity.Disposable;
20 import org.apache.avalon.framework.activity.Initializable;
21 import org.apache.avalon.framework.activity.Startable;
22 import org.apache.avalon.framework.activity.Suspendable;
23 import org.apache.avalon.framework.component.Composable;
24 import org.apache.avalon.framework.component.Recomposable;
25 import org.apache.avalon.framework.configuration.Configurable;
26 import org.apache.avalon.framework.configuration.Reconfigurable;
27 import org.apache.avalon.framework.context.Contextualizable;
28 import org.apache.avalon.framework.context.Recontextualizable;
29 import org.apache.avalon.framework.logger.LogEnabled;
30 import org.apache.avalon.framework.logger.Loggable;
31 import org.apache.avalon.framework.parameters.Parameterizable;
32 import org.apache.avalon.framework.service.Serviceable;
33
34 /**
35  * This class provides basic facilities for enforcing Avalon's contracts
36  * within your own code.
37  *
38  * Based on Avalon version from Sandbox.
39  *
40  * @deprecated ECM is no longer supported
41  *
42  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
43  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:27 $
44  */

45 public final class ComponentStateValidator
46 {
47     private static final String JavaDoc WRITE_FAIL = "Value is already bound";
48
49     // Interfaces
50
private static final long LOG_ENABLED = 0x00000001;
51     private static final long LOGGABLE = 0x00000002;
52     private static final long CONTEXTUALIZABLE = 0x00000004;
53     private static final long COMPOSABLE = 0x00000008;
54     private static final long SERVICEABLE = 0x00000010;
55     private static final long CONFIGURABLE = 0x00000020;
56     private static final long PARAMETERIZABLE = 0x00000040;
57     private static final long INITIALIZABLE = 0x00000080;
58     private static final long STARTABLE = 0x00000100;
59     private static final long SUSPENDABLE = 0x00001000;
60     private static final long RECONTEXTUALIZABLE = 0x00002000;
61     private static final long RECOMPOSABLE = 0x00004000;
62     private static final long RECONFIGURABLE = 0x00008000;
63     private static final long DISPOSABLE = 0x00100000;
64
65     // Initialization Methods.
66
private static final long ENABLE_LOGGING = 0x00000001;
67     private static final long SET_LOGGER = 0x00000002;
68     private static final long CONTEXTUALIZE = 0x00000004;
69     private static final long SERVICE = 0x00000008;
70     private static final long COMPOSE = 0x00000010;
71     private static final long CONFIGURE = 0x00000020;
72     private static final long PARAMETERIZE = 0x00000040;
73     private static final long INITIALIZE = 0x00000080;
74     private static final long START = 0x00000100;
75     private static final long INIT_COMPLETE = 0x00000400;
76
77     // Active Service Methods
78
private static final long SUSPEND = 0x00001000;
79     private static final long RECONTEXTUALIZE = 0x00002000;
80     private static final long RECOMPOSE = 0x00004000;
81     private static final long RECONFIGURE = 0x00008000;
82     private static final long RESUME = 0x00010000;
83
84     // Destruction Methods
85
private static final long STOP = 0x00100000;
86     private static final long DISPOSE = 0x00200000;
87
88     // Masks
89
private static final long INIT_MASK = ENABLE_LOGGING | SET_LOGGER |
90         CONTEXTUALIZE | COMPOSE | SERVICE | CONFIGURE | PARAMETERIZE | INITIALIZE |
91         START;
92
93     private final long m_interfaces;
94     private final long m_methods;
95     private long m_state;
96     private boolean m_active;
97     private final Object JavaDoc m_object;
98
99     /**
100      * Create state validator from object (this can be used for more than just
101      * components).
102      */

103     public ComponentStateValidator( final Object JavaDoc object )
104     {
105         m_object = object;
106
107         long methods = 0;
108         long interfaces = 0;
109
110         if( object instanceof LogEnabled )
111         {
112             interfaces |= LOG_ENABLED;
113             methods |= ENABLE_LOGGING;
114         }
115
116         if( object instanceof Loggable )
117         {
118             interfaces |= LOGGABLE;
119             methods |= SET_LOGGER;
120         }
121
122         if( object instanceof Contextualizable )
123         {
124             interfaces |= CONTEXTUALIZABLE;
125             methods |= CONTEXTUALIZE;
126         }
127
128         if( object instanceof Serviceable )
129         {
130             interfaces |= SERVICEABLE;
131             methods |= SERVICE;
132         }
133
134         if( object instanceof Composable )
135         {
136             if( ( interfaces & SERVICEABLE ) > 0 )
137             {
138                 throw new IllegalStateException JavaDoc( "Cannot implement Composable and Serviceable together" );
139             }
140
141             interfaces |= COMPOSABLE;
142             methods |= COMPOSE;
143         }
144
145         if( object instanceof Configurable )
146         {
147             interfaces |= CONFIGURABLE;
148             methods |= CONFIGURE;
149         }
150
151         if( object instanceof Parameterizable )
152         {
153             interfaces |= PARAMETERIZABLE;
154             methods |= PARAMETERIZE;
155         }
156
157         if( object instanceof Initializable )
158         {
159             interfaces |= INITIALIZABLE;
160             methods |= INITIALIZE;
161         }
162
163         if( object instanceof Startable )
164         {
165             interfaces |= STARTABLE;
166             methods |= START | STOP;
167         }
168
169         if( object instanceof Suspendable )
170         {
171             interfaces |= SUSPENDABLE;
172             methods |= SUSPEND | RESUME;
173         }
174
175         if( object instanceof Recontextualizable )
176         {
177             interfaces |= RECONTEXTUALIZABLE;
178             methods |= RECONTEXTUALIZE;
179         }
180
181         if( object instanceof Recomposable )
182         {
183             interfaces |= RECOMPOSABLE;
184             methods |= RECOMPOSE;
185         }
186
187         if( object instanceof Reconfigurable )
188         {
189             interfaces |= RECONFIGURABLE;
190             methods |= RECONFIGURE;
191         }
192
193         if( object instanceof Disposable )
194         {
195             interfaces |= DISPOSABLE;
196             methods |= DISPOSE;
197         }
198
199         m_methods = methods;
200         m_interfaces = interfaces;
201
202         generalCheckInitComplete();
203     }
204
205     private String JavaDoc getInterfaceName( long interfaceId )
206     {
207         if( interfaceId == LOG_ENABLED )
208         {
209             return LogEnabled.class.getName();
210         }
211         else if( interfaceId == LOGGABLE )
212         {
213             return Loggable.class.getName();
214         }
215         else if( interfaceId == CONTEXTUALIZABLE )
216         {
217             return Contextualizable.class.getName();
218         }
219         else if( interfaceId == SERVICEABLE )
220         {
221             return Serviceable.class.getName();
222         }
223         else if( interfaceId == COMPOSABLE )
224         {
225             return Composable.class.getName();
226         }
227         else if( interfaceId == CONFIGURABLE )
228         {
229             return Configurable.class.getName();
230         }
231         else if( interfaceId == PARAMETERIZABLE )
232         {
233             return Parameterizable.class.getName();
234         }
235         else if( interfaceId == INITIALIZABLE )
236         {
237             return Initializable.class.getName();
238         }
239         else if( interfaceId == STARTABLE )
240         {
241             return Startable.class.getName();
242         }
243         else if( interfaceId == SUSPENDABLE )
244         {
245             return Suspendable.class.getName();
246         }
247         else if( interfaceId == RECONTEXTUALIZABLE )
248         {
249             return Recontextualizable.class.getName();
250         }
251         else if( interfaceId == RECOMPOSABLE )
252         {
253             return Recomposable.class.getName();
254         }
255         else if( interfaceId == RECONFIGURABLE )
256         {
257             return Reconfigurable.class.getName();
258         }
259         else if( interfaceId == DISPOSABLE )
260         {
261             return Disposable.class.getName();
262         }
263         else
264         {
265             throw new IllegalStateException JavaDoc( "Unknown Interface Id " + interfaceId );
266         }
267     }
268
269     private String JavaDoc getMethodName( long methodId )
270     {
271         if( methodId == ENABLE_LOGGING )
272         {
273             return "enableLogging()";
274         }
275         else if( methodId == SET_LOGGER )
276         {
277             return "setLogger()";
278         }
279         else if( methodId == CONTEXTUALIZE )
280         {
281             return "contextualize()";
282         }
283         else if( methodId == SERVICE )
284         {
285             return "service()";
286         }
287         else if( methodId == COMPOSE )
288         {
289             return "compose()";
290         }
291         else if( methodId == CONFIGURE )
292         {
293             return "configure()";
294         }
295         else if( methodId == PARAMETERIZE )
296         {
297             return "parameterize()";
298         }
299         else if( methodId == INITIALIZE )
300         {
301             return "initialize()";
302         }
303         else if( methodId == START )
304         {
305             return "start()";
306         }
307         else if( methodId == SUSPEND )
308         {
309             return "suspend()";
310         }
311         else if( methodId == RECONTEXTUALIZE )
312         {
313             return "recontextualize()";
314         }
315         else if( methodId == RECOMPOSE )
316         {
317             return "recompose()";
318         }
319         else if( methodId == RECONFIGURE )
320         {
321             return "reconfigure()";
322         }
323         else if( methodId == RESUME )
324         {
325             return "resume()";
326         }
327         else if( methodId == STOP )
328         {
329             return "stop()";
330         }
331         else if( methodId == DISPOSE )
332         {
333             return "dispose()";
334         }
335         else
336         {
337             throw new IllegalStateException JavaDoc( "Unknown Method Id " + methodId );
338         }
339     }
340
341     private String JavaDoc getLastMethod( long state )
342     {
343         for( int i = 31; i >= 0; i-- )
344         {
345             long methodId = 0x1 << i;
346             if( ( state & methodId ) != 0 )
347             {
348                 return getMethodName( methodId );
349             }
350         }
351         throw new IllegalStateException JavaDoc( "No last state method found for state " + state );
352     }
353
354     /**
355      * Test to see if this was the last initialization method.
356      */

357     private void generalCheckInitComplete()
358     {
359         if( m_state == ( m_methods & INIT_MASK ) )
360         {
361             // All init methods called
362
m_active = true;
363         }
364     }
365
366     /**
367      * Initialization methods must be called in order, must all be called, may
368      * not be called more than once, and may not be called once any of the
369      * Descruction methods have been called.
370      */

371     private void generalCheckInit( final String JavaDoc message, final long interfaceId, final long methodId )
372     {
373         if( ( m_interfaces & interfaceId ) == 0 )
374         {
375             // Interface not implemented
376
if( message == null )
377             {
378                 throw new IllegalStateException JavaDoc( m_object.getClass().getName() +
379                                                  " does not implement " + getInterfaceName( interfaceId ) + "." );
380             }
381             else
382             {
383                 throw new IllegalStateException JavaDoc( message );
384             }
385         }
386         else if( ( m_state & methodId ) > 0 )
387         {
388             // Method already called.
389
if( message == null )
390             {
391                 throw new IllegalStateException JavaDoc( getMethodName( methodId ) + " already called." );
392             }
393             else
394             {
395                 throw new IllegalStateException JavaDoc( message );
396             }
397         }
398         else if( m_state > methodId )
399         {
400             // Method called after a descruction method was called.
401
if( message == null )
402             {
403                 throw new IllegalStateException JavaDoc( getMethodName( methodId ) +
404                                                  " can not be called after " + getLastMethod( m_state ) + "." );
405             }
406             else
407             {
408                 throw new IllegalStateException JavaDoc( message );
409             }
410         }
411         else if( ( m_state & ( methodId - 1 ) ) != ( m_methods & ( methodId - 1 ) ) )
412         {
413             // One or more of the methods that should have been called before
414
// this method was not.
415
if( message == null )
416             {
417                 throw new IllegalStateException JavaDoc( getMethodName( methodId ) +
418                                                  " called out of order. " + getLastMethod( m_methods & ( methodId - 1 ) ) +
419                                                  " must be called first." );
420             }
421             else
422             {
423                 throw new IllegalStateException JavaDoc( message );
424             }
425         }
426
427         // Add this method to the state
428
m_state |= methodId;
429
430         // See if the initialization is complete.
431
generalCheckInitComplete();
432     }
433
434     /**
435      * Active Service methods may only be called after all of the
436      * Initialization methods have been called, any before any of the
437      * Descruction methods have been called. While in the active state,
438      * the contracts of the methods allow the active state methods to be
439      * called any number of times, in any order.
440      * The resume() method should do nothing if suspend() has not yet been
441      * called for example.
442      */

443     private void generalCheckActive( final String JavaDoc message, final long interfaceId, final long methodId )
444     {
445         if( ( m_interfaces & interfaceId ) == 0 )
446         {
447             // Interface not implemented
448
if( message == null )
449             {
450                 throw new IllegalStateException JavaDoc( m_object.getClass().getName() +
451                                                  " does not implement " + getInterfaceName( interfaceId ) + "." );
452             }
453             else
454             {
455                 throw new IllegalStateException JavaDoc( message );
456             }
457         }
458         else if( !m_active )
459         {
460             // Component not in the active state.
461
if( m_state < INIT_COMPLETE )
462             {
463                 // Still expecting initialization methods.
464
if( message == null )
465                 {
466                     throw new IllegalStateException JavaDoc( getMethodName( methodId ) +
467                                                      " called before component was made active. " +
468                                                      getLastMethod( m_methods & ( INIT_COMPLETE - 1 ) ) +
469                                                      " must be called first." );
470                 }
471                 else
472                 {
473                     throw new IllegalStateException JavaDoc( message );
474                 }
475             }
476             else
477             {
478                 // One or more destruction methods have been called.
479
if( message == null )
480                 {
481                     throw new IllegalStateException JavaDoc( getMethodName( methodId ) +
482                                                      " called after component was made inactive. Cannot call after " +
483                                                      getLastMethod( m_state ) + "." );
484                 }
485                 else
486                 {
487                     throw new IllegalStateException JavaDoc( message );
488                 }
489             }
490         }
491     }
492
493     /**
494      * Descruction Methods must be called in order. They may be called before
495      * all of the Initialization methods have been called if there was an
496      * error.
497      */

498     private void generalCheckDest( final String JavaDoc message, final long interfaceId, final long methodId )
499     {
500         if( ( m_interfaces & interfaceId ) == 0 )
501         {
502             // Interface not implemented
503
if( message == null )
504             {
505                 throw new IllegalStateException JavaDoc( m_object.getClass().getName() +
506                                                  " does not implement " + getInterfaceName( interfaceId ) + "." );
507             }
508             else
509             {
510                 throw new IllegalStateException JavaDoc( message );
511             }
512         }
513         else if( m_state > methodId )
514         {
515             // Method called after a later descruction method was called.
516
if( message == null )
517             {
518                 throw new IllegalStateException JavaDoc( getMethodName( methodId ) +
519                                                  " can not be called after " + getLastMethod( m_state ) + "." );
520             }
521             else
522             {
523                 throw new IllegalStateException JavaDoc( message );
524             }
525         }
526
527         // Add this method to the state
528
m_state |= methodId;
529
530         // Deactivate
531
m_active = false;
532     }
533
534     /**
535      * Throw an exception if the initialization is out of order. It tests to see
536      * if the ENABLE_LOGGING state has already been set, if the component implements
537      * LogEnabled, and if the state has progressed beyond the Logger stage.
538      *
539      * @throws IllegalStateException if the state is manage out of order
540      */

541     public void checkLogEnabled()
542     {
543         checkLogEnabled( null );
544     }
545
546     /**
547      * Throw an exception if the initialization is out of order. It tests to see
548      * if the ENABLE_LOGGING state has already been set, if the component implements
549      * LogEnabled, and if the state has progressed beyond the Logger stage.
550      *
551      * @param message the message to include in the thrown exception
552      * @throws IllegalStateException if the state is manage out of order
553      */

554     public void checkLogEnabled( final String JavaDoc message )
555     {
556         generalCheckInit( message, LOG_ENABLED, ENABLE_LOGGING );
557     }
558
559     /**
560      * Throw an exception if the initialization is out of order. It tests to see
561      * if the SET_LOGGER state has already been set, if the component implements
562      * Loggable, and if the state has progressed beyond the Logger stage.
563      *
564      * @throws IllegalStateException if the state is manage out of order
565      */

566     public void checkLoggable()
567     {
568         checkLogEnabled( null );
569     }
570
571     /**
572      * Throw an exception if the initialization is out of order. It tests to see
573      * if the SET_LOGGER state has already been set, if the component implements
574      * Loggable, and if the state has progressed beyond the Logger stage.
575      *
576      * @param message the message to include in the thrown exception
577      * @throws IllegalStateException if the state is manage out of order
578      */

579     public void checkLoggable( final String JavaDoc message )
580     {
581         generalCheckInit( message, LOGGABLE, SET_LOGGER );
582     }
583
584     /**
585      * Throw an exception if the initialization is out of order. It tests to see
586      * if the CONTEXTUALIZED state has already been set, if the component implements
587      * Contextualizable, and if the state has progressed beyond the Context stage.
588      *
589      * @throws IllegalStateException if the state is manage out of order
590      */

591     public void checkContextualized()
592     {
593         checkContextualized( null );
594     }
595
596     /**
597      * Throw an exception if the initialization is out of order. It tests to see
598      * if the CONTEXTUALIZED state has already been set, if the component implements
599      * Contextualizable, and if the state has progressed beyond the Context stage.
600      *
601      * @param message the message to include in the thrown exception
602      * @throws IllegalStateException if the state is manage out of order
603      */

604     public void checkContextualized( final String JavaDoc message )
605     {
606         generalCheckInit( message, CONTEXTUALIZABLE, CONTEXTUALIZE );
607     }
608
609     /**
610      * Throw an exception if the initialization is out of order. It tests to see
611      * if the SERVICE state has already been set, if the component implements
612      * Composable, and if the state has progressed beyond the Configuration stage.
613      *
614      * @throws IllegalStateException if the state is manage out of order
615      */

616     public void checkServiced()
617     {
618         checkServiced( null );
619     }
620
621     /**
622      * Throw an exception if the initialization is out of order. It tests to see
623      * if the SERVICE state has already been set, if the component implements
624      * Composable, and if the state has progressed beyond the Configuration stage.
625      *
626      * @param message the message to include in the thrown exception
627      * @throws IllegalStateException if the state is manage out of order
628      */

629     public void checkServiced( final String JavaDoc message )
630     {
631         generalCheckInit( message, SERVICEABLE, SERVICE );
632     }
633
634     /**
635      * Throw an exception if the initialization is out of order. It tests to see
636      * if the COMPOSED state has already been set, if the component implements
637      * Composable, and if the state has progressed beyond the Configuration stage.
638      *
639      * @throws IllegalStateException if the state is manage out of order
640      */

641     public void checkComposed()
642     {
643         checkComposed( null );
644     }
645
646     /**
647      * Throw an exception if the initialization is out of order. It tests to see
648      * if the COMPOSED state has already been set, if the component implements
649      * Composable, and if the state has progressed beyond the Configuration stage.
650      *
651      * @param message the message to include in the thrown exception
652      * @throws IllegalStateException if the state is manage out of order
653      */

654     public void checkComposed( final String JavaDoc message )
655     {
656         generalCheckInit( message, COMPOSABLE, COMPOSE );
657     }
658
659     /**
660      * Throw an exception if the initialization is out of order. It tests to see
661      * if the CONFIGURED state has already been set, if the component implements
662      * Configurable, and if the state has progressed beyond the Configuration stage.
663      *
664      * @throws IllegalStateException if the state is manage out of order
665      */

666     public void checkConfigured()
667     {
668         checkConfigured( null );
669     }
670
671     /**
672      * Throw an exception if the initialization is out of order. It tests to see
673      * if the CONFIGURED state has already been set, if the component implements
674      * Configurable, and if the state has progressed beyond the Configuration stage.
675      *
676      * @param message the message to include in the thrown exception
677      * @throws IllegalStateException if the state is manage out of order
678      */

679     public void checkConfigured( final String JavaDoc message )
680     {
681         generalCheckInit( message, CONFIGURABLE, CONFIGURE );
682     }
683
684     /**
685      * Throw an exception if the initialization is out of order. It tests to see
686      * if the PARAMETERIZED state has already been set, if the component implements
687      * Parameterizable, and if the state has progressed beyond the Parameters stage.
688      *
689      * @throws IllegalStateException if the state is manage out of order
690      */

691     public void checkParameterized()
692     {
693         checkParameterized( null );
694     }
695
696     /**
697      * Throw an exception if the initialization is out of order. It tests to see
698      * if the PARAMETERIZED state has already been set, if the component implements
699      * Parameterizable, and if the state has progressed beyond the Parameters stage.
700      *
701      * @param message the message to include in the thrown exception
702      * @throws IllegalStateException if the state is manage out of order
703      */

704     public void checkParameterized( final String JavaDoc message )
705     {
706         generalCheckInit( message, PARAMETERIZABLE, PARAMETERIZE );
707     }
708
709     /**
710      * Throw an exception if the initialization is out of order. It tests to see
711      * if the INITIALIZED state has already been set, if the component implements
712      * Initializable, and if the state has progressed beyond the <code>initialize</code> stage.
713      *
714      * @throws IllegalStateException if the state is manage out of order
715      */

716     public void checkInitialized()
717     {
718         checkInitialized( null );
719     }
720
721     /**
722      * Throw an exception if the initialization is out of order. It tests to see
723      * if the INITIALIZED state has already been set, if the component implements
724      * Initializable, and if the state has progressed beyond the <code>initialize</code> stage.
725      *
726      * @param message the message to include in the thrown exception
727      * @throws IllegalStateException if the state is manage out of order
728      */

729     public void checkInitialized( final String JavaDoc message )
730     {
731         generalCheckInit( message, INITIALIZABLE, INITIALIZE );
732     }
733
734     /**
735      * Throw an exception if the initialization is out of order. It tests to see
736      * if the STARTED state has already been set, if the component implements
737      * Startable, and if the state has progressed beyond the <code>start</code> stage.
738      *
739      * @throws IllegalStateException if the state is manage out of order
740      */

741     public void checkStarted()
742     {
743         checkStarted( null );
744     }
745
746     /**
747      * Throw an exception if the initialization is out of order. It tests to see
748      * if the STARTED state has already been set, if the component implements
749      * Startable, and if the state has progressed beyond the <code>start</code> stage.
750      *
751      * @param message the message to include in the thrown exception
752      * @throws IllegalStateException if the state is manage out of order
753      */

754     public void checkStarted( final String JavaDoc message )
755     {
756         generalCheckInit( message, STARTABLE, START );
757     }
758
759     /**
760      * Throw an exception if the initialization is out of order. It tests to see
761      * if the SUSPENDED state has already been set, if the component implements
762      * Suspendable, and if the Component is active.
763      *
764      * @throws IllegalStateException if the state is manage out of order
765      */

766     public void checkSuspended()
767     {
768         checkSuspended( null );
769     }
770
771     /**
772      * Throw an exception if the initialization is out of order. It tests to see
773      * if the SUSPENDED state has already been set, if the component implements
774      * Suspendable, and if the Component is active.
775      *
776      * @param message the message to include in the thrown exception
777      * @throws IllegalStateException if the state is manage out of order
778      */

779     public void checkSuspended( final String JavaDoc message )
780     {
781         generalCheckActive( message, SUSPENDABLE, SUSPEND );
782     }
783
784     /**
785      * Throw an exception if the initialization is out of order. It tests to see
786      * if the SUSPENDED state has not been set, if the component implements
787      * Suspendable, and if the Component is active.
788      *
789      * @throws IllegalStateException if the state is manage out of order
790      */

791     public void checkResumed()
792     {
793         checkResumed( null );
794     }
795
796     /**
797      * Throw an exception if the initialization is out of order. It tests to see
798      * if the SUSPENDED state has not been set, if the component implements
799      * Suspendable, and if the Component is active.
800      *
801      * @param message the message to include in the thrown exception
802      * @throws IllegalStateException if the state is manage out of order
803      */

804     public void checkResumed( final String JavaDoc message )
805     {
806         generalCheckActive( message, SUSPENDABLE, RESUME );
807     }
808
809     /**
810      * Throw an exception if the initialization is out of order. It tests to see
811      * if the STOPPED state has not been set, if the component implements
812      * Startable, and if the Component is active.
813      *
814      * @throws IllegalStateException if the state is manage out of order
815      */

816     public void checkStopped()
817     {
818         checkStopped( null );
819     }
820
821     /**
822      * Throw an exception if the initialization is out of order. It tests to see
823      * if the STOPPED state has not been set, if the component implements
824      * Startable, and if the Component is active.
825      *
826      * @param message the message to include in the thrown exception
827      * @throws IllegalStateException if the state is manage out of order
828      */

829     public void checkStopped( final String JavaDoc message )
830     {
831         generalCheckDest( message, STARTABLE, STOP );
832     }
833
834     /**
835      * Throw an exception if the initialization is out of order. It tests to see
836      * if the DISPOSED state has not been set, if the component implements
837      * Disposable.
838      *
839      * @throws IllegalStateException if the state is manage out of order
840      */

841     public void checkDisposed()
842     {
843         checkDisposed( null );
844     }
845
846     /**
847      * Throw an exception if the initialization is out of order. It tests to see
848      * if the DISPOSED state has not been set, if the component implements
849      * Disposable.
850      *
851      * @param message the message to include in the thrown exception
852      * @throws IllegalStateException if the state is manage out of order
853      */

854     public void checkDisposed( final String JavaDoc message )
855     {
856         generalCheckDest( message, DISPOSABLE, DISPOSE );
857     }
858
859     /**
860      * Checks to see if the state is active.
861      *
862      * @throws IllegalStateException if the component is not active
863      */

864     public void checkActive()
865     {
866         checkActive( null );
867     }
868
869     /**
870      * Checks to see if the state is active.
871      *
872      * @param message the message to include in the thrown exception
873      * @throws IllegalStateException if the component is not active
874      */

875     public void checkActive( final String JavaDoc message )
876     {
877         if( isActive() )
878         {
879             return;
880         }
881
882         // Component not in the active state.
883
if( m_state < INIT_COMPLETE )
884         {
885             // Still expecting initialization methods.
886
if( message == null )
887             {
888                 throw new IllegalStateException JavaDoc( "Component not in the active state. " +
889                                                  getLastMethod( m_methods & ( INIT_COMPLETE - 1 ) ) +
890                                                  " was not called." );
891             }
892             else
893             {
894                 throw new IllegalStateException JavaDoc( message );
895             }
896         }
897         else
898         {
899             // One or more destruction methods have been called.
900
if( message == null )
901             {
902                 throw new IllegalStateException JavaDoc( "Component not in the active state because " +
903                                                  getLastMethod( m_state ) + " was called." );
904             }
905             else
906             {
907                 throw new IllegalStateException JavaDoc( message );
908             }
909         }
910     }
911
912     /**
913      * Checks to see if the state is active, and returns true or false.
914      *
915      * @return <code>true</code> if active, <code>false</code> if not
916      */

917     public boolean isActive()
918     {
919         return m_active;
920     }
921
922     /**
923      * Make sure object has not been assigned yet.
924      *
925      * @param object to test
926      * @throws IllegalStateException if the state is manage out of order
927      */

928     public void checkNotAssigned( final Object JavaDoc object )
929     {
930         checkNotAssigned( object, WRITE_FAIL );
931     }
932
933     /**
934      * Make sure object has not been assigned yet.
935      *
936      * @param object to test
937      * @param message the message to include in the thrown exception
938      * @throws IllegalStateException if the state is manage out of order
939      */

940     public void checkNotAssigned( final Object JavaDoc object, final String JavaDoc message )
941     {
942         if( null != object )
943         {
944             throw new IllegalStateException JavaDoc( message );
945         }
946     }
947 }
948
949
Popular Tags