powered by Jive Software

Setting a System property from a Spark plugin

Hi,

I am developing a Spark plugin where I need to use a natural language processing (NLP) technique, where in order for this to work first I have to set its System property application.home to the application’s installation directory. When trying the NLP technique separately it works, but when I try to set the System property pointing to the application’s installation directory within the plugin this does not work.

Anyone has any idea where such a directory can be placed within Spark’s builder since I tried several options like placing this directory within the builder’s lib directory and then pointing to the system property via a relative path but of which was not successfull.

This is vital for my plugin since its main function is based on it, thus I appreciate any help.

Thanks

Hello,

i did it using IQs (IQ Handler/IQ Request) if you find another solution you may tell me

lol

ps : you have to implement a plugin for openfire also…

Hi,

thanks for your reply! Is it possible that you explain to me how you managed to do it? since I still have not managed to do it!

Thanks and I appreciate it.

As i told you i don’t know if it’s the right way to do it or not …

i will show you a quick example for IQs … but you may try to search more about using IQs :

ok first you have to implement the IQ in spark ( let’s say IQ request a class that extends IQ )

/*

  • To change this template, choose Tools | Templates

  • and open the template in the editor.

*/

package org.jivesoftware.fastpath.iq;

//~— non-JDK imports --------------------------------------------------------

import org.jivesoftware.smack.PacketCollector;

import org.jivesoftware.smack.SmackConfiguration;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.filter.AndFilter;

import org.jivesoftware.smack.filter.PacketFilter;

import org.jivesoftware.smack.filter.PacketIDFilter;

import org.jivesoftware.smack.packet.IQ;

import org.jivesoftware.smack.provider.ProviderManager;

//~— JDK imports ------------------------------------------------------------

import java.io.IOException;

import org.jivesoftware.fastpath.provider.IQRelanceProvider;

import org.jivesoftware.spark.SparkManager;

/**

  • @author Omega

*/

public class IQRelance extends IQ {

private String chatID;

private XMPPConnection con;

private boolean isOk;

private String agentName;

public IQRelance(XMPPConnection connection, String chatid) {

con = connection;

this.chatID = chatid;

agentName = SparkManager.getUserManager().getNickname();

}

public boolean getResultOfRelance() {

return isOk;

}

public synchronized void SendQuery() throws XMPPException, IOException {

    ProviderManager.getInstance().addIQProvider("relance", "[http://jabber.org/protocol/relance](http://jabber.org/protocol/relance)",

new IQRelanceProvider());

this.setType(IQ.Type.GET);

this.setTo(con.getServiceName());

PacketFilter filter = new AndFilter(new PacketIDFilter(this.getPacketID()));

PacketCollector collector = con.createPacketCollector(filter); // add the filter for handling the response from the server

con.sendPacket(this); // send the packet to the openfire server

IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());

// Stop queuing results

collector.cancel();

if (result == null) {

throw new XMPPException(“No response from server.”);

} else if (result.getType() == IQ.Type.ERROR) {

throw new XMPPException(result.getError());

} else {

if (result instanceof IQRelanceAnswer) { // note that the IQRelanceAnswer is the IQ response from the server it’s a class implemented in the server and in spark ( a stuffed class with some properties, getters and setters

IQRelanceAnswer Resultat = (IQRelanceAnswer) result;

isOk = Resultat.isIsOk();

}

}

}

@Override

public String getChildElementXML() {

StringBuilder buf = new StringBuilder();

    buf.append("<relance xmlns=\"[http://jabber.org/protocol/relance](http://jabber.org/protocol/relance)\" agent=\""+agentName+"\" chatID=\"" + chatID + "\"  >");

buf.append("");

return buf.toString();

}

}

then in Openfire you must implement the IQ handler for this request

/*

  • To change this template, choose Tools | Templates

  • and open the template in the editor.

*/

package org.jivesoftware.fastpath.iq;

//~— non-JDK imports --------------------------------------------------------

import org.jivesoftware.smack.packet.IQ;

/**

  • @author Omega

*/

public class IQRelanceAnswer extends IQ {

private boolean IsOk;

public boolean isIsOk() {

return IsOk;

}

public void setIsOk(boolean IsOk) {

this.IsOk = IsOk;

}

public void setIsOk(String IsOk) {

this.IsOk = Boolean.getBoolean(IsOk);

}

@Override

public String getChildElementXML() {

return null;

}

}

