View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.message;
21  
22  
23  import java.util.Observable;
24  import java.util.Observer;
25  
26  
27  /**
28   * The base abandonable request message class. All such requests have a response
29   * type.
30   * 
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   */
33  public abstract class AbstractAbandonableRequest extends AbstractRequest implements AbandonableRequest
34  {
35      static final long serialVersionUID = -4511116249089399040L;
36  
37      /** Flag indicating whether or not this request returns a response. */
38      private boolean abandoned = false;
39  
40      /** A filed used to check if the request has been abandonned */
41      private RequestObservable observable;
42  
43  
44      /**
45       * Subclasses must provide these parameters via a super constructor call.
46       * 
47       * @param id the sequential message identifier
48       * @param type the request type enum
49       */
50      protected AbstractAbandonableRequest( final int id, final MessageTypeEnum type )
51      {
52          super( id, type, true );
53      }
54  
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public void abandon()
61      {
62          if ( abandoned )
63          {
64              return;
65          }
66  
67          abandoned = true;
68          
69          if ( observable == null )
70          {
71              observable = new RequestObservable();
72          }
73          
74          observable.setChanged();
75          observable.notifyObservers();
76          observable.deleteObservers();
77      }
78  
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public boolean isAbandoned()
85      {
86          return abandoned;
87      }
88  
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public AbandonableRequest addAbandonListener( final AbandonListener listener )
95      {
96          if ( observable == null )
97          {
98              observable = new RequestObservable();
99          }
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 }