001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management.remote;
010:
011: import java.io.IOException;
012: import java.util.HashSet;
013: import java.util.Map;
014: import java.util.Set;
015: import javax.management.MBeanNotificationInfo;
016: import javax.management.MBeanRegistration;
017: import javax.management.MBeanServer;
018: import javax.management.NotificationBroadcasterSupport;
019: import javax.management.ObjectName;
020:
021: /**
022: * @version $Revision: 1.9 $
023: */
024: public abstract class JMXConnectorServer extends
025: NotificationBroadcasterSupport implements
026: JMXConnectorServerMBean, MBeanRegistration {
027: public static final String AUTHENTICATOR = "jmx.remote.authenticator";
028:
029: private static final MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
030: new String[] { JMXConnectionNotification.OPENED,
031: JMXConnectionNotification.CLOSED,
032: JMXConnectionNotification.FAILED },
033: JMXConnectionNotification.class.getName(),
034: "Notifications emitted by the JMXConnectorServer MBean upon opening, closing or failing of a connection") };
035:
036: private static long notificationSequenceNumber;
037:
038: private MBeanServer server;
039: private ObjectName name;
040: private final HashSet connections = new HashSet();
041:
042: public JMXConnectorServer() {
043: }
044:
045: public JMXConnectorServer(MBeanServer server) {
046: this .server = server;
047: }
048:
049: public ObjectName preRegister(MBeanServer server, ObjectName name) {
050: if (name == null)
051: throw new NullPointerException("ObjectName cannot be null");
052: if (this .server == null)
053: this .server = server;
054: this .name = name;
055: return name;
056: }
057:
058: public void postRegister(Boolean registrationDone) {
059: }
060:
061: public void preDeregister() throws Exception {
062: if (isActive())
063: stop();
064: }
065:
066: public void postDeregister() {
067: }
068:
069: public MBeanNotificationInfo[] getNotificationInfo() {
070: return notifications;
071: }
072:
073: public MBeanServer getMBeanServer() {
074: return server;
075: }
076:
077: public void setMBeanServerForwarder(MBeanServerForwarder forwarder)
078: throws IllegalArgumentException {
079: if (forwarder.getMBeanServer() == null) {
080: if (server == null)
081: throw new IllegalStateException(
082: "This JMXConnectorServer is not attached to an MBeanServer");
083: }
084: forwarder.setMBeanServer(server);
085: this .server = forwarder;
086: }
087:
088: public JMXConnector toJMXConnector(Map environment)
089: throws IOException {
090: JMXServiceURL address = getAddress();
091: return JMXConnectorFactory
092: .newJMXConnector(address, environment);
093: }
094:
095: public String[] getConnectionIds() {
096: Set copy = null;
097: synchronized (connections) {
098: copy = (Set) connections.clone();
099: }
100: return (String[]) copy.toArray(new String[copy.size()]);
101: }
102:
103: protected void connectionOpened(String connectionId,
104: String message, Object userData) {
105: synchronized (connections) {
106: boolean added = connections.add(connectionId);
107: if (!added)
108: throw new IllegalStateException(
109: "Duplicate connection ID: " + connectionId);
110: }
111:
112: Object source = name;
113: if (source == null)
114: source = this ;
115: JMXConnectionNotification notification = new JMXConnectionNotification(
116: JMXConnectionNotification.OPENED, source, connectionId,
117: getNextSequenceNumber(), message, userData);
118: sendNotification(notification);
119: }
120:
121: protected void connectionClosed(String connectionId,
122: String message, Object userData) {
123: synchronized (connections) {
124: boolean removed = connections.remove(connectionId);
125: if (!removed)
126: throw new IllegalStateException(
127: "Connection ID not present: " + connectionId);
128: }
129:
130: Object source = name;
131: if (source == null)
132: source = this ;
133: JMXConnectionNotification notification = new JMXConnectionNotification(
134: JMXConnectionNotification.CLOSED, source, connectionId,
135: getNextSequenceNumber(), message, userData);
136: sendNotification(notification);
137: }
138:
139: protected void connectionFailed(String connectionId,
140: String message, Object userData) {
141: synchronized (connections) {
142: boolean removed = connections.remove(connectionId);
143: if (!removed)
144: throw new IllegalStateException(
145: "Connection ID not present: " + connectionId);
146: }
147:
148: Object source = name;
149: if (source == null)
150: source = this ;
151: JMXConnectionNotification notification = new JMXConnectionNotification(
152: JMXConnectionNotification.FAILED, source, connectionId,
153: getNextSequenceNumber(), message, userData);
154: sendNotification(notification);
155: }
156:
157: private long getNextSequenceNumber() {
158: synchronized (JMXConnectorServer.class) {
159: return ++notificationSequenceNumber;
160: }
161: }
162: }
|