Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright 2010 JBoss Inc
  *
  * Licensed 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 org.drools.guvnor.server.util;
Manages the locks for assets.
public class AssetLockManager {
    private static final long LOCK_EXPIRATION_TIME = 1200000;
    // UUID, Lock
    private final Map<StringLockmap = new HashMap<StringLock>();
    class Lock {
        final String userName;
        final long timeStamp;
        Lock(String userName) {
            this. = userName;
             = System.currentTimeMillis();
        }
    }
    public boolean isAssetLocked(String uuid) {
        if (.keySet().contains(uuid)) {
            long timeStamp = .get(uuid).;
            long currentTime = System.currentTimeMillis();
            // Check if time expiration time has passed
            if ((currentTime - timeStamp) > ) {
                // Remove the lock
                .remove(uuid);
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    
Return the lockers user name.

Parameters:
uuid Id of the asset.
Returns:
Lockers user name or null if there is no lock with this uuid.
    public String getAssetLockerUserName(String uuid) {
        if (isAssetLocked(uuid)) {
            return .get(uuid).;
        } else {
            return null;
        }
    }

    
Locks the asset, if a lock already exists this over writes it.

Parameters:
uuid Id of the asset.
userName User name of the user that is locking the asset.
    public void lockAsset(String uuid,
                          String userName) {
        .put(uuid,
                new Lock(userName));
    }
    public void unLockAsset(String uuid) {
        .remove(uuid);
    }
New to GrepCode? Check out our FAQ X