Search in sources :

Example 1 with LockDescriptor

use of com.haulmont.cuba.core.entity.LockDescriptor in project cuba by cuba-platform.

the class LockManager method expireLocks.

@Override
public void expireLocks() {
    log.debug("Expiring locks");
    ArrayList<LockKey> list = new ArrayList<>(locks.keySet());
    for (LockKey key : list) {
        LockInfo lockInfo = locks.get(key);
        if (lockInfo != null) {
            LockDescriptor ld = getConfig().get(key.name);
            if (ld == null) {
                log.debug("Lock " + key.name + "/" + key.id + " configuration not found, remove it");
                locks.remove(key);
            } else {
                Integer timeoutSec = ld.getTimeoutSec();
                if (timeoutSec != null && timeoutSec > 0) {
                    Date since = lockInfo.getSince();
                    if (since.getTime() + timeoutSec * 1000 < AppBeans.get(TimeSource.class).currentTimestamp().getTime()) {
                        log.debug("Lock " + key.name + "/" + key.id + " expired");
                        locks.remove(key);
                    }
                }
            }
        }
    }
}
Also used : LockDescriptor(com.haulmont.cuba.core.entity.LockDescriptor) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 2 with LockDescriptor

use of com.haulmont.cuba.core.entity.LockDescriptor in project cuba by cuba-platform.

the class LockManager method getConfig.

private Map<String, LockDescriptor> getConfig() {
    if (this.config == null) {
        synchronized (this) {
            if (this.config == null) {
                Map<String, LockDescriptor> config = new ConcurrentHashMap<>();
                Transaction tx = persistence.createTransaction();
                try {
                    EntityManager em = persistence.getEntityManager();
                    TypedQuery<LockDescriptor> q = em.createQuery("select d from sys$LockDescriptor d", LockDescriptor.class);
                    List<LockDescriptor> list = q.getResultList();
                    for (LockDescriptor ld : list) {
                        config.put(ld.getName(), ld);
                    }
                    tx.commit();
                } finally {
                    tx.end();
                }
                this.config = config;
            }
        }
    }
    return config;
}
Also used : LockDescriptor(com.haulmont.cuba.core.entity.LockDescriptor) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with LockDescriptor

use of com.haulmont.cuba.core.entity.LockDescriptor in project cuba by cuba-platform.

the class LockEditor method init.

@Override
public void init(Map<String, Object> params) {
    Map<String, Object> options = new TreeMap<>();
    for (MetaClass metaClass : metadata.getTools().getAllPersistentMetaClasses()) {
        if (metadata.getExtendedEntities().getExtendedClass(metaClass) == null) {
            MetaClass originalMetaClass = metadata.getExtendedEntities().getOriginalMetaClass(metaClass);
            String originalName = originalMetaClass == null ? metaClass.getName() : originalMetaClass.getName();
            options.put(messages.getTools().getEntityCaption(metaClass) + " (" + metaClass.getName() + ")", originalName);
        }
    }
    entityNameLookupField = componentsFactory.createComponent(LookupField.class);
    entityNameLookupField.setDatasource(lockDescriptorDs, "name");
    entityNameLookupField.setOptionsMap(options);
    entityNameLookupField.setCaption(messages.getMessage(LockDescriptor.class, "LockDescriptor.name"));
    fieldGroup.getFieldNN("entity").setComponent(entityNameLookupField);
    if (((LockDescriptor) WindowParams.ITEM.getEntity(params)).getName() != null) {
        nameTypeOptionsGroup.setVisible(false);
        entityNameLookupField.setVisible(false);
        nameField.setEditable(false);
        timeoutSecField.requestFocus();
    } else {
        nameTypeOptionsGroup.setOptionsEnum(LockDescriptorNameType.class);
        nameTypeOptionsGroup.addValueChangeListener(e -> {
            if (LockDescriptorNameType.ENTITY.equals(e.getValue())) {
                nameField.setVisible(false);
                entityNameLookupField.setVisible(true);
                entityNameLookupField.requestFocus();
            } else {
                nameField.setVisible(true);
                nameField.requestFocus();
                entityNameLookupField.setVisible(false);
            }
        });
        nameTypeOptionsGroup.setValue(LockDescriptorNameType.ENTITY);
    }
}
Also used : LockDescriptor(com.haulmont.cuba.core.entity.LockDescriptor) MetaClass(com.haulmont.chile.core.model.MetaClass) TreeMap(java.util.TreeMap)

Example 4 with LockDescriptor

use of com.haulmont.cuba.core.entity.LockDescriptor in project cuba by cuba-platform.

the class LockManagerTest method setUp.

@Before
public void setUp() throws Exception {
    cont.persistence().runInTransaction(em -> {
        LockDescriptor lockDescriptor = cont.metadata().create(LockDescriptor.class);
        lockDescriptor.setName("sys$Server");
        lockDescriptor.setTimeoutSec(300);
        em.persist(lockDescriptor);
    });
    lockManager = AppBeans.get(LockManagerAPI.class);
    lockManager.reloadConfiguration();
}
Also used : LockDescriptor(com.haulmont.cuba.core.entity.LockDescriptor) LockManagerAPI(com.haulmont.cuba.core.app.LockManagerAPI) Before(org.junit.Before)

Example 5 with LockDescriptor

use of com.haulmont.cuba.core.entity.LockDescriptor in project cuba by cuba-platform.

the class LockManager method lock.

@Override
public LockInfo lock(String name, String id) {
    LockKey key = new LockKey(name, id);
    LockInfo lockInfo = locks.get(key);
    if (lockInfo != null) {
        log.debug("Already locked: " + lockInfo);
        return lockInfo;
    }
    LockDescriptor ld = getConfig().get(name);
    if (ld == null) {
        return new LockNotSupported();
    }
    lockInfo = new LockInfo(userSessionSource.getUserSession().getCurrentOrSubstitutedUser(), name, id);
    locks.put(key, lockInfo);
    log.debug("Locked " + name + "/" + id);
    clusterManager.send(lockInfo);
    return null;
}
Also used : LockDescriptor(com.haulmont.cuba.core.entity.LockDescriptor)

Aggregations

LockDescriptor (com.haulmont.cuba.core.entity.LockDescriptor)5 MetaClass (com.haulmont.chile.core.model.MetaClass)1 EntityManager (com.haulmont.cuba.core.EntityManager)1 Transaction (com.haulmont.cuba.core.Transaction)1 LockManagerAPI (com.haulmont.cuba.core.app.LockManagerAPI)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Before (org.junit.Before)1