1 25 26 package org.snipsnap.xmlrpc; 27 28 import org.radeox.util.logging.Logger; 29 import org.snipsnap.app.Application; 30 import org.snipsnap.config.Configuration; 31 import org.snipsnap.snip.Snip; 32 import org.snipsnap.snip.SnipSpace; 33 import org.snipsnap.xmlrpc.ping.PingHandler; 34 import org.snipsnap.notification.Message; 35 import org.snipsnap.notification.MessageService; 36 import org.snipsnap.notification.Consumer; 37 import org.snipsnap.container.Components; 38 import org.apache.xmlrpc.XmlRpc; 39 40 import java.io.BufferedReader ; 41 import java.io.FileInputStream ; 42 import java.io.IOException ; 43 import java.io.InputStreamReader ; 44 import java.io.ByteArrayInputStream ; 45 import java.io.InputStream ; 46 import java.util.ArrayList ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.Map ; 50 51 57 58 public class WeblogsPing extends Thread implements Consumer { 59 private final static String WEBLOGSPING = "SnipSnap/config/weblogsping"; 60 61 private Configuration config; 62 private Map appParams; 63 64 private Snip weblog; 65 private static List handlers; 66 67 public WeblogsPing(Configuration configuration, Snip weblog) { 68 this.appParams = Application.get().getParameters(); 69 this.config = configuration; 70 this.weblog = weblog; 71 72 MessageService service = (MessageService) Components.getComponent(MessageService.class); 73 service.register(this); 74 75 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 76 Snip weblogsPingSnip = space.load(WEBLOGSPING); 77 try { 78 updateHandlers(weblogsPingSnip.getContent()); 79 } catch (IOException e) { 80 Logger.warn("unable to initialize weblogs ping handlers: " + e); 81 e.printStackTrace(); 82 } 83 } 84 85 public void run() { 86 Application.get().setConfiguration(config); 88 Application.get().setParameters(appParams); 89 if (config.allow(Configuration.APP_PERM_WEBLOGSPING)) { 90 if (handlers.size() > 0) { 91 Iterator iterator = handlers.iterator(); 92 while (iterator.hasNext()) { 93 PingHandler handler = (PingHandler) iterator.next(); 94 handler.ping(weblog); 95 } 96 } 97 } 98 } 99 100 public static void ping(Snip weblog) { 101 new WeblogsPing(Application.get().getConfiguration(), weblog).start(); 102 } 103 104 public void updateHandlers(String data) throws IOException { 105 handlers = new ArrayList (); 106 107 BufferedReader reader = 108 new BufferedReader (new InputStreamReader (new ByteArrayInputStream (data.getBytes()))); 109 String line; 110 while ((line = reader.readLine()) != null) { 111 if (!line.startsWith("#")) { 112 int index = line.indexOf(" "); 113 String type = line.substring(0, index); 114 try { 115 PingHandler handler = (PingHandler) Class.forName(type).newInstance(); 116 handler.setPingUrl(line.substring(index + 1)); 117 handlers.add(handler); 118 } catch (Exception e) { 119 Logger.warn("WeblogsPing: Unable to add handler for: " + line, e); 120 } 121 } 122 } 123 } 124 125 public void consume(Message message) { 126 if (Message.SNIP_MODIFIED.equals(message.getType())) { 127 Snip snip = (Snip) message.getValue(); 128 if (WEBLOGSPING.equals(snip.getName())) { 129 try { 130 Logger.log("reloading weblogs ping handler for: " + snip.getApplication()); 131 updateHandlers(snip.getContent()); 132 } catch (IOException e) { 133 System.err.println("ConfigurationManager: unable to reload configuration: " + e); 134 e.printStackTrace(); 135 } 136 } 137 } 138 } 139 } 140 | Popular Tags |