001/* 002 * Copyright 2002-2008 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 * https://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 017package org.springframework.jmx.export.assembler; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024import javax.management.modelmbean.ModelMBeanNotificationInfo; 025 026import org.springframework.jmx.export.metadata.JmxMetadataUtils; 027import org.springframework.jmx.export.metadata.ManagedNotification; 028import org.springframework.util.StringUtils; 029 030/** 031 * Base class for MBeanInfoAssemblers that support configurable 032 * JMX notification behavior. 033 * 034 * @author Rob Harrop 035 * @author Juergen Hoeller 036 * @since 2.0 037 */ 038public abstract class AbstractConfigurableMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssembler { 039 040 private ModelMBeanNotificationInfo[] notificationInfos; 041 042 private final Map<String, ModelMBeanNotificationInfo[]> notificationInfoMappings = 043 new HashMap<String, ModelMBeanNotificationInfo[]>(); 044 045 046 public void setNotificationInfos(ManagedNotification[] notificationInfos) { 047 ModelMBeanNotificationInfo[] infos = new ModelMBeanNotificationInfo[notificationInfos.length]; 048 for (int i = 0; i < notificationInfos.length; i++) { 049 ManagedNotification notificationInfo = notificationInfos[i]; 050 infos[i] = JmxMetadataUtils.convertToModelMBeanNotificationInfo(notificationInfo); 051 } 052 this.notificationInfos = infos; 053 } 054 055 public void setNotificationInfoMappings(Map<String, Object> notificationInfoMappings) { 056 for (Map.Entry<String, Object> entry : notificationInfoMappings.entrySet()) { 057 this.notificationInfoMappings.put(entry.getKey(), extractNotificationMetadata(entry.getValue())); 058 } 059 } 060 061 062 @Override 063 protected ModelMBeanNotificationInfo[] getNotificationInfo(Object managedBean, String beanKey) { 064 ModelMBeanNotificationInfo[] result = null; 065 if (StringUtils.hasText(beanKey)) { 066 result = this.notificationInfoMappings.get(beanKey); 067 } 068 if (result == null) { 069 result = this.notificationInfos; 070 } 071 return (result != null ? result : new ModelMBeanNotificationInfo[0]); 072 } 073 074 private ModelMBeanNotificationInfo[] extractNotificationMetadata(Object mapValue) { 075 if (mapValue instanceof ManagedNotification) { 076 ManagedNotification mn = (ManagedNotification) mapValue; 077 return new ModelMBeanNotificationInfo[] {JmxMetadataUtils.convertToModelMBeanNotificationInfo(mn)}; 078 } 079 else if (mapValue instanceof Collection) { 080 Collection<?> col = (Collection<?>) mapValue; 081 List<ModelMBeanNotificationInfo> result = new ArrayList<ModelMBeanNotificationInfo>(); 082 for (Object colValue : col) { 083 if (!(colValue instanceof ManagedNotification)) { 084 throw new IllegalArgumentException( 085 "Property 'notificationInfoMappings' only accepts ManagedNotifications for Map values"); 086 } 087 ManagedNotification mn = (ManagedNotification) colValue; 088 result.add(JmxMetadataUtils.convertToModelMBeanNotificationInfo(mn)); 089 } 090 return result.toArray(new ModelMBeanNotificationInfo[result.size()]); 091 } 092 else { 093 throw new IllegalArgumentException( 094 "Property 'notificationInfoMappings' only accepts ManagedNotifications for Map values"); 095 } 096 } 097 098}