|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panel.common; |
|
27 |
|
28 import java.beans.PropertyChangeEvent; |
|
29 import java.io.IOException; |
|
30 import java.util.logging.*; |
|
31 import javax.management.*; |
|
32 import com.oracle.solaris.adr.Stability; |
|
33 import com.oracle.solaris.rad.jmx.*; |
|
34 import com.oracle.solaris.vp.util.misc.ObjectUtil; |
|
35 import com.oracle.solaris.vp.util.misc.event.PropertyChangeListeners; |
|
36 |
|
37 /** |
|
38 * The {@code MXBeanTracker} is a {@link ConnectionTracker} that automatically |
|
39 * creates and re-creates an {@link #getBean MXBean} after changes in the {@link |
|
40 * #setMBeanServerConnection MBeanServerConnection} or {@link #setObjectName |
|
41 * ObjectName}. |
|
42 * </p> |
|
43 * A property change notification is sent out when the tracked {@code MXBean} |
|
44 * changes. |
|
45 */ |
|
46 public class MXBeanTracker<T> extends ConnectionTracker { |
|
47 // |
|
48 // Static data |
|
49 // |
|
50 |
|
51 /** |
|
52 * The name of the property that changes with {@link #setBean}. |
|
53 */ |
|
54 public static final String PROPERTY_BEAN = "bean"; |
|
55 |
|
56 // |
|
57 // Instance data |
|
58 // |
|
59 |
|
60 private Class<T> clazz; |
|
61 private T bean; |
|
62 private Stability stability; |
|
63 |
|
64 // |
|
65 // Constructors |
|
66 // |
|
67 |
|
68 public MXBeanTracker(ObjectName oName, Class<T> clazz, Stability stability) |
|
69 { |
|
70 super(oName); |
|
71 try { |
|
72 setClass(clazz, stability); |
|
73 |
|
74 // Impossible because mbsc not yet set |
|
75 } catch (TrackerException impossible) { |
|
76 } |
|
77 } |
|
78 |
|
79 public MXBeanTracker(ObjectName oName, Class<T> clazz, Stability stability, |
|
80 ClientContext context) throws TrackerException { |
|
81 |
|
82 this(oName, clazz, stability); |
|
83 setClientContext(context); |
|
84 } |
|
85 |
|
86 // |
|
87 // ConnectionTracker methods |
|
88 // |
|
89 |
|
90 @Override |
|
91 public void setMBeanServerConnection(MBeanServerConnection mbsc) |
|
92 throws TrackerException { |
|
93 |
|
94 if (getMBeanServerConnection() != mbsc) { |
|
95 super.setMBeanServerConnection(mbsc); |
|
96 setBean(); |
|
97 } |
|
98 } |
|
99 |
|
100 @Override |
|
101 public void setObjectName(ObjectName oName) |
|
102 throws TrackerException { |
|
103 |
|
104 if (!ObjectUtil.equals(getObjectName(), oName)) { |
|
105 super.setObjectName(oName); |
|
106 setBean(); |
|
107 } |
|
108 } |
|
109 |
|
110 // |
|
111 // MXBeanTracker methods |
|
112 // |
|
113 |
|
114 protected T createBean() throws JMException, |
|
115 IncompatibleVersionException, IOException { |
|
116 |
|
117 T bean = null; |
|
118 MBeanServerConnection mbsc = getMBeanServerConnection(); |
|
119 ObjectName oName = getObjectName(); |
|
120 if (mbsc != null && oName != null && clazz != null && stability != null) |
|
121 { |
|
122 bean = RadJMX.newMXBeanProxy(mbsc, oName, clazz, stability); |
|
123 } |
|
124 |
|
125 return bean; |
|
126 } |
|
127 |
|
128 public T getBean() { |
|
129 return bean; |
|
130 } |
|
131 |
|
132 public Class<T> getBeanClass() { |
|
133 return clazz; |
|
134 } |
|
135 |
|
136 public Stability getStability() { |
|
137 return stability; |
|
138 } |
|
139 |
|
140 public void setBean(T bean) { |
|
141 if (this.bean != bean) { |
|
142 PropertyChangeEvent e = new PropertyChangeEvent( |
|
143 this, PROPERTY_BEAN, this.bean, bean); |
|
144 this.bean = bean; |
|
145 getPropertyChangeListeners().propertyChange(e); |
|
146 } |
|
147 } |
|
148 |
|
149 // |
|
150 // Private methods |
|
151 // |
|
152 |
|
153 private void setClass(Class<T> clazz, Stability stability) |
|
154 throws TrackerException { |
|
155 |
|
156 this.clazz = clazz; |
|
157 this.stability = stability; |
|
158 setBean(); |
|
159 } |
|
160 |
|
161 private void setBean() throws TrackerException { |
|
162 T bean = null; |
|
163 String message = null; |
|
164 Throwable cause = null; |
|
165 |
|
166 try { |
|
167 bean = createBean(); |
|
168 |
|
169 } catch (IncompatibleVersionException e) { |
|
170 cause = e; |
|
171 message = "Incompatible client and server versions for: " |
|
172 + clazz.getSimpleName(); |
|
173 |
|
174 } catch (JMException e) { |
|
175 cause = e; |
|
176 message = "Error getting MBean information for: " + getObjectName(); |
|
177 |
|
178 } catch (IOException e) { |
|
179 cause = e; |
|
180 message = "Error contacting MBean server while " |
|
181 + "creating proxy for: " + clazz.getSimpleName(); |
|
182 } |
|
183 |
|
184 setBean(bean); |
|
185 |
|
186 if (cause != null) { |
|
187 Logger.getLogger(getClass().getName()).log( |
|
188 Level.SEVERE, message, cause); |
|
189 throw new TrackerException(cause); |
|
190 } |
|
191 } |
|
192 } |