/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. package com.hazelcast.client;
  17. import com.hazelcast.core.ILock;
  18. import com.hazelcast.impl.ClusterOperation;
  19. import com.hazelcast.impl.FactoryImpl;
  20. import com.hazelcast.monitor.LocalLockStats;
  21. import java.util.concurrent.TimeUnit;
  22. import java.util.concurrent.locks.Condition;
  23. import static com.hazelcast.client.ProxyHelper.check;
  24. public class LockClientProxy implements ILock {
  25. final ProxyHelper proxyHelper;
  26. final Object lockObject;
  27. public LockClientProxy(Object object, HazelcastClient client) {
  28. proxyHelper = new ProxyHelper("", client);
  29. lockObject = object;
  30. check(lockObject);
  31. }
  32. public Object getLockObject() {
  33. return lockObject;
  34. }
  35. public void lockInterruptibly() throws InterruptedException {
  36. throw new UnsupportedOperationException("lockInterruptibly is not implemented!");
  37. }
  38. public void lock() {
  39. doLock(-1, null);
  40. }
  41. public boolean isLocked() {
  42. ClusterOperation operation = ClusterOperation.LOCK_IS_LOCKED;
  43. Packet request = proxyHelper.prepareRequest(operation, lockObject, null);
  44. Packet response = proxyHelper.callAndGetResult(request);
  45. return (Boolean)proxyHelper.getValue(response);
  46. }
  47. public boolean tryLock() {
  48. return (Boolean) doLock(0, null);
  49. }
  50. public boolean tryLock(long time, TimeUnit timeunit) {
  51. ProxyHelper.checkTime(time, timeunit);
  52. return (Boolean) doLock(time, timeunit);
  53. }
  54. public void unlock() {
  55. proxyHelper.doOp(ClusterOperation.LOCK_UNLOCK, lockObject, null);
  56. }
  57. public void forceUnlock() {
  58. proxyHelper.doOp(ClusterOperation.LOCK_FORCE_UNLOCK, lockObject, null);
  59. }
  60. private Object doLock(long timeout, TimeUnit timeUnit) {
  61. ClusterOperation operation = ClusterOperation.LOCK_LOCK;
  62. Packet request = proxyHelper.prepareRequest(operation, lockObject, null);
  63. request.setTimeout(timeUnit == null ? timeout : timeUnit.toMillis(timeout));
  64. Packet response = proxyHelper.callAndGetResult(request);
  65. return proxyHelper.getValue(response);
  66. }
  67. public Condition newCondition() {
  68. return null;
  69. }
  70. public InstanceType getInstanceType() {
  71. return InstanceType.LOCK;
  72. }
  73. public void destroy() {
  74. proxyHelper.destroy();
  75. }
  76. public Object getId() {
  77. return new FactoryImpl.ProxyKey("lock", lockObject);
  78. }
  79. @Override
  80. public boolean equals(Object o) {
  81. if (o != null && o instanceof ILock) {
  82. return getId().equals(((ILock) o).getId());
  83. } else {
  84. return false;
  85. }
  86. }
  87. @Override
  88. public int hashCode() {
  89. return getId().hashCode();
  90. }
  91. public LocalLockStats getLocalLockStats() {
  92. throw new UnsupportedOperationException();
  93. }
  94. }