author | David Powell <David.Powell@sun.com> |
Wed, 16 Dec 2009 19:06:12 -0800 | |
changeset 401 | fc1223edbd8d |
parent 391 | 71abce159a62 |
child 433 | e629b84699e3 |
permissions | -rw-r--r-- |
391 | 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 2009 Sun Microsystems, Inc. All rights reserved. |
|
24 |
* Use is subject to license terms. |
|
25 |
*/ |
|
26 |
||
27 |
package org.opensolaris.os.rad.jmx; |
|
28 |
||
29 |
import java.io.File; |
|
30 |
import java.io.IOException; |
|
31 |
import java.net.InetAddress; |
|
32 |
import java.net.MalformedURLException; |
|
33 |
import java.util.HashMap; |
|
34 |
import java.util.Map; |
|
35 |
import javax.management.ListenerNotFoundException; |
|
36 |
import javax.management.MBeanServerConnection; |
|
37 |
import javax.management.NotificationBroadcasterSupport; |
|
38 |
import javax.management.NotificationFilter; |
|
39 |
import javax.management.NotificationListener; |
|
40 |
import javax.management.remote.JMXConnectionNotification; |
|
41 |
import javax.management.remote.JMXConnector; |
|
42 |
import javax.management.remote.JMXServiceURL; |
|
43 |
import javax.security.auth.Subject; |
|
44 |
import org.opensolaris.os.rad.Client; |
|
45 |
import org.opensolaris.os.rad.ClientFramer; |
|
46 |
||
47 |
public class RadJMXConnector implements JMXConnector |
|
48 |
{ |
|
49 |
private File path_ = null; |
|
50 |
private String host_; |
|
51 |
private int port_; |
|
52 |
private boolean tls_; |
|
53 |
private Map<String, ?> env_; |
|
54 |
||
55 |
private Client client_ = null; |
|
56 |
private boolean closed_ = false; |
|
57 |
private MBeanServerConnection mbsc_ = null; |
|
58 |
private NotificationBroadcasterSupport notify_ = |
|
59 |
new NotificationBroadcasterSupport(); |
|
60 |
private int serial_ = 0; |
|
61 |
||
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
62 |
RadJMXConnector(JMXServiceURL url, Map<String, ?> env, boolean usetls) { |
391 | 63 |
host_ = url.getHost(); |
64 |
port_ = url.getPort(); |
|
65 |
env_ = env; |
|
66 |
tls_ = usetls; |
|
67 |
if (port_ == 0) |
|
68 |
port_ = 12302; |
|
69 |
} |
|
70 |
||
71 |
RadJMXConnector(JMXServiceURL url, Map<String, ?> env) |
|
72 |
throws MalformedURLException { |
|
73 |
||
74 |
path_ = new File(url.getURLPath()); |
|
75 |
if (!path_.isAbsolute()) |
|
76 |
throw new MalformedURLException("Absolute path required"); |
|
77 |
env_ = env; |
|
78 |
} |
|
79 |
||
80 |
private void checkState(boolean connect) throws IOException { |
|
81 |
if (closed_) |
|
82 |
throw new IOException("Connection was closed."); |
|
83 |
if (!connect && client_ == null) |
|
84 |
throw new IOException("Connection not established."); |
|
85 |
} |
|
86 |
||
87 |
public void connect() throws IOException { |
|
88 |
connect(env_); |
|
89 |
} |
|
90 |
||
91 |
public void connect(Map<String, ?> env) throws IOException { |
|
92 |
checkState(true); |
|
93 |
||
94 |
if (env == null) { |
|
95 |
env = env_; |
|
96 |
} else if (env_ != null) { |
|
97 |
Map<String, Object> t = new HashMap<String, Object>(env_); |
|
98 |
t.putAll(env); |
|
99 |
env = t; |
|
100 |
} |
|
101 |
ClientFramer framer = path_ != null ? new ClientFramer(path_) : |
|
102 |
new ClientFramer(InetAddress.getByName(host_), port_, tls_, env); |
|
103 |
client_ = new Client(framer); |
|
104 |
notify_.sendNotification(new JMXConnectionNotification( |
|
105 |
JMXConnectionNotification.OPENED, this, getConnectionId(), |
|
106 |
++serial_, null, null)); |
|
107 |
} |
|
108 |
||
109 |
public MBeanServerConnection getMBeanServerConnection() throws IOException { |
|
110 |
checkState(false); |
|
111 |
if (mbsc_ == null) |
|
112 |
mbsc_ = new RadMBeanServerConnection(client_); |
|
113 |
return (mbsc_); |
|
114 |
} |
|
115 |
||
116 |
public MBeanServerConnection getMBeanServerConnection( |
|
117 |
Subject delegationSubject) throws IOException { |
|
118 |
||
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
119 |
if (delegationSubject == null) |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
120 |
return getMBeanServerConnection(); |
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
121 |
|
391 | 122 |
checkState(false); |
401
fc1223edbd8d
13421 apache: o.o.o.rad.ContainerException: system error: error talking to slave
David Powell <David.Powell@sun.com>
parents:
391
diff
changeset
|
123 |
throw new IOException("Subject delegation not supported"); |
391 | 124 |
} |
125 |
||
126 |
public void close() throws IOException { |
|
127 |
if (client_ == null) |
|
128 |
return; |
|
129 |
||
130 |
String id = getConnectionId(); |
|
131 |
closed_ = true; |
|
132 |
client_.close(); |
|
133 |
client_ = null; |
|
134 |
mbsc_ = null; |
|
135 |
||
136 |
notify_.sendNotification(new JMXConnectionNotification( |
|
137 |
JMXConnectionNotification.CLOSED, this, id, ++serial_, null, null)); |
|
138 |
} |
|
139 |
||
140 |
public void addConnectionNotificationListener(NotificationListener listener, |
|
141 |
NotificationFilter filter, Object handback) { |
|
142 |
||
143 |
notify_.addNotificationListener(listener, filter, handback); |
|
144 |
} |
|
145 |
||
146 |
public void removeConnectionNotificationListener( |
|
147 |
NotificationListener listener) throws ListenerNotFoundException { |
|
148 |
||
149 |
notify_.removeNotificationListener(listener); |
|
150 |
} |
|
151 |
||
152 |
public void removeConnectionNotificationListener(NotificationListener l, |
|
153 |
NotificationFilter f, Object handback) throws ListenerNotFoundException |
|
154 |
{ |
|
155 |
notify_.removeNotificationListener(l, f, handback); |
|
156 |
} |
|
157 |
||
158 |
public String getConnectionId() throws IOException { |
|
159 |
checkState(false); |
|
160 |
/* XXX */ |
|
161 |
return "service:jmx:rad://" + host_ + ":" + port_ + " " + "foo 1"; |
|
162 |
} |
|
163 |
} |