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);
}
}
}
}
}
}
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;
}
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);
}
}
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();
}
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;
}
Aggregations