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:jenkins.plugins.git.GitSCMFileSystem.java

<V> V invoke(final FSFunction<V> function) throws IOException, InterruptedException {
    Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry);
    cacheLock.lock();
    try {/*  w w  w .jav a  2 s  .  c  o m*/
        File cacheDir = AbstractGitSCMSource.getCacheDir(cacheEntry);
        if (cacheDir == null || !cacheDir.isDirectory()) {
            throw new IOException("Closed");
        }
        return client.withRepository(new RepositoryCallback<V>() {
            @Override
            public V invoke(Repository repository, VirtualChannel virtualChannel)
                    throws IOException, InterruptedException {
                return function.invoke(repository);
            }
        });
    } finally {
        cacheLock.unlock();
    }
}

From source file:be.fgov.kszbcss.rhq.websphere.config.CellConfiguration.java

private void discardSession(boolean destroy) {
    Lock writeLock = sessionLock.writeLock();
    writeLock.lock();
    try {//from ww w.j  ava  2  s  . c o  m
        if (session != null) {
            if (log.isDebugEnabled()) {
                log.debug("Discarding session " + session);
            }
            try {
                configService.discard(session);
            } catch (Exception ex) {
                log.warn("Unexpected exception when discarding workspace " + session, ex);
            }
            synchronized (resolverCache) {
                log.debug("Clearing resolver cache");
                resolverCache.clear();
            }
            synchronized (configObjectCache) {
                log.debug("Clearing config object cache");
                configObjectCache.clear();
            }
        }
        if (destroy) {
            destroyed = true;
        }
    } finally {
        writeLock.unlock();
    }
}

From source file:com.cip.crane.agent.sheduler.ExecuteTaskThread.java

@Override
public void run() {
    Lock lock = LockHelper.getLock(taskAttempt);
    ScheduleConf conf = null;//from  w w w  .  jav a 2  s .com
    ScheduleStatus status = null;
    try {
        lock.lock();
        conf = (ScheduleConf) cs.getConf(localIp, taskAttempt);
        status = (ScheduleStatus) cs.getStatus(localIp, taskAttempt);
        cs.addRunningJob(localIp, taskAttempt);
        status.setStatus(ScheduleStatus.EXECUTING);
        cs.updateStatus(localIp, taskAttempt, status);
    } catch (Exception e) {
        LOGGER.error(e, e);
    } finally {
        lock.unlock();
    }
    try {
        executeJob(conf, status);
    } catch (Exception e) {
        LOGGER.error(e, e);
        status.setStatus(ScheduleStatus.EXECUTE_FAILED);
        cs.removeRunningJob(localIp, taskAttempt);
    }
    try {
        lock.lock();
        cs.updateConf(localIp, taskAttempt, conf);
        ScheduleStatus thisStatus = (ScheduleStatus) cs.getStatus(localIp, taskAttempt);
        if (thisStatus.getStatus() != ScheduleStatus.DELETE_SUCCESS) {
            cs.updateStatus(localIp, taskAttempt, status);
            cs.removeRunningJob(localIp, taskAttempt);
        }
        LOGGER.debug(taskAttempt + " end execute");
    } catch (Exception e) {
        LOGGER.error(e, e);
    } finally {
        lock.unlock();
    }
}

From source file:com.mg.framework.service.DatabaseAuditServiceImpl.java

public void auditCreate(PostInsertEvent createEvent) {
    if (!isAuditActivated)
        return;//from  w w  w  . j  a v  a 2s  . c om

    try {
        Lock lock = entityAuditSetupLock.readLock();
        lock.lock();
        try {
            EntityAuditSetup auditSetup = entityAuditSetup.get(createEvent.getPersister().getEntityName());
            if (auditSetup == null || !auditSetup.isAuditCreate())
                return;
        } finally {
            lock.unlock();
        }

        String[] names = createEvent.getPersister().getPropertyNames();
        String[] stateStr = new String[names.length];
        String[] oldStateStr = new String[names.length];

        for (int i = 0; i < names.length; i++) {
            Type type = createEvent.getPersister().getPropertyType(names[i]);
            if (type.isCollectionType())
                continue;

            if (type.isEntityType()) {
                ClassMetadata metadata = createEvent.getPersister().getFactory()
                        .getClassMetadata(type.getName());
                stateStr[i] = entityPropertyToString(createEvent.getState()[i], metadata);
                oldStateStr[i] = null;
            } else {
                stateStr[i] = createEvent.getState()[i] == null ? null : createEvent.getState()[i].toString();
                oldStateStr[i] = null;
            }
        }

        sendAuditMessage(new EntityAuditEvent(createEvent.getPersister().getEntityName(),
                DatabaseAuditType.CREATE, createEvent.getId().toString(),
                createEvent.getPersister().getIdentifierPropertyName(), names, stateStr, oldStateStr));
    } catch (Exception e) {
        logger.error("audit create failed", e);
    }
}

From source file:org.apache.synapse.endpoints.algorithms.WeightedRoundRobin.java

