KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > util > CommentSpamChecker


1 package org.roller.util;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.roller.business.ThreadManagerImpl;
6 import org.roller.model.RollerFactory;
7 import org.roller.model.ThreadManager;
8 import org.roller.pojos.CommentData;
9
10 /**
11  * Created on Mar 9, 2004
12  * @author lance.lavandowska
13  */

14 public class CommentSpamChecker
15 {
16     private static Log mLogger = LogFactory.getLog(CommentSpamChecker.class);
17     private Blacklist blacklist = Blacklist.getBlacklist(null,null);
18
19     // -----------------------------------------------------------------------
20
/**
21      * Runs comment check on comment, sets spam flag on comment.
22      */

23     public void testComment(CommentData comment)
24     {
25         try
26         {
27             // by using the OR conditional it'll test each
28
// one in order and fall into the body without
29
// having to test each and every condition.
30
// Not sure which is the optimal order to check, though.
31
boolean isSpam = blacklist.isBlacklisted(comment.getUrl());
32             isSpam = blacklist.isBlacklisted(comment.getContent())?true:isSpam;
33             isSpam = blacklist.isBlacklisted(comment.getEmail())?true:isSpam;
34
35             if (isSpam)
36             {
37                 comment.setSpam(Boolean.TRUE);
38                 comment.save();
39                 RollerFactory.getRoller().commit();
40             }
41         }
42         catch (Exception JavaDoc e)
43         {
44             mLogger.error("Processing Comment",e);
45         }
46         finally
47         {
48             RollerFactory.getRoller().release();
49         }
50     }
51     
52     // -----------------------------------------------------------------------
53
/**
54      * Spawns thread to run comment check.
55      */

56     public void testComment(CommentData comment, ThreadManager threadMgr)
57     {
58         try
59         {
60             if (threadMgr != null)
61             {
62                 threadMgr.executeInBackground(new CommentCheckerRunnable(comment));
63             }
64             else
65             {
66                 mLogger.warn("No thread manager found.");
67             }
68         }
69         catch (InterruptedException JavaDoc e) {
70             mLogger.warn("Interrupted during Comment Spam check",e);
71         }
72     }
73
74     // -----------------------------------------------------------------------
75
/**
76      * Runnable to run spam check on it's own thread.
77      */

78     private class CommentCheckerRunnable implements Runnable JavaDoc
79     {
80         private CommentData mComment = null;
81         public CommentCheckerRunnable( CommentData comment)
82         {
83             mComment = comment;
84         }
85         public void run()
86         {
87             testComment(mComment);
88         }
89     }
90 }
Popular Tags