KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > media > filters > MainFilter


1 /*
2  
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5  
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8  
9  */

10
11 package org.mmbase.applications.media.filters;
12
13 import org.mmbase.util.logging.Logger;
14 import org.mmbase.util.logging.Logging;
15 import org.mmbase.util.xml.DocumentReader;
16 import org.mmbase.util.*;
17
18 import org.w3c.dom.Element JavaDoc;
19
20 import java.util.*;
21 import java.io.File JavaDoc;
22
23 /**
24  * This is the main class for the filter process. It maintains list of
25  * Filters (which content can be configured by the 'filters.xml'
26  * configuration file. It does not do any filtering itself, it is only
27  * the access to the actual filters, so filtering is completably
28  * configurable.
29  *
30  * Since there can be only one 'main' filter this class is a
31  * Singleton, and its one instance can be gotten by the getInstance
32  * function (and this is done by the Media builders when they need url
33  * representations to the stream they describe).
34  *
35  *
36  * @author Rob Vermeulen (VPRO)
37  * @author Michiel Meeuwissen
38  */

39 public class MainFilter {
40     private static Logger log = Logging.getLoggerInstance(MainFilter.class);
41
42     public static final String JavaDoc MAIN_TAG = "mainFilter";
43     public static final String JavaDoc FILTERCONFIGS_TAG = "filterConfigs";
44     public static final String JavaDoc FILTERCONFIG_TAG = "config";
45     public static final String JavaDoc CHAIN_TAG = "chain";
46     public static final String JavaDoc FILTER_TAG = "filter";
47     public static final String JavaDoc FILTER_ATT = "filter";
48     public static final String JavaDoc ID_ATT = "id";
49     public static final String JavaDoc CONFIG_FILE = "media" + File.separator + "filters.xml";
50         
51     private FileWatcher configWatcher = new FileWatcher(true) {
52         public void onChange(File JavaDoc file) {
53             readConfiguration(file);
54         }
55     };
56     
57     private List filters = new ArrayList();
58
59     /**
60      * Construct the MainFilter
61      */

62     private MainFilter() {
63         File JavaDoc configFile = new File JavaDoc(org.mmbase.module.core.MMBaseContext.getConfigPath(), CONFIG_FILE);
64         if (! configFile.exists()) {
65             log.error("Configuration file for mediasourcefilter " + configFile + " does not exist");
66             return;
67         }
68         readConfiguration(configFile);
69         configWatcher.add(configFile);
70         configWatcher.setDelay(10 * 1000); // check every 10 secs if config changed
71
configWatcher.start();
72     }
73
74     
75     private static MainFilter filter = null;
76
77     public static MainFilter getInstance() {
78         if (filter == null) filter = new MainFilter();
79         return filter;
80     }
81
82     
83     /**
84      * read the MainFilter configuration
85      */

86     private synchronized void readConfiguration(File JavaDoc configFile) {
87         if (log.isServiceEnabled()) {
88             log.service("Reading " + configFile);
89         }
90         filters.clear();
91
92         DocumentReader reader = new XMLBasicReader(configFile.toString(), getClass());
93         Element JavaDoc filterConfigs = reader.getElementByPath(MAIN_TAG + "." + FILTERCONFIGS_TAG);
94
95         ChainSorter chainComp = new ChainSorter();
96         // When chaining 'comparators' then they are combined to one comparator
97
// Then only one 'sort' has to be done, which is more efficient.
98

99         for(Iterator e = reader.getChildElements(MAIN_TAG + "." + CHAIN_TAG, FILTER_TAG); e.hasNext();) {
100             Element JavaDoc chainElement =(Element JavaDoc)e.next();
101             String JavaDoc clazz = reader.getElementValue(chainElement);
102             String JavaDoc elementId = chainElement.getAttribute(ID_ATT);
103             try {
104                 Class JavaDoc newclass = Class.forName(clazz);
105                 Filter filter = (Filter) newclass.newInstance();
106                 if (filter instanceof Sorter) {
107                     chainComp.add((Sorter) filter);
108                 } else {
109                     if (chainComp.size() > 0) {
110                         filters.add(chainComp);
111                         chainComp = new ChainSorter();
112                     }
113                     filters.add(filter);
114                 }
115                 log.service("Added filter " + clazz + "(id=" + elementId + ")");
116                 if (elementId != null && ! "".equals(elementId)) {
117                     // find right configuration
118
// not all filters necessarily have there own configuration
119
boolean found = false;
120                     
121                     for (Iterator configIter = reader.getChildElements(filterConfigs, FILTERCONFIG_TAG); configIter.hasNext();) {
122                         Element JavaDoc config = (Element JavaDoc) configIter.next();
123                         String JavaDoc filterAtt = reader.getElementAttributeValue(config, FILTER_ATT);
124                         if (filterAtt.equals(elementId)) {
125                             log.service("Configuring " + elementId);
126                             filter.configure(reader, config);
127                             found = true;
128                             break;
129                         }
130                     }
131                     if (! found) log.debug("No configuration found for filter " + elementId);
132                 }
133             } catch (ClassNotFoundException JavaDoc ex) {
134                 log.error("Cannot load filter " + clazz + "\n" + ex);
135             } catch (InstantiationException JavaDoc ex1) {
136                 log.error("Cannot load filter " + clazz + "\n" + ex1);
137             } catch (IllegalAccessException JavaDoc ex2) {
138                 log.error("Cannot load filter " + clazz + "\n" + ex2);
139             }
140         }
141         if (chainComp.size() > 0) filters.add(chainComp); // make sure it is at least empty
142
}
143
144     /**
145      * Perform the actual filter task
146      */

147
148     public List filter(List urls) {
149         Iterator i = filters.iterator();
150         while (i.hasNext()) {
151             Filter filter = (Filter) i.next();
152             if (log.isDebugEnabled()) {
153                 log.debug("Using filter " + filter);
154                 log.debug("before: " + urls);
155             }
156   
157             try {
158                 urls = filter.filter(urls);
159             } catch (Exception JavaDoc filterException) {
160         log.error("Check filter "+filter+" "+filterException);
161             }
162             if (log.isDebugEnabled()) {
163                 log.debug("after: " + urls);
164             }
165         }
166         return urls;
167     }
168
169
170     // ====================================================================================
171
// Test-code
172

173     private static void addTestData(Collection c) {
174         c.add("hoi");
175         c.add("hallo");
176         c.add("heeej");
177         c.add("hello");
178         c.add("daag");
179         c.add("ajuus");
180         c.add("saluton");
181         c.add("cao");
182         c.add("arriverderci");
183         c.add("gxis");
184         c.add("hej");
185         c.add("komop");
186         c.add("1234");
187     }
188     private static class TestComparator implements Comparator {
189         private int i;
190         TestComparator(int i) {
191             this.i = i;
192         }
193         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
194             return o1.hashCode() - o2.hashCode();
195         }
196         
197     }
198
199     public static void main(String JavaDoc[] args) {
200         // what is quicker: sorting a list, or creating a new sortedset:
201
final int ITERATIONS = 200000;
202
203         List list = new ArrayList();
204         addTestData(list);
205         long start = System.currentTimeMillis();
206         for (int i = 0; i < ITERATIONS; i++) {
207             Comparator c = new TestComparator(i);
208             Collections.sort(list, c);
209         }
210         log.debug("list duration: " + (System.currentTimeMillis() - start));
211         log.debug(list);
212         SortedSet sortedSet = new TreeSet();
213         addTestData(sortedSet);
214         start = System.currentTimeMillis();
215         for (int i = 0; i < ITERATIONS; i++) {
216             SortedSet s = new TreeSet(new TestComparator(i));
217             s.addAll(sortedSet);
218             sortedSet = s;
219         }
220         log.debug("sortedset duration: " + (System.currentTimeMillis() - start));
221         log.debug(sortedSet);
222     }
223
224     
225 }
226
Popular Tags