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      public void abandon()
58      {
59          if ( abandoned )
60          {
61              return;
62          }
63  
64          abandoned = true;
65          if ( o == null )
66          {
67              o = new RequestObservable();
68          }
69          o.setChanged();
70          o.notifyObservers();
71          o.deleteObservers();
72      }
73  
74  
75      public boolean isAbandoned()
76      {
77          return abandoned;
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      public AbandonableRequest addAbandonListener( final AbandonListener listener )
85      {
86          if ( o == null )
87          {
88              o = new RequestObservable();
89          }
90  
91          o.addObserver( new Observer()
92          {
93              public void update( Observable o, Object arg )
94              {
95                  listener.requestAbandoned( AbstractAbandonableRequest.this );
96              }
97          } );
98  
99          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 }