001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *****************************************************************************/
008    package org.picocontainer.lifecycle;
009    
010    import org.picocontainer.PicoCompositionException;
011    
012    import java.io.Serializable;
013    
014    /**
015     * Bean-like implementation of LifecycleState.
016     * @author Paul Hammant
017     * @author Michael Rimov
018     *
019     */
020    @SuppressWarnings("serial")
021    public class DefaultLifecycleState implements LifecycleState, Serializable {
022    
023        /**
024             * Default state of a container once it has been built.
025             */
026            private static final String CONSTRUCTED = "CONSTRUCTED";
027    
028            /**
029             * 'Start' Lifecycle has been called.
030             */
031            private static final String STARTED = "STARTED";
032    
033            /**
034             * 'Stop' lifecycle has been called.
035             */
036            private static final String STOPPED = "STOPPED";
037    
038            /**
039             * 'Dispose' lifecycle has been called.
040             */
041            private static final String DISPOSED = "DISPOSED";
042    
043            /**
044             * Initial state.
045             */
046        private String state = CONSTRUCTED;
047    
048        /** {@inheritDoc} **/
049        public void removingComponent() {
050            if (isStarted()) {
051                throw new PicoCompositionException("Cannot remove components after the container has started");
052            }
053    
054            if (isDisposed()) {
055                throw new PicoCompositionException("Cannot remove components after the container has been disposed");
056            }
057        }
058    
059        /** {@inheritDoc} **/
060        public void starting() {
061                    if (isConstructed() || isStopped()) {
062                state = STARTED;
063                            return;
064                    }
065                throw new IllegalStateException("Cannot start.  Current container state was: " + state);
066        }
067    
068    
069        /** {@inheritDoc} **/
070        public void stopping() {
071            if (!(isStarted())) {
072                throw new IllegalStateException("Cannot stop.  Current container state was: " + state);
073            }
074        }
075    
076        /** {@inheritDoc} **/
077        public void stopped() {
078            state = STOPPED;
079        }
080    
081        /** {@inheritDoc} **/
082        public boolean isStarted() {
083            return state == STARTED;
084        }
085    
086        /** {@inheritDoc} **/
087        public void disposing() {
088            if (!(isStopped() || isConstructed())) {
089                throw new IllegalStateException("Cannot dispose.  Current lifecycle state is: " + state);
090            }
091    
092        }
093    
094        /** {@inheritDoc} **/
095        public void disposed() {
096            state = DISPOSED;
097        }
098    
099        
100        /** {@inheritDoc} **/
101            public boolean isDisposed() {
102                    return state == DISPOSED;
103        }
104    
105        /** {@inheritDoc} **/
106            public boolean isStopped() {
107                    return state == STOPPED;
108        }
109    
110            /**
111             * Returns true if no other state has been triggered so far.
112             * @return
113             */
114            public boolean isConstructed() {
115                    return state == CONSTRUCTED;
116            }
117    
118    }