001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.message;
021    
022    
023    import java.util.Observable;
024    import java.util.Observer;
025    
026    
027    /**
028     * The base abandonable request message class. All such requests have a reponse
029     * type.
030     * 
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Rev: 764131 $
033     */
034    public class AbstractAbandonableRequest extends InternalAbstractRequest implements InternalAbandonableRequest
035    {
036        static final long serialVersionUID = -4511116249089399040L;
037    
038        /** Flag indicating whether or not this request returns a response. */
039        private boolean abandoned = false;
040    
041        private RequestObservable o;
042    
043    
044        /**
045         * Subclasses must provide these parameters via a super constructor call.
046         * 
047         * @param id
048         *            the sequential message identifier
049         * @param type
050         *            the request type enum
051         */
052        protected AbstractAbandonableRequest( final int id, final MessageTypeEnum type )
053        {
054            super( id, type, true );
055        }
056    
057    
058        public void abandon()
059        {
060            if ( abandoned )
061            {
062                return;
063            }
064            
065            abandoned = true;
066            if ( o == null )
067            {
068                o = new RequestObservable();
069            }
070            o.setChanged();
071            o.notifyObservers();
072            o.deleteObservers();
073        }
074    
075    
076        public boolean isAbandoned()
077        {
078            return abandoned;
079        }
080    
081    
082        public void addAbandonListener( final AbandonListener listener )
083        {
084            if ( o == null )
085            {
086                o = new RequestObservable();
087            }
088            
089            o.addObserver( new Observer()
090            {
091                public void update( Observable o, Object arg )
092                {
093                    listener.requestAbandoned( AbstractAbandonableRequest.this );
094                }
095            } );
096        }
097    
098        
099        class RequestObservable extends Observable
100        {
101            public void setChanged()
102            {
103                super.setChanged();
104            }
105        }
106    }