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    @SuppressWarnings("serial")
015    public class DefaultLifecycleState implements LifecycleState, Serializable {
016    
017        /**
018             * Default state of a container once it has been built.
019             */
020            private static final String CONSTRUCTED = "CONSTRUCTED";
021    
022            /**
023             * 'Start' Lifecycle has been called.
024             */
025            private static final String STARTED = "STARTED";
026    
027            /**
028             * 'Stop' lifecycle has been called.
029             */
030            private static final String STOPPED = "STOPPED";
031    
032            /**
033             * 'Dispose' lifecycle has been called.
034             */
035            private static final String DISPOSED = "DISPOSED";
036    
037        private String state = CONSTRUCTED;
038    
039        public void removingComponent() {
040            if (state == STARTED) {
041                throw new PicoCompositionException("Cannot remove components after the container has started");
042            }
043    
044            if (state == DISPOSED) {
045                throw new PicoCompositionException("Cannot remove components after the container has been disposed");
046            }
047        }
048    
049        /** {@inheritDoc} **/
050        public void starting() {
051                    if (state == CONSTRUCTED || state == STOPPED) {
052                state = STARTED;
053                            return;
054                    }
055                throw new IllegalStateException("Cannot start.  Current container state was: " + state);
056        }
057    
058    
059        /** {@inheritDoc} **/
060        public void stopping() {
061            if (!(state == STARTED)) {
062                throw new IllegalStateException("Cannot stop.  Current container state was: " + state);
063            }
064        }
065    
066        public void stopped() {
067            state = STOPPED;
068        }
069    
070        public boolean isStarted() {
071            return state == STARTED;
072        }
073    
074        /** {@inheritDoc} **/
075        public void disposing() {
076            if (!(state == STOPPED || state == CONSTRUCTED)) {
077                throw new IllegalStateException("Cannot dispose.  Current lifecycle state is: " + state);
078            }
079    
080        }
081    
082        public void disposed() {
083            state = DISPOSED;
084        }
085    
086    }