001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ServiceAssemblyStatistics.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.system;
030:
031: import java.util.Date;
032: import java.util.HashMap;
033: import java.util.Map;
034: import java.util.List;
035: import java.util.ArrayList;
036: import java.util.logging.Logger;
037: import java.util.Set;
038: import javax.management.openmbean.CompositeData;
039: import javax.management.openmbean.CompositeDataSupport;
040: import javax.management.openmbean.CompositeType;
041: import javax.management.openmbean.OpenDataException;
042: import javax.management.openmbean.OpenType;
043: import javax.management.openmbean.SimpleType;
044: import javax.management.openmbean.ArrayType;
045:
046: /**
047: * This class is used to store statistic information about a SA
048: * @author Sun Microsystems, Inc.
049: */
050: public class ServiceAssemblyStatistics {
051: /**
052: * name of the sa
053: */
054: private String mName;
055:
056: /**
057: * instance name
058: */
059: private String mInstanceName;
060:
061: /**
062: * time this SA was started last
063: */
064: private Date mLastStartupTime;
065:
066: /**
067: * time this SA was stopped last
068: */
069: private Date mLastStopTime;
070:
071: /**
072: * time this SA was shutdown last
073: */
074: private Date mLastShutdownTime;
075:
076: /**
077: * SA startup times
078: */
079: private Value mStartupTime = new Value();
080:
081: /**
082: * SA stop times
083: */
084: private Value mStopTime = new Value();
085:
086: /**
087: * SA shutdown times
088: */
089: private Value mShutdownTime = new Value();
090:
091: /**
092: * CompositeType for SU Stats
093: */
094: private CompositeType mServiceUnitStatsType;
095:
096: /**
097: * CompositeType for SA Stats
098: */
099: private CompositeType mServiceAssemblyStatsType;
100:
101: /**
102: * a list of SU stats
103: */
104: private Map<String, ServiceUnitStatistics> mServiceUnitStatistics = new HashMap<String, ServiceUnitStatistics>();
105:
106: /**
107: * constructor
108: */
109: ServiceAssemblyStatistics(String saName, String instanceName)
110: throws Exception {
111: mName = saName;
112: mInstanceName = instanceName;
113: createCompositeTypes();
114: }
115:
116: /**
117: * get last startup time
118: * @return Date last startup time
119: */
120: public Date getLastStartupTime() {
121: return mLastStartupTime;
122: }
123:
124: /**
125: * get last stop time
126: * @return Date last stop time
127: */
128: public Date getLastStopTime() {
129: return mLastStopTime;
130: }
131:
132: /**
133: * get last shutdown time
134: * @return Date last stop time
135: */
136: public Date getLastShutdownTime() {
137: return mLastShutdownTime;
138: }
139:
140: /**
141: * get startup times
142: * @return Value startup times
143: */
144: public Value getStartupTime() {
145: return mStartupTime;
146: }
147:
148: /**
149: * get stop times
150: * @return Value stop times
151: */
152: public Value getStopTime() {
153: return mStopTime;
154: }
155:
156: /**
157: * get shutdown times
158: * @return Value shutdown times
159: */
160: public Value getShutdownTime() {
161: return mShutdownTime;
162: }
163:
164: /**
165: * get the name of the su
166: */
167: public String getName() {
168: return mName;
169: }
170:
171: /**
172: * get the instance name
173: * @return String the instance name
174: */
175: public String getInstanceName() {
176: return mInstanceName;
177: }
178:
179: /**
180: * set the su list
181: * @param serviceUnitStats su stats object list
182: */
183: public void setServiceUnitList(
184: Map<String, ServiceUnitStatistics> serviceUnitStats) {
185: mServiceUnitStatistics = serviceUnitStats;
186: }
187:
188: /**
189: * this method is used to update the statistics of a SA
190: * @param beginTime time when the start operation was begun
191: * @param endTime time when the start operation finished
192: * @param suTimes map of suNames and their startupTimes
193: */
194: public void updateStartupStatistics(Date beginTime, Date endTime,
195: Map<String, Long> suTimes) {
196: mLastStartupTime = endTime;
197: mStartupTime.addSample(endTime.getTime() - beginTime.getTime());
198:
199: Set<String> serviceUnits = mServiceUnitStatistics.keySet();
200: for (String su : serviceUnits) {
201: if (suTimes.containsKey(su)) {
202: mServiceUnitStatistics.get(su).updateStartupStatistics(
203: suTimes.get(su));
204: }
205: }
206: }
207:
208: /**
209: * this method is used to update the statistics of a SA
210: * @param beginTime time when the stop operation was begun
211: * @param endTime time when the stop operation finished
212: * @param suTimes map of suNames and their stopTimes
213: */
214: public void updateStopStatistics(Date beginTime, Date endTime,
215: Map<String, Long> suTimes) {
216: mLastStopTime = endTime;
217: mStopTime.addSample(endTime.getTime() - beginTime.getTime());
218:
219: Set<String> serviceUnits = mServiceUnitStatistics.keySet();
220: for (String su : serviceUnits) {
221: if (suTimes.containsKey(su)) {
222: mServiceUnitStatistics.get(su).updateStopStatistics(
223: suTimes.get(su));
224: }
225: }
226: }
227:
228: /**
229: * this method is used to update the statistics of a SA
230: * @param beginTime time when the shutdown operation was begun
231: * @param endTime time when the shutdown operation finished
232: * @param suTimes map of suNames and their shutdownTimes
233: */
234: public void updateShutdownStatistics(Date beginTime, Date endTime,
235: Map<String, Long> suTimes) {
236: mLastShutdownTime = endTime;
237: mStopTime.addSample(endTime.getTime() - beginTime.getTime());
238:
239: Set<String> serviceUnits = mServiceUnitStatistics.keySet();
240: for (String su : serviceUnits) {
241: if (suTimes.containsKey(su)) {
242: mServiceUnitStatistics.get(su).updateStopStatistics(
243: suTimes.get(su));
244: }
245: }
246: }
247:
248: /**
249: * return the stats as composite data
250: */
251: public CompositeData getCompositeData() throws OpenDataException {
252:
253: Set<String> serviceUnits = mServiceUnitStatistics.keySet();
254: CompositeData[] serviceUnitStats = new CompositeData[serviceUnits
255: .size()];
256: int i = 0;
257:
258: for (String su : serviceUnits) {
259: serviceUnitStats[i++] = mServiceUnitStatistics.get(su)
260: .getCompositeData();
261: }
262: long upTime = 0;
263: if (getLastStartupTime() != null) {
264: upTime = System.currentTimeMillis()
265: - getLastStartupTime().getTime();
266: }
267:
268: Object values[] = { getInstanceName(), getName(), upTime,
269: getLastStartupTime(),
270: (long) getStartupTime().getAverage(),
271: (long) getStopTime().getAverage(),
272: (long) getShutdownTime().getAverage(), serviceUnitStats };
273: return new CompositeDataSupport(mServiceAssemblyStatsType,
274: SA_STATS_ITEMS, values);
275: }
276:
277: class ServiceUnitStatistics {
278: /**
279: * su name
280: */
281: private String mName;
282:
283: /**
284: * SA startup times
285: */
286: private Value mStartupTime = new Value();
287:
288: /**
289: * SA stop times
290: */
291: private Value mStopTime = new Value();
292:
293: /**
294: * SA shutdown times
295: */
296: private Value mShutdownTime = new Value();
297:
298: /**
299: * list of endpoints
300: */
301: private List mEndpoints = new ArrayList();
302:
303: /**
304: * constructor
305: */
306: ServiceUnitStatistics(String suName) {
307: mName = suName;
308: }
309:
310: /**
311: * get startup times
312: * @return Value startup times
313: */
314: public Value getStartupTime() {
315: return mStartupTime;
316: }
317:
318: /**
319: * get stop times
320: * @return Value stop times
321: */
322: public Value getStopTime() {
323: return mStopTime;
324: }
325:
326: /**
327: * get shutdown times
328: * @return Value shutdown times
329: */
330: public Value getShutdownTime() {
331: return mShutdownTime;
332: }
333:
334: /**
335: * get the name of the su
336: */
337: public String getName() {
338: return mName;
339: }
340:
341: /**
342: * sets the list of endpoints in this SU
343: * @param endpointList endpoints
344: */
345: public void setEndpointsList(List<String> endpoints) {
346: mEndpoints = endpoints;
347: }
348:
349: /**
350: * This method is used to get a list of the endpoints
351: * in this SU
352: */
353: public List<String> getEndpointsList() {
354: return mEndpoints;
355: }
356:
357: /**
358: * This method is used to update the startup statistics
359: * for this SU.
360: * @param startupTime startuptime
361: */
362: public void updateStartupStatistics(Long startupTime) {
363: mStartupTime.addSample(startupTime.longValue());
364: }
365:
366: /**
367: * This method is used to update the stop time statistics
368: * for this SU.
369: * @param stopTime stopTime
370: */
371: public void updateStopStatistics(Long stopTime) {
372: mStopTime.addSample(stopTime.longValue());
373: }
374:
375: /**
376: * This method is used to update the startup statistics
377: * for this SU.
378: * @param shutdownTime shutdownTime
379: */
380: public void updateShutdownStatistics(Long shutdownTime) {
381: mShutdownTime.addSample(shutdownTime.longValue());
382: }
383:
384: /**
385: * This method is used to obtain a CompositeData represenation
386: * of the collected statistics
387: * @return CompositeData
388: */
389: public CompositeData getCompositeData()
390: throws OpenDataException {
391:
392: Object values[] = new Object[] { getName(),
393: (long) getStartupTime().getAverage(),
394: (long) getStopTime().getAverage(),
395: (long) getShutdownTime().getAverage() };
396: return new CompositeDataSupport(mServiceUnitStatsType,
397: SU_STATS_ITEMS, values);
398:
399: }
400: }
401:
402: class Value {
403:
404: /**
405: * sum of all the values
406: */
407: private double sum;
408:
409: /**
410: * squared sum
411: */
412: private double squaredsum;
413:
414: /**
415: * min value
416: */
417: private long min;
418:
419: /**
420: * max value
421: */
422: private long max;
423:
424: /**
425: * count
426: */
427: private long count;
428:
429: /** reset value */
430: public void zero() {
431: sum = 0.0;
432: squaredsum = 0.0;
433: min = 0;
434: max = 0;
435: count = 0;
436: }
437:
438: /**
439: * add a sample
440: * @param sample
441: */
442: public void addSample(long sample) {
443: if (sample < min || count == 0) {
444: min = sample;
445: }
446: if (sample > max || count == 0) {
447: max = sample;
448: }
449: count++;
450: sum += sample;
451: squaredsum += ((double) sample) * ((double) sample);
452: }
453:
454: /**
455: * get the average
456: * @return double the average
457: */
458: public double getAverage() {
459: return (sum / count);
460: }
461:
462: /**
463: * get the minimum value
464: * @return long min value
465: */
466: public long getMin() {
467: return (min);
468: }
469:
470: /**
471: * get the maximum value
472: * @return long max value
473: */
474: public long getMax() {
475: return (max);
476: }
477:
478: /**
479: * get the number of values
480: * @return long count
481: */
482: public long getCount() {
483: return (count);
484: }
485:
486: /**
487: * a stirng representation
488: */
489: public String toString() {
490: StringBuilder sb = new StringBuilder();
491:
492: sb.append("Count(" + count + ")");
493: sb.append("Min(" + min + ")");
494: sb.append("Avg(" + getAverage() + ")");
495: sb.append("Max(" + max + ")");
496:
497: return (sb.toString());
498: }
499:
500: }
501:
502: /**
503: * This is a helper method that is used to create the
504: * composite types for SA stats and SU stats
505: */
506: private void createCompositeTypes() throws OpenDataException {
507:
508: try {
509: OpenType[] SU_STATS_ITEM_TYPES = new OpenType[] {
510: SimpleType.STRING, SimpleType.LONG,
511: SimpleType.LONG, SimpleType.LONG };
512:
513: //CompositeType for Service Unit stats
514: mServiceUnitStatsType = new CompositeType(
515: SU_STATISTICS_ITEM_NAME,
516: SU_STATISTICS_ITEM_DESCRIPTION, SU_STATS_ITEMS,
517: SU_STATS_DESCRIPTIONS, SU_STATS_ITEM_TYPES);
518:
519: //Item types for SA stats
520: OpenType[] SA_STATS_TYPES = new OpenType[] {
521: SimpleType.STRING, SimpleType.STRING,
522: SimpleType.LONG, SimpleType.DATE, SimpleType.LONG,
523: SimpleType.LONG, SimpleType.LONG,
524: new ArrayType(1, mServiceUnitStatsType) };
525:
526: //CompositeType for SA stats
527: mServiceAssemblyStatsType = new CompositeType(
528: SA_STATISTICS_ITEM_NAME,
529: SA_STATISTICS_ITEM_DESCRIPTION, SA_STATS_ITEMS,
530: SA_STATS_DESCRIPTIONS, SA_STATS_TYPES);
531: } catch (OpenDataException ode) {
532: throw ode;
533: }
534: }
535:
536: /**
537: * name for SA statistics table item
538: */
539: public static String SA_STATISTICS_ITEM_NAME = "ServiceAssemblyStatisticsItem";
540:
541: /**
542: * description for SA statistics table item
543: */
544: public static String SA_STATISTICS_ITEM_DESCRIPTION = "SA statistics for an instance";
545:
546: /**
547: * name for SU statistics table item
548: */
549: public static String SU_STATISTICS_ITEM_NAME = "ServiceUnitStatisticsItem";
550:
551: /**
552: * description for SU statistics table item
553: */
554: public static String SU_STATISTICS_ITEM_DESCRIPTION = "Service unit statistics for an instance";
555:
556: /**
557: * service unit name
558: */
559: public static String SERVICE_UNIT_NAME = "ServiceUnitName";
560:
561: /**
562: * service unit startup time
563: */
564: public static String SERVICE_UNIT_STARTUP_TIME = "ServiceUnitStartupTime Avg (ms)";
565:
566: /**
567: * service unit stop time
568: */
569: public static String SERVICE_UNIT_STOP_TIME = "ServiceUnitStopTime Avg (ms)";
570:
571: /**
572: * service unit shutdown time
573: */
574: public static String SERVICE_UNIT_SHUTDOWN_TIME = "ServiceUnitShutdownTime Avg (ms)";
575:
576: /**
577: * instance name
578: */
579: public static String INSTANCE_NAME = "InstanceName";
580:
581: /**
582: * SA Name
583: */
584: public static String SERVICE_ASSEMBLY_NAME = "ServiceAssemblyName";
585:
586: /**
587: * SA Uptime
588: */
589: public static String SERVICE_ASSEMBLY_UPTIME = "UpTime (ms)";
590:
591: /**
592: * last startup time
593: */
594: public static String SERVICE_ASSEMBLY_LAST_STARTUP_TIME = "LastStartupTime";
595:
596: /**
597: * SA startup time
598: */
599: public static final String SERVICE_ASSEMBLY_STARTUP_TIME = "StartupTime Avg (ms)";
600:
601: /**
602: * SA stop time
603: */
604: public static final String SERVICE_ASSEMBLY_STOP_TIME = "StopTime Avg (ms)";
605:
606: /**
607: * SA Shutdown time
608: */
609: public static final String SERVICE_ASSEMBLY_SHUTDOWN_TIME = "ShutdownTime Avg (ms)";
610:
611: /**
612: * SA list of SU statistics
613: */
614: public static final String SERVICE_ASSEMBLY_SU_STATISTICS = "ServiceUnitStatistics";
615:
616: /**
617: * Service unit name description
618: */
619: public static final String SERVICE_UNIT_NAME_DESCRIPTION = "Name of the service unit";
620:
621: /**
622: * Average of service unit startup times in ms.
623: */
624: public static final String SERVICE_UNIT_STARTUP_TIME_DESCRIPTION = "Service Unit StartupTime Avg (ms)";
625:
626: /**
627: * Average of service unit stop times in ms.
628: */
629: public static final String SERVICE_UNIT_STOP_TIME_DESCRIPTION = "Service Unit StopTime Avg (ms)";
630:
631: /**
632: * Average of service unit shutdown time
633: */
634: public static final String SERVICE_UNIT_SHUTDOWN_TIME_DESCRIPTION = "Service Unit ShutdownTime Avg (ms)";
635:
636: /**
637: * instance name description
638: */
639: public static final String JBI_INSTANCE_NAME_DESCRIPTION = "JBI Instance Name";
640:
641: /**
642: * SA name description
643: */
644: public static final String SERVICE_ASSEMBLY_NAME_DESCRIPTION = "Service Assembly Name";
645:
646: /**
647: * SA uptime desctiption
648: */
649: public static final String SERVICE_ASSEMBLY_UPTIME_DESCRIPTION = "Uptime of service assembly in ms.";
650: /**
651: * SA startup time description
652: */
653: public static final String SERVICE_ASSEMBLY_LAST_STARTUP_TIME_DESCRIPTION = "Last Startup Time";
654:
655: /**
656: * SA startup time - description
657: */
658: public static final String SERVICE_ASSEMBLY_STARTUP_TIME_DESCRIPTION = "Startup Time Avg (ms)";
659:
660: /**
661: * SA stop time - description
662: */
663: public static final String SERVICE_ASSEMBLY_STOP_TIME_DESCRIPTION = "Stop Time Avg (ms)";
664:
665: /**
666: * SA shutdown time - description
667: */
668: public static final String SERVICE_ASSEMBLY_SHUTDOWN_TIME_DESCTIPTION = "Shutdown Time Avg (ms)";
669:
670: /**
671: * SU statistics list - description
672: */
673: public static final String SERVICE_ASSEMBLY_SU_STATISTICS_DESCRIPTION = "ServiceUnit Statistics";
674:
675: /**
676: * item names for service unit stats
677: */
678: private static String[] SU_STATS_ITEMS = { SERVICE_UNIT_NAME,
679: SERVICE_UNIT_STARTUP_TIME, SERVICE_UNIT_STOP_TIME,
680: SERVICE_UNIT_SHUTDOWN_TIME
681:
682: };
683:
684: /**
685: * descriptions for service unit stats
686: */
687: private static String[] SU_STATS_DESCRIPTIONS = {
688: SERVICE_UNIT_NAME_DESCRIPTION,
689: SERVICE_UNIT_STARTUP_TIME_DESCRIPTION,
690: SERVICE_UNIT_STOP_TIME_DESCRIPTION,
691: SERVICE_UNIT_SHUTDOWN_TIME_DESCRIPTION };
692:
693: /**
694: * item names for SA stats
695: */
696: private static String[] SA_STATS_ITEMS = { INSTANCE_NAME,
697: SERVICE_ASSEMBLY_NAME, SERVICE_ASSEMBLY_UPTIME,
698: SERVICE_ASSEMBLY_LAST_STARTUP_TIME,
699: SERVICE_ASSEMBLY_STARTUP_TIME, SERVICE_ASSEMBLY_STOP_TIME,
700: SERVICE_ASSEMBLY_SHUTDOWN_TIME,
701: SERVICE_ASSEMBLY_SU_STATISTICS };
702:
703: /**
704: * item descriptions for SA stats
705: */
706: private static String[] SA_STATS_DESCRIPTIONS = {
707: JBI_INSTANCE_NAME_DESCRIPTION,
708: SERVICE_ASSEMBLY_NAME_DESCRIPTION,
709: SERVICE_ASSEMBLY_UPTIME_DESCRIPTION,
710: SERVICE_ASSEMBLY_LAST_STARTUP_TIME_DESCRIPTION,
711: SERVICE_ASSEMBLY_STARTUP_TIME_DESCRIPTION,
712: SERVICE_ASSEMBLY_STOP_TIME_DESCRIPTION,
713: SERVICE_ASSEMBLY_SHUTDOWN_TIME_DESCTIPTION,
714: SERVICE_ASSEMBLY_SU_STATISTICS_DESCRIPTION };
715:
716: }
|