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.opennap.net;
21  
22  import java.util.Hashtable;
23  import java.util.Iterator;
24  
25  import org.xnap.plugin.opennap.OpenNapPlugin;
26  import org.xnap.search.MediaType;
27  import org.xnap.search.Search;
28  import org.xnap.search.SearchFilter;
29  import org.xnap.search.SearchManager;
30  import org.xnap.search.SearchProvider;
31  
32  public class OpenNapSearchManager implements SearchProvider {
33      
34      //--- Constant(s) ---
35  
36      //--- Data field(s) ---
37  
38      //private static Logger logger = Logger.getLogger(OpenNapSearchManager.class);
39  
40  	private Hashtable mediaTypeByRealm = new Hashtable();
41  
42      //--- Constructor(s) ---
43      
44      public OpenNapSearchManager()
45      {
46  		mediaTypeByRealm.put("audio", SearchManager.MEDIA_AUDIO);
47  		mediaTypeByRealm.put("video", SearchManager.MEDIA_VIDEO);
48  		mediaTypeByRealm.put("image", SearchManager.MEDIA_IMAGES);
49  		mediaTypeByRealm.put("application", SearchManager.MEDIA_SOFTWARE);
50  		mediaTypeByRealm.put("text", SearchManager.MEDIA_DOCUMENTS);
51      }
52  
53      //--- Method(s) ---
54  
55  	public MediaType getMediaType(String realm)
56  	{
57  		return (MediaType)mediaTypeByRealm.get(realm);
58  	}
59  
60  	public String getRealm(MediaType mt)
61  	{
62  		for (Iterator i = mediaTypeByRealm.keySet().iterator(); i.hasNext();) {
63  			String realm = (String)i.next();
64  			if (mediaTypeByRealm.get(realm) == mt) {
65  				return realm;
66  			}
67  		}
68  		return null;
69  	}
70  
71      public MediaType[] getSupportedMediaTypes()
72      {
73  		return new MediaType[] {
74  			SearchManager.MEDIA_ANYTHING, SearchManager.MEDIA_AUDIO, 
75  			SearchManager.MEDIA_DOCUMENTS, SearchManager.MEDIA_IMAGES, 
76  			SearchManager.MEDIA_SOFTWARE, SearchManager.MEDIA_VIDEO,
77  		};
78      }
79  
80      public String getName()
81      {
82  		return OpenNapPlugin.getInstance().getInfo().getName();
83      }
84  
85  	/***
86  	 * Returns true, if realm is a valid OpenNap realm.
87  	 */
88  	public boolean isRealm(String realm)
89  	{
90  		return mediaTypeByRealm.containsKey(realm);
91  	}
92  
93      /***
94       * Invoked when a search is requested.
95       */
96      public Search search(SearchFilter filter)
97      {
98  		// from this point on it is not possible to cleanly disable
99  		// the plugin anymore, since OpenNap plugin objects will get
100 		// distributed all over the place (like SearchResult objects
101 		// etc.)
102 		OpenNapPlugin.getInstance().getInfo().setDisableable(false);
103 
104 		OpenNapServer s = (OpenNapServer)filter.get("nap.server");
105 		if (s != null) {
106 			return new OpenNapSearch(filter, OpenNapSearch.PRIORITY_HIGH,
107 								 new OpenNapServer[] { s });
108 		}
109 		else {
110 			return new OpenNapSearch
111 				(filter, OpenNapSearch.PRIORITY_HIGH,
112 				 OpenNapPlugin.getServerManager().getConnectedServers());
113 		}
114     }
115     
116 }