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   *    http://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 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      private RequestObservable o;
41  
42  
43      /**
44       * Subclasses must provide these parameters via a super constructor call.
45       * 
46       * @param id
47       *            the sequential message identifier
48       * @param type
49       *            the request type enum
50       */
51      protected AbstractAbandonableRequest( final int id, final MessageTypeEnum type )
52      {
53          super( id, type, true );
54      }
55  
56  
57      @Override
58      public void abandon()
59      {
60          if ( abandoned )
61          {
62              return;
63          }
64  
65          abandoned = true;
66          if ( o == null )
67          {
68              o = new RequestObservable();
69          }
70          o.setChanged();
71          o.notifyObservers();
72          o.deleteObservers();
73      }
74  
75  
76      @Override
77      public boolean isAbandoned()
78      {
79          return abandoned;
80      }
81  
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public AbandonableRequest addAbandonListener( final AbandonListener listener )
88      {
89          if ( o == null )
90          {
91              o = new RequestObservable();
92          }
93  
94          o.addObserver( new Observer()
95          {
96              @Override
97              public void update( Observable o, Object arg )
98              {
99                  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 }