View Javadoc

1   package org.jmage.filterchain;
2   
3   import org.apache.log4j.Logger;
4   import org.jmage.ApplicationContext;
5   import org.jmage.filter.FilterException;
6   import org.jmage.filter.ImageFilter;
7   
8   import java.net.URI;
9   import java.util.Properties;
10  
11  /***
12   * Convenience factory creates a FilterChain from a single ImageFilter class
13   */
14  public class SimpleFilterChainFactory implements FilterChainFactory {
15      protected static Logger log = Logger.getLogger(SimpleFilterChainFactory.class.getName());
16  
17      /***
18       * Default Constructur
19       */
20      public SimpleFilterChainFactory() {
21          //do nothing
22      }
23  
24      /***
25       * Configure SimpleFilterChainFactory
26       *
27       * @param context the ApplicationContext
28       */
29      public void configure(ApplicationContext context) {
30          //do nothing
31      }
32  
33      /***
34       * Creates a simple FilterChain with one filter for a given filter class name
35       *
36       * @param name the unique name
37       * @return the filter filterchain
38       */
39      public FilterChain createFrom(URI name) throws FilterChainException {
40          try {
41              String className = this.mapURItoClass(name);
42              Class clazz = Class.forName(className);
43              Object o = clazz.newInstance();
44              ImageFilter filter = (ImageFilter) o;
45  
46              FilterChain chain = new FilterChain(className);
47              chain.addFilter(filter);
48              if (log.isDebugEnabled()) log.debug(" created SimpleFilterChain for filter: " + filter.toString());
49              return chain;
50          } catch (ClassNotFoundException c) {
51              String message = "unable to create FilterChain, filter class not found: " + c.getMessage();
52              if (log.isDebugEnabled()) log.debug(message);
53              throw new FilterChainException(message);
54          } catch (Exception e) {
55              String message = "unable to create Filterchain for: " + name + " cause: " + e.getMessage();
56              if (log.isDebugEnabled()) log.debug(message);
57              throw new FilterChainException(message);
58          }
59      }
60  
61      /***
62       * Creates a simple FilterChain with one filter for a given filter class name and additional
63       * filter properties
64       *
65       * @param name             the unique name
66       * @param filterProperties the additional filter properties
67       * @return the FilterChain
68       * @throws FilterChainException
69       */
70      public FilterChain createFrom(URI name, Properties filterProperties) throws FilterChainException {
71          try {
72              FilterChain chain = this.createFrom(name);
73              chain.updateConfigurableFilters(filterProperties);
74              return chain;
75          } catch (FilterException e) {
76              String message = "unable to create simple filter filterchain for: " + name + " cause: " + e.getMessage();
77              if (log.isInfoEnabled()) log.info(message);
78              throw new FilterChainException(message);
79          }
80      }
81  
82      protected String mapURItoClass(URI uri) {
83          String schemeSpecific = uri.getSchemeSpecificPart();
84          return schemeSpecific;
85      }
86  
87      public String toString() {
88          return "[" + this.getClass().getName() + "#" + this.hashCode() + "]";
89      }
90  }