1 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 ; 30 import java.net.MalformedURLException ; 31 import java.net.UnknownHostException ; 32 import java.util.*; 33 34 46 public class WeblogUpdatePinger { 47 public static final Log logger = LogFactory.getLog(WeblogUpdatePinger.class); 48 49 52 public static class PingResult { 53 boolean error; 54 String message; 55 56 public PingResult(Boolean error, String 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 getMessage() { 70 return message; 71 } 72 73 public void setMessage(String message) { 74 this.message = message; 75 } 76 77 public String toString() { 78 return "PingResult{" + "error=" + error + ", message='" + message + "'" + "}"; 79 } 80 } 81 82 private WeblogUpdatePinger() { 84 } 85 86 95 public static PingResult sendPing(PingTargetData pingTarget, WebsiteData website) throws IOException , XmlRpcException { 96 String websiteUrl = website.getAbsoluteURL(); 97 String pingTargetUrl = pingTarget.getPingUrl(); 98 Set variantOptions = PingConfig.getVariantOptions(pingTargetUrl); 99 100 Vector params = new Vector(); 102 if (!variantOptions.contains("noname")) { 103 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 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 obj) { 120 if (obj == null) return new PingResult(null,null); 122 try { 123 Hashtable result = (Hashtable) obj; 125 return new PingResult((Boolean ) result.get("flerror"), (String ) result.get("message")); 126 } catch (Exception ex) { 127 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 140 public static boolean shouldRetry(Exception ex) { 141 if (ex instanceof UnknownHostException ) { 145 return false; 147 } else if (ex instanceof MalformedURLException ) { 148 return false; 150 } 151 return true; 152 } 153 154 } 155 | Popular Tags |