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