Search in sources :

Example 1 with HeaderParam

use of org.jaffa.modules.messaging.services.HeaderParam in project jaffa-framework by jaffa-projects.

the class RaiseEventService method raiseSoaEvent.

// TODO-SWAT add script events here
public void raiseSoaEvent(UOW uow, String eventName, String description, String category, List<HeaderParam> headerParams) throws FrameworkException, ApplicationExceptions {
    // there too.
    if (!SOAEventEnabledConfigurationFactory.instance().isEnabled(eventName)) {
        if (log.isDebugEnabled())
            log.debug("SOA event disabled: " + eventName);
        return;
    }
    UOW localUow = null;
    try {
        if (uow == null || !uow.isActive()) {
            localUow = new UOW();
        }
        SOAEventQueueMessage message = new SOAEventQueueMessage();
        message.setEventName(eventName);
        message.setDescription(description);
        message.setCreatedOn(new DateTime());
        message.setCreatedBy(SecurityManager.getPrincipal() != null ? SecurityManager.getPrincipal().getName() : null);
        if (category != null) {
            message.setCategory(category);
        }
        if (headerParams != null && headerParams.size() > 0) {
            message.setHeaderParams(headerParams.toArray(new HeaderParam[0]));
        }
        if (log.isDebugEnabled()) {
            log.debug("Creating SoaEvent Message" + message);
        }
        if (localUow != null) {
            localUow.addMessage(message);
            localUow.commit();
        } else {
            uow.addMessage(message);
        }
    } finally {
        if (localUow != null && localUow.isActive()) {
            try {
                localUow.rollback();
            } catch (Exception e) {
                log.error(e);
            }
        }
    }
}
Also used : HeaderParam(org.jaffa.modules.messaging.services.HeaderParam) UOW(org.jaffa.persistence.UOW) DateTime(org.jaffa.datatypes.DateTime) FrameworkException(org.jaffa.exceptions.FrameworkException)

Example 2 with HeaderParam

use of org.jaffa.modules.messaging.services.HeaderParam in project jaffa-framework by jaffa-projects.

the class TestThread method run.

@Override
public void run() {
    UOW uow = null;
    UserContextWrapper ucw = null;
    try {
        synchronized (this) {
            ucw = UserContextWrapperFactory.instance(userId);
        }
        uow = new UOW();
        HeaderParam headerParam = new HeaderParam(soaEventParamName, soaEventParamValue);
        RaiseEventService raiseEventService = new RaiseEventService();
        List<HeaderParam> headerParamList = new ArrayList<HeaderParam>();
        headerParamList.add(headerParam);
        raiseEventService.raiseSoaEvent(uow, eventName, eventDesc, null, headerParamList);
        uow.commit();
    } catch (ApplicationExceptions applicationExceptions) {
        applicationExceptions.printStackTrace();
    } catch (FrameworkException fe) {
        fe.printStackTrace();
    } catch (ApplicationException ae) {
        ae.printStackTrace();
    } finally {
        synchronized (this) {
            if (ucw != null)
                ucw.unsetContext();
        }
        if (uow != null) {
            try {
                uow.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            // the uow cannot be rolled back
            }
        }
    }
}
Also used : HeaderParam(org.jaffa.modules.messaging.services.HeaderParam) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ApplicationException(org.jaffa.exceptions.ApplicationException) FrameworkException(org.jaffa.exceptions.FrameworkException) UserContextWrapper(org.jaffa.modules.user.services.UserContextWrapper) RaiseEventService(org.jaffa.soa.services.RaiseEventService) ArrayList(java.util.ArrayList) UOW(org.jaffa.persistence.UOW) FrameworkException(org.jaffa.exceptions.FrameworkException) ApplicationException(org.jaffa.exceptions.ApplicationException)

Example 3 with HeaderParam

use of org.jaffa.modules.messaging.services.HeaderParam in project jaffa-framework by jaffa-projects.

the class SOAEventBaseHandler method createSOAEventParameters.

/**
 * Creates the required SOAEventParam header params, based on the input
 *
 * @param parameters   A semi-colon separated list of parameters (name-value pairs).
 * @param targetObject The target Object. Parameters will be considered static if this argument is null.
 * @throws FrameworkException exception if bean script fails
 */
