1 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 ; 20 import java.io.OutputStream ; 21 import java.lang.reflect.Constructor ; 22 import java.util.HashMap ; 23 24 28 public class UploadFilterManager { 29 private final transient static Log log = LogFactory.getLog(UploadFilterManager.class); 30 31 private static HashMap filterMappings = new HashMap (); 32 33 public static void registerFilter(String name, Class filter, String errorText) { 34 if (!FilterOutputStream .class.isAssignableFrom(filter)) 35 throw new IllegalArgumentException ("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 name, Class filter) { 43 registerFilter(name, filter, null); 44 } 45 46 public static Class getFilterClass(String name) { 47 int dividerIndex = name.indexOf(SConstants.UID_DIVIDER); 48 name = name.substring(dividerIndex + 1); 49 50 return (Class ) ((Entry) filterMappings.get(name)).filterClass; 51 } 52 53 public static FilterOutputStream createFilterInstance(String name, OutputStream out) { 54 FilterOutputStream filter = null; 55 try { 56 Entry entry = getFilterEntry(name); 57 if (entry == null) 58 filter = new FilterOutputStream (out); 59 else { 60 Class filterClass = entry.filterClass; 61 if (filterClass != null) { 62 log.info("using " + filterClass.getName() + " for " + name); 63 Constructor constructor = filterClass.getConstructor(new Class []{OutputStream .class}); 64 filter = (FilterOutputStream ) constructor.newInstance(new Object []{out}); 65 entry.filterInstance = filter; 66 } 67 } 68 } catch (Exception e) { 69 log.fatal("Exception", e); 70 } 71 return filter; 72 } 73 74 public static FilterOutputStream getFilterInstance(String name) { 75 try { 76 Entry entry = getFilterEntry(name); 77 FilterOutputStream filterInstance = entry.filterInstance; 78 return filterInstance; 79 } catch (Exception e) { 80 log.fatal(null, e); 81 return null; 82 } 83 } 84 85 private static Entry getFilterEntry(String 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 filterClass; 94 public FilterOutputStream filterInstance; 95 public String errorText; 96 97 98 public Entry(Class filterClass) { 99 this.filterClass = filterClass; 100 } 101 102 103 public void setInstance(FilterOutputStream filterInstance) { 104 this.filterInstance = filterInstance; 105 } 106 } 107 } 108 109 110 | Popular Tags |