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

org.infinispan.lock.impl.functions.IsLocked Maven / Gradle / Ivy

There is a newer version: 15.1.1.Final
Show newest version
package org.infinispan.lock.impl.functions;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collections;
import java.util.Set;
import java.util.function.Function;

import org.infinispan.commons.logging.LogFactory;
import org.infinispan.commons.marshall.AdvancedExternalizer;
import org.infinispan.functional.EntryView;
import org.infinispan.lock.impl.entries.ClusteredLockKey;
import org.infinispan.lock.impl.entries.ClusteredLockState;
import org.infinispan.lock.impl.entries.ClusteredLockValue;
import org.infinispan.lock.impl.externalizers.ExternalizerIds;
import org.infinispan.lock.logging.Log;

/**
 * IsLocked function that allows to know if a lock is already acquired. It returns {@link Boolean#TRUE} when the lock is
 * acquired and {@link Boolean#FALSE} when it is not.
 *
 * @author Katia Aresti, [email protected]
 * @since 9.2
 */
public class IsLocked implements Function, Boolean> {

   private static final Log log = LogFactory.getLog(IsLocked.class, Log.class);

   public static final AdvancedExternalizer EXTERNALIZER = new Externalizer();

   private final Object requestor;

   public IsLocked() {
      requestor = null;
   }

   public IsLocked(Object requestor) {
      this.requestor = requestor;
   }

   @Override
   public Boolean apply(EntryView.ReadWriteEntryView entryView) {
      ClusteredLockValue lock = entryView.find().orElseThrow(() -> log.lockDeleted());
      Boolean result = Boolean.FALSE;
      if (lock.getState() == ClusteredLockState.ACQUIRED &&
            (requestor == null || (lock.getOwner() != null && lock.getOwner().equals(requestor)))) {
         result = Boolean.TRUE;
      }
      return result;
   }

   private static class Externalizer implements AdvancedExternalizer {

      @Override
      public Set> getTypeClasses() {
         return Collections.singleton(IsLocked.class);
      }

      @Override
      public Integer getId() {
         return ExternalizerIds.IS_LOCKED_FUNCTION;
      }

      @Override
      public void writeObject(ObjectOutput output, IsLocked object) throws IOException {
         output.writeObject(object.requestor);
      }

      @Override
      public IsLocked readObject(ObjectInput input) throws IOException, ClassNotFoundException {
         return new IsLocked(input.readObject());
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy