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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.procedure2.store;
20  
21  import java.util.concurrent.CopyOnWriteArrayList;
22  import java.util.concurrent.atomic.AtomicBoolean;
23  
24  /**
25   * Base class for {@link ProcedureStore}s.
26   */
27  public abstract class ProcedureStoreBase implements ProcedureStore {
28    private final CopyOnWriteArrayList<ProcedureStoreListener> listeners =
29        new CopyOnWriteArrayList<ProcedureStoreListener>();
30  
31    private final AtomicBoolean running = new AtomicBoolean(false);
32  
33    /**
34     * Change the state to 'isRunning',
35     * returns true if the store state was changed,
36     * false if the store was already in that state.
37     * @param isRunning the state to set.
38     * @return true if the store state was changed, otherwise false.
39     */
40    protected boolean setRunning(boolean isRunning) {
41      return running.getAndSet(isRunning) != isRunning;
42    }
43  
44    @Override
45    public boolean isRunning() {
46      return running.get();
47    }
48  
49    @Override
50    public void registerListener(ProcedureStoreListener listener) {
51      listeners.add(listener);
52    }
53  
54    @Override
55    public boolean unregisterListener(ProcedureStoreListener listener) {
56      return listeners.remove(listener);
57    }
58  
59    protected void sendPostSyncSignal() {
60      if (!this.listeners.isEmpty()) {
61        for (ProcedureStoreListener listener : this.listeners) {
62          listener.postSync();
63        }
64      }
65    }
66  
67    protected void sendAbortProcessSignal() {
68      if (!this.listeners.isEmpty()) {
69        for (ProcedureStoreListener listener : this.listeners) {
70          listener.abortProcess();
71        }
72      }
73    }
74  }