001: /*
002: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.wso2.esb.services;
018:
019: import org.apache.log4j.Logger;
020: import org.apache.synapse.SynapseConstants;
021: import org.wso2.esb.ServiceBusConstants;
022: import org.wso2.esb.ServiceBusException;
023: import org.wso2.esb.persistence.PersistenceManager;
024: import org.wso2.esb.persistence.dataobject.StatisticsDO;
025: import org.wso2.esb.services.tos.GraphData;
026: import org.wso2.esb.services.tos.StatisticsData;
027: import org.wso2.esb.statistics.persistence.StatisticsDBUtils;
028: import org.wso2.esb.util.HibernateConfigCache;
029:
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: /**
035: *
036: *
037: */
038:
039: public class ServiceAdmin extends AbstractESBAdmin {
040:
041: private static Logger log = Logger.getLogger(ServiceAdmin.class);
042:
043: private PersistenceManager pm;
044:
045: public ServiceAdmin() {
046: this .pm = new PersistenceManager(
047: HibernateConfigCache
048: .getHibernateConfig(
049: org.wso2.esb.ServiceBusConstants.WSO2ESB_HB_CONFIG_KEY,
050: System
051: .getProperty(ServiceBusConstants.ESB_HIBERNATE_CFG_XML)));
052: }
053:
054: private StatisticsData getData(int category, String serverId) {
055: StatisticsData statisticsData = new StatisticsData();
056: statisticsData.setCategory(category);
057: statisticsData.setStatisticsRecords(StatisticsDBUtils
058: .getStatisticsForServer(pm, serverId, category));
059: return statisticsData;
060:
061: }
062:
063: private StatisticsData getMessageMediationData(String serverId) {
064:
065: StatisticsData statisticsData = new StatisticsData();
066: statisticsData
067: .setCategory(SynapseConstants.PROXYSERVICE_STATISTICS);
068: statisticsData.setStatisticsRecords(StatisticsDBUtils
069: .getStatistics(pm, serverId,
070: SynapseConstants.PROXYSERVICE_STATISTICS,
071: SynapseConstants.SYNAPSE_SERVICE_NAME));
072: return statisticsData;
073: }
074:
075: private StatisticsDO[] getRecords(int category, String name) {
076: return StatisticsDBUtils.getStatistics(pm, category, name);
077: }
078:
079: private StatisticsDO listToStatisticsDO(Object[] data) {
080: StatisticsDO statisticsDO = new StatisticsDO();
081: if (data != null && data.length == 5) {
082: statisticsDO
083: .setTotalCount(data[0] != null ? ((Long) data[0])
084: .longValue() : 0);
085: statisticsDO
086: .setFaultCount(data[1] != null ? ((Long) data[1])
087: .longValue() : 0);
088: statisticsDO.setMaxTime(data[2] != null ? ((Float) data[2])
089: .longValue() : 0);
090: statisticsDO.setMinTime(data[3] != null ? ((Float) data[3])
091: .longValue() : 0);
092: statisticsDO
093: .setAvgTime(data[4] != null ? (Math
094: .round(((Double) data[4]).doubleValue() * 100.0) / 100.0)
095: : 0); // show only up to first two decimal point
096: }
097: return statisticsDO;
098: }
099:
100: private StatisticsDO mergeStatisticsDO(StatisticsDO statisticsDO1,
101: StatisticsDO statisticsDO2) {
102: if (statisticsDO1 == null && statisticsDO2 == null) {
103: return null;
104: } else if (statisticsDO2 == null) {
105: return statisticsDO1;
106: } else if (statisticsDO1 == null) {
107: return statisticsDO2;
108: } else {
109: StatisticsDO statisticsDO = new StatisticsDO();
110: statisticsDO.setTotalCount(statisticsDO1.getTotalCount()
111: + statisticsDO2.getTotalCount());
112: statisticsDO.setFaultCount(statisticsDO1.getFaultCount()
113: + statisticsDO2.getFaultCount());
114: statisticsDO
115: .setMaxTime(statisticsDO1.getMaxTime() > statisticsDO2
116: .getMaxTime() ? statisticsDO1.getMaxTime()
117: : statisticsDO2.getMaxTime());
118: statisticsDO
119: .setMinTime(statisticsDO1.getMinTime() < statisticsDO2
120: .getMinTime() ? statisticsDO1.getMinTime()
121: : statisticsDO2.getMinTime());
122: statisticsDO
123: .setAvgTime((statisticsDO1.getAvgTime() + statisticsDO2
124: .getAvgTime()) / 2);
125: return statisticsDO;
126: }
127:
128: }
129:
130: public StatisticsDO[] getSingleServerStatistics(String serverID)
131: throws ServiceBusException {
132: Object inPSStatisticsSummary = StatisticsDBUtils
133: .getSummaryStatisticsForServer(pm, serverID, 0,
134: SynapseConstants.PROXYSERVICE_STATISTICS);
135: Object outStatisticsSummary = StatisticsDBUtils
136: .getSummaryStatisticsForServer(pm, serverID, 1,
137: SynapseConstants.PROXYSERVICE_STATISTICS);
138: StatisticsDO[] statisticsDOs = null;
139: StatisticsDO inStatisticsDO = null;
140: StatisticsDO ouStatisticsDO = null;
141: if (inPSStatisticsSummary instanceof Object[]) {
142: inStatisticsDO = listToStatisticsDO((Object[]) inPSStatisticsSummary);
143: inStatisticsDO.setServerId(serverID);
144: inStatisticsDO.setDirection(0);
145:
146: }
147: if (outStatisticsSummary instanceof Object[]) {
148: ouStatisticsDO = listToStatisticsDO((Object[]) outStatisticsSummary);
149: ouStatisticsDO.setServerId(serverID);
150: ouStatisticsDO.setDirection(1);
151: }
152: if (inStatisticsDO != null && ouStatisticsDO != null) {
153: statisticsDOs = new StatisticsDO[2];
154: statisticsDOs[0] = inStatisticsDO;
155: statisticsDOs[1] = ouStatisticsDO;
156: } else if (inStatisticsDO == null && ouStatisticsDO != null) {
157: statisticsDOs = new StatisticsDO[1];
158: statisticsDOs[0] = ouStatisticsDO;
159: } else if (inStatisticsDO != null) {
160: statisticsDOs = new StatisticsDO[1];
161: statisticsDOs[0] = inStatisticsDO;
162: }
163: return statisticsDOs;
164: }
165:
166: public String echoServerId(String serverID) {
167: return serverID;
168: }
169:
170: public String[] listServers() throws ServiceBusException {
171: Object namesObject = StatisticsDBUtils.getServersIds(pm);
172: String[] names = new String[0];
173: if (namesObject instanceof List) {
174: List idObjects = (List) namesObject;
175: names = new String[idObjects.size()];
176: for (int i = 0; i < idObjects.size(); i++) {
177: names[i] = (String) idObjects.get(i);
178: }
179: } else if (namesObject != null && namesObject instanceof String) {
180: names = new String[] { (String) namesObject };
181: }
182: return names;
183: }
184:
185: public String[] listSequence() throws ServiceBusException {
186: Object namesObject = StatisticsDBUtils.getUniqueNames(pm,
187: SynapseConstants.SEQUENCE_STATISTICS);
188: String[] names = new String[0];
189: if (namesObject instanceof List) {
190: List idObjects = (List) namesObject;
191: names = new String[idObjects.size()];
192: for (int i = 0; i < idObjects.size(); i++) {
193: names[i] = (String) idObjects.get(i);
194: }
195: } else if (namesObject != null && namesObject instanceof String) {
196: names = new String[] { (String) namesObject };
197: }
198: return names;
199: }
200:
201: public String[] listProxyServices() throws ServiceBusException {
202: Object namesObject = StatisticsDBUtils.getUniqueNames(pm,
203: SynapseConstants.PROXYSERVICE_STATISTICS);
204: String[] names = new String[0];
205: if (namesObject instanceof List) {
206: List idObjects = (List) namesObject;
207: if (idObjects
208: .contains(SynapseConstants.SYNAPSE_SERVICE_NAME)) {
209: names = new String[idObjects.size() - 1];
210: } else {
211: names = new String[idObjects.size()];
212: }
213: int index = 0;
214: for (int i = 0; i < idObjects.size(); i++) {
215: String name = (String) idObjects.get(i);
216: if (!name.equals(SynapseConstants.SYNAPSE_SERVICE_NAME)) {
217: names[index] = name;
218: index++;
219: }
220: }
221: } else if (namesObject != null
222: && namesObject instanceof String
223: && !(namesObject
224: .equals(SynapseConstants.SYNAPSE_SERVICE_NAME))) {
225: names = new String[] { (String) namesObject };
226: }
227: return names;
228:
229: }
230:
231: public GraphData getDataForGraph() throws ServiceBusException {
232: GraphData graphData = new GraphData();
233: List serverData = StatisticsDBUtils.getTotalCountForServerS(pm,
234: SynapseConstants.PROXYSERVICE_STATISTICS);
235: List psData = StatisticsDBUtils.getTotalCountForCategory(pm,
236: SynapseConstants.PROXYSERVICE_STATISTICS);
237: List epData = StatisticsDBUtils.getTotalCountForCategory(pm,
238: SynapseConstants.ENDPOINT_STATISTICS);
239: List sequenceData = StatisticsDBUtils.getTotalCountForCategory(
240: pm, SynapseConstants.SEQUENCE_STATISTICS);
241: String serverDataStr = "";
242: String psDataStr = "";
243: String epDataStr = "";
244: String sequenceDataStr = "";
245:
246: for (Iterator iterator = serverData.iterator(); iterator
247: .hasNext();) {
248: Object[] row = (Object[]) iterator.next();
249: serverDataStr += row[0] + "-[" + row[1] + "]-," + row[1]
250: + ";";
251: }
252: for (Iterator iterator = psData.iterator(); iterator.hasNext();) {
253: Object[] row = (Object[]) iterator.next();
254: String name = (String) row[0];
255: if (!name.equals(SynapseConstants.SYNAPSE_SERVICE_NAME)) {
256: psDataStr += row[0] + "-[" + row[1] + "]-," + row[1]
257: + ";";
258: }
259: }
260: for (Iterator iterator = epData.iterator(); iterator.hasNext();) {
261: Object[] row = (Object[]) iterator.next();
262: epDataStr += row[0] + "-[" + row[1] + "]-," + row[1] + ";";
263: }
264: for (Iterator iterator = sequenceData.iterator(); iterator
265: .hasNext();) {
266: Object[] row = (Object[]) iterator.next();
267: sequenceDataStr += row[0] + "-[" + row[1] + "]-," + row[1]
268: + ";";
269: }
270:
271: graphData.setEndPointData(epDataStr);
272: graphData.setProxyServiceData(psDataStr);
273: graphData.setSequenceData(sequenceDataStr);
274: graphData.setServerData(serverDataStr);
275: return graphData;
276: }
277:
278: public String[] listEndPoint() throws ServiceBusException {
279: Object namesObject = StatisticsDBUtils.getUniqueNames(pm,
280: SynapseConstants.ENDPOINT_STATISTICS);
281: String[] names = new String[0];
282: if (namesObject instanceof List) {
283: List idObjects = (List) namesObject;
284: names = new String[idObjects.size()];
285: for (int i = 0; i < idObjects.size(); i++) {
286: names[i] = (String) idObjects.get(i);
287: }
288: } else if (namesObject != null && namesObject instanceof String) {
289: names = new String[] { (String) namesObject };
290: }
291: return names;
292: }
293:
294: public StatisticsData getServerProxyStatistics(String serverID)
295: throws ServiceBusException {
296: StatisticsData statisticsData = getData(
297: SynapseConstants.PROXYSERVICE_STATISTICS, serverID);
298: StatisticsDO[] resultSet = statisticsData
299: .getStatisticsRecords();
300: List resultList = new ArrayList();
301:
302: for (int i = 0; i < resultSet.length; i++) {
303: StatisticsDO statisticsDO = resultSet[i];
304: if (statisticsDO != null
305: && !(statisticsDO.getName()
306: .equals(SynapseConstants.SYNAPSE_SERVICE_NAME))) {
307: resultList.add(statisticsDO);
308: }
309: }
310: resultSet = (StatisticsDO[]) resultList
311: .toArray(new StatisticsDO[resultList.size()]);
312: statisticsData.setStatisticsRecords(resultSet);
313: return statisticsData;
314: }
315:
316: public StatisticsDO[] getProxyServiceStatistics(String proxyName)
317: throws ServiceBusException {
318: return getRecords(SynapseConstants.PROXYSERVICE_STATISTICS,
319: proxyName);
320: }
321:
322: public StatisticsData getServerSequenceStatistics(String serverID)
323: throws ServiceBusException {
324: return getData(SynapseConstants.SEQUENCE_STATISTICS, serverID);
325: }
326:
327: public StatisticsDO[] getSequenceStatistics(String sequenceName)
328: throws ServiceBusException {
329: return getRecords(SynapseConstants.SEQUENCE_STATISTICS,
330: sequenceName);
331: }
332:
333: public StatisticsData getServerEndPointStatistics(String serverID)
334: throws ServiceBusException {
335: return getData(SynapseConstants.ENDPOINT_STATISTICS, serverID);
336: }
337:
338: public StatisticsDO[] getEndPointStatistics(String endPointName)
339: throws ServiceBusException {
340: return getRecords(SynapseConstants.ENDPOINT_STATISTICS,
341: endPointName);
342: }
343:
344: public StatisticsData getServerMessageMediationStatistics(
345: String serverID) throws ServiceBusException {
346: return getMessageMediationData(serverID);
347: }
348:
349: public StatisticsDO[] getSingleStatistics(String name, int type)
350: throws ServiceBusException {
351: switch (type) {
352: case SynapseConstants.SEQUENCE_STATISTICS: {
353: return getSequenceStatistics(name);
354: }
355: case SynapseConstants.ENDPOINT_STATISTICS: {
356: return getEndPointStatistics(name);
357: }
358: case SynapseConstants.PROXYSERVICE_STATISTICS: {
359: return getProxyServiceStatistics(name);
360: }
361: default: {
362: return new StatisticsDO[1];
363: }
364: }
365: }
366:
367: public int clearAllStatistics() {
368: return StatisticsDBUtils.clearAll(pm);
369: }
370: }
|