001    package org.hackystat.tickertape.notifier.twitter;
002    
003    import java.util.logging.Logger;
004    import org.hackystat.utilities.stacktrace.StackTrace;
005    import twitter4j.TwitterException;
006    
007    /**
008     * Creates a Twitter notifier. 
009     * @author Philip Johnson
010     */
011    public class TwitterNotifier {
012    
013      /** The twitter user. */
014      private String user;
015      /** The logger for this notifier. */
016      private Logger logger;
017      /** The underlying twitter API. */
018      private twitter4j.Twitter twitter4j;
019    
020      /**
021       * Creates a Twitter notifier. 
022       * @param user The user. 
023       * @param password The password.
024       * @param logger The logger. 
025       */
026      public TwitterNotifier(String user, String password, Logger logger) {
027        this.user = user;
028        this.logger = logger;
029        this.twitter4j = new twitter4j.Twitter(user, password);
030        this.twitter4j.setSource("Tickertape");
031    
032      }
033     
034      /**
035       * Notify Twitter with the passed message.
036       * @param message The message. 
037       */
038      public void notify(String message) {
039        this.logger.info(String.format("Notifying Twitter user %s: %s", this.user, message));
040        try {
041          twitter4j.updateStatus(message);
042        }
043        catch (TwitterException e) {
044          this.logger.warning("Tweet failed: " + StackTrace.toString(e));
045        }    
046      }
047    }