01: package org.cougaar.demo.mandelbrot.v2;
02:
03: import java.io.IOException;
04: import java.io.OutputStream;
05: import javax.servlet.ServletException;
06: import javax.servlet.http.HttpServletRequest;
07: import javax.servlet.http.HttpServletResponse;
08: import org.cougaar.core.servlet.ComponentServlet;
09: import org.cougaar.demo.mandelbrot.util.Arguments;
10: import org.cougaar.demo.mandelbrot.util.ImageOutput;
11:
12: /**
13: * This servlet uses the {@link FractalService} to calculate the image data.
14: */
15: public class FractalServlet extends ComponentServlet {
16:
17: private FractalService svc;
18:
19: public void load() {
20: // get the FractalService
21: svc = (FractalService) getServiceBroker().getService(this ,
22: FractalService.class, null);
23: if (svc == null) {
24: throw new RuntimeException(
25: "Unable to obtain the FractalService");
26: }
27:
28: super .load();
29: }
30:
31: public void unload() {
32: // shutting down, release the FractalService
33: if (svc != null) {
34: getServiceBroker().releaseService(this ,
35: FractalService.class, svc);
36: svc = null;
37: }
38:
39: super .unload();
40: }
41:
42: protected void doGet(HttpServletRequest req, HttpServletResponse res)
43: throws ServletException, IOException {
44:
45: // parse the URL parameters
46: Arguments args = new Arguments(req.getParameterMap());
47:
48: // calculate the image data
49: byte[] data = svc.calculate(args.getInt("width"), args
50: .getInt("height"), args.getDouble("x_min"), args
51: .getDouble("x_max"), args.getDouble("y_min"), args
52: .getDouble("y_max"));
53:
54: // write the data as a JPEG
55: res.setContentType("image/jpeg");
56: OutputStream out = res.getOutputStream();
57: ImageOutput.writeJPG(args.getInt("width"), args
58: .getInt("height"), data, out);
59: out.close();
60: }
61: }
|