public Endpoint getNextEndpoint(MessageContext synapseMessageContext, AlgorithmContext algorithmContext) {

    Lock readLock = lock.readLock();
    readLock.lock();
    try {/*  w w w  . j a va2  s . co m*/
        if (!isThreadLocal) {
            synchronized (this) {
                EndpointState state = endpointStates[endpointCursor];
                if (state.getCurrentWeight() == 0) {
                    // reset the current state
                    state.reset();

                    // go to the next endpoint
                    if (endpointCursor == endpointStates.length - 1) {
                        endpointCursor = 0;
                    } else {
                        ++endpointCursor;
                    }

                    state = endpointStates[endpointCursor];
                }

                // we are about to use this endpoint, so decrement its current count
                state.decrementCurrentWeight();

                // return the endpoint corresponding to the current position
                return endpoints.get(state.getEndpointPosition());
            }
        } else {
            if (threadedAlgorithm != null) {
                Algorithm algo = threadedAlgorithm.get();

                int position = algo.getNextEndpoint();

                return endpoints.get(position);
            } else {
                String msg = "Algorithm: WeightedRoundRobin algorithm not initialized properly";
                log.error(msg);
                throw new SynapseException(msg);
            }
        }
    } finally {
        readLock.unlock();
    }
}

From source file:com.mg.framework.service.DatabaseAuditServiceImpl.java

public void loadAuditSetup() {
    boolean startTran = false;
    try {/*  w w  w.  j av  a  2  s .  c  om*/
        logger.info("load audit setup");

        startTran = ServerUtils.getTransactionManager().getStatus() == Status.STATUS_NO_TRANSACTION;
        if (startTran)
            ServerUtils.getTransactionManager().begin();

        List<PersistentObject> list = OrmTemplate.getInstance()
                .findByCriteria(OrmTemplate.createCriteria("com.mg.merp.core.model.DatabaseAuditSetup"));

        Lock lock = entityAuditSetupLock.writeLock();
        lock.lock();
        try {
            if (entityAuditSetup != null)
                entityAuditSetup.clear();

            for (PersistentObject item : list) {
                String entityName = (String) item.getAttribute("AuditedEntityName");
                EntityAuditSetup auditSetup = entityAuditSetup.get(entityName);
                if (auditSetup == null)
                    entityAuditSetup.put(entityName, new EntityAuditSetup(item));
                else
                    auditSetup.setAudit(item);
            }
        } finally {
            lock.unlock();
        }

        if (startTran)
            ServerUtils.getTransactionManager().commit();
    } catch (Exception e) {
        logger.error("load audit setup failed", e);
        try {
            if (startTran)
                ServerUtils.getTransactionManager().rollback();
        } catch (Exception ee) {
            logger.error("transaction rollback failed", ee);
        }
    }
}

From source file:com.mg.framework.service.DatabaseAuditServiceImpl.java

public void auditRemove(PostDeleteEvent removeEvent) {
    if (!isAuditActivated)
        return;/*from   w  ww. j  ava 2s.c o  m*/

    try {
        Lock lock = entityAuditSetupLock.readLock();
        lock.lock();
        try {
            EntityAuditSetup auditSetup = entityAuditSetup.get(removeEvent.getPersister().getEntityName());
            if (auditSetup == null || !auditSetup.isAuditRemove())
                return;
        } finally {
            lock.unlock();
        }

        String[] names = removeEvent.getPersister().getPropertyNames();
        String[] stateStr = new String[names.length];
        String[] oldStateStr = new String[names.length];

        for (int i = 0; i < names.length; i++) {
            Type type = removeEvent.getPersister().getPropertyType(names[i]);
            if (type.isCollectionType())
                continue;

            if (type.isEntityType()) {
                ClassMetadata metadata = removeEvent.getPersister().getFactory()
                        .getClassMetadata(type.getName());
                oldStateStr[i] = entityPropertyToString(removeEvent.getDeletedState()[i], metadata);
                stateStr[i] = null;
            } else {
                oldStateStr[i] = removeEvent.getDeletedState()[i] == null ? null
                        : removeEvent.getDeletedState()[i].toString();
                stateStr[i] = null;
            }
        }

        sendAuditMessage(new EntityAuditEvent(removeEvent.getPersister().getEntityName(),
                DatabaseAuditType.REMOVE, removeEvent.getId().toString(),
                removeEvent.getPersister().getIdentifierPropertyName(), names, stateStr, oldStateStr));
    } catch (Exception e) {
        logger.error("audit create failed", e);
    }
}

From source file:com.iternox.piggate.samples.PiggateLogin.Activity_Logged.java

public void addPendingBeacons(ArrayList<PiggateBeacon> bar) {
    Lock l = rwLock.writeLock();
    l.lock();
    try {//from   w w w  . jav  a2 s.  c o  m
        this.pending.addAll(bar);
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
}

From source file:com.iternox.piggate.samples.PiggateLogin.Activity_Logged.java

public void addOffers(JSONArray bar) {

    Lock l = rwLock2.writeLock();
    l.lock();
    try {//  ww w.  j  av a2 s .co  m
        registryOffers(bar);
    } catch (Exception ex) {
    } finally {
        l.unlock();
    }
}

From source file:org.openhab.binding.neeo.internal.handler.NeeoBrainHandler.java

/**
 * Gets the {@link NeeoBrainApi} used by this bridge
 *
 * @return a possibly null {@link NeeoBrainApi}
 *///from   www . j  av  a 2  s  .  c o  m
@Nullable
public NeeoBrainApi getNeeoBrainApi() {
    final Lock readerLock = stateLock.readLock();
    readerLock.lock();
    try {
        return neeoBrainApi;
    } finally {
        readerLock.unlock();
    }
}