1 15 package org.apache.tapestry.workbench.chart; 16 17 import java.io.IOException ; 18 import java.io.OutputStream ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import org.apache.hivemind.util.Defense; 23 import org.apache.tapestry.IComponent; 24 import org.apache.tapestry.IPage; 25 import org.apache.tapestry.IRequestCycle; 26 import org.apache.tapestry.engine.IEngineService; 27 import org.apache.tapestry.engine.ILink; 28 import org.apache.tapestry.error.RequestExceptionReporter; 29 import org.apache.tapestry.services.LinkFactory; 30 import org.apache.tapestry.services.ServiceConstants; 31 import org.apache.tapestry.util.ContentType; 32 import org.apache.tapestry.web.WebResponse; 33 import org.jCharts.Chart; 34 import org.jCharts.encoders.JPEGEncoder13; 35 36 44 45 public class ChartService implements IEngineService 46 { 47 public static final String SERVICE_NAME = "chart"; 48 49 50 private RequestExceptionReporter _exceptionReporter; 51 52 53 private LinkFactory _linkFactory; 54 55 56 private WebResponse _response; 57 58 public ILink getLink(IRequestCycle cycle, Object parameter) 59 { 60 Defense.isAssignable(parameter, IComponent.class, "parameter"); 61 62 IComponent component = (IComponent) parameter; 63 64 Map parameters = new HashMap (); 65 66 parameters.put(ServiceConstants.SERVICE, SERVICE_NAME); 67 parameters.put(ServiceConstants.PAGE, component.getPage().getPageName()); 68 parameters.put(ServiceConstants.COMPONENT, component.getIdPath()); 69 70 return _linkFactory.constructLink(cycle, parameters, true); 71 } 72 73 public void service(IRequestCycle cycle) throws IOException 74 { 75 String pageName = cycle.getParameter(ServiceConstants.PAGE); 76 String componentId = cycle.getParameter(ServiceConstants.COMPONENT); 77 78 IPage page = cycle.getPage(pageName); 79 IComponent component = page.getNestedComponent(componentId); 80 81 try 82 { 83 IChartProvider provider = (IChartProvider) component; 84 85 Chart chart = provider.getChart(); 86 87 OutputStream output = _response.getOutputStream(new ContentType("image/jpeg")); 88 89 93 synchronized (this) 94 { 95 JPEGEncoder13.encode(chart, 1.0f, output); 96 } 97 } 98 catch (ClassCastException ex) 99 { 100 _exceptionReporter.reportRequestException("Component " + component.getExtendedId() 101 + " does not implement IChartProvider.", ex); 102 103 return; 104 } 105 catch (Throwable ex) 106 { 107 _exceptionReporter.reportRequestException("Error creating JPEG stream.", ex); 108 109 return; 110 } 111 112 return; 113 } 114 115 public String getName() 116 { 117 return SERVICE_NAME; 118 } 119 120 121 public void setExceptionReporter(RequestExceptionReporter exceptionReporter) 122 { 123 _exceptionReporter = exceptionReporter; 124 } 125 126 127 public void setLinkFactory(LinkFactory linkFactory) 128 { 129 _linkFactory = linkFactory; 130 } 131 132 133 public void setResponse(WebResponse response) 134 { 135 _response = response; 136 } 137 } | Popular Tags |