Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ public interface EPContext {
*/
public Service getService(String name);

/**
* This method returns the named parameter if available.
*
* @param name The parameter name
* @return The parameter value, or null if not found
*/
public Object getParameter(String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public abstract class EventProcessor {
private java.util.Map<String,Service> _services=
new java.util.HashMap<String,Service>();
private boolean _async=false;

private java.util.Map<String,Object> _parameters=
new java.util.HashMap<String,Object>();

private ResultHandler _handler=null;

/**
Expand All @@ -52,6 +54,24 @@ public void setServices(java.util.Map<String,Service> services) {
_services = services;
}

/**
* This method returns the map of names to parameters.
*
* @return The parameters
*/
public java.util.Map<String,Object> getParameters() {
return (_parameters);
}

/**
* This method sets the map of names to parameters.
*
* @param parameters The parameters
*/
public void setParameters(java.util.Map<String,Object> parameters) {
_parameters = parameters;
}

/**
* This method indicates whether an asynchronous results listener will be required
* for this event processor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class DefaultEPContext implements EPContext {
private static final ThreadLocal<Object> RESULT=new ThreadLocal<Object>();

private java.util.Map<String,Service> _services=null;
private java.util.Map<String,Object> _parameters=null;
private ResultHandler _handler=null;

/**
Expand All @@ -53,6 +54,17 @@ public DefaultEPContext(java.util.Map<String,Service> services) {
_services = services;
}

/**
* This constructor initializes the service map.
*
* @param services The map of services available
* @param parameters The map of parameters avaiable
*/
public DefaultEPContext(java.util.Map<String,Service> services, java.util.Map<String,Object> parameters) {
_services = services;
_parameters = parameters;
}

/**
* This method sets the result handler.
*
Expand Down Expand Up @@ -144,4 +156,17 @@ public Service getService(String name) {

return (ret);
}

/**
* {@inheritDoc}
*/
public Object getParameter(String name) {
Object ret=null;

if (_parameters != null) {
ret = _parameters.get(name);
}

return (ret);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008-13, Red Hat Middleware LLC, and others contributors as indicated
* by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.overlord.rtgov.ep;

import static org.junit.Assert.*;

import java.io.Serializable;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;

public class EventProcessorTest {

@Test
public void testEventProcessorSerialization() {
ObjectMapper mapper=new ObjectMapper();

TestEventProcessor ep=new TestEventProcessor();
ep.getParameters().put("testparam1", "hello");
ep.getParameters().put("testparam2", 5);

try {
String result=mapper.writeValueAsString(ep);

TestEventProcessor epresult=mapper.readValue(result, TestEventProcessor.class);

if (epresult == null) {
fail("Result is null");
}

if (!epresult.getParameters().get("testparam1").equals("hello")) {
fail("Test param 1 is incorrect: "+epresult.getParameters().get("testparam1"));
}

int val=(Integer)epresult.getParameters().get("testparam2");

if (val != 5) {
fail("Test param 2 is not 5: "+val);
}

} catch (Exception e) {
fail("Failed to serialize ep: "+e);
}
}

@Test
public void testEventProcessorSerializationUnknownType() {
ObjectMapper mapper=new ObjectMapper();

TestEventProcessor ep=new TestEventProcessor();
ep.getParameters().put("testparam1", "hello");
ep.getParameters().put("testparam2", new TestObject());

try {
String result=mapper.writeValueAsString(ep);

TestEventProcessor epresult=mapper.readValue(result, TestEventProcessor.class);

if (epresult == null) {
fail("Result 1 is null");
}

if (!epresult.getParameters().get("testparam1").equals("hello")) {
fail("Test param 1 is incorrect: "+epresult.getParameters().get("testparam1"));
}

Object obj=epresult.getParameters().get("testparam2");

if (obj == null) {
fail("Result 2 is null");
}

@SuppressWarnings("unchecked")
java.util.Map<String,Object> map=(java.util.Map<String,Object>)obj;

String val2=(String)map.get("val");

if (!val2.equals("TestValue")) {
fail("Incorrect value: "+val2);
}

} catch (Exception e) {
fail("Failed to serialize ep: "+e);
}
}

public static class TestEventProcessor extends EventProcessor {

@Override
public Serializable process(String source, Serializable event,
int retriesLeft) throws Exception {
// TODO Auto-generated method stub
return null;
}

}

public static class TestObject {

private String _val="TestValue";

public String getVal() {
return (_val);
}

public void setVal(String val) {
_val = val;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void init() throws Exception {
+"' must NOT be configured as 'asynchronous' when using 'cloud' eventProcessingMode");
}

_context = new DefaultEPContext(getServices());
_context = new DefaultEPContext(getServices(), getParameters());

_session = createSession();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,35 @@ public void testCloudEventProcessingModeAsync() {
}
}

@Test
public void testAccessParameter() {
DroolsEventProcessor ep=new DroolsEventProcessor();
ep.setRuleName("AccessParameter");
ep.getParameters().put("param", "testParamValue");

try {
ep.init();

RequestReceived me1=new RequestReceived();
me1.setTimestamp(System.currentTimeMillis());
me1.getProperties().put("customer", "Ivan");
me1.setMessageId("me1");

Object result1=ep.process("Event", me1, 0);

if (result1 == null) {
fail("Result 1 is null");
}

if (!result1.equals("testParamValue")) {
fail("Paramater value incorrect: "+result1);
}
} catch(Exception e) {
e.printStackTrace();
fail("Exception: "+e);
}
}

public class TestResultHandler implements ResultHandler {

private java.util.List<Serializable> _results=new java.util.ArrayList<Serializable>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import org.overlord.rtgov.activity.model.soa.RequestReceived

global org.overlord.rtgov.ep.EPContext epc

declare RequestReceived
@role( event )
@expires( 20s )
end

rule "Access parameter rule"
when
$r: RequestReceived() from entry-point "Event"
then
epc.handle(epc.getParameter("param"));
end
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void init() throws Exception {
_idScriptExpression = null;
}

_context = new DefaultEPContext(getServices());
_context = new DefaultEPContext(getServices(), getParameters());

/**
* expect type SimpleDocumentRepo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void init() throws Exception {
}
}

_context = new DefaultEPContext(getServices());
_context = new DefaultEPContext(getServices(), getParameters());
}

/**
Expand Down
6 changes: 6 additions & 0 deletions samples/jbossas/ordermgmt/epn/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@

<build>
<finalName>ordermgmt-epn</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions samples/jbossas/ordermgmt/epn/src/main/resources/epn.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name" : "OrderManagementEPN",
"version" : "${project.version}",
"subscriptions" : [ {
"nodeName" : "CheckForException",
"subject" : "ActivityUnits"
Expand All @@ -23,6 +24,5 @@
"subject" : "Situations"
} ]
}
],
"version" : "1"
]
}
6 changes: 6 additions & 0 deletions samples/jbossas/ordermgmt/ip/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@

<build>
<finalName>ordermgmt-ip</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion samples/jbossas/ordermgmt/ip/src/main/resources/ip.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[{
"name":"OrderManagementIP",
"version":"1",
"version":"${project.version}",
"typeProcessors":{
"{urn:switchyard-quickstart-demo:orders:1.0}submitOrder":{
"contexts":[{
Expand Down
6 changes: 6 additions & 0 deletions samples/jbossas/policy/async/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@

<build>
<finalName>policy-async</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ if (customer == null) {

cm = epc.getService("CacheManager");

creditLimit = epc.getParameter("creditLimit");

// Attempt to lock the entry
if (!cm.lock("Principals", customer)) {
epc.handle(new Exception("Unable to lock entry for principal '"+customer+"'"));
Expand Down Expand Up @@ -34,7 +36,7 @@ if (event.operation == "submitOrder") {

int newtotal=current+total;

if (newtotal > 150 && current <= 150) {
if (newtotal > creditLimit && current <= creditLimit) {
principal.put("suspended", Boolean.TRUE);
}

Expand All @@ -46,7 +48,7 @@ if (event.operation == "submitOrder") {

int newamount=current-amount;

if (newamount <= 150 && current > 150) {
if (newamount <= creditLimit && current > creditLimit) {
principal.put("suspended", Boolean.FALSE);
}

Expand Down
Loading