|
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 this(sourceURL,optionsMap,sourceNamespace,false); |
|
72 } |
|
73 |
|
74 public JMXRemoteTargetNamespace(JMXServiceURL sourceURL, |
|
75 Map<String,?> optionsMap, String sourceNamespace, |
|
76 boolean createEventClient) { |
|
77 super(sourceURL,optionsMap); |
|
78 this.sourceNamespace = sourceNamespace; |
|
79 this.createEventClient = createEventClient(optionsMap); |
|
80 } |
|
81 |
|
82 private boolean createEventClient(Map<String,?> options) { |
|
83 if (options == null) return false; |
|
84 final Object createValue = options.get(CREATE_EVENT_CLIENT); |
|
85 if (createValue == null) return false; |
|
86 if (createValue instanceof Boolean) |
|
87 return ((Boolean)createValue).booleanValue(); |
|
88 if (createValue instanceof String) |
|
89 return Boolean.valueOf((String)createValue); |
|
90 throw new IllegalArgumentException("Bad type for value of property " + |
|
91 CREATE_EVENT_CLIENT+": "+createValue.getClass().getName()); |
|
92 } |
|
93 |
|
94 @Override |
|
95 protected JMXConnector newJMXConnector(JMXServiceURL url, |
|
96 Map<String, ?> env) throws IOException { |
|
97 JMXConnector sup = super.newJMXConnector(url, env); |
|
98 if (sourceNamespace == null || "".equals(sourceNamespace)) |
|
99 return sup; |
|
100 if (createEventClient) |
|
101 sup = EventClient.withEventClient(sup); |
|
102 return JMXNamespaces.narrowToNamespace(sup, sourceNamespace); |
|
103 } |
|
104 |
|
105 |
|
106 /** |
|
107 * Creates a target name space to mirror a remote source name space in |
|
108 * the target server. |
|
109 * @param targetServer A connection to the target MBean server in which |
|
110 * the new name space should be created. |
|
111 * @param targetPath the name space to create in the target server. Note |
|
112 * that if the target name space is a path - that is if |
|
113 * {@code targetPath} contains '//', then the parent name space |
|
114 * must be pre-existing in the target server. Attempting to create |
|
115 * {code targetPath="a//b//c"} in {@code targetServer} |
|
116 * will fail if name space {@code "a//b"} doesn't already exists |
|
117 * in {@code targetServer}. |
|
118 * @param sourceURL a JMX service URL that can be used to connect to the |
|
119 * source MBean server. |
|
120 * @param options the set of options to use when creating the |
|
121 * {@link #JMXRemoteNamespace JMXRemoteNamespace} that will |
|
122 * handle the new name space. |
|
123 * @return An {@code ObjectInstance} representing the |
|
124 * {@link JMXRemoteNamespaceMBean} which handles the |
|
125 * new name space. |
|
126 * |
|
127 **/ |
|
128 public static ObjectInstance createNamespace( |
|
129 MBeanServerConnection targetServer, |
|
130 String targetPath, |
|
131 JMXServiceURL sourceURL, |
|
132 Map<String,?> options) |
|
133 throws IOException, InstanceAlreadyExistsException, |
|
134 MBeanRegistrationException, MBeanException { |
|
135 final ObjectName name = |
|
136 JMXNamespaces.getNamespaceObjectName(targetPath); |
|
137 return createInstance(targetServer, name, sourceURL, options, null); |
|
138 } |
|
139 |
|
140 /** |
|
141 * Creates a target name space to mirror a remote source name space in |
|
142 * the target server. |
|
143 * @param targetServer A connection to the target MBean server in which |
|
144 * the new name space should be created. |
|
145 * @param targetPath the name space to create in the target server. Note |
|
146 * that if the target name space is a path - that is if |
|
147 * {@code targetPath} contains '//', then the parent name space |
|
148 * must be pre-existing in the target server. Attempting to create |
|
149 * {code targetPath="a//b//c"} in {@code targetServer} |
|
150 * will fail if name space {@code "a//b"} doesn't already exists |
|
151 * in {@code targetServer}. |
|
152 * @param sourceURL a JMX service URL that can be used to connect to the |
|
153 * source MBean server. |
|
154 * @param sourcePath the source namespace path insode the source server. |
|
155 * @param options the set of options to use when creating the |
|
156 * {@link #JMXRemoteNamespace JMXRemoteNamespace} that will |
|
157 * handle the new name space. |
|
158 * @return An {@code ObjectInstance} representing the |
|
159 * {@link JMXRemoteNamespaceMBean} which handles the |
|
160 * new name space. |
|
161 * |
|
162 **/ |
|
163 public static ObjectInstance createNamespace( |
|
164 MBeanServerConnection targetServer, |
|
165 String targetPath, |
|
166 JMXServiceURL sourceURL, |
|
167 Map<String,?> options, |
|
168 String sourcePath) |
|
169 throws IOException, InstanceAlreadyExistsException, |
|
170 MBeanRegistrationException, MBeanException { |
|
171 final ObjectName name = |
|
172 JMXNamespaces.getNamespaceObjectName(targetPath); |
|
173 return createInstance(targetServer, name, sourceURL, options, sourcePath); |
|
174 } |
|
175 |
|
176 /** |
|
177 * Creates and registers a {@link JMXRemoteNamespaceMBean} in a target |
|
178 * server, to mirror a remote source name space. |
|
179 * |
|
180 * @param server A connection to the target MBean server in which |
|
181 * the new name space should be created. |
|
182 * @param handlerName the name of the JMXRemoteNamespace to create. |
|
183 * This must be a compliant name space handler name as returned |
|
184 * by {@link |
|
185 * JMXNamespaces#getNamespaceObjectName JMXNamespaces.getNamespaceObjectName}. |
|
186 * @param sourceURL a JMX service URL that can be used to connect to the |
|
187 * source MBean server. |
|
188 * @param sourcePath the path inside the source server |
|
189 * @param options the set of options to use when creating the |
|
190 * {@link #JMXRemoteNamespace JMXRemoteNamespace} that will |
|
191 * handle the new name space. |
|
192 * @return An {@code ObjectInstance} representing the new |
|
193 * {@link JMXRemoteNamespaceMBean} created. |
|
194 * @see #createNamespace createNamespace |
|
195 */ |
|
196 static ObjectInstance createInstance(MBeanServerConnection server, |
|
197 ObjectName handlerName, |
|
198 JMXServiceURL sourceURL, Map<String,?> options, |
|
199 String sourcePath) |
|
200 throws IOException, InstanceAlreadyExistsException, |
|
201 MBeanRegistrationException, MBeanException { |
|
202 try { |
|
203 final String[] signature = { |
|
204 JMXServiceURL.class.getName(), |
|
205 Map.class.getName(), |
|
206 String.class.getName() |
|
207 }; |
|
208 final Object[] params = { |
|
209 sourceURL,options,sourcePath |
|
210 }; |
|
211 final ObjectInstance instance = |
|
212 server.createMBean(JMXRemoteTargetNamespace.class.getName(), |
|
213 handlerName,params,signature); |
|
214 return instance; |
|
215 } catch (NotCompliantMBeanException ex) { |
|
216 throw new RuntimeException("unexpected exception: " + ex, ex); |
|
217 } catch (ReflectionException ex) { |
|
218 throw new RuntimeException("unexpected exception: " + ex, ex); |
|
219 } |
|
220 } |
|
221 |
|
222 } |