001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2001-2003 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
024 import net.jetrix.*;
025 import net.jetrix.config.*;
026
027 /**
028 * Abstract class defining a channel filter. A filter transforms a given message
029 * into a list of messages. Concrete filters just need to inherit from this
030 * class and implement the process() method.
031 *
032 * @author Emmanuel Bourg
033 * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
034 */
035 public abstract class MessageFilter
036 {
037 private Properties props;
038 private Channel channel;
039 protected FilterConfig config;
040
041 /**
042 * Indicates if the filter is shared or not. A shared filter should be
043 * a singleton if it's independant from the channel context (for example:
044 * a color stripper or a profanity filter). By default a filter is not
045 * a singleton. This method must be overwritten to return true if the
046 * filter is meant to be instanciated only once.
047 *
048 * @return <tt>false</tt>
049 */
050 public boolean isShared()
051 {
052 return false;
053 }
054
055 /**
056 * Called by the channel to indicate to a filter that the filter is being
057 * placed into service.
058 */
059 public void init() { }
060
061 /**
062 * Set the configuration used to initialize this filter.
063 */
064 public void setConfig(FilterConfig config)
065 {
066 this.config = config;
067 }
068
069 /**
070 * Return the configuration used to initialize this filter.
071 */
072 public FilterConfig getConfig()
073 {
074 return config;
075 }
076
077 /**
078 * Called by the channel to indicate to a filter that the filter is being
079 * taken out of service.
080 */
081 public void destroy()
082 {
083 }
084
085 /**
086 * Process a message and outputs messages to the specified List.
087 */
088 public abstract void process(Message m, List<Message> out);
089
090 /**
091 * Returns the name of this filter.
092 */
093 public String getName()
094 {
095 return "unknown filter";
096 }
097
098 /**
099 * 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 }