Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
jbvanzuylen committed Feb 17, 2016
1 parent 6f45ae2 commit 02a34a1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@
package org.primeoservices.cfgateway.jms.railo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.primeoservices.cfgateway.jms.JMSExchanger;
import org.primeoservices.cfgateway.jms.JMSListener;

import railo.runtime.gateway.GatewayEngine;
import railo.runtime.type.Collection.Key;
import railo.runtime.type.Struct;

public class RailoJMSListenerGateway extends AbstractRailoJMSGateway
{
private static final String GATEWAY_ID_KEY = "GATEWAYID";
private static final Key GATEWAY_ID_KEY = RailoUtils.createKey("gatewayId");

private static final String GATEWAY_TYPE_KEY = "GATEWAYTYPE";
private static final Key GATEWAY_TYPE_KEY = RailoUtils.createKey("gatewayType");

private static final String DATA_KEY = "DATA";
private static final Key DATA_KEY = RailoUtils.createKey("data");

private static final Key EVENT_KEY = RailoUtils.createKey("event");

private GatewayEngine engine;

Expand Down Expand Up @@ -60,15 +62,14 @@ public String sendMessage(final Map data) throws IOException
}

@Override
@SuppressWarnings("unchecked")
public void handleMessage(final Map<String, Object> data) throws IOException
{
final Struct event = RailoUtils.createStruct();
event.put(GATEWAY_ID_KEY, this.getId());
event.put(GATEWAY_TYPE_KEY, GATEWAY_TYPE);
event.put(DATA_KEY, data);
final Map<String, Struct> arguments = new HashMap<String, Struct>(1);
arguments.put("event", event);
event.setEL(GATEWAY_ID_KEY, this.getId());
event.setEL(GATEWAY_TYPE_KEY, GATEWAY_TYPE);
event.setEL(DATA_KEY, data);
final Struct arguments = RailoUtils.createStruct();
arguments.setEL(EVENT_KEY, event);
final boolean success = this.engine.invokeListener(this, LISTENER_INVOKE_METHOD, arguments);
if (!success) throw new IOException("Error while invoke listener cfc");
}
Expand Down
43 changes: 41 additions & 2 deletions src/org/primeoservices/cfgateway/jms/railo/RailoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;

import railo.loader.engine.CFMLEngineFactory;
import railo.runtime.type.Collection.Key;
import railo.runtime.type.Struct;

/**
Expand All @@ -34,9 +36,46 @@ private RailoUtils()
}

/**
* Creates a new structure
* Converts the specified data in a <code>Map</code> to a structure object
*
* @return the structure just created
* @param data the data to be converted
*
* @return the structure object containing the passed data
*/
@SuppressWarnings("unchecked")
public static Struct toStruct(Map<String, Object> data)
{
final Struct result = createStruct();
for (Map.Entry<String, Object> entry : data.entrySet())
{
if (Map.class.isAssignableFrom(entry.getValue().getClass()))
{
result.setEL(createKey(entry.getKey()), toStruct((Map<String, Object>) entry.getValue()));
}
else
{
result.setEL(createKey(entry.getKey()), entry.getValue());
}
}
return result;
}

/**
* Creates a new key object from the specified string
*
* @param key the string for which the key object is to be created
*
* @return a key object just created for the given string
*/
public static Key createKey(final String key)
{
return CFMLEngineFactory.getInstance().getCreationUtil().createKey(key);
}

/**
* Creates a new structure object
*
* @return the structure object just created
*/
public static Struct createStruct()
{
Expand Down

0 comments on commit 02a34a1

Please sign in to comment.