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    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
027    import org.apache.directory.shared.ldap.message.internal.InternalAbandonableRequest;
028    import org.apache.directory.shared.ldap.message.internal.InternalAbstractRequest;
029    
030    
031    /**
032     * The base abandonable request message class. All such requests have a reponse
033     * type.
034     * 
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev: 910150 $
037     */
038    public class AbstractAbandonableRequest extends InternalAbstractRequest implements InternalAbandonableRequest
039    {
040        static final long serialVersionUID = -4511116249089399040L;
041    
042        /** Flag indicating whether or not this request returns a response. */
043        private boolean abandoned = false;
044    
045        private RequestObservable o;
046    
047    
048        /**
049         * Subclasses must provide these parameters via a super constructor call.
050         * 
051         * @param id
052         *            the sequential message identifier
053         * @param type
054         *            the request type enum
055         */
056        protected AbstractAbandonableRequest( final int id, final MessageTypeEnum type )
057        {
058            super( id, type, true );
059        }
060    
061    
062        public void abandon()
063        {
064            if ( abandoned )
065            {
066                return;
067            }
068            
069            abandoned = true;
070            if ( o == null )
071            {
072                o = new RequestObservable();
073            }
074            o.setChanged();
075            o.notifyObservers();
076            o.deleteObservers();
077        }
078    
079    
080        public boolean isAbandoned()
081        {
082            return abandoned;
083        }
084    
085    
086        public void addAbandonListener( final AbandonListener listener )
087        {
088            if ( o == null )
089            {
090                o = new RequestObservable();
091            }
092            
093            o.addObserver( new Observer()
094            {
095                public void update( Observable o, Object arg )
096                {
097                    listener.requestAbandoned( AbstractAbandonableRequest.this );
098                }
099            } );
100        }
101    
102        
103        class RequestObservable extends Observable
104        {
105            public void setChanged()
106            {
107                super.setChanged();
108            }
109        }
110    }