/*
 * Copyright 2010-2011 Ning, Inc.
 *
 * Ning licenses this file to you 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.
 */

package com.ning.billing.util.globallocker;

public class MockGlobalLocker implements GlobalLocker {
    @Override
    public GlobalLock lockWithNumberOfTries(final LockerType service,
                                            final String lockKey, final int retry) {
        return new GlobalLock() {
            @Override
            public void release() {
            }
        };
    }

    @Override
    public Boolean isFree(final LockerType service, final String lockKey) {
        return Boolean.TRUE;
    }
}

@@ -666,232 +666,1148 @@
 r;%0A%0A
-public class MockGlobalLocker implements GlobalLocker %7B%0A    @Override%0A    public GlobalLock lockWithNumberOfTries(final LockerType service,%0A                                            final String lockKey, final int retry) %7B
+import java.util.Map;%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A%0Apublic class MockGlobalLocker implements GlobalLocker %7B%0A%0A    private final Map%3CString, AtomicBoolean%3E locks = new ConcurrentHashMap%3CString, AtomicBoolean%3E();%0A%0A    @Override%0A    public GlobalLock lockWithNumberOfTries(final LockerType service,%0A                                            final String lockKey,%0A                                            final int retry) %7B%0A        final String lockName = getLockName(service, lockKey);%0A        int tries_left = retry;%0A        while (tries_left-- %3E 0) %7B%0A            final GlobalLock lock = lock(lockName);%0A            if (lock != null) %7B%0A                return lock;%0A            %7D%0A        %7D%0A        throw new LockFailedException();%0A    %7D%0A%0A    private synchronized GlobalLock lock(final String lockName) throws LockFailedException %7B%0A        if (!isFree(lockName)) %7B%0A            return null;%0A        %7D%0A%0A        if (locks.get(lockName) == null) %7B%0A            locks.put(lockName, new AtomicBoolean(true));%0A        %7D else %7B%0A            locks.get(lockName).set(true);%0A        %7D%0A
 %0A   
@@ -1891,24 +1891,72 @@
 release() %7B%0A
+                locks.get(lockName).set(false);%0A
             
@@ -2000,16 +2000,29 @@
  public 
+synchronized 
 Boolean 
@@ -2090,27 +2090,422 @@
     
-return Boolean.TRUE
+final String lockName = getLockName(service, lockKey);%0A        return isFree(lockName);%0A    %7D%0A%0A    private synchronized Boolean isFree(final String lockName) %7B%0A        final AtomicBoolean lock = locks.get(lockName);%0A        return lock == null %7C%7C !lock.get();%0A    %7D%0A%0A    private String getLockName(final LockerType service, final String lockKey) %7B%0A        return String.format(%22%25s-%25s%22, service.toString(), lockKey)
 ;%0A  

implement an in-memory locker in MockGlobalLocker