KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > assembler > AbstractConfigurableMBeanInfoAssembler


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jmx.export.assembler;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.management.modelmbean.ModelMBeanNotificationInfo JavaDoc;
27
28 import org.springframework.jmx.export.metadata.JmxMetadataUtils;
29 import org.springframework.jmx.export.metadata.ManagedNotification;
30 import org.springframework.util.StringUtils;
31
32 /**
33  * Base class for MBeanInfoAssemblers that support configurable
34  * JMX notification behavior.
35  *
36  * @author Rob Harrop
37  * @author Juergen Hoeller
38  * @since 2.0
39  */

40 public abstract class AbstractConfigurableMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssembler {
41
42     private ModelMBeanNotificationInfo JavaDoc[] notificationInfos;
43
44     private final Map JavaDoc notificationInfoMappings = new HashMap JavaDoc();
45
46
47     public void setNotificationInfos(ManagedNotification[] notificationInfos) {
48         ModelMBeanNotificationInfo JavaDoc[] infos = new ModelMBeanNotificationInfo JavaDoc[notificationInfos.length];
49         for (int i = 0; i < notificationInfos.length; i++) {
50             ManagedNotification notificationInfo = notificationInfos[i];
51             infos[i] = JmxMetadataUtils.convertToModelMBeanNotificationInfo(notificationInfo);
52         }
53         this.notificationInfos = infos;
54     }
55
56     public void setNotificationInfoMappings(Map JavaDoc notificationInfoMappings) {
57         Iterator JavaDoc entries = notificationInfoMappings.entrySet().iterator();
58         while (entries.hasNext()) {
59             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entries.next();
60             if (!(entry.getKey() instanceof String JavaDoc)) {
61                 throw new IllegalArgumentException JavaDoc("Property [notificationInfoMappings] only accepts Strings for Map keys");
62             }
63             this.notificationInfoMappings.put(entry.getKey(), extractNotificationMetadata(entry.getValue()));
64         }
65     }
66
67
68     protected ModelMBeanNotificationInfo JavaDoc[] getNotificationInfo(Object JavaDoc managedBean, String JavaDoc beanKey) {
69         ModelMBeanNotificationInfo JavaDoc[] result = null;
70
71         if (StringUtils.hasText(beanKey)) {
72             result = (ModelMBeanNotificationInfo JavaDoc[]) this.notificationInfoMappings.get(beanKey);
73         }
74
75         if (result == null) {
76             result = this.notificationInfos;
77         }
78
79         return (result == null) ? new ModelMBeanNotificationInfo JavaDoc[0] : result;
80     }
81
82     private ModelMBeanNotificationInfo JavaDoc[] extractNotificationMetadata(Object JavaDoc mapValue) {
83         if (mapValue instanceof ManagedNotification) {
84             ManagedNotification mn = (ManagedNotification) mapValue;
85             return new ModelMBeanNotificationInfo JavaDoc[] {JmxMetadataUtils.convertToModelMBeanNotificationInfo(mn)};
86         }
87         else if (mapValue instanceof Collection JavaDoc) {
88             Collection JavaDoc col = (Collection JavaDoc) mapValue;
89             List JavaDoc result = new ArrayList JavaDoc();
90             for (Iterator JavaDoc iterator = col.iterator(); iterator.hasNext();) {
91                 Object JavaDoc colValue = iterator.next();
92                 if (!(colValue instanceof ManagedNotification)) {
93                     throw new IllegalArgumentException JavaDoc(
94                             "Property 'notificationInfoMappings' only accepts ManagedNotifications for Map values");
95                 }
96                 ManagedNotification mn = (ManagedNotification) colValue;
97                 result.add(JmxMetadataUtils.convertToModelMBeanNotificationInfo(mn));
98             }
99             return (ModelMBeanNotificationInfo JavaDoc[]) result.toArray(new ModelMBeanNotificationInfo JavaDoc[result.size()]);
100         }
101         else {
102             throw new IllegalArgumentException JavaDoc(
103                     "Property 'notificationInfoMappings' only accepts ManagedNotifications for Map values");
104         }
105     }
106
107 }
108
Popular Tags