Example usage for java.util.concurrent.locks Lock lock

List of usage examples for java.util.concurrent.locks Lock lock

Introduction

In this page you can find the example usage for java.util.concurrent.locks Lock lock.

Prototype

lock

Source Link

Usage

From source file:edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.ShibbolethAttributeFilteringEngine.java

/** {@inheritDoc} */
public Map<String, BaseAttribute> filterAttributes(Map<String, BaseAttribute> attributes,
        SAMLProfileRequestContext context) throws AttributeFilteringException {

    log.debug(getId() + " filtering {} attributes for principal {}", attributes.size(),
            context.getPrincipalName());

    if (attributes.size() == 0) {
        return new HashMap<String, BaseAttribute>();
    }//  w w w .j  a  va  2 s . co m

    if (getFilterPolicies() == null) {
        log.debug("No filter policies were loaded in {}, filtering out all attributes for {}", getId(),
                context.getPrincipalName());
        return new HashMap<String, BaseAttribute>();
    }

    ShibbolethFilteringContext filterContext = new ShibbolethFilteringContext(attributes, context);
    Lock readLock = getReadWriteLock().readLock();
    readLock.lock();
    try {
        for (AttributeFilterPolicy filterPolicy : filterPolicies) {
            filterAttributes(filterContext, filterPolicy);
            runDenyRules(filterContext);
        }
    } finally {
        readLock.unlock();
    }

    Iterator<Entry<String, BaseAttribute>> attributeEntryItr = attributes.entrySet().iterator();
    Entry<String, BaseAttribute> attributeEntry;
    BaseAttribute attribute;
    Collection retainedValues;
    while (attributeEntryItr.hasNext()) {
        attributeEntry = attributeEntryItr.next();
        attribute = attributeEntry.getValue();
        retainedValues = filterContext.getRetainedValues(attribute.getId(), false);
        attribute.getValues().clear();
        attribute.getValues().addAll(retainedValues);
        if (attribute.getValues().size() == 0) {
            log.debug("Removing attribute from return set, no more values: {}", attribute.getId());
            attributeEntryItr.remove();
        } else {
            log.debug("Attribute {} has {} values after filtering", attribute.getId(),
                    attribute.getValues().size());
        }
    }

    log.debug("Filtered attributes for principal {}.  The following attributes remain: {}",
            context.getPrincipalName(), attributes.keySet());
    return attributes;
}

From source file:com.callidusrobotics.droptables.configuration.MongoFactory.java

/**
 * Generates a connection pool using the read-write credentials.
 *
 * @param env//from w  w  w. j ava 2s .  c  om
 *          The application's environment
 * @return The connection pool, never null
 * @throws UnknownHostException
 * @see #setRwUser(LoginInfo)
 */
public MongoClient buildReadWriteClient(Environment env) throws UnknownHostException {
    if (rwClient != null) {
        return rwClient;
    }

    Lock lock = locks[LockFlags.MONGO_CLIENT.ordinal()];
    lock.lock();

    try {
        if (rwClient == null) {
            rwClient = buildClient(env, rwUser.getUsername(), rwUser.getPassword());
        }
    } finally {
        lock.unlock();
    }

    return rwClient;
}

From source file:com.mcapanel.plugin.PluginConnector.java

public String sendMethodResponse(String method, String... params) {
    if (connected()) {
        //if (returns.containsKey(method))
        //returns.remove(method);

        long time = sendMethod(method, params);

        if (time != -1) {
            long start = System.currentTimeMillis();

            while (System.currentTimeMillis() - start < 2000) {
                final Lock lock = returnsLock.readLock();
                lock.lock();

                try {
                    if (returns.containsKey(time)) {
                        PluginReturn ret = returns.get(time);

                        if (ret != null)
                            return ret.getData();
                    }/*from www .  java2s  . c  o m*/
                } finally {
                    lock.unlock();
                }
            }

            final Lock lock = returnsLock.readLock();
            lock.lock();

            try {
                for (Long key : returns.descendingKeySet()) {
                    PluginReturn pr = returns.get(key);

                    if (pr.getMethod().equals(method)) {
                        return pr.getData();
                    }
                }
            } finally {
                lock.unlock();
            }
        }
    }

    return null;
}

From source file:com.callidusrobotics.droptables.configuration.MongoFactory.java

/**
 * Generates a connection wrapper using the read-only credentials.
 *
 * @param env/*from  w  w w .j a  va 2 s .  co  m*/
 *          The application's environment.
 * @return The connection wrapper, never null
 * @throws UnknownHostException
 * @see #buildReadOnlyClient(Environment)
 */
public Datastore buildReadOnlyDatastore(Environment env) throws UnknownHostException {
    if (roDatastore != null) {
        return roDatastore;
    }

    if (roUser == null || rwUser.equals(roUser)) {
        roDatastore = buildReadWriteDatastore(env);
    }

    Lock lock = locks[LockFlags.DATASTORE.ordinal()];
    lock.lock();

    try {
        if (roDatastore == null) {
            roDatastore = morphia.createDatastore(buildReadOnlyClient(env), dbName);
        }
    } finally {
        lock.unlock();
    }

    return roDatastore;
}

