KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > admin > Maintenance


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

26 package org.snipsnap.net.admin;
27
28 import org.radeox.util.logging.Logger;
29 import org.snipsnap.app.Application;
30 import org.snipsnap.config.Configuration;
31 import org.snipsnap.container.Components;
32 import org.snipsnap.snip.Access;
33 import org.snipsnap.snip.Links;
34 import org.snipsnap.snip.Snip;
35 import org.snipsnap.snip.SnipSpace;
36
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.HashSet JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.Set JavaDoc;
48
49 public class Maintenance implements SetupHandler {
50   public String JavaDoc getName() {
51     return "maintenance";
52   }
53
54   private Map JavaDoc workerThreads = new HashMap JavaDoc();
55
56   public Map JavaDoc setup(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Configuration config, Map JavaDoc errors) {
57     String JavaDoc appOid = (String JavaDoc) Application.get().getObject(Application.OID);
58     CheckConsistency workerThread = (CheckConsistency) workerThreads.get(appOid);
59     if (workerThread != null && workerThread.isAlive()) {
60       setRunning(workerThread, request.getSession());
61       return errors;
62     }
63
64     if (request.getParameter("check") != null) {
65       if (workerThread != null) {
66         workerThreads.remove(appOid);
67         workerThread = null;
68       }
69       workerThread = new CheckConsistency(appOid, false);
70       workerThread.start();
71       workerThreads.put(appOid, workerThread);
72
73       setRunning(workerThread, request.getSession());
74     } else if (request.getParameter("dorepair") != null) {
75       CheckConsistency repairThread = new CheckConsistency(appOid, true);
76       repairThread.setParentsToFix(workerThread.getParentsToFix());
77       repairThread.setCommentsToFix(workerThread.getCommentsToFix());
78       repairThread.setDuplicates(workerThread.getDuplicates());
79       repairThread.setSpamList(workerThread.getSpamList());
80       repairThread.start();
81       workerThreads.put(appOid, repairThread);
82
83       setRunning(repairThread, request.getSession());
84     } else {
85       request.getSession().removeAttribute("running");
86       if (workerThread != null && !workerThread.isAlive()) {
87         request.setAttribute("fixComments", workerThread.getCommentsToFix());
88         request.setAttribute("fixParents", workerThread.getParentsToFix());
89         request.setAttribute("duplicates", workerThread.getDuplicates());
90         request.setAttribute("notFixable", workerThread.getNonFixable());
91         request.setAttribute("spamedSnips", new Integer JavaDoc(workerThread.getSpamList().size()));
92       }
93     }
94
95     return errors;
96   }
97
98   private void setRunning(CheckConsistency workerThread, HttpSession JavaDoc session) {
99     Map JavaDoc statusMap = (Map JavaDoc) session.getAttribute("running");
100     if (null == statusMap) {
101       statusMap = new HashMap JavaDoc();
102     }
103     statusMap.put("max", new Integer JavaDoc(workerThread.getMax()));
104     statusMap.put("current", new Integer JavaDoc(workerThread.getCurrent()));
105     if (workerThread.isRepairing()) {
106       statusMap.put("maintenance", "repairing");
107     } else {
108       statusMap.put("maintenance", "checking");
109     }
110     session.setAttribute("running", statusMap);
111   }
112
113   class CheckConsistency extends Thread JavaDoc {
114     private String JavaDoc appOid = null;
115     private List JavaDoc fixDuplicates = new ArrayList JavaDoc();
116     private List JavaDoc fixComments = new ArrayList JavaDoc();
117     private List JavaDoc fixParents = new ArrayList JavaDoc();
118     private List JavaDoc noFix = new ArrayList JavaDoc();
119     private List JavaDoc fixSpam = new ArrayList JavaDoc();
120
121     private boolean repair = false;
122
123     private int currentCount = 0;
124     private int snipCount = 0;
125
126     public CheckConsistency(String JavaDoc appOid, boolean repair) {
127       super();
128       this.appOid = appOid;
129       this.repair = repair;
130     }
131
132     public boolean isRepairing() {
133       return repair;
134     }
135
136     public void run() {
137       Application.get().storeObject(Application.OID, appOid);
138       SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
139
140       if (!repair) {
141         List JavaDoc allSnips = Collections.unmodifiableList(space.getAll());
142         Set JavaDoc uniqeSnipNames = new HashSet JavaDoc();
143         Iterator JavaDoc snipIt = allSnips.iterator();
144         snipCount = allSnips.size();
145         Logger.debug("Need to check " + snipCount + " snips.");
146         while (snipIt.hasNext()) {
147           Snip snip = (Snip) snipIt.next();
148           check(snip, space);
149           if (!uniqeSnipNames.add(snip.getName())) {
150             fixDuplicates.add(snip);
151           }
152           currentCount++;
153         }
154       } else {
155         snipCount = fixParents.size() +
156                     fixComments.size() + fixDuplicates.size() + fixSpam.size();
157         currentCount = 0;
158         Iterator JavaDoc parentIt = fixParents.iterator();
159         while (parentIt.hasNext()) {
160           Snip snip = (Snip) parentIt.next();
161           fixParent(snip);
162           space.systemStore(snip);
163           currentCount++;
164           parentIt.remove();
165         }
166         Iterator JavaDoc commentIt = fixComments.iterator();
167         while (commentIt.hasNext()) {
168           Snip snip = (Snip) commentIt.next();
169           fixComment(snip, space);
170           space.systemStore(snip);
171           currentCount++;
172           commentIt.remove();
173         }
174         List JavaDoc blackList = Access.getReferrerBlackList();
175         Iterator JavaDoc spamIt = fixSpam.iterator();
176         while (spamIt.hasNext()) {
177           Snip snip = (Snip) spamIt.next();
178           Links backLinks = snip.getBackLinks();
179
180           Iterator JavaDoc blackListIt = blackList.iterator();
181           boolean storeModifiedSnip = false;
182           while (blackListIt.hasNext()) {
183             String JavaDoc entry = ((String JavaDoc) blackListIt.next()).toLowerCase();
184             if (entry.startsWith("pattern:")) {
185               if (backLinks.removeLinkByPattern(entry.substring("pattern:".length()).trim()) > 0) {
186                 storeModifiedSnip = true;
187                 Logger.debug("removed link by pattern: '" + entry.substring("pattern:".length()).trim() + "'");
188               }
189             } else {
190               if (backLinks.removeLink(entry.trim()) > 0) {
191                 storeModifiedSnip = true;
192                 Logger.debug("removed link by domain: " + entry);
193               }
194             }
195           }
196           if (storeModifiedSnip) {
197             space.systemStore(snip);
198           }
199           spamIt.remove();
200           currentCount++;
201         }
202       }
203     }
204
205     public void setCommentsToFix(List JavaDoc snips) {
206       fixComments = snips;
207     }
208
209     public List JavaDoc getCommentsToFix() {
210       return fixComments;
211     }
212
213     public void setParentsToFix(List JavaDoc snips) {
214       fixParents = snips;
215     }
216
217     public List JavaDoc getParentsToFix() {
218       return fixParents;
219     }
220
221     public void setDuplicates(List JavaDoc snips) {
222       fixDuplicates = snips;
223     }
224
225     public List JavaDoc getDuplicates() {
226       return fixDuplicates;
227     }
228
229     public List JavaDoc getNonFixable() {
230       return noFix;
231     }
232
233     public void setSpamList(List JavaDoc snips) {
234       fixSpam = snips;
235     }
236
237     public List JavaDoc getSpamList() {
238       return fixSpam;
239     }
240
241     private void check(Snip snip, SnipSpace space) {
242       String JavaDoc snipName = snip.getName();
243       if (snipName.startsWith("comment-")) {
244         if (null == snip.getCommentedSnip() && (null == snip.getCommentedName() || "".equals(snip.getCommentedName()))) {
245           String JavaDoc commentedName = snipName.substring("comment-".length(), snipName.lastIndexOf("-"));
246           if (!space.exists(commentedName)) {
247             Logger.warn("non-fixable snip found: '" + snipName + "' (commented snip '" + commentedName + "' missing)");
248             noFix.add(snip);
249           } else {
250             Logger.warn("snip '" + snipName + "' is missing its commented snip '" + commentedName + "'");
251             fixComments.add(snip);
252           }
253         }
254       }
255
256       if (snipName.matches("\\d\\d\\d\\d-\\d\\d-\\d\\d")) {
257         if (null == snip.getParent() && (null == snip.getParentName() || "".equals(snip.getParentName()))) {
258           Logger.warn("snip '" + snipName + "' is missing its parent snip");
259           fixParents.add(snip);
260         }
261       }
262
263       Iterator JavaDoc backLinksIt = snip.getBackLinks().iterator();
264       boolean foundspam = false;
265       while (backLinksIt.hasNext()) {
266         String JavaDoc url = (String JavaDoc) backLinksIt.next();
267         if (!Access.isValidReferrer(url)) {
268           foundspam = true;
269           fixSpam.add(snip);
270           break;
271         }
272       }
273       if (foundspam) {
274         Logger.debug("Found spam-referrer links at '" + snipName + "'");
275       }
276     }
277
278     private void fixParent(Snip snip) {
279       String JavaDoc snipName = snip.getName();
280       if (snipName.matches("\\d\\d\\d\\d-\\d\\d-\\d\\d")) {
281         if (null == snip.getParent() && (null == snip.getParentName() || "".equals(snip.getParentName()))) {
282           Logger.warn("fixing snip parent of '" + snipName + "'");
283           snip.setParentName("start");
284         }
285       }
286     }
287
288     private void fixComment(Snip snip, SnipSpace space) {
289       String JavaDoc snipName = snip.getName();
290       if (snipName.startsWith("comment-")) {
291         if (null == snip.getCommentedSnip() && (null == snip.getCommentedName() || "".equals(snip.getCommentedName()))) {
292           String JavaDoc commentedName = snipName.substring("comment-".length(), snipName.lastIndexOf("-"));
293           if (!space.exists(commentedName)) {
294             Logger.warn("commented snip of snip '" + snipName + "' got lost?");
295           } else {
296             Logger.warn("fixing commented snip '" + snipName + "' -> '" + commentedName + "'");
297             snip.setCommentedName(commentedName);
298           }
299         }
300       }
301     }
302
303     public int getMax() {
304       return snipCount;
305     }
306
307     public int getCurrent() {
308       return currentCount;
309     }
310   }
311 }
312
313
Popular Tags