Package org.xmpp.component
Class AbstractComponent
java.lang.Object
org.xmpp.component.AbstractComponent
- All Implemented Interfaces:
Component
This class provides a default
Component implementation. Most of the
default functionality can be overridden by overriding specific methods.
These XMPP features are implemented in the abstract component:
- Service Discovery (XEP-0030)
- XMPP Ping (XEP-0199)
- Last Activity (XEP-0012)
- Entity Time(XEP-0202)
get or set type, as required by
the XMPP specification. If the abstract component cannot formulate a valid
response and the extending implementation does not provide a response either
(by returning null on invocations of handleIQGet(IQ) and
handleIQSet(IQ)) an IQ error response is returned.
The behavior described above can be disabled by setting a corresponding flag
in one of the constructors. If an instance is configured in such a way,
null responses provided by the extending implementation are not
translated in an IQ error. This allows the extending implementation to
respond to IQ requests in an asynchrous manner. It will be up to the
extending implementation to ensure that every IQ request is responded to.
Note that instances of this class can be used to implement internal (e.g.
Openfire plugins) as well as external components.- Author:
- Guus der Kinderen, guus.der.kinderen@gmail.com
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ComponentManagerThe component manager to which this Component has been registered.protected JIDThe JID of the component, set after registration with a Component manager.protected final org.slf4j.LoggerThe object that's responsible for logging.static final StringThe XMPP 'service discovery info' namespace.static final StringThe XMPP 'service discovery items' namespace.static final StringThe 'Entity Time' namespacestatic final StringThe 'Last Activity' namespacestatic final StringThe 'XMPP Ping' namespace -
Constructor Summary
ConstructorsConstructorDescriptionInstantiates a new AbstractComponent with a maximum thread pool size of 17 and a maximum queue size of 1000.AbstractComponent(int maxThreadpoolSize, int maxQueueSize, boolean enforceIQResult) Instantiates a new AbstractComponent. -
Method Summary
Modifier and TypeMethodDescriptionprotected String[]This method returns a String array that should contain all namespaces of features that this component offers.protected StringReturns the category of the Service Discovery Identity of this component (this implementation will only hold exactly one Identity for the component).protected StringReturns the type of the Service Discovery Identity of this component (this implementation will only hold exactly one Identity for the component).protected booleandropStanza(IQ iq) abstract StringReturns the description of this component.Returns the XMPP domain to which this component is registered to.getJID()Returns the XMPP address / Jabber ID (JID) of this component.abstract StringgetName()Returns the name of this component.protected IQhandleDiscoInfo(IQ iq) Default handler of Service Discovery Info requests.protected IQhandleDiscoItems(IQ iq) Default handler of Service Discovery Info requests.protected IQhandleEntityTime(IQ iq) Default handler of Entity Time requests (XEP-0202).protected voidhandleIQError(IQ iq) Override this method to handle the IQ stanzas of typeerrorthat are received by the component.protected IQhandleIQGet(IQ iq) Override this method to handle the IQ stanzas of typegetthat could not be processed by theAbstractComponentimplementation.protected voidhandleIQResult(IQ iq) Override this method to handle the IQ stanzas of typeresultthat are received by the component.protected IQhandleIQSet(IQ iq) Override this method to handle the IQ stanzas of typesetthat could not be processed by theAbstractComponentimplementation.protected IQhandleLastActivity(IQ iq) Default handler of Last Activity requests (XEP-0012).protected voidhandleMessage(Message message) Override this method to handle the Message stanzas that are received by the component.protected IQhandlePing(IQ iq) Default handler of Ping requests (XEP-0199).protected voidhandlePresence(Presence presence) Override this method to handle the Presence stanzas that are received by the component.final voidinitialize(JID jid, ComponentManager componentManager) Initialize the abstract component.voidThis method gets called as part of the Component shutdown routine.voidThis method gets called as part of the Component 'start' routine.voidThis method gets called as part of the Component shutdown routine.voidThis method gets called as part of the Component 'start' routine.final voidprocessPacket(Packet packet) Processes a packet sent to this Component.protected voidHelper method to send packets.booleanChecks if the component can only be used by users of the XMPP domain that the component has registered to.final voidshutdown()Default implementation of the shutdown() method of theComponentinterface.voidstart()Default implementation of the start() method of theComponentinterface.
-
Field Details
-
log
protected final org.slf4j.Logger logThe object that's responsible for logging. -
NAMESPACE_DISCO_ITEMS
The XMPP 'service discovery items' namespace.- See Also:
-
NAMESPACE_DISCO_INFO
The XMPP 'service discovery info' namespace.- See Also:
-
NAMESPACE_XMPP_PING
The 'XMPP Ping' namespace- See Also:
-
NAMESPACE_LAST_ACTIVITY
The 'Last Activity' namespace- See Also:
-
NAMESPACE_ENTITY_TIME
The 'Entity Time' namespace- See Also:
-
compMan
The component manager to which this Component has been registered. -
jid
The JID of the component, set after registration with a Component manager.
-
-
Constructor Details
-
AbstractComponent
public AbstractComponent()Instantiates a new AbstractComponent with a maximum thread pool size of 17 and a maximum queue size of 1000. -
AbstractComponent
public AbstractComponent(int maxThreadpoolSize, int maxQueueSize, boolean enforceIQResult) Instantiates a new AbstractComponent.- Parameters:
maxThreadpoolSize- the maximum number of threads that will process work for this component.maxQueueSize- capacity of the queue that holds tasks that are to be executed by the thread pool.enforceIQResult- iftrue, the component will make sure that every request that is received is answered, as specified by the XMPP specification.
-
-
Method Details
-
initialize
Initialize the abstract component.- Specified by:
initializein interfaceComponent- Parameters:
jid- the XMPP address that this component is available at.componentManager- the component manager.- Throws:
ComponentException- if an error occured while initializing the component.- See Also:
-
processPacket
Description copied from interface:ComponentProcesses a packet sent to this Component.- Specified by:
processPacketin interfaceComponent- Parameters:
packet- the packet.- See Also:
-
dropStanza
-
handleIQResult
Override this method to handle the IQ stanzas of typeresultthat are received by the component. If you do not override this method, the stanzas are ignored.- Parameters:
iq- The IQ stanza of typeresultthat was received by this component.
-
handleIQError
Override this method to handle the IQ stanzas of typeerrorthat are received by the component. If you do not override this method, the stanzas are ignored.- Parameters:
iq- The IQ stanza of typeerrorthat was received by this component.
-
handleIQGet
Override this method to handle the IQ stanzas of typegetthat could not be processed by theAbstractComponentimplementation.
Note that, as any IQ stanza of typegetmust be replied to, returningnullfrom this method equals returning an IQ error stanza of type 'feature-not-implemented' (this behavior can be disabled by setting theenforceIQResultargument in the constructor tofalse).
Note that if this method throws an Exception, an IQ stanza of typeerror, condition 'internal-server-error' will be returned to the sender of the original request.
The default implementation of this method returnsnull. It is expected that most child classes will override this method.- Parameters:
iq- The IQ request stanza of typegetthat was received by this component.- Returns:
- the response the to request stanza, or
nullto indicate 'feature-not-available'. - Throws:
Exception- if internal error occurs.
-
handleIQSet
Override this method to handle the IQ stanzas of typesetthat could not be processed by theAbstractComponentimplementation.
Note that, as any IQ stanza of typesetmust be replied to, returningnullfrom this method equals returning an IQ error stanza of type 'feature-not-implemented' {this behavior can be disabled by setting theenforceIQResultargument in the constructor tofalse).
Note that if this method throws an Exception, an IQ stanza of typeerror, condition 'internal-server-error' will be returned to the sender of the original request.
The default implementation of this method returnsnull. It is expected that most child classes will override this method.- Parameters:
iq- The IQ request stanza of typesetthat was received by this component.- Returns:
- the response the to request stanza, or
nullto indicate 'feature-not-available'. - Throws:
Exception- if internal error occurs.
-
handleDiscoItems
Default handler of Service Discovery Info requests. Unless overridden, this method returnsnull, which will result into a 'service-unavailable' response to be returned as a response to the original request.- Parameters:
iq- The Service Discovery Items- Returns:
- Service Discovery Items response.
-
handleDiscoInfo
Default handler of Service Discovery Info requests. Unless overridden, this method returns an IQresultpacket that includes:
- One Service Discovery 'Identity'. The attributes of the identity are
determined in this way:
- attribute 'category' : the return value of
discoInfoIdentityCategory(); - attribute 'type' : the return value of
discoInfoIdentityCategoryType(); -
attribute 'name' : the name of the entity, as returned by getName()
method of the
Componentinterface (which this class implements).
- attribute 'category' : the return value of
- A list of Service Discovery 'Features', which consist of the 'Service
Discovery Info' feature, and the results of
discoInfoFeatureNamespaces().
Note that you should include the 'Service Discovery Items' feature ifhandleDiscoItems(IQ)returns a non-null value.- Parameters:
iq- The Service Discovery 'info' request stanza.- Returns:
- A response to the received Service Discovery 'info' request.
- One Service Discovery 'Identity'. The attributes of the identity are
determined in this way:
-
handlePing
Default handler of Ping requests (XEP-0199). Unless overridden, this method returns an empty result stanza, which is the expected response to a Ping.- Parameters:
iq- The Ping request stanza.- Returns:
- The XMPP way of saying 'pong'.
-
handleLastActivity
Default handler of Last Activity requests (XEP-0012). Unless overridden, this method returns a result stanza that specifies how long this component has been running since it was last (re)started.- Parameters:
iq- The Last Activity request stanza.- Returns:
- Last Activity response that reports back the uptime of this component.
-
handleEntityTime
Default handler of Entity Time requests (XEP-0202). Unless overridden, this method returns the current local time as specified by the XEP.- Parameters:
iq- Entity Time request stanza.- Returns:
- Result stanza including the local current time.
-
getDescription
Description copied from interface:ComponentReturns the description of this component.- Specified by:
getDescriptionin interfaceComponent- Returns:
- the description of this component.
-
getName
Description copied from interface:ComponentReturns the name of this component. -
getDomain
Returns the XMPP domain to which this component is registered to. To full address of this component should be a subdomain of the domain returned by this method.- Returns:
- The XMPP domain name, or
nullif this component has not been initialized yet.
-
getJID
Returns the XMPP address / Jabber ID (JID) of this component.- Returns:
- The JID of this component, or
nullif this component has not been initialized yet.
-
discoInfoIdentityCategory
Returns the category of the Service Discovery Identity of this component (this implementation will only hold exactly one Identity for the component).
The default Category of a component, which is used if this method is not overridden, is 'component'.- Returns:
- the category of the Service Discovery Identity of this component.
- See Also:
-
discoInfoIdentityCategoryType
Returns the type of the Service Discovery Identity of this component (this implementation will only hold exactly one Identity for the component).
The default Category type of a component, which is used if this method is not overridden, is 'generic'.- Returns:
- the type of the Service Discovery Identity of this component.
- See Also:
-
discoInfoFeatureNamespaces
This method returns a String array that should contain all namespaces of features that this component offers. The result of this method will be included in Service Discovery responses.
Note that the features that are returned by this method should also be implemented in eitherhandleIQGet(IQ)and/orhandleIQSet(IQ)methods. Also note that the default namespaces for service discovery should not be returned by this method.
The default implementation of this method returns an empty array. Override this method to include new namespaces.- Returns:
- Namespaces of all features provided by this Component
-
handleMessage
Override this method to handle the Message stanzas that are received by the component. If you do not override this method, the stanzas are ignored.- Parameters:
message- The Message stanza that was received by this component.
-
handlePresence
Override this method to handle the Presence stanzas that are received by the component. If you do not override this method, the stanzas are ignored.- Parameters:
presence- The Presence stanza that was received by this component.
-
servesLocalUsersOnly
public boolean servesLocalUsersOnly()Checks if the component can only be used by users of the XMPP domain that the component has registered to. If this method returnstrue, this will happen:- Messages and Presence stanzas are silently ignored.
- IQ stanza's of type
resultanderrorare silently ignored. - IQ stanza's of type
getorsetare responded to with a IQerrorresponse, type 'cancel', condition 'not-allowed'.
false. You can override this method to change the behavior.- Returns:
trueif this component serves local users only,falseotherwise.
-
shutdown
public final void shutdown()Default implementation of the shutdown() method of theComponentinterface. -
send
Helper method to send packets.- Parameters:
packet- The packet to send.
-
preComponentShutdown
public void preComponentShutdown()This method gets called as part of the Component shutdown routine. This method gets called before the other shutdown methods get executed. This enables extending classes to initiate a cleanup before the component gets completely shut down. -
postComponentShutdown
public void postComponentShutdown()This method gets called as part of the Component shutdown routine. This method gets called after the other shutdown methods got executed. This enables extending classes to finish cleaning up after all other cleanup has been performed. -
start
public void start()Default implementation of the start() method of theComponentinterface. Unless overridden, this method doesn't do anything. We get called once for each host that we connect to, so we have to take care to avoid double initialization. -
preComponentStart
public void preComponentStart()This method gets called as part of the Component 'start' routine. This method gets called before the other 'start' methods get executed. This enables extending classes to initialize resources before the component gets completely started. This method is called once for each host that we connect to, so we have to take care to avoid double initialization. -
postComponentStart
public void postComponentStart()This method gets called as part of the Component 'start' routine. This method gets called after the other 'start' methods got executed. This enables extending classes to finish initializing resources after all other resources has been initialized. This method is called once for each host that we connect to, so we have to take care to avoid double initialization.
-