IN OPENFIRE :

/*

  • To change this template, choose Tools | Templates

  • and open the template in the editor.

*/

package com.oceanys.smsplugin.iqhandlers;

//~— non-JDK imports --------------------------------------------------------

import com.oceanys.smsplugin.handlers.zong.ZongSMSReceptionHandler;

import org.dom4j.Element;

import org.jivesoftware.openfire.IQHandlerInfo;

import org.jivesoftware.openfire.XMPPServer;

import org.jivesoftware.openfire.auth.UnauthorizedException;

import org.jivesoftware.openfire.handler.IQHandler;

import org.xmpp.packet.IQ;

import org.xmpp.packet.PacketError.Condition;

/**

  • @author Omega

*/

public class IQRelanceHandler extends IQHandler {

private IQHandlerInfo info;

private String serverName;

public IQRelanceHandler() {

super(“relance”);

    info = new IQHandlerInfo("relance", "[http://jabber.org/protocol/relance](http://jabber.org/protocol/relance)");

}

@Override

public IQ handleIQ(IQ packet) throws UnauthorizedException {

IQ result = IQ.createResultIQ(packet);

Element iqChatId = packet.getChildElement();

iqChatId.getData();

String chatID = iqChatId.attribute(“chatID”).getValue();

String agentName = iqChatId.attribute(“agent”).getValue();

if (chatID != null) {

try {

String HistoryXml = “”;

ZongSMSReceptionHandler zongReceptionHandler = new ZongSMSReceptionHandler();

zongReceptionHandler.Relance(chatID,agentName);

            result.setChildElement("relance", "[http://jabber.org/protocol/relance](http://jabber.org/protocol/relance)").setText(HistoryXml); // sending the response in XML  ( i think you don't need to send response  in your case may be you can send a simple ACK.

} catch (Exception ex) {

result.setError(Condition.bad_request);

}

} else {

result.setError(Condition.bad_request);

}

return result;

}

public IQHandlerInfo getInfo() {

return info;

}

@Override

public void initialize(XMPPServer server) {

super.initialize(server);

serverName = server.getServerInfo().getXMPPDomain();

}

}

FINALLY YOU HAVE TO REGISTER YOUR IQ HANDLER IN YOUR SERVER : SOMEWHERE IN PLUGIN INITIALISATION :

package com.oceanys.smsplugin;

//~— non-JDK imports --------------------------------------------------------

import com.oceanys.smsplugin.iqhandlers.IQAgentHistoryHandler;

import com.oceanys.smsplugin.iqhandlers.IQChatRoomHistoryHandler;

import com.oceanys.smsplugin.iqhandlers.IQRelanceHandler;

import com.oceanys.smsplugin.managers.SendingSMSQueue;

import org.jivesoftware.admin.AuthCheckFilter;

import org.jivesoftware.openfire.IQRouter;

import org.jivesoftware.openfire.XMPPServer;

import org.jivesoftware.openfire.container.Plugin;

import org.jivesoftware.openfire.container.PluginManager;

import org.jivesoftware.smsplugin.webchat.SMSChatManager;

//~— JDK imports ------------------------------------------------------------

import java.io.File;

/**

  • A sample plugin for Openfire.

*/

public class SMSPlugin implements Plugin { // the main class of the plugin in openfire

IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();

SMSChatManager chatManager = SMSChatManager.getInstance();

IQRelanceHandler iqRelanceHandler;

public void initializePlugin(PluginManager manager, File pluginDirectory) {

System.out.println(“Starting SMS Plugin.”);

AuthCheckFilter.addExclude(“smsplugin/smstest”);

iqRelanceHandler = new IQRelanceHandler();

iqRouter.addHandler(iqRelanceHandler);

}

public void destroyPlugin() {

System.out.println(“Shutting down SMS Plugin.”);

AuthCheckFilter.removeExclude(“smsplugin/smstest”);

iqRouter.removeHandler(iqRelanceHandler);

}

}

i did it quickly hope that you will understand that

Thanks, I will look at your example. I have never used IQ’s so I will try to understand why they are used for. If I find another way I will post it but till now I still have not found anything even though I have tried several different approaches.

Regarding the system property of the library that you want to load, I cannot see where it is being set in the example.

Thanks a lot for your example.

No one has ever needed to set a system property from inside a Spark plugin, possibly? Since this is really vital for my plugin since all processes commence after this is set. Any help is much appreciated guys.

Thanks