jspocp 2007-01-24

jspocp is a java-based SPOCP client.
jspocp is a java-based SPOCP client.

This project (jspocp) is a client for SPOCP using java. It is available using maven2 from the maven2 repository. The generated documentation contains javadoc and usage examples.

jSPOCP Usage Example

The following source code example is preliminary and subject to testing.

package org.spocp.client;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
*
* @author alund
*/
public class Example
{

private static final Log log = LogFactory.getLog(SPOCPClient.class);

/**
*
* @return an instance of a connection factory used to create connections to a spocp server
*/
public static SPOCPConnectionFactory connectionFactory(String host, int port)
{
SPOCPConnectionFactoryImpl cf = new SPOCPConnectionFactoryImpl();
cf.setServer(host);
cf.setPort(port);

return cf;
}

/**
* @param args the command line arguments
*/
public static void main(String args[])
throws Exception
{
SPOCPResult result;
String host = args[0];
int port = Integer.parseInt(args[1]);
SPOCPConnectionFactory cf = SPOCPClient.connectionFactory(host,port);
SPOCPConnection c = cf.getConnection();
log.debug(c);

// ---- Add some rules ----
result = c.issueCommand(new AddCommand(null,"(a (b c))"));
if(result.getResultCode() != SPOCPResult.SPOCP_SUCCESS)
System.err.println(result.getMessage());

AddCommand addCmd = new AddCommand();
addCmd.setPath("/example");
addCmd.setSExpr("(abc def)");
result = c.issueCommand(addCmd);
if(result.getResultCode() != SPOCPResult.SPOCP_SUCCESS)
System.err.println(result.getMessage());

addCmd.setPath("/anlu0008");
addCmd.setSExpr("(UB book (sab P u))");
addCmd.setBoundaryCondition("NULL");
addCmd.setReturnInfo("anlu0008@somedomain.se".getBytes());
result = c.issueCommand(addCmd);
if(result.getResultCode() != SPOCPResult.SPOCP_SUCCESS)
System.err.println(result.getMessage());

addCmd.setPath("/jolo0001");
addCmd.setSExpr("(UB book (sab P u))");
addCmd.setBoundaryCondition("NULL");
addCmd.setReturnInfo("johanna.lofstrom@somedomain.se".getBytes());
result = c.issueCommand(addCmd);
if(result.getResultCode() != SPOCPResult.SPOCP_SUCCESS)
System.err.println(result.getMessage());


// Make some queries
result = c.issueCommand(new QueryCommand(null, "(abc def ghi)"));
if(result.getResultCode() == SPOCPResult.SPOCP_DENIED)
System.err.println("The query did not match any rules");

// A query that gives rise to a multi-line respons
// because of a return information blob associated with
// the matching rule
result = c.issueCommand(new QueryCommand(null, "(UB book (sab P u))"));
if(result.getResultCode() == SPOCPResult.SPOCP_SUCCESS)
{
List resultList = result.getResultInfoList();
if(resultList.size() == 1)
{
SPOCPResultInfo ri = (SPOCPResultInfo)resultList.get(0);
System.out.println("Return information for matching rule: " +
new String(ri.getBlob()));
}


}

// List the rules, show them and delete them
result = c.issueCommand(new ListCommand());
List resultList = result.getResultInfoList();
if(resultList != null)
{
for(int i=0;i < resultList.size(); i++)
{
SPOCPResultInfo ri = (SPOCPResultInfo)resultList.get(i);
String path = ri.getPath();
String ruleId = ri.getRuleId();

// In this context, we have access to everything that there
// is to know about the listed rules through the accessor methods
// of SPOCPResultInfo. However, for the sake of demonstration
// we will use the show command to get information about what
// s-expression a partiuclar rule identity is asscociated with

ShowCommand showCmd = new ShowCommand(path, ruleId);
SPOCPResult r = c.issueCommand(showCmd);
SPOCPResultInfo showInfo = (SPOCPResultInfo)r.getResultInfoList().get(0);
System.out.println("Rule id: " + ruleId + ", s-expression: " +
showInfo.getSExpr().toString());
c.issueCommand(new DeleteCommand(path, ruleId));
}
}

c.close();
}

}

last updated on:
April 5th, 2007, 14:00 GMT
price:
FREE!
developed by:
Leif Johansson
license type:
GPL (GNU General Public License) 
category:
ROOT \ Utilities

FREE!

In a hurry? Add it to your Download Basket!

user rating

UNRATED
0.0/5
 

0/5

Add your review!

SUBMIT