private List<HeaderParam> createSOAEventParameters(String parameters, Object targetObject, Object args, RuleMetaData rule) throws ApplicationExceptions {
    List<HeaderParam> headerParamsList = new ArrayList<>();
    if (parameters != null) {
        for (String parameter : parameters.split(";")) {
            if (log.isDebugEnabled())
                log.debug("Handling parameter: " + parameter);
            int i = parameter.indexOf('=');
            if (i <= 0 || i == (parameter.length() - 1)) {
                String str = "Illegal argument passed: " + parameters;
                log.error(str);
                throw new IllegalArgumentException(str);
            }
            String name = parameter.substring(0, i);
            String value = parameter.substring(i + 1);
            if (targetObject != null) {
                try {
                    Object fieldValue = BeanHelper.getField(targetObject, value);
                    value = format(fieldValue);
                } catch (NoSuchMethodException e) {
                    // Assume the dynamic parameter to be a script
                    Map beans = new HashMap();
                    beans.put(ScriptHelper.CONTEXT_BEAN, targetObject);
                    if (args != null) {
                        beans.put(ScriptHelper.CONTEXT_ARGUMENTS, args);
                    }
                    try {
                        Object fieldValue = ScriptHelper.instance(rule.getParameter(RuleMetaData.PARAMETER_LANGUAGE)).evaluate(null, value, beans, rule.getSource(), rule.getLine() != null ? rule.getLine() : 0, 0);
                        value = format(fieldValue);
                    } catch (Throwable t) {
                        throw new ApplicationExceptions(new ApplicationException(t.getLocalizedMessage(), null, t));
                    }
                }
                if (log.isDebugEnabled())
                    log.debug("Value is: " + value);
            }
            HeaderParam headerParam = new HeaderParam(name, value);
            if (log.isDebugEnabled())
                log.debug("Created " + headerParam);
            headerParamsList.add(headerParam);
        }
    }
    return headerParamsList;
}
Also used : HeaderParam(org.jaffa.modules.messaging.services.HeaderParam) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ApplicationException(org.jaffa.exceptions.ApplicationException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with HeaderParam

use of org.jaffa.modules.messaging.services.HeaderParam in project jaffa-framework by jaffa-projects.

the class SOAEventBaseHandler method raiseSOAEvent.

/**
 * Raises the SOA event defined in the rule meta data
 *
 * @param uow          UOW for handler life cycle event
 * @param targetObject the target object
 * @param rule         the raise soa event rule meta data
 * @throws ApplicationExceptions
 * @throws FrameworkException
 */
private void raiseSOAEvent(UOW uow, Object targetObject, RuleMetaData rule, Object[] args) throws ApplicationExceptions, FrameworkException {
    UOW localUow = uow;
    if (uow == null || !uow.isActive()) {
        localUow = getUOW(targetObject);
    }
    if (log.isDebugEnabled()) {
        log.debug("Applying " + rule + " on " + targetObject);
    }
    // Add Parameters for the SOAEvent
    List<HeaderParam> headerParamsList = new ArrayList<>();
    headerParamsList.addAll(createSOAEventParameters(rule.getParameter("staticParameters"), null, null, null));
    headerParamsList.addAll(createSOAEventParameters(rule.getParameter("dynamicParameters"), targetObject, args, rule));
    RaiseEventService raiseEventService = new RaiseEventService();
    raiseEventService.raiseSoaEvent(localUow, rule.getParameter("eventName"), rule.getParameter("description"), rule.getParameter("category"), headerParamsList);
}
Also used : HeaderParam(org.jaffa.modules.messaging.services.HeaderParam) RaiseEventService(org.jaffa.soa.services.RaiseEventService) ArrayList(java.util.ArrayList) UOW(org.jaffa.persistence.UOW)

Aggregations

HeaderParam (org.jaffa.modules.messaging.services.HeaderParam)4 ArrayList (java.util.ArrayList)3 UOW (org.jaffa.persistence.UOW)3 ApplicationException (org.jaffa.exceptions.ApplicationException)2 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)2 FrameworkException (org.jaffa.exceptions.FrameworkException)2 RaiseEventService (org.jaffa.soa.services.RaiseEventService)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DateTime (org.jaffa.datatypes.DateTime)1 UserContextWrapper (org.jaffa.modules.user.services.UserContextWrapper)1