001
014
015 package com.liferay.portlet.admin.action;
016
017 import com.liferay.portal.kernel.util.ContentTypes;
018 import com.liferay.portal.kernel.util.ParamUtil;
019 import com.liferay.portal.kernel.util.StringBundler;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.struts.PortletAction;
022 import com.liferay.portal.theme.ThemeDisplay;
023 import com.liferay.portal.util.WebKeys;
024
025 import java.awt.BasicStroke;
026 import java.awt.Color;
027 import java.awt.Font;
028
029 import java.io.OutputStream;
030
031 import javax.portlet.PortletConfig;
032 import javax.portlet.ResourceRequest;
033 import javax.portlet.ResourceResponse;
034
035 import org.apache.struts.action.ActionForm;
036 import org.apache.struts.action.ActionMapping;
037
038 import org.jfree.chart.ChartUtilities;
039 import org.jfree.chart.JFreeChart;
040 import org.jfree.chart.plot.DialShape;
041 import org.jfree.chart.plot.MeterInterval;
042 import org.jfree.chart.plot.MeterPlot;
043 import org.jfree.data.Range;
044 import org.jfree.data.general.DefaultValueDataset;
045 import org.jfree.data.general.ValueDataset;
046
047
050 public class ViewChartAction extends PortletAction {
051
052 @Override
053 public void serveResource(
054 ActionMapping actionMapping, ActionForm actionForm,
055 PortletConfig portletConfig, ResourceRequest resourceRequest,
056 ResourceResponse resourceResponse)
057 throws Exception {
058
059 ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
060 WebKeys.THEME_DISPLAY);
061
062 String type = ParamUtil.getString(resourceRequest, "type", "max");
063 long maxMemory = ParamUtil.getLong(resourceRequest, "maxMemory");
064 long totalMemory = ParamUtil.getLong(resourceRequest, "totalMemory");
065 long usedMemory = ParamUtil.getLong(resourceRequest, "usedMemory");
066
067 ValueDataset valueDataset = null;
068
069 StringBundler sb = new StringBundler(5);
070
071 sb.append(themeDisplay.translate("used-memory"));
072 sb.append(StringPool.SPACE);
073 sb.append(StringPool.FORWARD_SLASH);
074 sb.append(StringPool.SPACE);
075
076 if (type.equals("total")) {
077 valueDataset = new DefaultValueDataset(
078 (usedMemory * 100) / totalMemory);
079
080 sb.append(themeDisplay.translate("total-memory"));
081 }
082 else {
083 valueDataset = new DefaultValueDataset(
084 (usedMemory * 100) / maxMemory);
085
086 sb.append(themeDisplay.translate("maximum-memory"));
087 }
088
089 MeterPlot meterPlot = getMeterPlot(themeDisplay, valueDataset);
090
091 JFreeChart jFreeChart = getJFreeChart(sb.toString(), meterPlot);
092
093 resourceResponse.setContentType(ContentTypes.IMAGE_JPEG);
094
095 OutputStream outputStream = resourceResponse.getPortletOutputStream();
096
097 ChartUtilities.writeChartAsPNG(outputStream, jFreeChart, 280, 180);
098 }
099
100 protected JFreeChart getJFreeChart(String title, MeterPlot meterPlot) {
101 JFreeChart jFreeChart = new JFreeChart(
102 title, new Font(null, Font.PLAIN, 13), meterPlot, true);
103
104 jFreeChart.removeLegend();
105 jFreeChart.setBackgroundPaint(Color.white);
106
107 return jFreeChart;
108 }
109
110 protected MeterPlot getMeterPlot(
111 ThemeDisplay themeDisplay, ValueDataset valueDataset) {
112
113 MeterPlot meterPlot = new MeterPlot(valueDataset);
114
115 meterPlot.addInterval(
116 new MeterInterval(
117 themeDisplay.translate("normal"), new Range(0.0D, 75D),
118 Color.lightGray, new BasicStroke(2.0F),
119 new Color(0, 255, 0, 64)));
120
121 meterPlot.addInterval(
122 new MeterInterval(
123 themeDisplay.translate("warning"), new Range(75D, 90D),
124 Color.lightGray, new BasicStroke(2.0F),
125 new Color(255, 255, 0, 64)));
126
127 meterPlot.addInterval(
128 new MeterInterval(
129 themeDisplay.translate("critical"), new Range(90D, 100D),
130 Color.lightGray, new BasicStroke(2.0F),
131 new Color(255, 0, 0, 128)));
132
133 meterPlot.setDialBackgroundPaint(Color.white);
134 meterPlot.setDialShape(DialShape.PIE);
135 meterPlot.setDialOutlinePaint(Color.gray);
136 meterPlot.setTickLabelFont(new Font(null, Font.PLAIN, 10));
137 meterPlot.setTickLabelPaint(Color.darkGray);
138 meterPlot.setTickLabelsVisible(true);
139 meterPlot.setTickPaint(Color.lightGray);
140 meterPlot.setTickSize(5D);
141 meterPlot.setMeterAngle(180);
142 meterPlot.setNeedlePaint(Color.darkGray);
143 meterPlot.setRange(new Range(0.0D, 100D));
144 meterPlot.setValueFont(new Font(null, Font.PLAIN, 10));
145 meterPlot.setValuePaint(Color.black);
146 meterPlot.setUnits("%");
147
148 return meterPlot;
149 }
150
151 @Override
152 protected boolean isCheckMethodOnProcessAction() {
153 return _CHECK_METHOD_ON_PROCESS_ACTION;
154 }
155
156 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
157
158 }