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 *    https://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 abstract 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    /** A filed used to check if the request has been abandonned */
041    private RequestObservable observable;
042
043
044    /**
045     * Subclasses must provide these parameters via a super constructor call.
046     * 
047     * @param id the sequential message identifier
048     * @param type the request type enum
049     */
050    protected AbstractAbandonableRequest( final int id, final MessageTypeEnum type )
051    {
052        super( id, type, true );
053    }
054
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public void abandon()
061    {
062        if ( abandoned )
063        {
064            return;
065        }
066
067        abandoned = true;
068        
069        if ( observable == null )
070        {
071            observable = new RequestObservable();
072        }
073        
074        observable.setChanged();
075        observable.notifyObservers();
076        observable.deleteObservers();
077    }
078
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public boolean isAbandoned()
085    {
086        return abandoned;
087    }
088
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public AbandonableRequest addAbandonListener( final AbandonListener listener )
095    {
096        if ( observable == null )
097        {
098            observable = new RequestObservable();
099        }
100
101        observable.addObserver( new Observer()
102        {
103            @Override
104            public void update( Observable o, Object arg )
105            {
106                listener.requestAbandoned( AbstractAbandonableRequest.this );
107            }
108        } );
109
110        return this;
111    }
112
113    // False positive
114    static class RequestObservable extends Observable
115    {
116        @Override
117        public void setChanged()
118        {
119            super.setChanged();
120        }
121    }
122}