JBoss.orgCommunity Documentation
The example application is defined by a service descriptor, which refers the Root SBB. The Root SBB does not defines child relations, which means the application is a single SBB.
To obtain the example's complete source code please refer to Section 2.2, “Mobicents JAIN SLEE Google Talk Bot Example Source Code”.
The service descriptor is plain simple, it just defines the service ID, the ID of the root SBB and its default priority. The complete XML is:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE service-xml PUBLIC
"-//Sun Microsystems, Inc.//DTD JAIN SLEE Service 1.1//EN"
"http://java.sun.com/dtd/slee-service-xml_1_1.dtd">
<service-xml>
<service>
<description />
<service-name>GoogleTalkBotService</service-name>
<service-vendor>mobicents</service-vendor>
<service-version>0.1</service-version>
<root-sbb>
<description />
<sbb-name>GoogleTalkBotSbb</sbb-name>
<sbb-vendor>mobicents</sbb-vendor>
<sbb-version>0.1</sbb-version>
</root-sbb>
<default-priority>0</default-priority>
</service>
</service-xml>
The Google Talk Bot Example's Root SBB is composed by the abstract class and the XML descriptor.
The class org.mobicents.examples.googletalk.GoogleTalkBotSbb
includes all the service logic for the example.
The javax.slee.SbbObject
's setSbbContext(SbbContext)
is used by SBBs to store the SBB's context into a class field. The SBB should take the opportunity to also store objects, such as SLEE facilities, or env entries property values, which are common for all service logic entities, a.k.a. SbbEntities, and thus may be stored in the SbbObject instance.
The class fields and setSbbContext(SbbContext)
method's and related code:
/*
* (non-Javadoc)
*
* @see javax.slee.Sbb#setSbbContext(javax.slee.SbbContext)
*/
public void setSbbContext(SbbContext context) {
this.sbbContext = context;
this.tracer = sbbContext.getTracer(getClass().getSimpleName());
try {
Context myEnv = (Context) new InitialContext()
.lookup("java:comp/env");
xmppSbbInterface = (XmppResourceAdaptorSbbInterface) myEnv
.lookup("slee/resources/xmpp/2.0/xmppinterface");
xmppActivityContextInterfaceFactory = (XmppActivityContextInterfaceFactory) myEnv
.lookup("slee/resources/xmpp/2.0/factoryprovider");
// env-entries
username = (String) myEnv.lookup("username");
password = (String) myEnv.lookup("password");
tracer.info("setSbbContext() Retrieved uid[" + username + "],"
+ " passwd[" + password + "]");
} catch (NamingException ne) {
tracer.severe("Could not set SBB context:" + ne.getMessage(), ne);
}
}
The ServiceStartedEvent is fired by the SLEE Container when the service activation is complete, in the example that is the time to connect to Google Talk Server.
The event handler method's code:
/**
* Init the xmpp connection to GOOGLE TALK when the service is activated by
* SLEE
*
* @param event
* @param aci
*/
public void onStartServiceEvent(
javax.slee.serviceactivity.ServiceStartedEvent event,
ActivityContextInterface aci) {
try {
// connect to google talk xmpp server
XmppConnection connection = xmppSbbInterface.connectClient(
connectionID, serviceHost, servicePort, serviceName,
username, password, resource, Arrays
.asList(packetsToListen));
xmppActivityContextInterfaceFactory.getActivityContextInterface(
connection).attach(sbbContext.getSbbLocalObject());
} catch (XMPPException e) {
tracer.severe("Connection to server failed!",e);
}
}
The ActivityEndEvent is fired by the SLEE Container whenever an activity ends. Regarding the example application, it needs to close the XMPP connection when the service is deactivated, and for an SBB the only way to known this happen is by checking for ActivityEndEvents on the ServiceActivity.
The event handler method's code:
/**
* Handler to disconnect from Google when the service is being deactivated.
*
* @param event
* @param aci
*/
public void onActivityEndEvent(ActivityEndEvent event,
ActivityContextInterface aci) {
if (aci.getActivity() instanceof ServiceActivity) {
try {
xmppSbbInterface.disconnect(connectionID);
} catch (Exception e) {
tracer.severe(e.getMessage(), e);
}
}
}
The Presence event indicates that an XMPP PRESENCE
stanza was received in the managed XMPP connection. The application simply checks if its TYPE
is available
, and if that is the case the Hi, I'm online too!
message is sent back to the sender.
The event handler method's code:
/**
* Here we handle the Presence messages.
*
* @param packet
* @param aci
*/
public void onPresence(org.jivesoftware.smack.packet.Presence packet,
ActivityContextInterface aci) {
tracer.info("XMPP Presence event type! Status: '" + packet.getType()
+ "'. " + "Sent by '" + packet.getFrom() + "'.");
// reply hello msg if receives notification of available presence state
if (packet.getType() == Presence.Type.AVAILABLE) {
Message msg = new Message(packet.getFrom(), Message.Type.CHAT);
msg.setBody("Hi. I'am online too.");
xmppSbbInterface.sendPacket(connectionID, msg);
}
}
The Message event indicates that an XMPP MESSAGE
stanza was received in the managed XMPP connection. The application simply checks if its body
is time
, and if that is the case sends back the system time, otherwise sends the count of chars received..
The event handler method's code:
/**
* This is the point where we already have a chat session with the user, so,
* when they send us messages, we count the chars and reply or tell time :)
*
* @param message
* @param aci
*/
public void onMessage(org.jivesoftware.smack.packet.Message message,
ActivityContextInterface aci) {
// only process messages which are not an error and not sent by another
// bot instance
if (!message.getType().equals(Message.Type.ERROR)
&& !StringUtils.parseBareAddress(message.getFrom()).equals(
username + "@" + serviceName)) {
tracer.info("XMPP Message event type! Message Body: '"
+ message.getBody() + "'. " + "Sent by '"
+ message.getFrom() + "'.");
String body = null;
if (message.getBody() != null) {
if (message.getBody().equalsIgnoreCase("time")) {
body = "My system time is " + new Date().toString();
} else {
body = message.getBody().length() + " chars in message <"
+ message.getBody() + ">.";
}
}
Message msg = new Message(message.getFrom(), message.getType());
msg.setBody(body);
xmppSbbInterface.sendPacket(connectionID, msg);
}
}
The Root SBB XML Descriptor has to be provided and match the abstract class code.
First relevant part is the declaration of the sbb-classes
element, where the sbb class abstract name must be specified:
<sbb-classes>
<sbb-abstract-class>
<sbb-abstract-class-name>
org.mobicents.examples.googletalk.GoogleTalkBotSbb
</sbb-abstract-class-name>
</sbb-abstract-class>
</sbb-classes>
Then the events handled by the SBB must be specified too:
<event event-direction="Receive" initial-event="True">
<event-name>StartServiceEvent</event-name>
<event-type-ref>
<event-type-name>
javax.slee.serviceactivity.ServiceStartedEvent
</event-type-name>
<event-type-vendor>javax.slee</event-type-vendor>
<event-type-version>1.1</event-type-version>
</event-type-ref>
<initial-event-select variable="ActivityContext" />
</event>
<event event-direction="Receive" initial-event="False">
<event-name>ActivityEndEvent</event-name>
<event-type-ref>
<event-type-name>
javax.slee.ActivityEndEvent
</event-type-name>
<event-type-vendor>javax.slee</event-type-vendor>
<event-type-version>1.0</event-type-version>
</event-type-ref>
</event>
<event event-direction="Receive" initial-event="False">
<event-name>Message</event-name>
<event-type-ref>
<event-type-name>
org.jivesoftware.smack.packet.Message
</event-type-name>
<event-type-vendor>
org.jivesoftware.smack
</event-type-vendor>
<event-type-version>1.0</event-type-version>
</event-type-ref>
</event>
<event event-direction="Receive" initial-event="False">
<event-name>Presence</event-name>
<event-type-ref>
<event-type-name>
org.jivesoftware.smack.packet.Presence
</event-type-name>
<event-type-vendor>
org.jivesoftware.smack
</event-type-vendor>
<event-type-version>1.0</event-type-version>
</event-type-ref>
</event>
Note that there is a single event defined as initial, which triggers the xmpp connection creation, remaining events all happen in the XMPP connection activity, that the service instance is already attached.
Next are the env-entries
, which are used as configuration properties, and contains the Google Talk account credentials:
<env-entry>
<env-entry-name>username</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>mobicents.gtalk.bot</env-entry-value>
</env-entry>
<env-entry>
<env-entry-name>password</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>m0b1c3nts</env-entry-value>
</env-entry>
Finally, the XMPP Resource Adaptor must be specified also, otherwise SLEE won't put its SBB Interface in the SBB's JNDI Context:
<resource-adaptor-type-binding>
<resource-adaptor-type-ref>
<resource-adaptor-type-name>
XMPPResourceAdaptorType
</resource-adaptor-type-name>
<resource-adaptor-type-vendor>
org.mobicents
</resource-adaptor-type-vendor>
<resource-adaptor-type-version>
2.0
</resource-adaptor-type-version>
</resource-adaptor-type-ref>
<activity-context-interface-factory-name>
slee/resources/xmpp/2.0/factoryprovider
</activity-context-interface-factory-name>
<resource-adaptor-entity-binding>
<resource-adaptor-object-name>
slee/resources/xmpp/2.0/xmppinterface
</resource-adaptor-object-name>
<resource-adaptor-entity-link>
XMPPRA
</resource-adaptor-entity-link>
</resource-adaptor-entity-binding>
</resource-adaptor-type-binding>