001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jmx.export.assembler;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.management.modelmbean.ModelMBeanNotificationInfo;
027:
028: import org.springframework.jmx.export.metadata.JmxMetadataUtils;
029: import org.springframework.jmx.export.metadata.ManagedNotification;
030: import org.springframework.util.StringUtils;
031:
032: /**
033: * Base class for MBeanInfoAssemblers that support configurable
034: * JMX notification behavior.
035: *
036: * @author Rob Harrop
037: * @author Juergen Hoeller
038: * @since 2.0
039: */
040: public abstract class AbstractConfigurableMBeanInfoAssembler extends
041: AbstractReflectiveMBeanInfoAssembler {
042:
043: private ModelMBeanNotificationInfo[] notificationInfos;
044:
045: private final Map notificationInfoMappings = new HashMap();
046:
047: public void setNotificationInfos(
048: ManagedNotification[] notificationInfos) {
049: ModelMBeanNotificationInfo[] infos = new ModelMBeanNotificationInfo[notificationInfos.length];
050: for (int i = 0; i < notificationInfos.length; i++) {
051: ManagedNotification notificationInfo = notificationInfos[i];
052: infos[i] = JmxMetadataUtils
053: .convertToModelMBeanNotificationInfo(notificationInfo);
054: }
055: this .notificationInfos = infos;
056: }
057:
058: public void setNotificationInfoMappings(Map notificationInfoMappings) {
059: Iterator entries = notificationInfoMappings.entrySet()
060: .iterator();
061: while (entries.hasNext()) {
062: Map.Entry entry = (Map.Entry) entries.next();
063: if (!(entry.getKey() instanceof String)) {
064: throw new IllegalArgumentException(
065: "Property [notificationInfoMappings] only accepts Strings for Map keys");
066: }
067: this .notificationInfoMappings.put(entry.getKey(),
068: extractNotificationMetadata(entry.getValue()));
069: }
070: }
071:
072: protected ModelMBeanNotificationInfo[] getNotificationInfo(
073: Object managedBean, String beanKey) {
074: ModelMBeanNotificationInfo[] result = null;
075:
076: if (StringUtils.hasText(beanKey)) {
077: result = (ModelMBeanNotificationInfo[]) this .notificationInfoMappings
078: .get(beanKey);
079: }
080:
081: if (result == null) {
082: result = this .notificationInfos;
083: }
084:
085: return (result == null) ? new ModelMBeanNotificationInfo[0]
086: : result;
087: }
088:
089: private ModelMBeanNotificationInfo[] extractNotificationMetadata(
090: Object mapValue) {
091: if (mapValue instanceof ManagedNotification) {
092: ManagedNotification mn = (ManagedNotification) mapValue;
093: return new ModelMBeanNotificationInfo[] { JmxMetadataUtils
094: .convertToModelMBeanNotificationInfo(mn) };
095: } else if (mapValue instanceof Collection) {
096: Collection col = (Collection) mapValue;
097: List result = new ArrayList();
098: for (Iterator iterator = col.iterator(); iterator.hasNext();) {
099: Object colValue = iterator.next();
100: if (!(colValue instanceof ManagedNotification)) {
101: throw new IllegalArgumentException(
102: "Property 'notificationInfoMappings' only accepts ManagedNotifications for Map values");
103: }
104: ManagedNotification mn = (ManagedNotification) colValue;
105: result.add(JmxMetadataUtils
106: .convertToModelMBeanNotificationInfo(mn));
107: }
108: return (ModelMBeanNotificationInfo[]) result
109: .toArray(new ModelMBeanNotificationInfo[result
110: .size()]);
111: } else {
112: throw new IllegalArgumentException(
113: "Property 'notificationInfoMappings' only accepts ManagedNotifications for Map values");
114: }
115: }
116:
117: }
|