User: norbert
Date: 00/05/17 16:43:12
Added: src/java/org/spyderMQ/selectors Identifier.java
Operator.java Selector.java
Log:
Very basic selector system ( there's no parser yet )
Revision Changes Path
1.1 spyderMQ/src/java/org/spyderMQ/selectors/Identifier.java
Index: Identifier.java
===================================================================
/*
* spyderMQ, the OpenSource JMS implementation
*
* Distributable under GPL license.
* See terms of license at gnu.org.
*/
package org.spydermq.selectors;
/**
* This is a JMS identifier
*
* @author Norbert Lataille ([EMAIL PROTECTED])
*
* @version $Revision: 1.1 $
*/
public class Identifier
{
String name;
Object value;
public Identifier(String name)
{
this.name=name;
value=null;
}
}
1.1 spyderMQ/src/java/org/spyderMQ/selectors/Operator.java
Index: Operator.java
===================================================================
/*
* spyderMQ, the OpenSource JMS implementation
*
* Distributable under GPL license.
* See terms of license at gnu.org.
*/
package org.spydermq.selectors;
/**
* An operator implementation for the selector system
*
* @author Norbert Lataille ([EMAIL PROTECTED])
*
* @version $Revision: 1.1 $
*/
public class Operator
{
int operation;
Object oper1;
Object oper2;
public final static int EQUAL = 0;
public final static int NOT = 1;
public final static int AND = 2;
public final static int OR = 3;
public Operator(int operation, Object oper1, Object oper2)
{
this.operation=operation;
this.oper1=oper1;
this.oper2=oper2;
}
Object not() throws Exception
{
Object arg1=computeArgument(oper1);
if (arg1 instanceof Boolean) {
return new Boolean(!((Boolean)arg1).booleanValue());
}
throw new Exception("Bad object type");
}
Object and() throws Exception
{
Object arg1=computeArgument(oper1);
if (arg1 instanceof Boolean) {
if (!(((Boolean)arg1).booleanValue())) return Boolean.FALSE;
Object arg2=computeArgument(oper2);
if (!(arg2 instanceof Boolean)) throw new Exception("Bad
object type");
return arg2;
}
throw new Exception("Bad object type");
}
Object or() throws Exception
{
Object arg1=computeArgument(oper1);
if (arg1 instanceof Boolean) {
if (((Boolean)arg1).booleanValue()) return Boolean.TRUE;
Object arg2=computeArgument(oper2);
if (!(arg2 instanceof Boolean)) throw new Exception("Bad
object type");
return arg2;
}
throw new Exception("Bad object type");
}
Object equal() throws Exception
{
Object arg1=computeArgument(oper1);
if (arg1 instanceof String) {
Object arg2=computeArgument(oper2);
if (!(arg2 instanceof String)) throw new Exception("Bad object
type");
return new Boolean(arg1.equals(arg2));
}
throw new Exception("Bad object type");
}
Object computeArgument(Object arg) throws Exception
{
if (arg instanceof Identifier) {
return ((Identifier)arg).value;
} else if (oper1 instanceof Operator) {
return ((Operator)arg).apply();
} else return arg;
}
Object apply() throws Exception
{
switch (operation) {
case EQUAL: return equal();
case AND: return and();
case OR: return or();
case NOT: return not();
}
throw new Exception("Unknown operation");
}
}
1.1 spyderMQ/src/java/org/spyderMQ/selectors/Selector.java
Index: Selector.java
===================================================================
/*
* spyderMQ, the OpenSource JMS implementation
*
* Distributable under GPL license.
* See terms of license at gnu.org.
*/
package org.spydermq.selectors;
import org.spydermq.Log;
import org.spydermq.SpyMessage;
import javax.jms.JMSException;
import java.util.HashSet;
import java.util.Iterator;
/**
* This class implements a Message Selector.
*
* @author Norbert Lataille ([EMAIL PROTECTED])
*
* @version $Revision: 1.1 $
*/
public class Selector
{
public HashSet identifiers; // HashSet of Identifiers
public Object result;
public Selector(HashSet identifiers, Object result)
{
this.identifiers=identifiers;
this.result=result;
}
public boolean test(SpyMessage mes)
{
try {
//Set the identifiers values
Iterator i=identifiers.iterator();
while (i.hasNext())
{
Identifier id=(Identifier)i.next();
Object find=mes.getObjectProperty(id.name);
if (find==null) {
Log.log("Warning : missing property "+id.name);
id.value=null;
} else {
if ((find instanceof Boolean)||(find
instanceof String)||(find instanceof Double)||(find instanceof Integer)) id.value=find;
else throw new Exception("Bad property type
!");
}
}
//Compute the result of this operator
Object res;
if (result instanceof Identifier) {
res=((Identifier)result).value;
} else if (result instanceof Operator) {
res=((Operator)result).apply();
} else res=result;
if (res==null) {
Log.log("res==null");
return false;
}
if (!(res instanceof Boolean)) throw new Exception("Bad object
type");
return ((Boolean)res).booleanValue();
} catch (Exception e) {
Log.log("SELECTOR: Exception :"+e.getMessage());
return false;
}
}
}