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