KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > xmlrpc > WeblogsPing


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
41 import java.io.FileInputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.InputStreamReader JavaDoc;
44 import java.io.ByteArrayInputStream JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Map JavaDoc;
50
51 /**
52  * Pings weblogs.com
53  *
54  * @author stephan
55  * @version $Id: WeblogsPing.java 1656 2004-06-13 14:55:14Z leo $
56  */

57
58 public class WeblogsPing extends Thread JavaDoc implements Consumer {
59   private final static String JavaDoc WEBLOGSPING = "SnipSnap/config/weblogsping";
60
61   private Configuration config;
62   private Map JavaDoc appParams;
63
64   private Snip weblog;
65   private static List JavaDoc 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 JavaDoc e) {
80       Logger.warn("unable to initialize weblogs ping handlers: " + e);
81       e.printStackTrace();
82     }
83   }
84
85   public void run() {
86     // ensure the right configuration is set
87
Application.get().setConfiguration(config);
88     Application.get().setParameters(appParams);
89     if (config.allow(Configuration.APP_PERM_WEBLOGSPING)) {
90       if (handlers.size() > 0) {
91         Iterator JavaDoc 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 JavaDoc data) throws IOException JavaDoc {
105     handlers = new ArrayList JavaDoc();
106
107     BufferedReader JavaDoc reader =
108       new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new ByteArrayInputStream JavaDoc(data.getBytes())));
109     String JavaDoc line;
110     while ((line = reader.readLine()) != null) {
111       if (!line.startsWith("#")) {
112         int index = line.indexOf(" ");
113         String JavaDoc 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 JavaDoc 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 JavaDoc e) {
133           System.err.println("ConfigurationManager: unable to reload configuration: " + e);
134           e.printStackTrace();
135         }
136       }
137     }
138   }
139 }
140
Popular Tags