SearchManager.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.search |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.search;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.event.ListEvent;
import org.xnap.event.ListListener;
import org.xnap.event.ListSupport;
import org.xnap.util.FileHelper;
/**
* Manages search providers. SearchManager follows the singleton pattern.
*/
public class SearchManager
{
//--- Constant(s) ---
public static String MEDIA_TYPE_FILE = "mediatypes.properties";
/**
* Default media type that matches anything.
*/
public static MediaType MEDIA_ANYTHING = new AnythingMediaType();
/**
* Default media type that matches audio files.
*/
public static MediaType MEDIA_AUDIO
= new DefaultMediaType("audio", XNap.tr("Audio"));
/**
* Default media type that matches document files.
*/
public static MediaType MEDIA_DOCUMENTS
= new DefaultMediaType("text", XNap.tr("Documents"));
/**
* Default media type that matches image files.
*/
public static MediaType MEDIA_IMAGES
= new DefaultMediaType("image", XNap.tr("Images"));
/**
* Default media type that matches software files.
*/
public static MediaType MEDIA_SOFTWARE
= new DefaultMediaType("application", XNap.tr("Software"));
/**
* Default media type that matches video files.
*/
public static MediaType MEDIA_VIDEO
= new DefaultMediaType("video", XNap.tr("Video"));
public static MediaType[] DEFAULT_MEDIA_TYPES = {
MEDIA_ANYTHING, MEDIA_AUDIO, MEDIA_DOCUMENTS, MEDIA_IMAGES,
MEDIA_SOFTWARE, MEDIA_VIDEO,
};
//--- Data field(s) ---
private static Logger logger = Logger.getLogger(SearchManager.class);
private static SearchManager instance = new SearchManager();
private SearchManagerListener listener = null;
private List providers = new LinkedList();
private ListSupport providerSupport = new ListSupport(this);
//--- Constructor(s) ---
private SearchManager()
{
initializeMediaTypes();
}
//--- Method(s) ---
/**
* Returns the instance of <code>SearchManager</code>.
*/
public static SearchManager getInstance()
{
return instance;
}
public static MediaType getMediaType(String filename)
{
// skip anything media type
for (int i = 1; i < DEFAULT_MEDIA_TYPES.length; i++) {
if (DEFAULT_MEDIA_TYPES[i].matches(filename)) {
return DEFAULT_MEDIA_TYPES[i];
}
}
return null;
}
/**
* Adds <code>provider</code> to the list of available search providers.
*/
public synchronized void add(SearchProvider provider)
{
providers.add(provider);
providerSupport.fireItemAdded(provider);
}
/**
* <code>Listener</code> is notified when a new search provider is
* added.
*/
public synchronized void addListListener(ListListener listener)
{
providerSupport.addListListener(listener);
for (Iterator i = providers.iterator(); i.hasNext();) {
listener.itemAdded
(new ListEvent(this, ListEvent.ITEM_ADDED, i.next()));
}
}
/**
* Asks the search manager to handle <code>search</code>. The search object
* is passed to the listener that starts the search and displays the
* results. If no listener is set nothing happens.
*
* @see SearchManagerListener
*/
public synchronized void handle(Search search)
{
if (listener != null) {
listener.handle(search);
}
}
/**
* Reads the extensions for the default media types from a property file.
*/
public void initializeMediaTypes()
{
Properties props = new Properties();
InputStream in = null;
try {
File f = new File(FileHelper.getHomeDir() + MEDIA_TYPE_FILE);
if (f.exists()) {
logger.debug("reading media types file: " + f);
in = new FileInputStream(f);
props.load(in);
}
else {
in = FileHelper.getResourceAsStream(MEDIA_TYPE_FILE);
if (in != null) {
logger.debug("reading media types resource: "
+ MEDIA_TYPE_FILE);
props.load(in);
}
else {
logger.debug("media types file not found");
}
}
}
catch (IOException e) {
logger.debug("could not read media types file", e);
}
finally {
try {
if (in != null) {
in.close();
}
}
catch (Exception e) {
}
}
if (props != null) {
// skip MEDIA_ANYTHING
for (int i = 1; i < DEFAULT_MEDIA_TYPES.length; i++) {
String key = DEFAULT_MEDIA_TYPES[i].getRealm();
String extensions = props.getProperty(key, "");
StringTokenizer t = new StringTokenizer(extensions, ";");
// add extensions
while (t.hasMoreTokens()) {
DefaultMediaType dmt
= (DefaultMediaType)DEFAULT_MEDIA_TYPES[i];
dmt.add(t.nextToken());
}
}
}
}
/**
* Removes <code>provider</code> from the list of available search
* providers.
*/
public synchronized void remove(SearchProvider provider)
{
providers.remove(provider);
providerSupport.fireItemRemoved(provider);
}
/**
* <code>Listener</code> is notified when a new search provider is
* removed.
*/
public void removeListListener(ListListener listener)
{
providerSupport.removeListListener(listener);
}
/**
* Returns a new <code>SearchContainer</code> that searches all networks.
*/
public synchronized Search searchAll(SearchFilter filter)
{
SearchContainer c = new SearchContainer(filter);
for (Iterator i = providers.iterator(); i.hasNext();) {
c.add(((SearchProvider)i.next()).search(filter));
}
return c;
}
/**
* Registers <code>listener</code> as the user search handler.
*/
public synchronized void setListener(SearchManagerListener listener)
{
this.listener = listener;
}
}
The table below shows all metrics for SearchManager.java.




