KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > pings > WeblogUpdatePinger


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.core.pings;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.roller.config.PingConfig;
24 import org.apache.roller.pojos.PingTargetData;
25 import org.apache.roller.pojos.WebsiteData;
26 import org.apache.xmlrpc.XmlRpcClient;
27 import org.apache.xmlrpc.XmlRpcException;
28
29 import java.io.IOException JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.UnknownHostException JavaDoc;
32 import java.util.*;
33
34 /**
35  * Utility for sending a weblog update ping.
36  *
37  * This implements the <code>WeblogUpdates.ping<code> XML-RPC call described at
38  * <a HREF="http://www.xmlrpc.com/weblogsCom">www.xmlrpc.com</a>
39  * as well as some variants required to interoperate with certain
40  * buggy but popular ping targets.
41  *
42  *
43  * @author <a HREF="mailto:anil@busybuddha.org">Anil Gangolli</a>
44  * @author llavandowska (for code refactored from the now-defunct <code>RollerXmlRpcClient</code>)
45  */

46 public class WeblogUpdatePinger {
47     public static final Log logger = LogFactory.getLog(WeblogUpdatePinger.class);
48
49     /**
50      * Conveys a ping result.
51      */

52     public static class PingResult {
53         boolean error;
54         String JavaDoc message;
55
56         public PingResult(Boolean JavaDoc error, String JavaDoc message) {
57             this.error = error != null ? error.booleanValue() : false;
58             this.message = message != null ? message : "";
59         }
60
61         public boolean isError() {
62             return error;
63         }
64
65         public void setError(boolean error) {
66             this.error = error;
67         }
68
69         public String JavaDoc getMessage() {
70             return message;
71         }
72
73         public void setMessage(String JavaDoc message) {
74             this.message = message;
75         }
76
77         public String JavaDoc toString() {
78             return "PingResult{" + "error=" + error + ", message='" + message + "'" + "}";
79         }
80     }
81
82     // Inhibit construction
83
private WeblogUpdatePinger() {
84     }
85
86     /**
87      * Send a weblog update ping.
88      *
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      */

95     public static PingResult sendPing(PingTargetData pingTarget, WebsiteData website) throws IOException JavaDoc, XmlRpcException {
96         String JavaDoc websiteUrl = website.getAbsoluteURL();
97         String JavaDoc pingTargetUrl = pingTarget.getPingUrl();
98         Set variantOptions = PingConfig.getVariantOptions(pingTargetUrl);
99
100         // Set up the ping parameters.
101
Vector params = new Vector();
102         if (!variantOptions.contains("noname")) {
103             // ping variant for icerocket and anyone with similar bug, where we must omit the blog name.
104
params.addElement(website.getName());
105         }
106         params.addElement(websiteUrl);
107         if (logger.isDebugEnabled()) {
108             logger.debug("Executing ping to '" + pingTargetUrl + "' for website '" + websiteUrl + "' (" + website.getName() + ")" + (variantOptions.isEmpty() ? "" : " with variant options " + variantOptions));
109         }
110
111         // Send the ping.
112
XmlRpcClient client = new XmlRpcClient(pingTargetUrl);
113         PingResult pingResult = parseResult(client.execute("weblogUpdates.ping", params));
114
115         if (logger.isDebugEnabled()) logger.debug("Ping result is: " + pingResult);
116         return pingResult;
117     }
118
119     private static PingResult parseResult(Object JavaDoc obj) {
120         // Deal with the fact that some buggy ping targets may not respond with the proper struct type.
121
if (obj == null) return new PingResult(null,null);
122         try {
123             // normal case: response is a struct (represented as a Hashtable) with Boolean flerror and String fields.
124
Hashtable result = (Hashtable) obj;
125             return new PingResult((Boolean JavaDoc) result.get("flerror"), (String JavaDoc) result.get("message"));
126         } catch (Exception JavaDoc ex) {
127             // exception case: The caller responded with an unexpected type, though parsed at the basic XML RPC level.
128
// This effectively assumes flerror = false, and sets message = obj.toString();
129
if (logger.isDebugEnabled()) logger.debug("Invalid ping result of type: " + obj.getClass().getName() + "; proceeding with stand-in representative.");
130             return new PingResult(null,obj.toString());
131         }
132     }
133
134     /**
135      * Decide if the given exception appears to warrant later retrial attempts.
136      *
137      * @param ex an exception thrown by the <coce>sendPing</code> operation
138      * @return true if the error warrants retrial
139      */

140     public static boolean shouldRetry(Exception JavaDoc ex) {
141         // Determine if error appears transient (warranting retrial)
142
// We give most errors the "benefit of the doubt" by considering them transient
143
// This picks out a few that we consider non-transient
144
if (ex instanceof UnknownHostException JavaDoc) {
145             // User probably mistyped the url in the custom target.
146
return false;
147         } else if (ex instanceof MalformedURLException JavaDoc) {
148             // This should never happen due to validations but if we get here, retrial won't fix it.
149
return false;
150         }
151         return true;
152     }
153
154 }
155
Popular Tags