KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > pings > WeblogUpdatePinger


1 /*
2  * Copyright (c) 2004-2005
3  * Lance Lavandowska, Anil R. Gangolli.
4  * All rights reserved.
5  *
6  * Distributed with the Roller Weblogger Project under the terms of the Roller Software
7  * License.
8  */

9
10 package org.roller.presentation.pings;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.apache.xmlrpc.XmlRpcClient;
15 import org.apache.xmlrpc.XmlRpcException;
16 import org.roller.RollerException;
17 import org.roller.model.RollerFactory;
18 import org.roller.pojos.PingTargetData;
19 import org.roller.pojos.WebsiteData;
20
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  * Utility for sending a weblog update ping.
29  *
30  * @author <a HREF="mailto:anil@busybuddha.org">Anil Gangolli</author>
31  * @author llavandowska (for code refactored from the now-defunct <code>RollerXmlRpcClient</code>)
32  */

33 public class WeblogUpdatePinger
34 {
35     public static final Log logger = LogFactory.getLog(WeblogUpdatePinger.class);
36
37     /**
38      * Conveys a ping result.
39      */

40     public static class PingResult
41     {
42         boolean error;
43         String JavaDoc message;
44
45         public PingResult(Boolean JavaDoc error, String JavaDoc message)
46         {
47             this.error = error != null ? error.booleanValue() : false;
48             this.message = message;
49         }
50
51         public boolean isError()
52         {
53             return error;
54         }
55
56         public void setError(boolean error)
57         {
58             this.error = error;
59         }
60
61         public String JavaDoc getMessage()
62         {
63             return message;
64         }
65
66         public void setMessage(String JavaDoc message)
67         {
68             this.message = message;
69         }
70
71         public String JavaDoc toString()
72         {
73             return "PingResult{" +
74                 "error=" + error +
75                 ", message='" + message + "'" +
76                 "}";
77         }
78     }
79
80     // Inhibit construction
81
private WeblogUpdatePinger()
82     {
83     }
84
85     /**
86      * Send a weblog update ping.
87      *
88      * @param absoluteContextUrl the absolute context url of the Roller site.
89      * @param pingTarget the target site to ping
90      * @param website the website that changed (from which the ping originates)
91      * @return the result message string sent by the server.
92      * @throws IOException
93      * @throws XmlRpcException
94      * @throws RollerException
95      */

96     public static PingResult sendPing(String JavaDoc absoluteContextUrl, PingTargetData pingTarget, WebsiteData website)
97         throws RollerException, IOException JavaDoc, XmlRpcException
98     {
99         // Figure out the url of the user's website.
100
String JavaDoc websiteUrl =
101             RollerFactory.getRoller().getWeblogManager().getUrl(website.getUser(), absoluteContextUrl);
102
103         // Set up the ping parameters.
104
Vector JavaDoc params = new Vector JavaDoc();
105         params.addElement(website.getName());
106         params.addElement(websiteUrl);
107         if (logger.isDebugEnabled())
108         {
109             logger.debug("Executing ping to '" + pingTarget.getPingUrl() + "' for website '" +
110                 websiteUrl + "' (" + website.getName() + ")");
111         }
112
113         // Send the ping
114
XmlRpcClient client = new XmlRpcClient(pingTarget.getPingUrl());
115         Hashtable JavaDoc result = (Hashtable JavaDoc) client.execute("weblogUpdates.ping", params);
116         PingResult pingResult = new PingResult((Boolean JavaDoc) result.get("flerror"), (String JavaDoc) result.get("message"));
117         if (logger.isDebugEnabled()) logger.debug("Ping result is: " + pingResult);
118         return pingResult;
119     }
120
121     /**
122      * Decide if the given exception appears to warrant later retrial attempts.
123      *
124      * @param ex an exception thrown by the <coce>sendPing</code> operation
125      * @return true if the error warrants retrial
126      */

127     public static boolean shouldRetry(Exception JavaDoc ex)
128     {
129         // Determine if error appears transient (warranting retrial)
130
// We give most errors the "benefit of the doubt" by considering them transient
131
// This picks out a few that we consider non-transient
132
if (ex instanceof UnknownHostException JavaDoc)
133         {
134             // User probably mistyped the url in the custom target.
135
return false;
136         }
137         else if (ex instanceof MalformedURLException JavaDoc)
138         {
139             // This should never happen due to validations but if we get here, retrial won't fix it.
140
return false;
141         }
142         return true;
143     }
144
145 }
146
Popular Tags