PEPManager.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.pep;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.jivesoftware.smack.StanzaListener;
  21. import org.jivesoftware.smack.SmackException.NotConnectedException;
  22. import org.jivesoftware.smack.XMPPConnection;
  23. import org.jivesoftware.smack.filter.StanzaExtensionFilter;
  24. import org.jivesoftware.smack.filter.StanzaFilter;
  25. import org.jivesoftware.smack.packet.Message;
  26. import org.jivesoftware.smack.packet.Stanza;
  27. import org.jivesoftware.smack.packet.IQ.Type;
  28. import org.jivesoftware.smackx.pep.packet.PEPEvent;
  29. import org.jivesoftware.smackx.pep.packet.PEPItem;
  30. import org.jivesoftware.smackx.pep.packet.PEPPubSub;
  31. import org.jxmpp.jid.Jid;

  32. /**
  33.  *
  34.  * Manages Personal Event Publishing (XEP-163). A PEPManager provides a high level access to
  35.  * pubsub personal events. It also provides an easy way
  36.  * to hook up custom logic when events are received from another XMPP client through PEPListeners.
  37.  *
  38.  * Use example:
  39.  *
  40.  * <pre>
  41.  *   PEPManager pepManager = new PEPManager(smackConnection);
  42.  *   pepManager.addPEPListener(new PEPListener() {
  43.  *       public void eventReceived(String inFrom, PEPEvent inEvent) {
  44.  *           LOGGER.debug("Event received: " + inEvent);
  45.  *       }
  46.  *   });
  47.  *
  48.  *   PEPProvider pepProvider = new PEPProvider();
  49.  *   pepProvider.registerPEPParserExtension("http://jabber.org/protocol/tune", new TuneProvider());
  50.  *   ProviderManager.getInstance().addExtensionProvider("event", "http://jabber.org/protocol/pubsub#event", pepProvider);
  51.  *  
  52.  *   Tune tune = new Tune("jeff", "1", "CD", "My Title", "My Track");
  53.  *   pepManager.publish(tune);
  54.  * </pre>
  55.  *
  56.  * @author Jeff Williams
  57.  */
  58. public class PEPManager {

  59.     private List<PEPListener> pepListeners = new ArrayList<PEPListener>();

  60.     private XMPPConnection connection;

  61.     private StanzaFilter packetFilter = new StanzaExtensionFilter("event", "http://jabber.org/protocol/pubsub#event");
  62.     private StanzaListener packetListener;

  63.     /**
  64.      * Creates a new PEP exchange manager.
  65.      *
  66.      * @param connection an XMPPConnection which is used to send and receive messages.
  67.      */
  68.     public PEPManager(XMPPConnection connection) {
  69.         this.connection = connection;
  70.         init();
  71.     }

  72.     /**
  73.      * Adds a listener to PEPs. The listener will be fired anytime PEP events
  74.      * are received from remote XMPP clients.
  75.      *
  76.      * @param pepListener a roster exchange listener.
  77.      */
  78.     public void addPEPListener(PEPListener pepListener) {
  79.         synchronized (pepListeners) {
  80.             if (!pepListeners.contains(pepListener)) {
  81.                 pepListeners.add(pepListener);
  82.             }
  83.         }
  84.     }

  85.     /**
  86.      * Removes a listener from PEP events.
  87.      *
  88.      * @param pepListener a roster exchange listener.
  89.      */
  90.     public void removePEPListener(PEPListener pepListener) {
  91.         synchronized (pepListeners) {
  92.             pepListeners.remove(pepListener);
  93.         }
  94.     }

  95.     /**
  96.      * Publish an event.
  97.      *
  98.      * @param item the item to publish.
  99.      * @throws NotConnectedException
  100.      * @throws InterruptedException
  101.      */
  102.     public void publish(PEPItem item) throws NotConnectedException, InterruptedException {
  103.         // Create a new message to publish the event.
  104.         PEPPubSub pubSub = new PEPPubSub(item);
  105.         pubSub.setType(Type.set);
  106.         //pubSub.setFrom(connection.getUser());
  107.  
  108.         // Send the message that contains the roster
  109.         connection.sendStanza(pubSub);
  110.     }

  111.     /**
  112.      * Fires roster exchange listeners.
  113.      */
  114.     private void firePEPListeners(Jid from, PEPEvent event) {
  115.         PEPListener[] listeners = null;
  116.         synchronized (pepListeners) {
  117.             listeners = new PEPListener[pepListeners.size()];
  118.             pepListeners.toArray(listeners);
  119.         }
  120.         for (int i = 0; i < listeners.length; i++) {
  121.             listeners[i].eventReceived(from, event);
  122.         }
  123.     }

  124.     private void init() {
  125.         // Listens for all roster exchange packets and fire the roster exchange listeners.
  126.         packetListener = new StanzaListener() {
  127.             public void processPacket(Stanza packet) {
  128.                 Message message = (Message) packet;
  129.                 PEPEvent event = (PEPEvent) message.getExtension("event", "http://jabber.org/protocol/pubsub#event");
  130.                 // Fire event for roster exchange listeners
  131.                 firePEPListeners(message.getFrom(), event);
  132.             }
  133.         };
  134.         connection.addSyncStanzaListener(packetListener, packetFilter);
  135.     }

  136.     public void destroy() {
  137.         if (connection != null)
  138.             connection.removeSyncStanzaListener(packetListener);
  139.     }

  140.     protected void finalize() throws Throwable {
  141.         destroy();
  142.         super.finalize();
  143.     }
  144. }