1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.management; |
19 |
|
|
20 |
|
import java.util.HashSet; |
21 |
|
import java.util.Set; |
22 |
|
|
23 |
|
import javax.management.InstanceAlreadyExistsException; |
24 |
|
import javax.management.JMException; |
25 |
|
import javax.management.MBeanServer; |
26 |
|
import javax.management.NotCompliantMBeanException; |
27 |
|
import javax.management.ObjectInstance; |
28 |
|
import javax.management.ObjectName; |
29 |
|
import javax.management.modelmbean.InvalidTargetObjectTypeException; |
30 |
|
import javax.management.modelmbean.ModelMBeanInfo; |
31 |
|
import javax.management.modelmbean.RequiredModelMBean; |
32 |
|
|
33 |
|
import org.apache.camel.CamelContext; |
34 |
|
import org.apache.camel.CamelContextAware; |
35 |
|
import org.apache.camel.InstrumentationAgent; |
36 |
|
import org.apache.camel.impl.DefaultCamelContext; |
37 |
|
import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource; |
38 |
|
import org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler; |
39 |
|
|
40 |
|
public class InstrumentationAgentImpl implements InstrumentationAgent, CamelContextAware { |
41 |
|
|
42 |
|
private MBeanServer server; |
43 |
|
private CamelContext context; |
44 |
0 |
private Set<ObjectName> mbeans = new HashSet<ObjectName>(); |
45 |
|
MetadataMBeanInfoAssembler assembler; |
46 |
|
|
47 |
0 |
public InstrumentationAgentImpl() { |
48 |
0 |
assembler = new MetadataMBeanInfoAssembler(); |
49 |
0 |
assembler.setAttributeSource(new AnnotationJmxAttributeSource()); |
50 |
0 |
} |
51 |
|
public CamelContext getCamelContext() { |
52 |
0 |
return context; |
53 |
|
} |
54 |
|
|
55 |
|
public void setCamelContext(CamelContext camelContext) { |
56 |
0 |
context = camelContext; |
57 |
0 |
} |
58 |
|
|
59 |
|
public void setMBeanServer(MBeanServer server) { |
60 |
0 |
this.server = server; |
61 |
0 |
} |
62 |
|
|
63 |
|
public MBeanServer getMBeanServer() { |
64 |
0 |
return server; |
65 |
|
} |
66 |
|
|
67 |
|
public void register(Object obj, ObjectName name) throws JMException { |
68 |
0 |
register(obj, name, false); |
69 |
0 |
} |
70 |
|
|
71 |
|
public void register(Object obj, ObjectName name, boolean forceRegistration) throws JMException { |
72 |
|
try { |
73 |
0 |
registerMBeanWithServer(obj, name, forceRegistration); |
74 |
0 |
} catch (NotCompliantMBeanException e) { |
75 |
|
|
76 |
0 |
ModelMBeanInfo mbi = null; |
77 |
0 |
mbi = assembler.getMBeanInfo(obj, name.toString()); |
78 |
0 |
RequiredModelMBean mbean = (RequiredModelMBean)server.instantiate(RequiredModelMBean.class.getName()); |
79 |
0 |
mbean.setModelMBeanInfo(mbi); |
80 |
|
try { |
81 |
0 |
mbean.setManagedResource(obj, "ObjectReference"); |
82 |
0 |
} catch (InvalidTargetObjectTypeException itotex) { |
83 |
0 |
throw new JMException(itotex.getMessage()); |
84 |
0 |
} |
85 |
0 |
registerMBeanWithServer(mbean, name, forceRegistration); |
86 |
0 |
} |
87 |
0 |
} |
88 |
|
|
89 |
|
public void unregister(ObjectName name) throws JMException { |
90 |
0 |
} |
91 |
|
|
92 |
|
public void start() { |
93 |
0 |
if (context == null) { |
94 |
|
|
95 |
0 |
return; |
96 |
|
} |
97 |
|
|
98 |
0 |
if (context instanceof DefaultCamelContext) { |
99 |
0 |
DefaultCamelContext dc = (DefaultCamelContext)context; |
100 |
0 |
InstrumentationLifecycleStrategy ls = new InstrumentationLifecycleStrategy(this); |
101 |
0 |
dc.setLifecycleStrategy(ls); |
102 |
0 |
ls.onContextCreate(context); |
103 |
|
} |
104 |
0 |
} |
105 |
|
|
106 |
|
public void stop() { |
107 |
|
|
108 |
0 |
Object[] mBeans = mbeans.toArray(); |
109 |
0 |
for (Object name : mBeans) { |
110 |
0 |
mbeans.remove((ObjectName)name); |
111 |
|
try { |
112 |
0 |
unregister((ObjectName)name); |
113 |
0 |
} catch (JMException jmex) { |
114 |
|
|
115 |
0 |
} |
116 |
|
} |
117 |
0 |
} |
118 |
|
|
119 |
|
private void registerMBeanWithServer(Object obj, ObjectName name, boolean forceRegistration) |
120 |
|
throws JMException { |
121 |
|
|
122 |
0 |
ObjectInstance instance = null; |
123 |
|
try { |
124 |
0 |
instance = server.registerMBean(obj, name); |
125 |
0 |
} catch (InstanceAlreadyExistsException e) { |
126 |
0 |
if (forceRegistration) { |
127 |
0 |
server.unregisterMBean(name); |
128 |
0 |
instance = server.registerMBean(obj, name); |
129 |
0 |
} else { |
130 |
0 |
throw e; |
131 |
|
} |
132 |
0 |
} |
133 |
|
|
134 |
0 |
if (instance != null) { |
135 |
0 |
mbeans.add(name); |
136 |
|
} |
137 |
0 |
} |
138 |
|
} |