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