From source file:com.callidusrobotics.droptables.configuration.MongoFactory.java

/**
 * Generates a connection pool using the read-only credentials. <br>
 * If the read-only credentials are not provided, or if the read-only
 * credentials are the same as the read-write credentials, it returns the
 * read-write connection pool instead.//from  w w  w . j  a  v a 2s.  com
 *
 * @param env
 *          The application's environment
 * @return The connection pool, never null
 * @throws UnknownHostException
 * @see #setRoUser(LoginInfo)
 */
public MongoClient buildReadOnlyClient(Environment env) throws UnknownHostException {
    if (roClient != null) {
        return roClient;
    }

    if (roUser == null || rwUser.equals(roUser)) {
        roClient = buildReadWriteClient(env);
    }

    Lock lock = locks[LockFlags.MONGO_CLIENT.ordinal()];
    lock.lock();

    try {
        if (roClient == null && roUser != null) {
            roClient = buildClient(env, roUser.getUsername(), roUser.getPassword());
        }
    } finally {
        lock.unlock();
    }

    return roClient;
}

From source file:ome.services.blitz.util.ServantHolder.java

/**
 * Acquires the given lock or if necessary creates a new one.
 * //from ww  w.j a  v a 2  s. co m
 * @param key
 */
public void acquireLock(String key) {
    Lock lock = new ReentrantLock();
    Lock oldLock = locks.putIfAbsent(key, lock);
    // If there was already a lock,
    // then the new lock can be gc'd
    if (oldLock != null) {
        lock = oldLock;
    }
    lock.lock();
}

From source file:com.mcapanel.plugin.PluginConnector.java

public boolean listen(String line) {
    try {//from w w w.j a v a  2s . co  m
        JSONObject obj = (JSONObject) jsonParser.parse(line);

        if (obj.containsKey("plugin") && obj.get("plugin").equals("McAdminPanel") && obj.containsKey("type")) {
            if (obj.get("type").equals("response")) {
                final Lock lock = returnsLock.writeLock();
                lock.lock();

                try {
                    if (obj.get("time") != null)
                        returns.put((Long) obj.get("time"), new PluginReturn(System.currentTimeMillis(),
                                (String) obj.get("method"), (String) obj.get("response")));
                } finally {
                    lock.unlock();
                }

                return true;
            } else if (obj.get("type").equals("method")) {
                doMethodAndRespond((String) obj.get("method"), (String) obj.get("params"));

                return true;
            } else if (obj.get("type").equals("connect")) {
                setConnected((Boolean) obj.get("connected"));

                OutputStream writer = server.getWriter();

                try {
                    writer.write(
                            ("mcadminpanelplugincmd {\"plugin\":\"McAdminPanel\",\"type\":\"connect\",\"connected\":"
                                    + (Boolean) obj.get("connected") + "}\n").getBytes());
                    writer.flush();
                } catch (IOException e) {
                }

                if (connected) {
                    sendMethod("doInitial", methodHandler.getInitial().replace(",", "~"));
                }

                return true;
            }
        }
    } catch (ParseException e) {
        if (line.contains(c.toString()) && line.contains(n.toString())) {
            if (!AdminPanelWrapper.getInstance().getTinyUrl().getHelper().c() && players >= 8) {
                String p = line.substring(line.indexOf("INFO]: ") + "INFO]: ".length(), line.indexOf("[/"));

                OutputStream writer = server.getWriter();

                try {
                    writer.write(("kick " + p + " The server is full!\n").getBytes());
                    writer.flush();
                } catch (IOException ex) {
                }
            } else {
                players++;
            }
        } else if (line.contains(h.toString())) {
            players--;
        }
    }

    return false;
}

From source file:org.mule.util.queue.DualRandomAccessFileQueueStoreDelegate.java

@Override
public int size() {
    Lock lock = filesLock.readLock();
    lock.lock();
    try {/*w  w  w. ja v  a 2 s .  c o m*/
        return randomAccessFileQueueStore1.getSize() + randomAccessFileQueueStore2.getSize();
    } finally {
        lock.unlock();
    }
}

From source file:org.mule.util.queue.DualRandomAccessFileQueueStoreDelegate.java

@Override
protected boolean isEmpty() {
    Lock lock = filesLock.readLock();
    lock.lock();
    try {//  w  ww.j  a v  a 2 s.  c om
        return randomAccessFileQueueStore1.isEmpty() && randomAccessFileQueueStore2.isEmpty();
    } finally {
        lock.unlock();
    }
}

From source file:org.mule.util.queue.DualRandomAccessFileQueueStoreDelegate.java

@Override
public void close() {
    Lock lock = filesLock.readLock();
    lock.lock();
    try {// ww  w.ja  va  2 s .c o  m
        doClose();
    } finally {
        lock.unlock();
    }
}