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 mx4j.tools.remote;
010:
011: import java.io.IOException;
012: import java.util.Collections;
013: import java.util.Map;
014: import javax.management.MBeanServer;
015: import javax.management.remote.JMXConnectorServer;
016: import javax.management.remote.JMXServiceURL;
017:
018: import mx4j.log.Log;
019: import mx4j.log.Logger;
020: import mx4j.remote.MX4JRemoteUtils;
021:
022: /**
023: * Extends the implementation of JMXConnectorServer by implementing most
024: * JMXConnectorServer methods following the JSR 160 specification and delegating
025: * implementation specific operations using the template method pattern.
026: *
027: * @version $Revision: 1.8 $
028: * @see ConnectionManager
029: */
030: public abstract class AbstractJMXConnectorServer extends
031: JMXConnectorServer {
032: private JMXServiceURL url;
033: private final Map environment;
034: private volatile boolean active;
035: private volatile boolean stopped;
036:
037: public AbstractJMXConnectorServer(JMXServiceURL url,
038: Map environment, MBeanServer server) {
039: super (server);
040: this .url = url;
041: this .environment = environment;
042: }
043:
044: public synchronized JMXServiceURL getAddress() {
045: return url;
046: }
047:
048: /**
049: * Sets the JMXServiceURL that represent the address of this JMXConnectorServer
050: */
051: protected synchronized void setAddress(JMXServiceURL url) {
052: this .url = url;
053: }
054:
055: public synchronized Map getAttributes() {
056: Map env = MX4JRemoteUtils
057: .removeNonSerializableEntries(getEnvironment());
058: return Collections.unmodifiableMap(env);
059: }
060:
061: /**
062: * Returns the environment Map as is, without removing non-serializable entries like {@link #getAttributes} does.
063: */
064: protected synchronized Map getEnvironment() {
065: return environment;
066: }
067:
068: public boolean isActive() {
069: return active;
070: }
071:
072: /**
073: * Returns whether the {@link #stop} method of this JMXConnectorServer has been called.
074: */
075: protected boolean isStopped() {
076: return stopped;
077: }
078:
079: public synchronized void start() throws IOException,
080: IllegalStateException {
081: Logger logger = getLogger();
082:
083: if (isActive()) {
084: if (logger.isEnabledFor(Logger.TRACE))
085: logger
086: .trace("This JMXConnectorServer has already been started");
087: return;
088: }
089: if (isStopped()) {
090: if (logger.isEnabledFor(Logger.TRACE))
091: logger
092: .trace("This JMXConnectorServer has already been stopped");
093: throw new IOException(
094: "This RMIConnectorServer has already been stopped");
095: }
096:
097: doStart();
098:
099: active = true;
100:
101: if (logger.isEnabledFor(Logger.INFO))
102: logger.info("JMXConnectorServer started at: "
103: + getAddress());
104: }
105:
106: /**
107: * Template method implemented by subclasses to start this JMXConnectorServer
108: */
109: protected abstract void doStart() throws IOException,
110: IllegalStateException;
111:
112: public synchronized void stop() throws IOException {
113: if (!isActive() || isStopped())
114: return;
115:
116: stopped = true;
117: active = false;
118:
119: doStop();
120:
121: Logger logger = getLogger();
122: if (logger.isEnabledFor(Logger.INFO))
123: logger.info("JMXConnectorServer stopped at: "
124: + getAddress());
125: }
126:
127: /**
128: * Template method implemented by subclasses to stop this JMXConnectorServer
129: */
130: protected abstract void doStop() throws IOException;
131:
132: protected Logger getLogger() {
133: return Log.getLogger(getClass().getName());
134: }
135:
136: public void connectionOpened(String connectionId, String message,
137: Object userData) {
138: super .connectionOpened(connectionId, message, userData);
139: }
140:
141: public void connectionClosed(String connectionId, String message,
142: Object userData) {
143: super .connectionClosed(connectionId, message, userData);
144: }
145:
146: public void connectionFailed(String connectionId, String message,
147: Object userData) {
148: super.connectionFailed(connectionId, message, userData);
149: }
150: }
|