updatenotificationpingsclient.java

### File Source: updatenotificationpingsclient.java

             /*
        P/P   *  Method: net.sourceforge.pebble.webservice.UpdateNotificationPingsClient__static_init
              *
              *  Postconditions:
              *    init'ed(log)
              */
         1  /*
         2   * Copyright (c) 2003-2006, Simon Brown
         3   * All rights reserved.
         4   *
         5   * Redistribution and use in source and binary forms, with or without
         6   * modification, are permitted provided that the following conditions are met:
         7   *
         8   *   - Redistributions of source code must retain the above copyright
         9   *     notice, this list of conditions and the following disclaimer.
        10   *
        11   *   - Redistributions in binary form must reproduce the above copyright
        12   *     notice, this list of conditions and the following disclaimer in
        13   *     the documentation and/or other materials provided with the
        14   *     distribution.
        15   *
        16   *   - Neither the name of Pebble nor the names of its contributors may
        17   *     be used to endorse or promote products derived from this software
        18   *     without specific prior written permission.
        19   *
        20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
        24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        30   * POSSIBILITY OF SUCH DAMAGE.
        31   */
        32  package net.sourceforge.pebble.webservice;
        33
        34  import net.sourceforge.pebble.domain.Blog;
        35  import net.sourceforge.pebble.util.StringUtils;
        36
        37  import org.apache.commons.logging.Log;
        38  import org.apache.commons.logging.LogFactory;
             /*
        P/P   *  Method: void net.sourceforge.pebble.webservice.UpdateNotificationPingsClient()
              */
        39  import org.apache.xmlrpc.AsyncCallback;
        40  import org.apache.xmlrpc.XmlRpcClient;
        41
        42  import java.io.IOException;
        43  import java.net.URL;
        44  import java.util.Hashtable;
        45  import java.util.Vector;
        46
        47  /**
        48   * A simple client to ping (notify) sites like weblogs.com when this blog has
        49   * been updated.
        50   *
        51   * @author    Simon Brown
        52   */
        53  public class UpdateNotificationPingsClient {
        54
        55    /** the log used by this class */
               /*
        P/P     *  Method: Log access$0()
                *
                *  Preconditions:
                *    init'ed(log)
                *
                *  Postconditions:
                *    return_value == log
                *    init'ed(return_value)
                */
        56    private static Log log = LogFactory.getLog(UpdateNotificationPingsClient.class);
        57
        58    /** the name of the method to call via XML-RPC */
        59    private static final String WEBLOGS_METHOD_NAME = "weblogUpdates.ping";
        60
        61    /**
        62     * Sends a weblogUpdates.ping indicating this the specified blog has
        63     * recently been updated. This version sends the blog's home URL.
        64     *
        65     * @param blog    the Blog representing the updated blog
        66     * @param sites   the list of sites (URLs) to ping
        67     */
               /*
        P/P     *  Method: void sendUpdateNotificationPing(Blog, String[])
                *
                *  Preconditions:
                *    blog != null
                *    (soft) log != null
                *    (soft) sites != null
                *    (soft) sites.length <= 232-1
                *    (soft) init'ed(sites[...])
                */
        68    public void sendUpdateNotificationPing(Blog blog, String[] sites) {
        69      sendUpdateNotificationPing(blog, blog.getUrl(), sites);
        70    }
        71
        72    /**
        73     * Sends a weblogUpdates.ping indicating this the specified blog has
        74     * recently been updated. This version sends an arbitrary URL.
        75     *
        76     * @param blog    the Blog representing the updated blog
        77     * @param url     the URL to send the ping for
        78     * @param sites   the list of sites (URLs) to ping
        79     */
        80    public void sendUpdateNotificationPing(Blog blog, String url, String[] sites) {
        81      try {
        82        for (String site : sites) {
        83          log.info("Sending XML-RPC ping to " + site);
        84          blog.info("Sending XML-RPC ping to " + StringUtils.transformHTML(site));
                     /*
        P/P           *  Method: void sendUpdateNotificationPing(Blog, String, String[])
                      *
                      *  Preconditions:
                      *    (soft) blog != null
                      *    (soft) log != null
                      *    (soft) sites != null
                      *    (soft) sites.length <= 232-1
                      *    (soft) init'ed(sites[...])
                      */
        85          XmlRpcClient xmlrpc = new XmlRpcClient(site);
        86          Vector params = new Vector();
        87          params.addElement(blog.getName());
        88          params.addElement(url);
        89          xmlrpc.executeAsync(WEBLOGS_METHOD_NAME, params, new UpdateNotificationPingsAsyncCallback(blog));
        90        }
        91      } catch (IOException ioe) {
        92        log.error(ioe.getMessage(), ioe);
        93      }
        94    }
        95
        96    /**
        97     * A callback class used to log the result/error message.
        98     */
        99    class UpdateNotificationPingsAsyncCallback implements AsyncCallback {
       100
       101      private Blog blog;
       102
       103      public UpdateNotificationPingsAsyncCallback(Blog blog) {
       104        this.blog = blog;
       105      }
       106
       107      /**
       108       * Called if the XML-RPC was successful.
       109       *
       110       * @param o         the resulting Object
       111       * @param url       the original URL
       112       * @param method    the original method name
       113       */
                 /*
        P/P       *  Method: void handleResult(Object, URL, String)
                  *
                  *  Preconditions:
                  *    (soft) net/sourceforge/pebble/webservice/UpdateNotificationPingsClient.log != null
                  *    (soft) this.blog != null
                  *
                  *  Test Vectors:
                  *    o: Addr_Set{null}, Inverse{null}
                  */
       114      public void handleResult(Object o, URL url, String method) {
       115        Hashtable result = (Hashtable)o;
       116        if (result != null) {
       117          log.info("Result of XML-RPC ping to " + method + " at " + url + " was " + result.get("flerror") + ", " + result.get("message"));
       118          blog.info("Result of XML-RPC ping to " + t(method) + " at " + t(url) + " was " + t(result.get("flerror")) + ", " + t(result.get("message")));
       119        }
       120      }
       121
       122      /**
       123       * Called if the XML-RPC was not successful.
       124       *
       125       * @param e         the resulting Exception
       126       * @param url       the original URL
       127       * @param method    the original method name
       128       */
                 /*
        P/P       *  Method: void handleError(Exception, URL, String)
                  *
                  *  Preconditions:
                  *    net/sourceforge/pebble/webservice/UpdateNotificationPingsClient.log != null
                  */
       129      public void handleError(Exception e, URL url, String method) {
       130        log.error("Exception when calling " + method + " at " + url, e);
       131      }
       132
                 /*
        P/P       *  Method: String t(Object)
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    object: Inverse{null}, Addr_Set{null}
                  */
       133      private String t(Object object) {
       134    	  if(object == null) return null;
       135    	  return StringUtils.transformHTML(object.toString());
       136      }
       137    }
       138
       139  }

SofCheck Inspector Build Version : 2.22510

updatenotificationpingsclient.java | 2010-Jun-25 19:40:32
updatenotificationpingsclient.class | 2010-Jul-19 20:23:38
updatenotificationpingsclient$updatenotificationpingsasynccallback.class | 2010-Jul-19 20:23:38