All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.admin.remote.CancellationRegistry Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */
   
package com.gemstone.gemfire.internal.admin.remote;

import java.util.*;
import com.gemstone.gemfire.distributed.internal.membership.*;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;

/**
 * This class provides a way for a {@link CancellationMessage} to find its prey.
 * An {@link AdminRequest} that implements {@link Cancellable} should register
 * with this class before doing any work, and deregister just before returning
 * it's response.
 */
public class CancellationRegistry {
  private static CancellationRegistry internalRef;
  private Map map = new HashMap();
  
  private CancellationRegistry() {}

  public synchronized static CancellationRegistry getInstance() {
    if (internalRef == null) {
      internalRef = new CancellationRegistry();
    }
    return internalRef;
  }

  public synchronized void cancelMessage(InternalDistributedMember console, int msgId) {
    Key key = new Key(console, msgId);
    AdminRequest msg = (AdminRequest)map.get(key);
    if (msg instanceof Cancellable) {
      ((Cancellable)msg).cancel();
    }
  }

  public synchronized void registerMessage(AdminRequest msg) {
    Key key = new Key(msg.getSender(), msg.getMsgId());
    map.put(key, msg);
  }

  public synchronized void deregisterMessage(AdminRequest msg) {
    Key key = new Key(msg.getSender(), msg.getMsgId());
    map.remove(key);
  }

  /////// Inner Classes ////////////////////////////////////////

  static private class Key {
    private final InternalDistributedMember console;
    private final int msgId;

    public Key(InternalDistributedMember console, int msgId) {
      if (console == null) {
        throw new NullPointerException(LocalizedStrings.CancellationRegistry_NULL_CONSOLE.toLocalizedString());
      }

      this.console = console;
      this.msgId = msgId;
    }

    @Override
    public boolean equals(Object other) {
      if (this == other) return true;
      if (other instanceof Key) {
        Key toTest = (Key)other;
        return (toTest.console.equals(this.console) &&
                toTest.msgId == this.msgId);
      }
      return false;
    }

    @Override
    public int hashCode() {
      int result = 17;
      result = 37 * result + msgId;
      result = 37 * result + console.hashCode();
      return result;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy