KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > export > naming > MetadataNamingStrategy


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.naming;
18
19 import javax.management.MalformedObjectNameException JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21
22 import org.springframework.jmx.export.metadata.JmxAttributeSource;
23 import org.springframework.jmx.export.metadata.ManagedResource;
24 import org.springframework.jmx.support.ObjectNameManager;
25 import org.springframework.jmx.support.JmxUtils;
26 import org.springframework.util.StringUtils;
27 import org.springframework.aop.support.AopUtils;
28
29 /**
30  * An implementation of the <code>ObjectNamingStrategy</code> interface
31  * that reads the <code>ObjectName</code> from the source-level metadata.
32  *
33  * @author Rob Harrop
34  * @since 1.2
35  * @see ObjectNamingStrategy
36  */

37 public class MetadataNamingStrategy implements ObjectNamingStrategy {
38
39     /**
40      * The <code>JmxAttributeSource</code> implementation to use for reading metadata.
41      */

42     private JmxAttributeSource attributeSource;
43
44
45     /**
46      * Set the implementation of the <code>JmxAttributeSource</code> interface to use
47      * when reading the source level metadata.
48      */

49     public void setAttributeSource(JmxAttributeSource attributeSource) {
50         this.attributeSource = attributeSource;
51     }
52
53
54     /**
55      * Reads the <code>ObjectName</code> from the source level metadata associated
56      * with the managed resource's <code>Class</code>.
57      */

58     public ObjectName JavaDoc getObjectName(Object JavaDoc managedBean, String JavaDoc beanKey) throws MalformedObjectNameException JavaDoc {
59         if (AopUtils.isJdkDynamicProxy(managedBean)) {
60             throw new IllegalArgumentException JavaDoc(
61                             "MetadataNamingStrategy does not support JDK dynamic proxies - " +
62                                             "export the target beans directly or use CGLIB proxies instead");
63         }
64         Class JavaDoc managedClass = JmxUtils.getClassToExpose(managedBean);
65         ManagedResource mr = this.attributeSource.getManagedResource(managedClass);
66
67         // Check that the managed resource attribute has been specified.
68
if (mr == null) {
69             throw new MalformedObjectNameException JavaDoc("Your bean class [" + managedBean.getClass().getName() +
70                     "] must be marked with a valid ManagedResource attribute when using MetadataNamingStrategy");
71         }
72
73         // Check that an object name has been specified.
74
String JavaDoc objectName = mr.getObjectName();
75
76         if (!StringUtils.hasText(objectName)) {
77             throw new MalformedObjectNameException JavaDoc(
78                     "You must specify an ObjectName for class [" + managedBean.getClass().getName() + "]");
79         }
80
81         // Now try to parse the name.
82
return ObjectNameManager.getInstance(objectName);
83     }
84
85 }
86
Popular Tags