Search in sources :

Example 16 with EventTypeMatcher

use of com.serotonin.m2m2.rt.event.type.EventTypeMatcher in project ma-core-public by infiniteautomation.

the class EventHandlerDao method saveRelationalData.

@Override
public void saveRelationalData(AbstractEventHandlerVO existing, AbstractEventHandlerVO vo) {
    if (existing != null) {
        // Delete old mappings
        deleteEventHandlerMappings(vo.getId());
    }
    List<EventTypeMatcher> types = vo.getEventTypes();
    if (types.size() > 0) {
        BatchBindStep batchTypes = create.batch(create.insertInto(handlerMapping).columns(handlerMapping.eventHandlerId, handlerMapping.eventTypeName, handlerMapping.eventSubtypeName, handlerMapping.eventTypeRef1, handlerMapping.eventTypeRef2).values((Integer) null, null, null, null, null));
        for (EventTypeMatcher type : types) {
            String subType = type.getEventSubtype();
            subType = subType != null ? subType : "";
            batchTypes.bind(vo.getId(), type.getEventType(), subType, type.getReferenceId1(), type.getReferenceId2());
        }
        batchTypes.execute();
    }
    vo.getDefinition().saveRelationalData(existing, vo);
    if (existing != null) {
        if (!existing.getReadPermission().equals(vo.getReadPermission())) {
            permissionService.deletePermissions(existing.getReadPermission());
        }
        if (!existing.getEditPermission().equals(vo.getEditPermission())) {
            permissionService.deletePermissions(existing.getEditPermission());
        }
    }
}
Also used : EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) BatchBindStep(org.jooq.BatchBindStep)

Example 17 with EventTypeMatcher

use of com.serotonin.m2m2.rt.event.type.EventTypeMatcher in project ma-core-public by MangoAutomation.

the class EventHandlerImporter method importImpl.

@Override
protected void importImpl() {
    AbstractEventHandlerVO handler = null;
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            handler = service.get(xid);
        } catch (NotFoundException e) {
        // Nothing, done below
        }
    }
    if (handler == null) {
        String typeStr = json.getString("handlerType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.eventHandler.missingType", xid, ModuleRegistry.getEventHandlerDefinitionTypes());
        else {
            EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.eventHandler.invalidType", xid, typeStr, ModuleRegistry.getEventHandlerDefinitionTypes());
            else {
                handler = def.baseCreateEventHandlerVO();
                handler.setXid(xid);
            }
        }
    }
    JsonObject et = json.getJsonObject("eventType");
    JsonArray ets = json.getJsonArray("eventTypes");
    if (handler != null) {
        try {
            ctx.getReader().readInto(handler, json);
            Set<EventTypeMatcher> eventTypes = new HashSet<>(handler.getEventTypes());
            // Find the event type.
            if (et != null) {
                eventTypes.add(new EventTypeMatcher(ctx.getReader().read(EventType.class, et)));
            } else if (ets != null) {
                for (JsonValue jsonValue : ets) {
                    eventTypes.add(new EventTypeMatcher(ctx.getReader().read(EventType.class, jsonValue)));
                }
            }
            handler.setEventTypes(new ArrayList<>(eventTypes));
            boolean isnew = handler.getId() == Common.NEW_ID;
            if (isnew) {
                service.insert(handler);
                addSuccessMessage(true, "emport.eventHandler.prefix", xid);
            } else {
                service.update(handler.getId(), handler);
                addSuccessMessage(false, "emport.eventHandler.prefix", xid);
            }
        } catch (ValidationException e) {
            setValidationMessages(e.getValidationResult(), "emport.eventHandler.prefix", xid);
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.eventHandler.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.eventHandler.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) EventType(com.serotonin.m2m2.rt.event.type.EventType) JsonValue(com.serotonin.json.type.JsonValue) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) JsonArray(com.serotonin.json.type.JsonArray) HashSet(java.util.HashSet)

Example 18 with EventTypeMatcher

use of com.serotonin.m2m2.rt.event.type.EventTypeMatcher in project ma-core-public by MangoAutomation.

the class EmailEventHandlerServiceTest method newVO.

@Override
EmailEventHandlerVO newVO(User user) {
    EmailEventHandlerVO vo = (EmailEventHandlerVO) ModuleRegistry.getEventHandlerDefinition(EmailEventHandlerDefinition.TYPE_NAME).baseCreateEventHandlerVO();
    vo.setXid(UUID.randomUUID().toString());
    vo.setName(UUID.randomUUID().toString());
    ScriptPermissions permissions = new ScriptPermissions(Collections.singleton(readRole));
    vo.setScriptRoles(permissions);
    List<EventTypeMatcher> eventTypes = Collections.singletonList(new EventTypeMatcher(new MockEventType(readRole)));
    vo.setEventTypes(eventTypes);
    return vo;
}
Also used : EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) MockEventType(com.serotonin.m2m2.rt.event.type.MockEventType)

Example 19 with EventTypeMatcher

