PageRenderTime 23ms CodeModel.GetById 16ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast-client/src/main/java/com/hazelcast/client/LockClientProxy.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 116 lines | 79 code | 22 blank | 15 comment | 5 complexity | d4264c274a3da1a7cf38fb5ad583f74d MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.client;
 18
 19import com.hazelcast.core.ILock;
 20import com.hazelcast.impl.ClusterOperation;
 21import com.hazelcast.impl.FactoryImpl;
 22import com.hazelcast.monitor.LocalLockStats;
 23
 24import java.util.concurrent.TimeUnit;
 25import java.util.concurrent.locks.Condition;
 26
 27import static com.hazelcast.client.ProxyHelper.check;
 28
 29public class LockClientProxy implements ILock {
 30    final ProxyHelper proxyHelper;
 31    final Object lockObject;
 32
 33    public LockClientProxy(Object object, HazelcastClient client) {
 34        proxyHelper = new ProxyHelper("", client);
 35        lockObject = object;
 36        check(lockObject);
 37    }
 38
 39    public Object getLockObject() {
 40        return lockObject;
 41    }
 42
 43    public void lockInterruptibly() throws InterruptedException {
 44        throw new UnsupportedOperationException("lockInterruptibly is not implemented!");
 45    }
 46
 47    public void lock() {
 48        doLock(-1, null);
 49    }
 50
 51    public boolean isLocked() {
 52        ClusterOperation operation = ClusterOperation.LOCK_IS_LOCKED;
 53        Packet request = proxyHelper.prepareRequest(operation, lockObject, null);
 54        Packet response = proxyHelper.callAndGetResult(request);
 55        return (Boolean)proxyHelper.getValue(response);
 56    }
 57
 58    public boolean tryLock() {
 59        return (Boolean) doLock(0, null);
 60    }
 61
 62    public boolean tryLock(long time, TimeUnit timeunit) {
 63        ProxyHelper.checkTime(time, timeunit);
 64        return (Boolean) doLock(time, timeunit);
 65    }
 66
 67    public void unlock() {
 68        proxyHelper.doOp(ClusterOperation.LOCK_UNLOCK, lockObject, null);
 69    }
 70
 71    public void forceUnlock() {
 72        proxyHelper.doOp(ClusterOperation.LOCK_FORCE_UNLOCK, lockObject, null);
 73    }
 74
 75    private Object doLock(long timeout, TimeUnit timeUnit) {
 76        ClusterOperation operation = ClusterOperation.LOCK_LOCK;
 77        Packet request = proxyHelper.prepareRequest(operation, lockObject, null);
 78        request.setTimeout(timeUnit == null ? timeout : timeUnit.toMillis(timeout));
 79        Packet response = proxyHelper.callAndGetResult(request);
 80        return proxyHelper.getValue(response);
 81    }
 82
 83    public Condition newCondition() {
 84        return null;
 85    }
 86
 87    public InstanceType getInstanceType() {
 88        return InstanceType.LOCK;
 89    }
 90
 91    public void destroy() {
 92        proxyHelper.destroy();
 93    }
 94
 95    public Object getId() {
 96        return new FactoryImpl.ProxyKey("lock", lockObject);
 97    }
 98
 99    @Override
100    public boolean equals(Object o) {
101        if (o != null && o instanceof ILock) {
102            return getId().equals(((ILock) o).getId());
103        } else {
104            return false;
105        }
106    }
107
108    @Override
109    public int hashCode() {
110        return getId().hashCode();
111    }
112
113    public LocalLockStats getLocalLockStats() {
114        throw new UnsupportedOperationException();
115    }
116}