View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2001-2003  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  
24  import net.jetrix.*;
25  import net.jetrix.config.*;
26  
27  /***
28   * Abstract class defining a channel filter. A filter transforms a given message
29   * into a list of messages. Concrete filters just need to inherit from this
30   * class and implement the process() method.
31   *
32   * @author Emmanuel Bourg
33   * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
34   */
35  public abstract class MessageFilter
36  {
37      private Properties props;
38      private Channel channel;
39      protected FilterConfig config;
40  
41      /***
42       * Indicates if the filter is shared or not. A shared filter should be
43       * a singleton if it's independant from the channel context (for example:
44       * a color stripper or a profanity filter). By default a filter is not
45       * a singleton. This method must be overwritten to return true if the
46       * filter is meant to be instanciated only once.
47       *
48       * @return <tt>false</tt>
49       */
50      public boolean isShared()
51      {
52          return false;
53      }
54  
55      /***
56       * Called by the channel to indicate to a filter that the filter is being
57       * placed into service.
58       */
59      public void init() { }
60  
61      /***
62       * Set the configuration used to initialize this filter.
63       */
64      public void setConfig(FilterConfig config)
65      {
66          this.config = config;
67      }
68  
69      /***
70       * Return the configuration used to initialize this filter.
71       */
72      public FilterConfig getConfig()
73      {
74          return config;
75      }
76  
77      /***
78       * Called by the channel to indicate to a filter that the filter is being
79       * taken out of service.
80       */
81      public void destroy()
82      {
83      }
84  
85      /***
86       * Process a message and outputs messages to the specified List.
87       */
88      public abstract void process(Message m, List<Message> out);
89  
90      /***
91       * Returns the name of this filter.
92       */
93      public String getName()
94      {
95          return "unknown filter";
96      }
97  
98      /***
99       * Returns a short description of this filter.
100      */
101     public String getDescription()
102     {
103         return "no description";
104     }
105 
106     /***
107      * Returns the version of this filter
108      */
109     public String getVersion()
110     {
111         return "1.0";
112     }
113 
114     /***
115      * Returns the author of this filter.
116      */
117     public String getAuthor()
118     {
119         return "unknown";
120     }
121 
122     /***
123      * Gets the filter property indicated by the specified key.
124      */
125     public final String getProperty(String key)
126     {
127         return (props == null) ? null : props.getProperty(key);
128     }
129 
130     /***
131      * Sets the filter property indicated by the specified key.
132      */
133     public final void setProperty(String key, String value)
134     {
135         if (props == null)
136         {
137             props = new Properties();
138         }
139         props.setProperty(key, value);
140     }
141 
142     /***
143      * Returns the channel this filter applies on.
144      */
145     public final Channel getChannel()
146     {
147         return this.channel;
148     }
149 
150     /***
151      * Sets the channel this filter applies on.
152      */
153     public final void setChannel(Channel channel)
154     {
155         if (!isShared())
156         {
157             this.channel = channel;
158         }
159     }
160 }