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