use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by infiniteautomation.
the class DataSourceDaoDeadlockDetection method detectDeadlockFromDataSourceDeleteDataPointInsert.
/**
* See the deadlock when you insert data source, then point,
* then then delete source (which deletes the point inside a transaction)
*/
@Test
public void detectDeadlockFromDataSourceDeleteDataPointInsert() {
// 25;
int numThreads = 5;
// 100;
int numDataSources = 10;
PermissionService permissionService = Common.getBean(PermissionService.class);
DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
AtomicInteger failures = new AtomicInteger();
AtomicInteger successes = new AtomicInteger();
AtomicInteger running = new AtomicInteger(numThreads);
MutableObject<Exception> failure = new MutableObject<>(null);
for (int i = 0; i < numThreads; i++) {
new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < numDataSources; i++) {
// Create data source
MockDataSourceVO ds = new MockDataSourceVO();
ds.setName(Common.generateXid("Mock "));
dataSourceService.insert(ds);
// Create and save point
createMockDataPoint(ds, new MockPointLocatorVO());
// Delete data source
dataSourceService.delete(ds.getId());
successes.getAndIncrement();
}
} catch (Exception e) {
e.printStackTrace();
failure.setValue(e);
failures.getAndIncrement();
} finally {
running.decrementAndGet();
}
}
}.start();
}
while (running.get() > 0) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
if (failures.get() > 0) {
fail("Ran " + successes.get() + " queries: " + failure.getValue().getMessage());
}
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by infiniteautomation.
the class LazyFieldJsonTest method testLazyPermissionInObject.
@Test
public void testLazyPermissionInObject() {
RoleService roleService = Common.getBean(RoleService.class);
PermissionService permissionService = Common.getBean(PermissionService.class);
Role role1 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-1", "Role 1")).getRole();
Role role2 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-2", "Role 2")).getRole();
LazyContainer container = new LazyContainer();
container.supplyPermission(() -> MangoPermission.builder().minterm(role1, role2).build());
try (StringWriter stringWriter = new StringWriter()) {
JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
JsonValue value = typeWriter.writeObject(container);
writer.setPrettyIndent(0);
writer.setPrettyOutput(true);
writer.writeObject(value);
String json = stringWriter.toString();
JsonTypeReader typeReader = new JsonTypeReader(json);
JsonValue read = typeReader.read();
JsonObject root = read.toJsonObject();
JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
ImportContext context = new ImportContext(reader, new ProcessResult(), Common.getTranslations());
LazyContainer readContainer = new LazyContainer();
context.getReader().readInto(readContainer, root);
assertEquals(container.getPermission(), readContainer.getPermission());
} catch (IOException | JsonException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by infiniteautomation.
the class ScriptPermissionConverter method jsonRead.
// TODO Mango 4.2 improve performance with lazy field as PermissionService is not available at construct time
@Override
public Object jsonRead(JsonReader reader, JsonValue jsonValue, Type type) throws JsonException {
Set<Role> roles = new HashSet<>();
PermissionService service = Common.getBean(PermissionService.class);
if (jsonValue instanceof JsonArray) {
for (JsonValue val : (JsonArray) jsonValue) {
// Just a single string
Role r = service.getRole(val.toString());
if (r != null) {
roles.add(r);
} else {
// Let the validation pick this up as a missing role, the response to the user is cleaner
roles.add(new Role(Common.NEW_ID, val.toString()));
}
}
} else if (jsonValue instanceof JsonObject) {
// Could be the super-legacy version with 3 separate sets of roles
JsonObject o = (JsonObject) jsonValue;
Set<String> permissions = new HashSet<>();
permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(DATA_SOURCE)));
permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(DATA_POINT_SET)));
permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(DATA_POINT_READ)));
permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(CUSTOM)));
for (String role : permissions) {
Role r = service.getRole(role);
if (r != null) {
roles.add(r);
} else {
// Let the validation pick this up as a missing role, the response to the user is cleaner
roles.add(new Role(Common.NEW_ID, role));
}
}
}
return new ScriptPermissions(roles);
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by infiniteautomation.
the class ScriptEventHandlerVO method setScriptRoleXids.
private void setScriptRoleXids(Set<String> xids) {
PermissionService permissionService = Common.getBean(PermissionService.class);
this.scriptRoles = xids.stream().map(xid -> {
Role r = permissionService.getRole(xid);
return r != null ? r : null;
}).filter(Objects::nonNull).collect(Collectors.toSet());
}
use of com.infiniteautomation.mango.spring.service.PermissionService in project ma-core-public by MangoAutomation.
the class ScriptEventHandlerVO method setScriptRoleXids.
private void setScriptRoleXids(Set<String> xids) {
PermissionService permissionService = Common.getBean(PermissionService.class);
this.scriptRoles = xids.stream().map(xid -> {
Role r = permissionService.getRole(xid);
return r != null ? r : null;
}).filter(Objects::nonNull).collect(Collectors.toSet());
}
Aggregations