KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > UploadFilterManager


1 /*
2  * $Id: UploadFilterManager.java,v 1.4 2004/12/01 07:54:07 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import java.io.FilterOutputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 /**
25  * @author <a HREF="mailto:hengels@mercatis.de">Holger Engels</a>
26  * @version $Revision: 1.4 $
27  */

28 public class UploadFilterManager {
29     private final transient static Log log = LogFactory.getLog(UploadFilterManager.class);
30
31     private static HashMap JavaDoc filterMappings = new HashMap JavaDoc();
32
33     public static void registerFilter(String JavaDoc name, Class JavaDoc filter, String JavaDoc errorText) {
34         if (!FilterOutputStream JavaDoc.class.isAssignableFrom(filter))
35             throw new IllegalArgumentException JavaDoc("class is not a FilterOutputStream!");
36
37         Entry entry = new Entry(filter);
38         entry.errorText = errorText;
39         filterMappings.put(name, entry);
40     }
41
42     public static void registerFilter(String JavaDoc name, Class JavaDoc filter) {
43         registerFilter(name, filter, null);
44     }
45
46     public static Class JavaDoc getFilterClass(String JavaDoc name) {
47         int dividerIndex = name.indexOf(SConstants.UID_DIVIDER);
48         name = name.substring(dividerIndex + 1);
49
50         return (Class JavaDoc) ((Entry) filterMappings.get(name)).filterClass;
51     }
52
53     public static FilterOutputStream JavaDoc createFilterInstance(String JavaDoc name, OutputStream JavaDoc out) {
54         FilterOutputStream JavaDoc filter = null;
55         try {
56             Entry entry = getFilterEntry(name);
57             if (entry == null)
58                 filter = new FilterOutputStream JavaDoc(out);
59             else {
60                 Class JavaDoc filterClass = entry.filterClass;
61                 if (filterClass != null) {
62                     log.info("using " + filterClass.getName() + " for " + name);
63                     Constructor JavaDoc constructor = filterClass.getConstructor(new Class JavaDoc[]{OutputStream JavaDoc.class});
64                     filter = (FilterOutputStream JavaDoc) constructor.newInstance(new Object JavaDoc[]{out});
65                     entry.filterInstance = filter;
66                 }
67             }
68         } catch (Exception JavaDoc e) {
69             log.fatal("Exception", e);
70         }
71         return filter;
72     }
73
74     public static FilterOutputStream JavaDoc getFilterInstance(String JavaDoc name) {
75         try {
76             Entry entry = getFilterEntry(name);
77             FilterOutputStream JavaDoc filterInstance = entry.filterInstance;
78             return filterInstance;
79         } catch (Exception JavaDoc e) {
80             log.fatal(null, e);
81             return null;
82         }
83     }
84
85     private static Entry getFilterEntry(String JavaDoc name) {
86         int dividerIndex = name.indexOf(SConstants.UID_DIVIDER);
87         name = name.substring(dividerIndex + 1);
88
89         return (Entry) filterMappings.get(name);
90     }
91
92     private static class Entry {
93         public Class JavaDoc filterClass;
94         public FilterOutputStream JavaDoc filterInstance;
95         public String JavaDoc errorText;
96
97
98         public Entry(Class JavaDoc filterClass) {
99             this.filterClass = filterClass;
100         }
101
102
103         public void setInstance(FilterOutputStream JavaDoc filterInstance) {
104             this.filterInstance = filterInstance;
105         }
106     }
107 }
108
109
110
Popular Tags