1 package org.jmage.encoder;
2
3 import org.apache.log4j.Logger;
4 import org.apache.log4j.Priority;
5 import org.jmage.ApplicationContext;
6 import org.jmage.ImageRequest;
7 import org.jmage.JmageException;
8 import org.jmage.RequestHandler;
9
10 import java.util.Iterator;
11 import java.util.List;
12
13 /***
14 * Manages ImageEncoders.
15 */
16 public class EncoderManager implements RequestHandler {
17 protected List imageEncoders;
18 protected static Logger log = Logger.getLogger(EncoderManager.class.getName());
19 private static final String MISSING_ENCODERS = "error, no ImageEncoders configured";
20 private static final String ENCODER_ERROR = "unable to encode image, none of the configured ImageEncoders was able to handle: ";
21
22 protected EncoderManager() {
23
24 }
25
26 /***
27 * Creates a EncoderManager for an array of given ImageEncoders
28 *
29 * @param imageEncoders
30 */
31 public EncoderManager(List imageEncoders) {
32 this.imageEncoders = imageEncoders;
33 }
34
35 /***
36 * Configure encoder properties
37 *
38 * @param context
39 */
40 public void configure(ApplicationContext context) {
41
42 }
43
44 /***
45 * Encodes a JAI PlanarImage into a specific format
46 *
47 * @param request the ImageRequest
48 * @throws JmageException if the image couldn't be encoded or the format isn't supported.
49 */
50 public void handle(ImageRequest request) throws JmageException {
51 Iterator it = imageEncoders.iterator();
52 assert (imageEncoders.size() > 0) : MISSING_ENCODERS;
53 byte[] encoded = null;
54
55 while (it.hasNext()) {
56 ImageEncoder encoder = (ImageEncoder) it.next();
57 if (encoder.canHandle(request)) {
58 try {
59 encoder.initialize(request);
60 encoded = encoder.createFrom(request);
61 break;
62 } catch (CodecException e) {
63 throw new JmageException(e.getMessage());
64 }
65 }
66 }
67
68 if (encoded == null) {
69 String message = ENCODER_ERROR + request.getEncodingFormat();
70 if (log.isEnabledFor(Priority.ERROR)) log.error(message);
71 throw new JmageException(message);
72 }
73
74 request.setEncoded(encoded);
75 }
76
77 /***
78 * Test whether the ImageEncoder can handle the format
79 *
80 * @param request ImageRequest
81 * @return [true | false]
82 */
83 public boolean canHandle(ImageRequest request) {
84 boolean rval = false;
85
86 Iterator it = imageEncoders.iterator();
87 assert (imageEncoders.size() > 0) : MISSING_ENCODERS;
88 while (it.hasNext()) {
89 ImageEncoder encoder = (ImageEncoder) it.next();
90 rval = rval || encoder.canHandle(request);
91 }
92
93 return rval;
94 }
95
96 /***
97 * Initialize the ImageEncoder for a particular encodingformat using Properties
98 *
99 * @param request
100 */
101 public void initialize(ImageRequest request) throws CodecException {
102 Iterator it = imageEncoders.iterator();
103 assert (imageEncoders.size() > 0) : MISSING_ENCODERS;
104 while (it.hasNext()) {
105 ImageEncoder encoder = (ImageEncoder) it.next();
106 if (encoder.canHandle(request)) {
107 encoder.initialize(request);
108 }
109 }
110 }
111
112 public String toString() {
113 return "[" + this.getClass().getName() + "#" + this.hashCode() + "]";
114 }
115 }