01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import javax.management.ObjectName;
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: * Set the implementation of the <code>JmxAttributeSource</code> interface to use
46: * when reading the source level metadata.
47: */
48: public void setAttributeSource(JmxAttributeSource attributeSource) {
49: this .attributeSource = attributeSource;
50: }
51:
52: /**
53: * Reads the <code>ObjectName</code> from the source level metadata associated
54: * with the managed resource's <code>Class</code>.
55: */
56: public ObjectName getObjectName(Object managedBean, String beanKey)
57: throws MalformedObjectNameException {
58: if (AopUtils.isJdkDynamicProxy(managedBean)) {
59: throw new IllegalArgumentException(
60: "MetadataNamingStrategy does not support JDK dynamic proxies - "
61: + "export the target beans directly or use CGLIB proxies instead");
62: }
63: Class managedClass = JmxUtils.getClassToExpose(managedBean);
64: ManagedResource mr = this .attributeSource
65: .getManagedResource(managedClass);
66:
67: // Check that the managed resource attribute has been specified.
68: if (mr == null) {
69: throw new MalformedObjectNameException(
70: "Your bean class ["
71: + managedBean.getClass().getName()
72: + "] must be marked with a valid ManagedResource attribute when using MetadataNamingStrategy");
73: }
74:
75: // Check that an object name has been specified.
76: String objectName = mr.getObjectName();
77:
78: if (!StringUtils.hasText(objectName)) {
79: throw new MalformedObjectNameException(
80: "You must specify an ObjectName for class ["
81: + managedBean.getClass().getName() + "]");
82: }
83:
84: // Now try to parse the name.
85: return ObjectNameManager.getInstance(objectName);
86: }
87:
88: }
|