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.monitoring.statistics;
006:
007: import com.sun.portal.log.common.PortalLogger;
008:
009: import javax.management.*;
010: import javax.management.openmbean.CompositeData;
011: import javax.management.openmbean.CompositeDataSupport;
012: import javax.management.openmbean.OpenDataException;
013: import java.util.Iterator;
014: import java.util.Map;
015: import java.util.TreeMap;
016: import java.util.logging.Logger;
017: import java.util.logging.LogRecord;
018: import java.util.logging.Level;
019:
020: public class OpenStatistic implements DynamicMBean {
021: private static final Logger logger = PortalLogger
022: .getLogger(OpenStatistic.class);
023:
024: private static LogRecord getLogRecord(Level level, String message,
025: Object[] parameters, Throwable t) {
026: LogRecord result = new LogRecord(level, message);
027: result.setLoggerName(logger.getName());
028: result.setParameters(parameters);
029: result.setThrown(t);
030: return result;
031: }
032:
033: private StatisticWrapper statisticWrapper;
034:
035: public OpenStatistic(StatisticWrapper statisticWrapper) {
036: this .statisticWrapper = statisticWrapper;
037: }
038:
039: public StatisticWrapper getStatisticWrapper() {
040: return statisticWrapper;
041: }
042:
043: public Object getAttribute(String name)
044: throws AttributeNotFoundException, MBeanException,
045: ReflectionException {
046: if (name == null) {
047: throw new AttributeNotFoundException("null");
048: }
049:
050: if (name.length() == 0) {
051: throw new AttributeNotFoundException("");
052: }
053:
054: CompositeData compositeData;
055: try {
056: compositeData = statisticWrapper.toCompositeData();
057: } catch (OpenDataException e) {
058: if (logger.isLoggable(Level.SEVERE)) {
059: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
060: new Object[] { e.getLocalizedMessage() }, e));
061: }
062: throw new MBeanException(e);
063: }
064:
065: Object result;
066: if (compositeData.containsKey(name)) {
067: result = compositeData.get(name);
068: } else {
069: throw new AttributeNotFoundException(name);
070: }
071:
072: return result;
073: }
074:
075: private Map getItemNamesValues(CompositeData compositeData) {
076: String[] itemNames = statisticWrapper.getItemNames();
077: Object[] itemValues = compositeData.getAll(itemNames);
078: Map map = new TreeMap();
079: for (int i = 0; i < itemNames.length; i++) {
080: map.put(itemNames[i], itemValues[i]);
081: }
082:
083: return map;
084: }
085:
086: public void setAttribute(Attribute attribute)
087: throws AttributeNotFoundException,
088: InvalidAttributeValueException, MBeanException,
089: ReflectionException {
090: if (attribute == null) {
091: throw new AttributeNotFoundException("null");
092: } else if (attribute.getName() == null) {
093: throw new AttributeNotFoundException("null");
094: }
095:
096: CompositeData compositeData;
097: try {
098: compositeData = statisticWrapper.toCompositeData();
099: } catch (OpenDataException e) {
100: if (logger.isLoggable(Level.SEVERE)) {
101: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
102: new Object[] { e.getLocalizedMessage() }, e));
103: }
104: throw new MBeanException(e);
105: }
106:
107: if (compositeData.containsKey(attribute.getName())) {
108: Map itemNamesValues = getItemNamesValues(compositeData);
109: itemNamesValues.put(attribute.getName(), attribute
110: .getValue());
111: try {
112: compositeData = new CompositeDataSupport(compositeData
113: .getCompositeType(), itemNamesValues);
114: } catch (OpenDataException e) {
115: if (logger.isLoggable(Level.SEVERE)) {
116: logger.log(getLogRecord(Level.SEVERE,
117: "PSMN_CSPM0001", new Object[] { e
118: .getLocalizedMessage() }, e));
119: }
120: throw new MBeanException(e);
121: }
122:
123: try {
124: statisticWrapper.fromCompositeData(compositeData);
125: } catch (OpenDataException e) {
126: if (logger.isLoggable(Level.SEVERE)) {
127: logger.log(getLogRecord(Level.SEVERE,
128: "PSMN_CSPM0001", new Object[] { e
129: .getLocalizedMessage() }, e));
130: }
131: throw new MBeanException(e);
132: }
133: } else {
134: throw new AttributeNotFoundException(attribute.getName());
135: }
136: }
137:
138: public AttributeList getAttributes(String[] names) {
139: AttributeList result = null;
140:
141: if (names != null) {
142: if (names.length != 0) {
143: result = new AttributeList();
144: for (int i = 0; i < names.length; i++) {
145: try {
146: result.add(new Attribute(names[i],
147: getAttribute(names[i])));
148: } catch (AttributeNotFoundException e) {
149: if (logger.isLoggable(Level.INFO)) {
150: logger
151: .log(getLogRecord(
152: Level.INFO,
153: "PSMN_CSPM0001",
154: new Object[] { e
155: .getLocalizedMessage() },
156: e));
157: }
158: } catch (MBeanException e) {
159: if (logger.isLoggable(Level.SEVERE)) {
160: logger
161: .log(getLogRecord(
162: Level.SEVERE,
163: "PSMN_CSPM0001",
164: new Object[] { e
165: .getLocalizedMessage() },
166: e));
167: }
168: } catch (ReflectionException e) {
169: if (logger.isLoggable(Level.SEVERE)) {
170: logger
171: .log(getLogRecord(
172: Level.SEVERE,
173: "PSMN_CSPM0001",
174: new Object[] { e
175: .getLocalizedMessage() },
176: e));
177: }
178: }
179: }
180: }
181: }
182:
183: return result;
184: }
185:
186: public AttributeList setAttributes(AttributeList attributeList) {
187: AttributeList result = null;
188:
189: CompositeData compositeData;
190: try {
191: compositeData = statisticWrapper.toCompositeData();
192: } catch (OpenDataException e) {
193: if (logger.isLoggable(Level.SEVERE)) {
194: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
195: new Object[] { e.getLocalizedMessage() }, e));
196: }
197: throw new RuntimeException(e);
198: }
199:
200: Map itemNamesValues = getItemNamesValues(compositeData);
201: for (Iterator iterator = attributeList.iterator(); iterator
202: .hasNext();) {
203: Attribute attribute = (Attribute) iterator.next();
204: if (compositeData.containsKey(attribute.getName())) {
205: itemNamesValues.put(attribute.getName(), attribute
206: .getValue());
207: } else {
208: throw new RuntimeException(
209: new AttributeNotFoundException(attribute
210: .getName()));
211: }
212: }
213:
214: try {
215: compositeData = new CompositeDataSupport(compositeData
216: .getCompositeType(), itemNamesValues);
217: } catch (OpenDataException e) {
218: if (logger.isLoggable(Level.SEVERE)) {
219: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
220: new Object[] { e.getLocalizedMessage() }, e));
221: }
222: throw new RuntimeException(e);
223: }
224:
225: try {
226: statisticWrapper.fromCompositeData(compositeData);
227: } catch (OpenDataException e) {
228: if (logger.isLoggable(Level.SEVERE)) {
229: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
230: new Object[] { e.getLocalizedMessage() }, e));
231: }
232: throw new RuntimeException(e);
233: }
234:
235: return result;
236: }
237:
238: public Object invoke(String name, Object[] params,
239: String[] paramTypes) throws MBeanException,
240: ReflectionException {
241: if (name.equals("reset")) {
242: statisticWrapper.getStatisticImpl().reset();
243: return null;
244: }
245:
246: throw new ReflectionException(new NoSuchMethodException(name));
247: }
248:
249: public MBeanInfo getMBeanInfo() {
250: try {
251: return statisticWrapper.getMBeanInfo();
252: } catch (OpenDataException e) {
253: if (logger.isLoggable(Level.SEVERE)) {
254: logger.log(getLogRecord(Level.SEVERE, "PSMN_CSPM0001",
255: new Object[] { e.getLocalizedMessage() }, e));
256: }
257: throw new RuntimeException(e);
258: }
259: }
260: }
|