use of com.serotonin.m2m2.rt.event.type.EventTypeMatcher in project ma-core-public by MangoAutomation.

the class DataSourceDaoDeadlockDetection method detectDeadlockWithEventHandlerRoleMappingandDataSourceTablesExplicit.

@Test
public void detectDeadlockWithEventHandlerRoleMappingandDataSourceTablesExplicit() {
    // This will create 2x threads for each operating as one of the desired problem scenarios
    int numThreads = 5;
    int numDataSources = 10;
    AtomicInteger running = new AtomicInteger(numThreads * 2);
    PermissionService permissionService = Common.getBean(PermissionService.class);
    // Insert some roles
    int roleCount = 0;
    RoleService roleService = Common.getBean(RoleService.class);
    List<RoleVO> roleVOs = new ArrayList<>();
    Set<Role> roles = new HashSet<>();
    for (int i = 0; i < roleCount; i++) {
        RoleVO role = new RoleVO(Common.NEW_ID, Common.generateXid("ROLE_"), "Role " + i);
        roleVOs.add(role);
        roleService.insert(role);
        roles.add(role.getRole());
    }
    DataSource dataSource = Common.getBean(DatabaseProxy.class).getDataSource();
    JdbcConnectionPool pool = (JdbcConnectionPool) dataSource;
    pool.setMaxConnections(numThreads * 2);
    PlatformTransactionManager transactionManager = Common.getBean(DatabaseProxy.class).getTransactionManager();
    AtomicInteger successes = new AtomicInteger();
    AtomicInteger failures = new AtomicInteger();
    MutableObject<Exception> failure = new MutableObject<>(null);
    for (int i = 0; i < numThreads; i++) {
        // #5 lock eventHandlerMappings and roleMappings and then try to lock dataSources
        // Basically delete a data source
        new Thread() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < numDataSources; i++) {
                        // Insert an event handler
                        EventHandlerService eventHandlerService = Common.getBean(EventHandlerService.class);
                        ProcessEventHandlerVO eh = new ProcessEventHandlerVO();
                        eh.setDefinition(new ProcessEventHandlerDefinition());
                        eh.setName(Common.generateXid("Handler "));
                        eh.setActiveProcessCommand("ls");
                        eventHandlerService.insert(eh);
                        ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate(dataSource);
                        // Get event handler
                        AbstractEventHandlerVO myEventHandler = eventHandlerService.get(eh.getXid());
                        // Create data source
                        MockDataSourceVO ds = new MockDataSourceVO();
                        ds.setName(Common.generateXid("Mock "));
                        DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
                        dataSourceService.insert(ds);
                        // Insert a mapping
                        myEventHandler.setEventTypes(Collections.singletonList(new EventTypeMatcher(new DataSourceEventType(ds.getId(), ds.getPollAbortedExceptionEventId()))));
                        eventHandlerService.update(eh.getXid(), myEventHandler);
                        new TransactionTemplate(transactionManager).execute((status) -> {
                            // The order of these statements matters for deadlock, we must always lock groups of tables in the same order
                            ejt.update("DELETE FROM dataSources WHERE id=?", new Object[] { ds.getId() });
                            ejt.update("DELETE FROM eventHandlersMapping WHERE eventTypeName=? AND eventTypeRef1=?", new Object[] { EventTypeNames.DATA_SOURCE, ds.getId() });
                            return null;
                        });
                        successes.getAndIncrement();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    failure.setValue(e);
                    failures.getAndIncrement();
                } finally {
                    running.decrementAndGet();
                }
            }
        }.start();
        // #8 lock dataSources and try to lock roleMappings
        // Basically update a data source
        new Thread() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < numDataSources; i++) {
                        ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate(dataSource);
                        // Insert an event handler
                        EventHandlerService eventHandlerService = Common.getBean(EventHandlerService.class);
                        ProcessEventHandlerVO eh = new ProcessEventHandlerVO();
                        eh.setDefinition(new ProcessEventHandlerDefinition());
                        eh.setName(Common.generateXid("Handler "));
                        eh.setActiveProcessCommand("ls");
                        eventHandlerService.insert(eh);
                        // Get event handler
                        AbstractEventHandlerVO myEventHandler = eventHandlerService.get(eh.getXid());
                        // Create data source
                        MockDataSourceVO ds = new MockDataSourceVO();
                        ds.setName(Common.generateXid("Mock "));
                        DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
                        dataSourceService.insert(ds);
                        // Insert a mapping
                        myEventHandler.setEventTypes(Collections.singletonList(new EventTypeMatcher(new DataSourceEventType(ds.getId(), ds.getPollAbortedExceptionEventId()))));
                        eventHandlerService.update(eh.getXid(), myEventHandler);
                        new TransactionTemplate(transactionManager).execute((status) -> {
                            ejt.update("UPDATE dataSources SET xid=? WHERE id=?", new Object[] { ds.getXid() + "1", ds.getId() });
                            return null;
                        });
                        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());
    }
}
Also used : DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) Role(com.serotonin.m2m2.vo.role.Role) BeforeClass(org.junit.BeforeClass) EventHandlerService(com.infiniteautomation.mango.spring.service.EventHandlerService) ProcessEventHandlerDefinition(com.serotonin.m2m2.module.definitions.event.handlers.ProcessEventHandlerDefinition) EventTypeNames(com.serotonin.m2m2.rt.event.type.EventType.EventTypeNames) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) LoggerFactory(org.slf4j.LoggerFactory) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ArrayList(java.util.ArrayList) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) HashSet(java.util.HashSet) EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) DataSource(javax.sql.DataSource) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) Assert.fail(org.junit.Assert.fail) MutableObject(org.apache.commons.lang3.mutable.MutableObject) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO) Logger(org.slf4j.Logger) Common(com.serotonin.m2m2.Common) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Set(java.util.Set) Test(org.junit.Test) UUID(java.util.UUID) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) List(java.util.List) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Collections(java.util.Collections) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) RoleService(com.infiniteautomation.mango.spring.service.RoleService) JdbcConnectionPool(org.h2.jdbcx.JdbcConnectionPool) ArrayList(java.util.ArrayList) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) EventHandlerService(com.infiniteautomation.mango.spring.service.EventHandlerService) HashSet(java.util.HashSet) MutableObject(org.apache.commons.lang3.mutable.MutableObject) ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) JdbcConnectionPool(org.h2.jdbcx.JdbcConnectionPool) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) DataSource(javax.sql.DataSource) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) Role(com.serotonin.m2m2.vo.role.Role) ProcessEventHandlerDefinition(com.serotonin.m2m2.module.definitions.event.handlers.ProcessEventHandlerDefinition) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleService(com.infiniteautomation.mango.spring.service.RoleService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Test(org.junit.Test)

Example 20 with EventTypeMatcher

use of com.serotonin.m2m2.rt.event.type.EventTypeMatcher in project ma-core-public by MangoAutomation.

the class EmailEventHandlerDaoTest method assertVoEqual.

@Override
void assertVoEqual(EmailEventHandlerVO expected, EmailEventHandlerVO actual) {
    assertEquals(expected.getId(), actual.getId());
    assertEquals(expected.getXid(), actual.getXid());
    assertEquals(expected.getName(), actual.getName());
    assertPermission(expected.getReadPermission(), actual.getReadPermission());
    assertPermission(expected.getEditPermission(), actual.getEditPermission());
    assertRoles(((EmailEventHandlerVO) expected).getScriptRoles().getRoles(), ((EmailEventHandlerVO) actual).getScriptRoles().getRoles());
    List<EventTypeMatcher> actualEventTypes = actual.getEventTypes();
    List<EventTypeMatcher> expectedEventTypes = expected.getEventTypes();
    assertEquals(expectedEventTypes.size(), actualEventTypes.size());
    for (int i = 0; i < expectedEventTypes.size(); i++) {
        EventTypeMatcher actualEventMatcher = actualEventTypes.get(i);
        EventTypeMatcher expectedEventMatcher = expectedEventTypes.get(i);
        assertEquals(expectedEventMatcher.getEventType(), actualEventMatcher.getEventType());
        assertEquals(expectedEventMatcher.getEventSubtype(), actualEventMatcher.getEventSubtype());
        assertEquals(expectedEventMatcher.getReferenceId1(), actualEventMatcher.getReferenceId1());
        assertEquals(expectedEventMatcher.getReferenceId2(), actualEventMatcher.getReferenceId2());
    }
// TODO assert remaining
}
Also used : EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO)

