001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.monitoring;
006:
007: import com.sun.portal.monitoring.MonitoringException;
008: import com.sun.portal.monitoring.Subsystem;
009: import com.sun.portal.monitoring.statistics.OpenStatistic;
010: import com.sun.portal.monitoring.statistics.OpenStatisticMBeanRegistration;
011: import com.sun.portal.monitoring.statistics.RollingAvgTimeStatisticImpl;
012: import com.sun.portal.monitoring.statistics.RollingAvgTimeStatisticWrapper;
013: import com.sun.portal.monitoring.utilities.ActivityTime;
014: import com.sun.portal.log.common.PortalLogger;
015:
016: import javax.management.*;
017: import javax.management.openmbean.CompositeType;
018: import javax.management.openmbean.OpenDataException;
019: import java.util.Map;
020: import java.util.logging.Logger;
021: import java.util.logging.Level;
022: import java.util.logging.LogRecord;
023:
024: public class DesktopRequestStatisticImpl implements
025: DesktopRequestStatistic {
026: private static final Logger logger = PortalLogger
027: .getLogger(DesktopRequestStatisticImpl.class);
028:
029: private static LogRecord getLogRecord(Level level, String message,
030: Object[] parameters, Throwable t) {
031: LogRecord result = new LogRecord(level, message);
032: result.setLoggerName(logger.getName());
033: result.setParameters(parameters);
034: result.setThrown(t);
035: return result;
036: }
037:
038: private Subsystem subsystem;
039: private Map registry;
040:
041: public DesktopRequestStatisticImpl(Subsystem subsystem) {
042: this .subsystem = subsystem;
043: this .registry = subsystem.getRegistry();
044: }
045:
046: private OpenStatisticMBeanRegistration getOpenStatisticMBeanRegistration(
047: String requestType) {
048: RollingAvgTimeStatisticWrapper rollingAvgTimeStatisticWrapper = new RollingAvgTimeStatisticWrapper();
049: rollingAvgTimeStatisticWrapper.setCompositeTypeName(getClass()
050: .getName()
051: + "." + requestType);
052: rollingAvgTimeStatisticWrapper
053: .setResourceBundleBaseName(MonitoringSubsystem.DESKTOP_MONITORING_RESOURCE_BUNDLE_BASE_NAME);
054:
055: RollingAvgTimeStatisticImpl rollingAvgTimeStatistic = (RollingAvgTimeStatisticImpl) rollingAvgTimeStatisticWrapper
056: .getStatisticImpl();
057: try {
058: CompositeType compositeType = rollingAvgTimeStatisticWrapper
059: .getCompositeType();
060: rollingAvgTimeStatistic.setDescription(compositeType
061: .getDescription("Description"));
062: rollingAvgTimeStatistic.setName(compositeType
063: .getDescription("Name"));
064: rollingAvgTimeStatistic.setUnit(compositeType
065: .getDescription("Unit"));
066: } catch (OpenDataException e) {
067: if (logger.isLoggable(Level.SEVERE)) {
068: logger.log(getLogRecord(Level.SEVERE, "PSDT_CSPDM0001",
069: new Object[] { e.getLocalizedMessage() }, e));
070: }
071: }
072: rollingAvgTimeStatistic.setLowerBound(100);
073: rollingAvgTimeStatistic.setUpperBound(1000);
074:
075: return new OpenStatisticMBeanRegistration(registry,
076: rollingAvgTimeStatisticWrapper);
077: }
078:
079: private ObjectName getObjectName(String namingDomain,
080: String requestType) throws MalformedObjectNameException {
081: return new ObjectName(namingDomain
082: + ":type=DesktopRequestStatistic,name=" + requestType);
083: }
084:
085: private ActivityTime activityTime = new ActivityTime();
086:
087: public void mark() {
088: if (!subsystem.isDisabled().booleanValue()) {
089: activityTime.mark();
090: }
091: }
092:
093: public void measure(String requestType) {
094: if (!subsystem.isDisabled().booleanValue()) {
095: try {
096: ObjectName objectName = getObjectName(subsystem
097: .getNamingDomain(), requestType);
098: OpenStatistic openStatistic = (OpenStatistic) registry
099: .get(objectName);
100: if (openStatistic == null) {
101: openStatistic = getOpenStatisticMBeanRegistration(requestType);
102: try {
103: subsystem.registerMBean(openStatistic,
104: objectName.toString());
105: } catch (MonitoringException e) {
106: if (logger.isLoggable(Level.SEVERE)) {
107: logger
108: .log(getLogRecord(
109: Level.SEVERE,
110: "PSDT_CSPDM0001",
111: new Object[] { e
112: .getLocalizedMessage() },
113: e));
114: }
115: }
116: }
117:
118: RollingAvgTimeStatisticWrapper rollingAvgTimeStatisticWrapper = (RollingAvgTimeStatisticWrapper) openStatistic
119: .getStatisticWrapper();
120: RollingAvgTimeStatisticImpl rollingAvgTimeStatistic = (RollingAvgTimeStatisticImpl) rollingAvgTimeStatisticWrapper
121: .getStatisticImpl();
122: rollingAvgTimeStatistic.setTime(activityTime.measure());
123: } catch (MalformedObjectNameException e) {
124: if (logger.isLoggable(Level.SEVERE)) {
125: logger.log(getLogRecord(Level.SEVERE,
126: "PSDT_CSPDM0001", new Object[] { e
127: .getLocalizedMessage() }, e));
128: }
129: }
130: }
131: }
132: }
|