PageRenderTime 55ms CodeModel.GetById 20ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/impl/SemaphoreProxyImpl.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 493 lines | 389 code | 89 blank | 15 comment | 22 complexity | 1ecfc18090ba93746a8d0ce7f369ee5f 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.impl;
 18
 19import com.hazelcast.core.HazelcastInstance;
 20import com.hazelcast.core.InstanceDestroyedException;
 21import com.hazelcast.core.Prefix;
 22import com.hazelcast.core.RuntimeInterruptedException;
 23import com.hazelcast.impl.base.FactoryAwareNamedProxy;
 24import com.hazelcast.impl.monitor.LocalSemaphoreStatsImpl;
 25import com.hazelcast.impl.monitor.SemaphoreOperationsCounter;
 26import com.hazelcast.monitor.LocalSemaphoreStats;
 27import com.hazelcast.nio.Data;
 28
 29import java.util.concurrent.Future;
 30import java.util.concurrent.TimeUnit;
 31
 32import static com.hazelcast.nio.IOUtil.toData;
 33
 34public class SemaphoreProxyImpl extends FactoryAwareNamedProxy implements SemaphoreProxy {
 35    private transient SemaphoreProxy base = null;
 36    Data nameAsData = null;
 37
 38    public SemaphoreProxyImpl(String name, FactoryImpl factory) {
 39        setName(name);
 40        setHazelcastInstance(factory);
 41        base = new SemaphoreProxyReal();
 42    }
 43
 44    private void ensure() {
 45        factory.initialChecks();
 46        if (base == null) {
 47            base = (SemaphoreProxy) factory.getOrCreateProxyByName(name);
 48        }
 49    }
 50
 51    public String getLongName() {
 52        return name;
 53    }
 54
 55    public String getName() {
 56        return name.substring(Prefix.SEMAPHORE.length());
 57    }
 58
 59    Data getNameAsData() {
 60        if (nameAsData == null) {
 61            nameAsData = toData(getName());
 62        }
 63        return nameAsData;
 64    }
 65
 66    public Object getId() {
 67        ensure();
 68        return base.getId();
 69    }
 70
 71    @Override
 72    public String toString() {
 73        return "Semaphore [" + getName() + "]";
 74    }
 75
 76    @Override
 77    public boolean equals(Object o) {
 78        if (this == o)
 79            return true;
 80        if (o == null || getClass() != o.getClass())
 81            return false;
 82        SemaphoreProxyImpl that = (SemaphoreProxyImpl) o;
 83        return !(name != null ? !name.equals(that.name) : that.name != null);
 84    }
 85
 86    @Override
 87    public int hashCode() {
 88        return name != null ? name.hashCode() : 0;
 89    }
 90
 91    public InstanceType getInstanceType() {
 92        ensure();
 93        return base.getInstanceType();
 94    }
 95
 96    public LocalSemaphoreStats getLocalSemaphoreStats() {
 97        ensure();
 98        return base.getLocalSemaphoreStats();
 99    }
100
101    public SemaphoreOperationsCounter getOperationsCounter() {
102        ensure();
103        return base.getOperationsCounter();
104    }
105
106    public void acquire() throws InstanceDestroyedException, InterruptedException {
107        ensure();
108        base.acquire();
109    }
110
111    public void acquire(int permits) throws InstanceDestroyedException, InterruptedException {
112        check(permits);
113        ensure();
114        base.acquire(permits);
115    }
116
117    public Future acquireAsync() {
118        return doAsyncAcquire(1, false);
119    }
120
121    public Future acquireAsync(int permits) {
122        check(permits);
123        return doAsyncAcquire(permits, false);
124    }
125
126    public void acquireAttach() throws InstanceDestroyedException, InterruptedException {
127        ensure();
128        base.acquireAttach();
129    }
130
131    public void acquireAttach(int permits) throws InstanceDestroyedException, InterruptedException {
132        check(permits);
133        ensure();
134        base.acquireAttach(permits);
135    }
136
137    public Future acquireAttachAsync() {
138        return doAsyncAcquire(1, true);
139    }
140
141    public Future acquireAttachAsync(int permits) {
142        check(permits);
143        return doAsyncAcquire(permits, true);
144    }
145
146    public void attach() {
147        ensure();
148        base.attach();
149    }
150
151    public void attach(int permits) {
152        check(permits);
153        ensure();
154        base.attach(permits);
155    }
156
157    public int attachedPermits() {
158        ensure();
159        return base.attachedPermits();
160    }
161
162    public int availablePermits() {
163        ensure();
164        return base.availablePermits();
165    }
166
167    public void detach() {
168        ensure();
169        base.detach();
170    }
171
172    public void detach(int permits) {
173        check(permits);
174        ensure();
175        base.detach(permits);
176    }
177
178    public void destroy() {
179        ensure();
180        base.destroy();
181    }
182
183    public int drainPermits() {
184        ensure();
185        return base.drainPermits();
186    }
187
188    public void reducePermits(int permits) {
189        check(permits);
190        ensure();
191        base.reducePermits(permits);
192    }
193
194    public void release() {
195        ensure();
196        base.release();
197    }
198
199    public void release(int permits) {
200        check(permits);
201        ensure();
202        base.release(permits);
203    }
204
205    public void releaseDetach() {
206        ensure();
207        base.releaseDetach();
208    }
209
210    public void releaseDetach(int permits) {
211        check(permits);
212        ensure();
213        base.releaseDetach(permits);
214    }
215
216    public boolean tryAcquire() {
217        ensure();
218        return base.tryAcquire();
219    }
220
221    public boolean tryAcquire(int permits) {
222        check(permits);
223        ensure();
224        return base.tryAcquire(permits);
225    }
226
227    public boolean tryAcquire(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
228        ensure();
229        return base.tryAcquire(timeout, unit);
230    }
231
232    public boolean tryAcquire(int permits, long timeout, TimeUnit timeunit) throws InstanceDestroyedException, InterruptedException {
233        check(permits, timeout, timeunit);
234        ensure();
235        return base.tryAcquire(permits, timeout, timeunit);
236    }
237
238    public boolean tryAcquireAttach() {
239        ensure();
240        return base.tryAcquireAttach();
241    }
242
243    public boolean tryAcquireAttach(int permits) {
244        check(permits);
245        ensure();
246        return base.tryAcquireAttach(permits);
247    }
248
249    public boolean tryAcquireAttach(long timeout, TimeUnit timeunit) throws InstanceDestroyedException, InterruptedException {
250        ensure();
251        return base.tryAcquireAttach(timeout, timeunit);
252    }
253
254    public boolean tryAcquireAttach(int permits, long timeout, TimeUnit timeunit) throws InstanceDestroyedException, InterruptedException {
255        check(permits, timeout, timeunit);
256        ensure();
257        return base.tryAcquireAttach(permits, timeout, timeunit);
258    }
259
260    private void check(int permits) {
261        if (permits < 0)
262            throw new IllegalArgumentException("Number of permits can not be negative: " + permits);
263    }
264
265    private void check(int permits, long timeout, TimeUnit timeunit) {
266        check(permits);
267        if (timeout < -1)
268            throw new IllegalArgumentException("Invalid timeout value: " + timeout);
269        if (timeunit == null) {
270            throw new NullPointerException("TimeUnit can not be null.");
271        }
272    }
273
274    private Future doAsyncAcquire(final Integer permits, final Boolean attach) {
275        final SemaphoreProxyImpl semaphoreProxy = SemaphoreProxyImpl.this;
276        AsyncCall call = new AsyncCall() {
277            @Override
278            protected void call() {
279                try {
280                    if (attach)
281                        semaphoreProxy.acquireAttach(permits);
282                    else
283                        semaphoreProxy.acquire(permits);
284                    setResult(null);
285                } catch (InterruptedException e) {
286                    setResult(e);
287                } catch (InstanceDestroyedException e) {
288                    e.printStackTrace();
289                }
290            }
291
292            @Override
293            public boolean cancel(boolean mayInterruptIfRunning) {
294                ConcurrentMapManager.MSemaphore msemaphore = factory.node.concurrentMapManager.new MSemaphore();
295                return msemaphore.cancelAcquire(getNameAsData());
296            }
297        };
298        factory.node.executorManager.executeAsync(call);
299        return call;
300    }
301
302    private class SemaphoreProxyReal implements SemaphoreProxy {
303        SemaphoreOperationsCounter operationsCounter = new SemaphoreOperationsCounter();
304
305        public Object getId() {
306            return name;
307        }
308
309        public InstanceType getInstanceType() {
310            return InstanceType.SEMAPHORE;
311        }
312
313        public String getLongName() {
314            return name;
315        }
316
317        public String getName() {
318            return name.substring(Prefix.SEMAPHORE.length());
319        }
320
321        public void destroy() {
322            newMSemaphore().destroy(getNameAsData());
323            factory.destroyInstanceClusterWide(name, null);
324        }
325
326        public void acquire() throws InstanceDestroyedException, InterruptedException {
327            acquire(1);
328        }
329
330        public void acquire(int permits) throws InstanceDestroyedException, InterruptedException {
331            if (Thread.interrupted())
332                throw new InterruptedException();
333            try {
334                doTryAcquire(permits, false, -1);
335            } catch (RuntimeInterruptedException e) {
336                throw new InterruptedException();
337            }
338        }
339
340        public Future acquireAsync() {
341            throw new UnsupportedOperationException();
342        }
343
344        public Future acquireAsync(int permits) {
345            throw new UnsupportedOperationException();
346        }
347
348        public void acquireAttach() throws InstanceDestroyedException, InterruptedException {
349            acquireAttach(1);
350        }
351
352        public void acquireAttach(int permits) throws InstanceDestroyedException, InterruptedException {
353            if (Thread.interrupted())
354                throw new InterruptedException();
355            try {
356                doTryAcquire(permits, true, -1);
357            } catch (RuntimeInterruptedException e) {
358                throw new InterruptedException();
359            }
360        }
361
362        public Future acquireAttachAsync() {
363            throw new UnsupportedOperationException();
364        }
365
366        public Future acquireAttachAsync(int permits) {
367            throw new UnsupportedOperationException();
368        }
369
370        public void attach() {
371            attach(1);
372        }
373
374        public void attach(int permits) {
375            newMSemaphore().attachDetach(getNameAsData(), permits);
376        }
377
378        public int attachedPermits() {
379            return newMSemaphore().getAttached(getNameAsData());
380        }
381
382        public int availablePermits() {
383            return newMSemaphore().getAvailable(getNameAsData());
384        }
385
386        public void detach() {
387            detach(1);
388        }
389
390        public void detach(int permits) {
391            newMSemaphore().attachDetach(getNameAsData(), -permits);
392        }
393
394        public int drainPermits() {
395            return newMSemaphore().drainPermits(getNameAsData());
396        }
397
398        public void release() {
399            release(1);
400        }
401
402        public void release(int permits) {
403            newMSemaphore().release(getNameAsData(), permits, false);
404        }
405
406        public void releaseDetach() {
407            releaseDetach(1);
408        }
409
410        public void releaseDetach(int permits) {
411            newMSemaphore().release(getNameAsData(), permits, true);
412        }
413
414        public boolean tryAcquire() {
415            return tryAcquire(1);
416        }
417
418        public boolean tryAcquire(int permits) {
419            try {
420                return doTryAcquire(permits, false, 0);
421            } catch (Throwable e) {
422                return false;
423            }
424        }
425
426        public boolean tryAcquire(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
427            return tryAcquire(1, timeout, unit);
428        }
429
430        public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
431            if (Thread.interrupted())
432                throw new InterruptedException();
433            try {
434                return doTryAcquire(permits, false, unit.toMillis(timeout));
435            } catch (RuntimeInterruptedException e) {
436                throw new InterruptedException();
437            }
438        }
439
440        public boolean tryAcquireAttach() {
441            return tryAcquireAttach(1);
442        }
443
444        public boolean tryAcquireAttach(int permits) {
445            try {
446                return doTryAcquire(permits, true, 0);
447            } catch (Throwable e) {
448                return false;
449            }
450        }
451
452        public boolean tryAcquireAttach(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
453            return tryAcquireAttach(1, timeout, unit);
454        }
455
456        public boolean tryAcquireAttach(int permits, long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
457            if (Thread.interrupted())
458                throw new InterruptedException();
459            try {
460                return doTryAcquire(permits, true, unit.toMillis(timeout));
461            } catch (RuntimeInterruptedException e) {
462                throw new InterruptedException();
463            }
464        }
465
466        public void reducePermits(int permits) {
467            newMSemaphore().reduce(getNameAsData(), permits);
468        }
469
470        public LocalSemaphoreStats getLocalSemaphoreStats() {
471            LocalSemaphoreStatsImpl localSemaphoreStats = new LocalSemaphoreStatsImpl();
472            localSemaphoreStats.setOperationStats(operationsCounter.getPublishedStats());
473            return localSemaphoreStats;
474        }
475
476        public SemaphoreOperationsCounter getOperationsCounter() {
477            return operationsCounter;
478        }
479
480        private ConcurrentMapManager.MSemaphore newMSemaphore() {
481            ConcurrentMapManager.MSemaphore msemaphore = factory.node.concurrentMapManager.new MSemaphore();
482            msemaphore.setOperationsCounter(operationsCounter);
483            return msemaphore;
484        }
485
486        private boolean doTryAcquire(int permits, boolean attach, long timeout) throws InstanceDestroyedException {
487            return newMSemaphore().tryAcquire(getNameAsData(), permits, attach, timeout);
488        }
489
490        public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
491        }
492    }
493}