1 /* |
|
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 |
|
27 import java.io.IOException; |
|
28 import java.util.Map; |
|
29 import java.util.logging.Logger; |
|
30 |
|
31 import javax.management.InstanceAlreadyExistsException; |
|
32 import javax.management.MBeanException; |
|
33 import javax.management.MBeanRegistrationException; |
|
34 import javax.management.MBeanServerConnection; |
|
35 import javax.management.NotCompliantMBeanException; |
|
36 import javax.management.ObjectInstance; |
|
37 import javax.management.ObjectName; |
|
38 import javax.management.ReflectionException; |
|
39 import javax.management.event.EventClient; |
|
40 import javax.management.namespace.JMXNamespace; |
|
41 import javax.management.namespace.JMXNamespaces; |
|
42 import javax.management.namespace.JMXRemoteNamespace; |
|
43 import javax.management.namespace.JMXRemoteNamespaceMBean; |
|
44 import javax.management.remote.JMXConnector; |
|
45 import javax.management.remote.JMXServiceURL; |
|
46 |
|
47 // These options originally in the draft of javax/management/namespaces |
|
48 // but we decided to retire them - since they could be implemented |
|
49 // by subclasses. The JMXRemoteTargetNamespace is such a subclass. |
|
50 // |
|
51 public class JMXRemoteTargetNamespace extends JMXRemoteNamespace { |
|
52 |
|
53 /** |
|
54 * A logger for this class. |
|
55 **/ |
|
56 private static final Logger LOG = |
|
57 Logger.getLogger(JMXRemoteTargetNamespace.class.getName()); |
|
58 public static final String CREATE_EVENT_CLIENT = |
|
59 "jmx.test.create.event.client"; |
|
60 |
|
61 private final String sourceNamespace; |
|
62 private final boolean createEventClient; |
|
63 |
|
64 public JMXRemoteTargetNamespace(JMXServiceURL sourceURL, |
|
65 Map<String,?> optionsMap) { |
|
66 this(sourceURL,optionsMap,null); |
|
67 } |
|
68 |
|
69 public JMXRemoteTargetNamespace(JMXServiceURL sourceURL, |
|
70 Map<String,?> optionsMap, String sourceNamespace) { |
|
71 super(sourceURL, optionsMap); |
|
72 this.sourceNamespace = sourceNamespace; |
|
73 this.createEventClient = createEventClient(optionsMap); |
|
74 } |
|
75 |
|
76 private boolean createEventClient(Map<String,?> options) { |
|
77 if (options == null) return false; |
|
78 final Object createValue = options.get(CREATE_EVENT_CLIENT); |
|
79 if (createValue == null) return false; |
|
80 if (createValue instanceof Boolean) |
|
81 return ((Boolean)createValue).booleanValue(); |
|
82 if (createValue instanceof String) |
|
83 return Boolean.valueOf((String)createValue); |
|
84 throw new IllegalArgumentException("Bad type for value of property " + |
|
85 CREATE_EVENT_CLIENT+": "+createValue.getClass().getName()); |
|
86 } |
|
87 |
|
88 @Override |
|
89 protected MBeanServerConnection getMBeanServerConnection(JMXConnector jmxc) |
|
90 throws IOException { |
|
91 MBeanServerConnection mbsc = super.getMBeanServerConnection(jmxc); |
|
92 if (sourceNamespace != null && sourceNamespace.length() > 0) |
|
93 mbsc = JMXNamespaces.narrowToNamespace(mbsc, sourceNamespace); |
|
94 if (createEventClient) |
|
95 mbsc = EventClient.getEventClientConnection(mbsc); |
|
96 return mbsc; |
|
97 } |
|
98 |
|
99 |
|
100 /** |
|
101 * Creates a target name space to mirror a remote source name space in |
|
102 * the target server. |
|
103 * @param targetServer A connection to the target MBean server in which |
|
104 * the new name space should be created. |
|
105 * @param targetPath the name space to create in the target server. Note |
|
106 * that if the target name space is a path - that is if |
|
107 * {@code targetPath} contains '//', then the parent name space |
|
108 * must be pre-existing in the target server. Attempting to create |
|
109 * {code targetPath="a//b//c"} in {@code targetServer} |
|
110 * will fail if name space {@code "a//b"} doesn't already exists |
|
111 * in {@code targetServer}. |
|
112 * @param sourceURL a JMX service URL that can be used to connect to the |
|
113 * source MBean server. |
|
114 * @param options the set of options to use when creating the |
|
115 * {@link #JMXRemoteNamespace JMXRemoteNamespace} that will |
|
116 * handle the new name space. |
|
117 * @return An {@code ObjectInstance} representing the |
|
118 * {@link JMXRemoteNamespaceMBean} which handles the |
|
119 * new name space. |
|
120 * |
|
121 **/ |
|
122 public static ObjectInstance createNamespace( |
|
123 MBeanServerConnection targetServer, |
|
124 String targetPath, |
|
125 JMXServiceURL sourceURL, |
|
126 Map<String,?> options) |
|
127 throws IOException, InstanceAlreadyExistsException, |
|
128 MBeanRegistrationException, MBeanException { |
|
129 final ObjectName name = |
|
130 JMXNamespaces.getNamespaceObjectName(targetPath); |
|
131 return createInstance(targetServer, name, sourceURL, options, null); |
|
132 } |
|
133 |
|
134 /** |
|
135 * Creates a target name space to mirror a remote source name space in |
|
136 * the target server. |
|
137 * @param targetServer A connection to the target MBean server in which |
|
138 * the new name space should be created. |
|
139 * @param targetPath the name space to create in the target server. Note |
|
140 * that if the target name space is a path - that is if |
|
141 * {@code targetPath} contains '//', then the parent name space |
|
142 * must be pre-existing in the target server. Attempting to create |
|
143 * {code targetPath="a//b//c"} in {@code targetServer} |
|
144 * will fail if name space {@code "a//b"} doesn't already exists |
|
145 * in {@code targetServer}. |
|
146 * @param sourceURL a JMX service URL that can be used to connect to the |
|
147 * source MBean server. |
|
148 * @param sourcePath the source namespace path insode the source server. |
|
149 * @param options the set of options to use when creating the |
|
150 * {@link #JMXRemoteNamespace JMXRemoteNamespace} that will |
|
151 * handle the new name space. |
|
152 * @return An {@code ObjectInstance} representing the |
|
153 * {@link JMXRemoteNamespaceMBean} which handles the |
|
154 * new name space. |
|
155 * |
|
156 **/ |
|
157 public static ObjectInstance createNamespace( |
|
158 MBeanServerConnection targetServer, |
|
159 String targetPath, |
|
160 JMXServiceURL sourceURL, |
|
161 Map<String,?> options, |
|
162 String sourcePath) |
|
163 throws IOException, InstanceAlreadyExistsException, |
|
164 MBeanRegistrationException, MBeanException { |
|
165 final ObjectName name = |
|
166 JMXNamespaces.getNamespaceObjectName(targetPath); |
|
167 return createInstance(targetServer, name, sourceURL, options, sourcePath); |
|
168 } |
|
169 |
|
170 /** |
|
171 * Creates and registers a {@link JMXRemoteNamespaceMBean} in a target |
|
172 * server, to mirror a remote source name space. |
|
173 * |
|
174 * @param server A connection to the target MBean server in which |
|
175 * the new name space should be created. |
|
176 * @param handlerName the name of the JMXRemoteNamespace to create. |
|
177 * This must be a compliant name space handler name as returned |
|
178 * by {@link |
|
179 * JMXNamespaces#getNamespaceObjectName JMXNamespaces.getNamespaceObjectName}. |
|
180 * @param sourceURL a JMX service URL that can be used to connect to the |
|
181 * source MBean server. |
|
182 * @param sourcePath the path inside the source server |
|
183 * @param options the set of options to use when creating the |
|
184 * {@link #JMXRemoteNamespace JMXRemoteNamespace} that will |
|
185 * handle the new name space. |
|
186 * @return An {@code ObjectInstance} representing the new |
|
187 * {@link JMXRemoteNamespaceMBean} created. |
|
188 * @see #createNamespace createNamespace |
|
189 */ |
|
190 static ObjectInstance createInstance(MBeanServerConnection server, |
|
191 ObjectName handlerName, |
|
192 JMXServiceURL sourceURL, Map<String,?> options, |
|
193 String sourcePath) |
|
194 throws IOException, InstanceAlreadyExistsException, |
|
195 MBeanRegistrationException, MBeanException { |
|
196 try { |
|
197 final String[] signature = { |
|
198 JMXServiceURL.class.getName(), |
|
199 Map.class.getName(), |
|
200 String.class.getName() |
|
201 }; |
|
202 final Object[] params = { |
|
203 sourceURL,options,sourcePath |
|
204 }; |
|
205 final ObjectInstance instance = |
|
206 server.createMBean(JMXRemoteTargetNamespace.class.getName(), |
|
207 handlerName,params,signature); |
|
208 return instance; |
|
209 } catch (NotCompliantMBeanException ex) { |
|
210 throw new RuntimeException("unexpected exception: " + ex, ex); |
|
211 } catch (ReflectionException ex) { |
|
212 throw new RuntimeException("unexpected exception: " + ex, ex); |
|
213 } |
|
214 } |
|
215 |
|
216 } |
|