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  package org.xnap.plugin.freeway;
20  
21  import java.beans.PropertyChangeListener;
22  import java.io.File;
23  import java.security.Security;
24  import java.util.logging.Level;
25  
26  import javax.swing.Icon;
27  
28  import org.apache.log4j.Logger;
29  import org.bouncycastle.jce.provider.BouncyCastleProvider;
30  import org.gnu.freeway.Application;
31  import org.gnu.freeway.DaemonSocket;
32  import org.gnu.freeway.Prefs;
33  import org.gnu.freeway.protocol.afs.esed2.NNode;
34  import org.gnu.freeway.protocol.afs.esed2.Policy;
35  import org.gnu.freeway.protocol.afs.swing.SController;
36  import org.gnu.freeway.util.ConfigurationService;
37  import org.gnu.freeway.util.LoggingService;
38  import org.gnu.freeway.util.Service;
39  import org.gnu.freeway.util.ServiceManager;
40  import org.gnu.freeway.util.StarFormatter;
41  import org.gnu.freeway.util.ui.UIResources;
42  import org.xnap.XNap;
43  import org.xnap.gui.Dialogs;
44  import org.xnap.gui.XNapFrame;
45  import org.xnap.gui.util.IconHelper;
46  import org.xnap.plugin.AbstractPlugin;
47  import org.xnap.search.MediaType;
48  import org.xnap.search.Search;
49  import org.xnap.search.SearchFilter;
50  import org.xnap.search.SearchManager;
51  import org.xnap.search.SearchProvider;
52  
53  
54  /***
55   *
56   *
57   */
58  public class FreewayPlugin extends AbstractPlugin 
59  	implements SController, SearchProvider
60  {
61      private static FreewayPlugin instance = null;
62      private ConfigurationService config;
63      private UIResources resources;
64      private ServiceManager services;
65      private Prefs preferences;
66      private Policy policy;
67  
68      private static Logger logger = Logger.getLogger(FreewayPlugin.class);
69      
70  	public static final String ICON_FILENAME = "emacs.png";
71  
72  	public static final Icon ICON_16 = IconHelper.getIcon(ICON_FILENAME, 16, false);
73  	private FreewayPanel freewayPanel;
74  
75      public static FreewayPlugin getInstance()
76      {
77          return instance;
78      }
79  
80      /*
81       * @see org.gnu.freeway.util.ui.Controller#getApplication()
82       */
83      public Application getApplication()
84      {
85          // TODO Auto-generated method stub
86          return null;
87      }
88  
89      /*
90       * @see org.gnu.freeway.protocol.afs.swing.SController#getCache()
91       */
92      public File getCache()
93      {
94          return preferences.getSystemCache();
95      }
96  
97      public String getName()
98      {
99          return getInfo().getName();
100     }
101 
102     /*
103      * @see org.gnu.freeway.protocol.afs.swing.SController#getPolicy()
104      */
105     public Policy getPolicy()
106     {
107         return policy;
108     }
109 
110     /*
111      * @see org.gnu.freeway.protocol.afs.swing.SController#getPreferences()
112      */
113     public Prefs getPreferences()
114     {
115         return preferences;
116     }
117 
118     /*
119      * @see org.gnu.freeway.protocol.afs.swing.SController#getResources()
120      */
121     public UIResources getResources()
122     {
123         return resources;
124     }
125 
126     /*
127      * @see org.xnap.search.SearchProvider#getSupportedMediaTypes()
128      */
129     public MediaType[] getSupportedMediaTypes()
130     {
131         return new MediaType[] { SearchManager.MEDIA_ANYTHING };
132     }
133 
134     /*
135      * @see org.gnu.freeway.protocol.afs.swing.SController#addPropertyChangeListener(java.beans.PropertyChangeListener)
136      */
137     public void addPropertyChangeListener(PropertyChangeListener arg0)
138     {
139         // TODO Auto-generated method stub
140     }
141 
142     /*
143      * @see org.gnu.freeway.protocol.afs.swing.SController#addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
144      */
145     public void addPropertyChangeListener(String arg0,
146                                           PropertyChangeListener arg1)
147     {
148         // TODO Auto-generated method stub
149     }
150 
151     /*
152      * @see org.gnu.freeway.protocol.afs.swing.SController#connect()
153      */
154     public DaemonSocket connect()
155     {
156         // TODO Auto-generated method stub
157         return null;
158     }
159 
160     /*
161      * @see org.gnu.freeway.protocol.afs.swing.SController#displayDirectory(java.lang.String, org.gnu.freeway.protocol.afs.esed2.NNode)
162      */
163     public void displayDirectory(String arg0, NNode arg1)
164     {
165         // TODO Auto-generated method stub
166     }
167 
168     /*
169      * @see org.gnu.freeway.protocol.afs.swing.SController#download(org.gnu.freeway.protocol.afs.esed2.NNode)
170      */
171     public void download(NNode arg0)
172     {
173         // TODO Auto-generated method stub
174     }
175 
176     /*
177      * @see org.gnu.freeway.protocol.afs.swing.SController#enterCritical()
178      */
179     public void enterCritical()
180     {
181         // TODO Auto-generated method stub
182     }
183 
184     /*
185      * @see org.gnu.freeway.protocol.afs.swing.SController#err(java.lang.String, java.lang.Throwable)
186      */
187     public void err(String arg0, Throwable arg1)
188     {
189     	logger.error(arg0, arg1);
190     }
191 
192     /*
193      * @see org.gnu.freeway.protocol.afs.swing.SController#guiMessage(java.lang.String)
194      */
195     public void guiMessage(String arg0)
196     {
197     	Dialogs.showNotification(XNapFrame.getInstance(), "freewayNotification",
198     							 XNap.tr("XNap GNUnet notification"), arg0);
199     }
200 
201     /*
202      * @see org.gnu.freeway.protocol.afs.swing.SController#infoMessage(boolean, java.lang.String)
203      */
204     public void infoMessage(boolean arg0, String arg1)
205     {
206         // TODO Auto-generated method stub
207     }
208 
209     /*
210      * @see org.gnu.freeway.protocol.afs.swing.SController#leaveCritical()
211      */
212     public void leaveCritical()
213     {
214         // TODO Auto-generated method stub
215     }
216 
217     /*
218      * @see org.gnu.freeway.protocol.afs.swing.SController#log(java.util.logging.Level, java.lang.String)
219      */
220     public void log(Level arg0, String arg1) {}
221 
222     /*
223      * @see org.gnu.freeway.protocol.afs.swing.SController#refreshMenus()
224      */
225     public void refreshMenus()
226     {
227         // TODO Auto-generated method stub
228     }
229 
230     /*
231      * @see org.gnu.freeway.protocol.afs.swing.SController#removePropertyChangeListener(java.beans.PropertyChangeListener)
232      */
233     public void removePropertyChangeListener(PropertyChangeListener arg0)
234     {
235         // TODO Auto-generated method stub
236     }
237 
238     /*
239      * @see org.gnu.freeway.protocol.afs.swing.SController#removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
240      */
241     public void removePropertyChangeListener(String arg0,
242                                              						PropertyChangeListener arg1)
243     {
244         // TODO Auto-generated method stub
245     }
246 
247     /*
248      * @see org.xnap.search.SearchProvider#search(org.xnap.search.SearchFilter)
249      */
250     public Search search(SearchFilter filter)
251     {
252         return new FreewaySearch(filter, this);
253     }
254 
255     /*
256      * @see org.gnu.freeway.protocol.afs.swing.SController#service(java.lang.Class)
257      */
258     public Service service(Class c)
259     {
260         return services.service(c);
261     }
262 
263     /*
264      * @see org.gnu.freeway.protocol.afs.swing.SController#showStats()
265      */
266     public void showStats()
267     {
268         // TODO Auto-generated method stub
269     }
270 
271     /* (non-Javadoc)
272      * @see org.xnap.plugin.Plugin#start()
273      */
274     public void start() throws Exception
275     {
276         instance = this;
277 
278         try {
279             Security.insertProviderAt(new BouncyCastleProvider(), 0);
280         }
281         catch (SecurityException se) {
282             logger.error("Could not add Provider");
283         }
284 
285         LoggingService.configureLoggingToConsole(new StarFormatter());
286         Prefs.ensureInitialized();
287         services = ServiceManager.getInstance();
288         services.start();
289         preferences = new Prefs();
290         config = (ConfigurationService)service(ConfigurationService.class);
291         config.readConfiguration(preferences);
292         resources = new UIResources("main.xml", getCache());
293         resources.setGlobalTarget(this);
294         policy = new Policy(config, preferences);
295         policy.initAnonymityPolicy(null);
296         SearchManager.getInstance().add(this);
297     }
298 
299     /* (non-Javadoc)
300      * @see org.xnap.plugin.Plugin#startGUI()
301      */
302     public void startGUI()
303     {
304     	freewayPanel = new FreewayPanel(this);
305         XNapFrame.getInstance().insertTab
306 			(getInfo().getName(), IconHelper.getListIcon(ICON_FILENAME), 
307 			 freewayPanel);
308     }
309 
310     /* (non-Javadoc)
311      * @see org.xnap.plugin.Plugin#stop()
312      */
313     public void stop()
314     {
315         policy.doneAnonymityPolicy();
316         services.stop();
317         SearchManager.getInstance().remove(this);
318     }
319 
320     /* (non-Javadoc)
321      * @see org.xnap.plugin.Plugin#stopGUI()
322      */
323     public void stopGUI()
324     {
325         XNapFrame.getInstance().removeTab(freewayPanel);
326         freewayPanel = null;
327     }
328 }