File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\Graph.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import org.jfree.chart.JFreeChart;
27 import org.jfree.chart.ChartRenderingInfo;
28 import org.jfree.chart.ChartUtilities;
29 import org.kohsuke.stapler.StaplerRequest;
30 import org.kohsuke.stapler.StaplerResponse;
31
32 import javax.servlet.ServletOutputStream;
33 import javax.imageio.ImageIO;
34 import java.io.IOException;
35 import java.util.Calendar;
36 import java.awt.image.BufferedImage;
37 import java.awt.*;
38
39 /**
40 * A JFreeChart-generated graph that's bound to UI.
41 *
42 * <p>
43 * This object exposes two URLs:
44 * <dl>
45 * <dt>/png
46 * <dd>PNG image of a graph
47 *
48 * <dt>/map
49 * <dd>Clickable map
50 * </dl>
51 *
52 * @author Kohsuke Kawaguchi
53 * @since 1.320
54 */
55 public abstract class Graph {
56 private final long timestamp;
57 private final int defaultW;
58 private final int defaultH;
59 private volatile JFreeChart graph;
60
61 /**
62 * @param timestamp
63 * Timestamp of this graph. Used for HTTP cache related headers.
64 * If the graph doesn't have any timestamp to tie it to, pass -1.
65 */
66 protected Graph(long timestamp, int defaultW, int defaultH) {
67 this.timestamp = timestamp;
68 this.defaultW = defaultW;
69 this.defaultH = defaultH;
70 }
71
72 protected Graph(Calendar timestamp, int defaultW, int defaultH) {
73 this(timestamp.getTimeInMillis(),defaultW,defaultH);
74 }
75
76 /**
77 * Actually render a chart.
78 */
79 protected abstract JFreeChart createGraph();
80
81 private BufferedImage render(StaplerRequest req, ChartRenderingInfo info) {
82 String w = req.getParameter("width");
83 if(w==null) w=String.valueOf(defaultW);
84 String h = req.getParameter("height");
85 if(h==null) h=String.valueOf(defaultH);
86
87 if (graph==null) graph = createGraph();
88 return graph.createBufferedImage(Integer.parseInt(w),Integer.parseInt(h),info);
89 }
90
91 /**
92 * Renders a graph.
93 */
94 public void doPng(StaplerRequest req, StaplerResponse rsp) throws IOException {
95 if (req.checkIfModified(timestamp, rsp)) return;
96
97 try {
98 BufferedImage image = render(req,null);
99 rsp.setContentType("image/png");
100 ServletOutputStream os = rsp.getOutputStream();
101 ImageIO.write(image, "PNG", os);
102 os.close();
103 } catch(Error e) {
104 /* OpenJDK on ARM produces an error like this in case of headless error
105 Caused by: java.lang.Error: Probable fatal error:No fonts found.
106 at sun.font.FontManager.getDefaultPhysicalFont(FontManager.java:1088)
107 at sun.font.FontManager.initialiseDeferredFont(FontManager.java:967)
108 at sun.font.CompositeFont.doDeferredInitialisation(CompositeFont.java:254)
109 at sun.font.CompositeFont.getSlotFont(CompositeFont.java:334)
110 at sun.font.CompositeStrike.getStrikeForSlot(CompositeStrike.java:77)
111 at sun.font.CompositeStrike.getFontMetrics(CompositeStrike.java:93)
112 at sun.font.Font2D.getFontMetrics(Font2D.java:387)
113 at java.awt.Font.defaultLineMetrics(Font.java:2082)
114 at java.awt.Font.getLineMetrics(Font.java:2152)
115 at org.jfree.chart.axis.NumberAxis.estimateMaximumTickLabelHeight(NumberAxis.java:974)
116 at org.jfree.chart.axis.NumberAxis.selectVerticalAutoTickUnit(NumberAxis.java:1104)
117 at org.jfree.chart.axis.NumberAxis.selectAutoTickUnit(NumberAxis.java:1048)
118 at org.jfree.chart.axis.NumberAxis.refreshTicksVertical(NumberAxis.java:1249)
119 at org.jfree.chart.axis.NumberAxis.refreshTicks(NumberAxis.java:1149)
120 at org.jfree.chart.axis.ValueAxis.reserveSpace(ValueAxis.java:788)
121 at org.jfree.chart.plot.CategoryPlot.calculateRangeAxisSpace(CategoryPlot.java:2650)
122 at org.jfree.chart.plot.CategoryPlot.calculateAxisSpace(CategoryPlot.java:2669)
123 at org.jfree.chart.plot.CategoryPlot.draw(CategoryPlot.java:2716)
124 at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1222)
125 at org.jfree.chart.JFreeChart.createBufferedImage(JFreeChart.java:1396)
126 at org.jfree.chart.JFreeChart.createBufferedImage(JFreeChart.java:1376)
127 at org.jfree.chart.JFreeChart.createBufferedImage(JFreeChart.java:1361)
128 at hudson.util.ChartUtil.generateGraph(ChartUtil.java:116)
129 at hudson.util.ChartUtil.generateGraph(ChartUtil.java:99)
130 at hudson.tasks.test.AbstractTestResultAction.doPng(AbstractTestResultAction.java:196)
131 at hudson.tasks.test.TestResultProjectAction.doTrend(TestResultProjectAction.java:97)
132 ... 37 more
133 */
134 if(e.getMessage().contains("Probable fatal error:No fonts found")) {
135 rsp.sendRedirect2(req.getContextPath()+"/images/headless.png");
136 return;
137 }
138 throw e; // otherwise let the caller deal with it
139 } catch(HeadlessException e) {
140 // not available. send out error message
141 rsp.sendRedirect2(req.getContextPath()+"/images/headless.png");
142 }
143 }
144
145 /**
146 * Renders a clickable map.
147 */
148 public void doMap(StaplerRequest req, StaplerResponse rsp) throws IOException {
149 if (req.checkIfModified(timestamp, rsp)) return;
150
151 ChartRenderingInfo info = new ChartRenderingInfo();
152 render(req,info);
153
154 rsp.setContentType("text/plain;charset=UTF-8");
155 rsp.getWriter().println(ChartUtilities.getImageMap( "map", info ));
156 }
157 }
158