Aggregations

EventTypeMatcher (com.serotonin.m2m2.rt.event.type.EventTypeMatcher)20 AbstractEventHandlerVO (com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)8 EmailEventHandlerVO (com.serotonin.m2m2.vo.event.EmailEventHandlerVO)8 HashSet (java.util.HashSet)8 DataSourceService (com.infiniteautomation.mango.spring.service.DataSourceService)4 EventHandlerService (com.infiniteautomation.mango.spring.service.EventHandlerService)4 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)4 ScriptPermissions (com.infiniteautomation.mango.util.script.ScriptPermissions)4 ExtendedJdbcTemplate (com.serotonin.db.spring.ExtendedJdbcTemplate)4 Common (com.serotonin.m2m2.Common)4 DatabaseProxy (com.serotonin.m2m2.db.DatabaseProxy)4 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)4 ProcessEventHandlerDefinition (com.serotonin.m2m2.module.definitions.event.handlers.ProcessEventHandlerDefinition)4 DataSourceEventType (com.serotonin.m2m2.rt.event.type.DataSourceEventType)4 EventType (com.serotonin.m2m2.rt.event.type.EventType)4 MockEventType (com.serotonin.m2m2.rt.event.type.MockEventType)4 RoleVO (com.serotonin.m2m2.vo.role.RoleVO)4 Collections (java.util.Collections)4 List (java.util.List)4 Set (java.util.Set)4