001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2001-2004 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix.filter;
021
022 import java.util.*;
023 import java.lang.reflect.*;
024
025 /**
026 * Manages channel filters. Filter instances are obtained by calling the
027 * getFilter() method. The FilterManager is in charge to serve a new
028 * instance of the filter or the same unique instance depending on the
029 * type of the filter (static or not).
030 *
031 * @author Emmanuel Bourg
032 * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
033 */
034 public class FilterManager
035 {
036 private static FilterManager instance = new FilterManager();
037 private Map<String, MessageFilter> staticFilters;
038 private Map<String, String> filterAliases;
039
040 private FilterManager()
041 {
042 staticFilters = new LinkedHashMap<String, MessageFilter>();
043 filterAliases = new LinkedHashMap<String, String>();
044 }
045
046 /**
047 * Returns the instance of the FilterManager.
048 */
049 public static FilterManager getInstance()
050 {
051 return instance;
052 }
053
054 /**
055 * Returns a filter of the specified class. If the filter is declared as
056 * a shared, the instance will be stored and returned on further calls
057 * for the same filter.
058 *
059 * @param classname Classname of the filter to return
060 *
061 * @return Filter of the specified class.
062 */
063 public synchronized MessageFilter getFilter(String classname) throws FilterException
064 {
065 // is there an entry for this class in the hashtable ?
066 Object obj = staticFilters.get(classname);
067 if (obj != null)
068 {
069 return (MessageFilter) obj;
070 }
071
072 MessageFilter filter = null;
073
074 try
075 {
076 // create a new filter
077 filter = (MessageFilter) Class.forName(classname).newInstance();
078
079 // add the filter to the hashtable if it's a shared filter
080 if (filter.isShared())
081 {
082 staticFilters.put(classname, filter);
083 }
084 }
085 catch (Exception e)
086 {
087 throw new FilterException(e);
088 }
089
090 return filter;
091 }
092
093 /**
094 * Returns a filter matching the specified name.
095 *
096 * @param name name of the filter to return
097 *
098 * @return Filter of the specified class.
099 */
100 public MessageFilter getFilterByName(String name) throws FilterException
101 {
102 String classname = filterAliases.get(name);
103
104 if (classname != null)
105 {
106 return getFilter(classname);
107 }
108 else
109 {
110 throw new FilterException("Cannot find filter " + name);
111 }
112 }
113
114 /**
115 * Defines a new alias for a filter.
116 *
117 * @param name alias of the filter
118 * @param classname class of the filter
119 */
120 public void addFilterAlias(String name, String classname)
121 {
122 filterAliases.put(name, classname);
123 }
124
125 /**
126 * Return the map of filter aliases
127 *
128 * @since 0.2
129 */
130 public Map<String, String> getFilterAliases()
131 {
132 return filterAliases;
133 }
134
135 }