View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.openantivirus;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.log4j.Logger;
27  import org.openantivirus.credo.CredoException;
28  import org.openantivirus.credo.CredoParser;
29  import org.openantivirus.scanner.ScannerConfiguration;
30  import org.openantivirus.virushammer.ScanTarget;
31  import org.openantivirus.virushammer.ScanTargetList;
32  import org.openantivirus.virushammer.ScannerThread;
33  import org.xnap.XNapFacade;
34  import org.xnap.gui.AbstractPreferencesDialog;
35  import org.xnap.gui.StatusBar;
36  import org.xnap.gui.event.AbstractPreferencesDialogListener;
37  import org.xnap.plugin.AbstractPlugin;
38  import org.xnap.util.FileHelper;
39  import org.xnap.util.Preferences;
40  
41  /***
42   * Provides an anti-virus plugin based on OpenAntiVirus.
43   */
44  public class OAVPlugin extends AbstractPlugin 
45  {
46  
47      //--- Constant(s) ---
48      
49      //--- Data Field(s) ---
50  
51      private static Logger logger = Logger.getLogger(OAVPlugin.class);
52  	private static Preferences prefs = Preferences.getInstance();
53      private static OAVPlugin singleton;
54  
55  	private OAVPanel jpScanner;
56  	private OAVPreferences oavPrefs;
57      private PreferencesDialogListener pdl;
58  
59      private boolean started = false;
60  
61  	private ScannerThread runner;
62  	private ScannerConfiguration config;
63  	private ScanTargetList targets;
64  
65      //--- Constructor(s) ---
66  
67      public OAVPlugin()
68      {
69      }
70  
71      //--- Method(s) ---
72  
73      public static OAVPlugin getInstance()
74      {
75  		return singleton;
76      }
77  
78  	public static OAVPreferences getPreferences()
79  	{
80  		return singleton.oavPrefs;
81  	}
82  
83  	public static ScannerThread getScannerThread()
84  	{
85  		return singleton.runner;
86  	}
87  
88  	private void init() throws CredoException, IOException 
89  	{
90  		targets = new ScanTargetList();
91  		targets.addScanTarget
92  			(new ScanTarget(new File(prefs.getDownloadDir()), true));
93  
94  		config = new ScannerConfiguration();
95  
96  		// read signatures
97          CredoParser parser = new CredoParser(config);
98  		parser.parse(FileHelper.getResourceAsStream("VirusSignatures.credo"));
99  
100         config.getTrie().prepare();
101 
102 		runner = new ScannerThread(config); 
103 		runner.setScanTargetList(targets);
104 		runner.setTrie(config.getTrie());
105 
106 		Thread t = new Thread(runner);
107 		t.start();
108 	}
109 
110     /***
111      * Starts the plugin.
112      */
113     public void start() 
114     {
115 		singleton = this;
116 
117 		oavPrefs = new OAVPreferences();
118 		try {
119 			init();
120 		}
121 		catch (Exception e) {
122 			logger.debug("could not init scanner", e);
123 		}
124     }
125 
126     /***
127      * Starts the GUI of the plugin.
128      */
129     public void startGUI()
130     {
131 		pdl = new PreferencesDialogListener();
132 		XNapFacade.addPluginPreferencesDialogListener(pdl);
133 
134 		jpScanner = new OAVPanel();
135 		StatusBar.getInstance().addComponent(jpScanner);
136     }
137 
138     /***
139      * Stops the plugin. Disposes all singletons.
140      */
141     public void stop()
142     {
143 		oavPrefs = null;
144 
145 		singleton = null;
146     }
147 
148     /***
149      * Stops the GUI of the plugin.
150      */
151     public void stopGUI()
152     {
153 		StatusBar.getInstance().removeComponent(jpScanner);
154 		jpScanner = null;
155 
156 		pdl.dispose();
157 		XNapFacade.removePluginPreferencesDialogListener(pdl);
158 		pdl = null;
159     }
160 
161     // --- Inner Class(es) ---
162 
163     private class PreferencesDialogListener 
164 		extends AbstractPreferencesDialogListener
165     {
166 
167 		public void addPanels(AbstractPreferencesDialog dialog, List panels) 
168 		{
169 			OAVPreferencesPanel jpp = new OAVPreferencesPanel();
170 			panels.add(jpp);
171 			panels.add(dialog.addPanel(jpp, "khelpcenter.png"));
172 		}
173 
174     }
175 
176 }