View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2001-2004  Emmanuel Bourg
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
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 net.jetrix.filter;
21  
22  import java.util.*;
23  import java.lang.reflect.*;
24  
25  /***
26   * Manages channel filters. Filter instances are obtained by calling the
27   * getFilter() method. The FilterManager is in charge to serve a new
28   * instance of the filter or the same unique instance depending on the
29   * type of the filter (static or not).
30   *
31   * @author Emmanuel Bourg
32   * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
33   */
34  public class FilterManager
35  {
36      private static FilterManager instance = new FilterManager();
37      private Map<String, MessageFilter> staticFilters;
38      private Map<String, String> filterAliases;
39  
40      private FilterManager()
41      {
42          staticFilters = new LinkedHashMap<String, MessageFilter>();
43          filterAliases = new LinkedHashMap<String, String>();
44      }
45  
46      /***
47       * Returns the instance of the FilterManager.
48       */
49      public static FilterManager getInstance()
50      {
51          return instance;
52      }
53  
54      /***
55       * Returns a filter of the specified class. If the filter is declared as
56       * a shared, the instance will be stored and returned on further calls
57       * for the same filter.
58       *
59       * @param classname Classname of the filter to return
60       *
61       * @return Filter of the specified class.
62       */
63      public synchronized MessageFilter getFilter(String classname) throws FilterException
64      {
65          // is there an entry for this class in the hashtable ?
66          Object obj = staticFilters.get(classname);
67          if (obj != null)
68          {
69              return (MessageFilter) obj;
70          }
71  
72          MessageFilter filter = null;
73  
74          try
75          {
76              // create a new filter
77              filter = (MessageFilter) Class.forName(classname).newInstance();
78  
79              // add the filter to the hashtable if it's a shared filter
80              if (filter.isShared())
81              {
82                  staticFilters.put(classname, filter);
83              }
84          }
85          catch (Exception e)
86          {
87              throw new FilterException(e);
88          }
89  
90          return filter;
91      }
92  
93      /***
94       * Returns a filter matching the specified name.
95       *
96       * @param name name of the filter to return
97       *
98       * @return Filter of the specified class.
99       */
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 }