KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > util > test > ComponentStateValidator


1 /*
2  * Copyright 2003-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
18 package org.apache.avalon.fortress.util.test;
19
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.activity.Initializable;
22 import org.apache.avalon.framework.activity.Startable;
23 import org.apache.avalon.framework.activity.Suspendable;
24 import org.apache.avalon.framework.component.Composable;
25 import org.apache.avalon.framework.component.Recomposable;
26 import org.apache.avalon.framework.configuration.Configurable;
27 import org.apache.avalon.framework.configuration.Reconfigurable;
28 import org.apache.avalon.framework.context.Contextualizable;
29 import org.apache.avalon.framework.context.Recontextualizable;
30 import org.apache.avalon.framework.logger.LogEnabled;
31 import org.apache.avalon.framework.logger.Loggable;
32 import org.apache.avalon.framework.parameters.Parameterizable;
33 import org.apache.avalon.framework.service.Serviceable;
34
35 /**
36  * This class provides basic facilities for enforcing Avalon's contracts
37  * within your own code.
38  *
39  * Based on Avalon version from Sandbox.
40  *
41  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
42  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 15:16:27 $
43  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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