KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > workbench > chart > ChartService


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.workbench.chart;
16
17 import java.io.IOException JavaDoc;
18 import java.io.OutputStream JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
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 /**
37  * ServiceLink that works with a {@link Chart}to dynamically render a chart as a JPEG. This is a
38  * very limited implementation; a full version would include features such as setting the size of
39  * the image, and more flexibility in defining where the {@link Chart}instance is obtained from.
40  *
41  * @author Howard Lewis Ship
42  * @since 1.0.10
43  */

44
45 public class ChartService implements IEngineService
46 {
47     public static final String JavaDoc SERVICE_NAME = "chart";
48
49     /** @since 4.0 */
50     private RequestExceptionReporter _exceptionReporter;
51
52     /** @since 4.0 */
53     private LinkFactory _linkFactory;
54
55     /** @since 4.0 */
56     private WebResponse _response;
57
58     public ILink getLink(IRequestCycle cycle, Object JavaDoc parameter)
59     {
60         Defense.isAssignable(parameter, IComponent.class, "parameter");
61
62         IComponent component = (IComponent) parameter;
63
64         Map JavaDoc parameters = new HashMap JavaDoc();
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 JavaDoc
74     {
75         String JavaDoc pageName = cycle.getParameter(ServiceConstants.PAGE);
76         String JavaDoc 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 JavaDoc output = _response.getOutputStream(new ContentType("image/jpeg"));
88
89             // I've seen a few bits of wierdness (including a JVM crash) inside this code.
90
// Hopefully, its a multi-threading problem that can be resolved
91
// by synchronizing.
92

93             synchronized (this)
94             {
95                 JPEGEncoder13.encode(chart, 1.0f, output);
96             }
97         }
98         catch (ClassCastException JavaDoc ex)
99         {
100             _exceptionReporter.reportRequestException("Component " + component.getExtendedId()
101                     + " does not implement IChartProvider.", ex);
102
103             return;
104         }
105         catch (Throwable JavaDoc ex)
106         {
107             _exceptionReporter.reportRequestException("Error creating JPEG stream.", ex);
108
109             return;
110         }
111
112         return;
113     }
114
115     public String JavaDoc getName()
116     {
117         return SERVICE_NAME;
118     }
119
120     /** @since 4.0 */
121     public void setExceptionReporter(RequestExceptionReporter exceptionReporter)
122     {
123         _exceptionReporter = exceptionReporter;
124     }
125
126     /** @since 4.0 */
127     public void setLinkFactory(LinkFactory linkFactory)
128     {
129         _linkFactory = linkFactory;
130     }
131
132     /** @since 4.0 */
133     public void setResponse(WebResponse response)
134     {
135         _response = response;
136     }
137 }
Popular Tags