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    @Override
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    @Override
077    public boolean isAbandoned()
078    {
079        return abandoned;
080    }
081
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public AbandonableRequest addAbandonListener( final AbandonListener listener )
088    {
089        if ( o == null )
090        {
091            o = new RequestObservable();
092        }
093
094        o.addObserver( new Observer()
095        {
096            @Override
097            public void update( Observable o, Object arg )
098            {
099                listener.requestAbandoned( AbstractAbandonableRequest.this );
100            }
101        } );
102
103        return this;
104    }
105
106    // False positive
107    static class RequestObservable extends Observable
108    {
109        @Override
110        public void setChanged()
111        {
112            super.setChanged();
113        }
114    }
115}