View Javadoc

1   package org.jmage.dispatcher;
2   
3   import org.apache.log4j.Logger;
4   import org.jmage.ApplicationContext;
5   import org.jmage.ImageRequest;
6   import org.jmage.JmageException;
7   import org.jmage.encoder.EncoderManager;
8   import org.jmage.filterchain.FilterChainManager;
9   import org.jmage.resource.ResourceManager;
10  
11  /***
12   * FilteringRequestDispatcher dispatches all requests to ResourceManager, FilterChainManager
13   * and EncoderManager.
14   */
15  public class FilteringRequestDispatcher implements RequestDispatcher {
16      protected static Logger log = Logger.getLogger(FilteringRequestDispatcher.class.getName());
17      protected ApplicationContext context;
18  
19      protected static final String DISPATCH_SUCCESS = " dispatched imageRequest: ";
20  
21      public FilteringRequestDispatcher() {
22      }
23  
24      /***
25       * Configuration
26       *
27       * @param context
28       */
29      public void configure(ApplicationContext context) {
30          this.context = context;
31      }
32  
33      /***
34       * Wrapper method for proxy cache. This method is horrible but it works around the proxy issue by not calling
35       * createFrom on itself but the wrapped reference it obtains from the context.
36       * @param request
37       * @throws JmageException
38       */
39      public void dispatch(ImageRequest request) throws JmageException {
40          RequestDispatcher dispatcher = context.obtainRequestDispatcher();
41          dispatcher.configure(context);
42          request.setEncoded(dispatcher.createFrom(request));
43          context.releaseRequestDispatcher(dispatcher);
44      }
45  
46      public byte[] createFrom(ImageRequest imageRequest) throws JmageException {
47          //1) load image resource using ResourceManager
48          ResourceManager resourceManager = context.obtainResourceManager();
49          resourceManager.handle(imageRequest);
50          context.releaseResourceManager(resourceManager);
51  
52          //2) filter image through FilterChainManager
53          FilterChainManager filterChainManager = context.obtainFilterChainManager();
54          filterChainManager.handle(imageRequest);
55          context.releaseFilterChainManager(filterChainManager);
56  
57          //3) encoder results using EncoderManager
58          EncoderManager encoderManager = context.obtainEncoderManager();
59          encoderManager.handle(imageRequest);
60          context.releaseEncoderManager(encoderManager);
61  
62          if (log.isInfoEnabled()) log.info(DISPATCH_SUCCESS + imageRequest.toString());
63          return imageRequest.getEncoded();
64      }
65  
66      public String toString() {
67          return "[" + this.getClass().getName() + "#" + this.hashCode() + "]";
68      }
69  }