KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > LinkChecker


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module;
11
12 import java.io.IOException JavaDoc;
13 import java.net.*;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.mmbase.util.*;
18 import org.mmbase.module.core.*;
19 import org.mmbase.util.logging.*;
20
21 /**
22  * The linkChecker module detects broken urls in the urls builder and the jumpers builder.
23  * If the linkchecker module is active it will at start up (5 minutes after the MMBase initialisation)
24  * and start perfoming checks.
25  * This wil only happend once every time time MMBase has been started.<br />
26  * For the LinckChecker to work the sendmail modules has to be configured and has to be active.
27  *
28  * @author Rob vermeulen
29  * @author Kees Jongenburger
30  * @version $Id: LinkChecker.java,v 1.20 2005/11/30 15:58:04 pierre Exp $
31  **/

32
33 public class LinkChecker extends ProcessorModule implements Runnable JavaDoc {
34
35     private static final Logger log = Logging.getLoggerInstance(LinkChecker.class);
36
37     private static long INITIAL_WAIT_TIME = 5 * 60 * 1000; // initial wait time after startup, default 5 minutes
38
private static long WAIT_TIME = 0; // wait time between runs, default 0 (don't wait but terminate)
39
private static long WAIT_TIME_BETWEEN_CHECKS = 5 * 1000; // wait time bewteen individual checks, default 5 seconds
40

41     private MMBase mmbase;
42
43     public void init() {
44         super.init();
45         String JavaDoc value = getInitParameter("waittime");
46         if (value!=null) {
47             try {
48                 WAIT_TIME = Long.parseLong(value);
49             } catch (NumberFormatException JavaDoc nfe) {
50                 log.warn("The value for property 'waittime' (" + value + ") is not a valid number.");
51             }
52         }
53         value = getInitParameter("initialwaittime");
54         if (value!=null) {
55             try {
56                 INITIAL_WAIT_TIME = Long.parseLong(value);
57             } catch (NumberFormatException JavaDoc nfe) {
58                 log.warn("The value for property 'initialwaittime' (" + value + ") is not a valid number.");
59             }
60         }
61         value = getInitParameter("waittimebetweenchecks");
62         if (value!=null) {
63             try {
64                 WAIT_TIME_BETWEEN_CHECKS = Long.parseLong(value);
65             } catch (NumberFormatException JavaDoc nfe) {
66                 log.warn("The value for property 'waittimebetweenchecks' (" + value + ") is not a valid number.");
67             }
68         }
69
70         mmbase = MMBase.getMMBase();
71         log.info("Module LinkChecker started");
72         MMBaseContext.startThread(this, "LinkChecker");
73     }
74
75     public String JavaDoc getModuleInfo() {
76         return "This module checks all urls in the urls and jumpers builders";
77     }
78
79     public void run() {
80         long waitTime = INITIAL_WAIT_TIME; // wait a certain time after startup (default 5 minutes)
81
while (!mmbase.isShutdown()) {
82             try {
83                 Thread.sleep(waitTime);
84             } catch (InterruptedException JavaDoc ie) {
85                 return;
86             }
87             log.service("LinkChecker starting to check all Jumpers and Urls");
88
89             StringBuffer JavaDoc data = new StringBuffer JavaDoc();
90             checkUrls("urls", "url", data);
91             checkUrls("jumpers", "url", data);
92
93             try {
94                 SendMailInterface sendmail = (SendMailInterface) getModule("sendmail");
95                 if (sendmail != null) {
96                     // init variables for mail.
97
String JavaDoc from = getInitParameter("from");
98                     String JavaDoc to = getInitParameter("to");
99                     String JavaDoc subject = getInitParameter("subject");
100                     if (subject == null || subject.equals("")) {
101                         subject = "List of incorrect urls and jumpers";
102                     }
103                     // Send Email if needed.
104
if (!data.toString().equals("")) {
105                         Mail mail = new Mail(to, from);
106                         mail.setSubject(subject);
107                         mail.setText(data.toString());
108                         sendmail.sendMail(mail);
109                     }
110                 } else {
111                     log.warn("LinkChecker requires the sendmail module to be active");
112                 }
113             } catch (RuntimeException JavaDoc re) {
114                 log.error("LinkChecker -> Error in Run() " + re);
115             }
116             // determine how log to wait next time 'round
117
// if 0, terminate
118
waitTime = WAIT_TIME;
119             if (waitTime == 0) {
120                 log.debug("LinkChecker finished, thread terminated.");
121                 return;
122             }
123        }
124     }
125
126     /**
127      * Checks if the urls in a specified builder exist.
128      * @param builderName the builder to check
129      * @param fieldName the fieldname of the url to check
130      * @param data the StringBuffer to append error information to
131      * @since MMBase-1.7
132      */

133     protected void checkUrls(String JavaDoc builderName, String JavaDoc fieldName, StringBuffer JavaDoc data) {
134         // Get all urls.
135
MMObjectBuilder urls = mmbase.getBuilder(builderName);
136         if (urls != null) {
137             List JavaDoc nodes = urls.getNodes();
138             for (Iterator JavaDoc iter = nodes.iterator(); iter.hasNext();) {
139                 MMObjectNode node = (MMObjectNode) iter.next();
140                 long number = node.getNumber();
141                 String JavaDoc url = "" + node.getValue(fieldName);
142                 // Check if an url is correct.
143
try {
144                     if (!checkUrl(url)) {
145                         data.append("Incorrect url: " + url + " (objectnumber: " + number + ")\n");
146                     }
147                 } catch (MalformedURLException mue) {
148                     data.append("Error in url (malformed): " + url + " (objectnumber: " + number + ")\n");
149                 } catch (IOException JavaDoc ioe) {
150                     data.append("Error in url (IO failure): " + url + " (objectnumber: " + number + ")\n");
151                 } catch (RuntimeException JavaDoc re) {
152                     data.append("Error in url (unknown): " + url + " (objectnumber: " + number + ", error: "+re.getMessage()+")\n");
153                 }
154                 try {
155                     Thread.sleep(WAIT_TIME_BETWEEN_CHECKS);
156                 } catch (InterruptedException JavaDoc ie) {} //wait 5 seconds
157
}
158         }
159     }
160
161     /**
162      * Checks if an url exists.
163      * @param url the url to check
164      * @return <code>false</code> if the url does not exist, <code>true</code> if the url exists
165      */

166     protected boolean checkUrl(String JavaDoc url) throws MalformedURLException, IOException JavaDoc {
167         if (url.indexOf("http") == 0 || url.indexOf("ftp") == 0) {
168             URL urlToCheck = new URL(url);
169             URLConnection uc = urlToCheck.openConnection();
170             String JavaDoc header = uc.getHeaderField(0);
171             return header.indexOf("404") == -1;
172         } else {
173             // I don't know if the url is wrong, so lets say it's correct.
174
return true;
175         }
176     }
177 }
178
Popular Tags