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.Serializable;
012: import java.net.InetAddress;
013: import java.net.MalformedURLException;
014: import java.net.UnknownHostException;
015:
016: /**
017: * @version $Revision: 1.12 $
018: */
019: public class JMXServiceURL implements Serializable {
020: private static final long serialVersionUID = 8173364409860779292l;
021:
022: /**
023: * @serial The protocol
024: */
025: private String protocol;
026: /**
027: * @serial The host
028: */
029: private String host;
030: /**
031: * @serial The port
032: */
033: private int port;
034: /**
035: * @serial The path
036: */
037: private String urlPath;
038:
039: private transient int hash;
040:
041: public JMXServiceURL(String url) throws MalformedURLException {
042: if (url == null)
043: throw new NullPointerException("Null JMXServiceURL string");
044: parse(url);
045: }
046:
047: public JMXServiceURL(String protocol, String host, int port)
048: throws MalformedURLException {
049: this (protocol, host, port, null);
050: }
051:
052: public JMXServiceURL(String protocol, String host, int port,
053: String urlPath) throws MalformedURLException {
054: if (port < 0)
055: throw new MalformedURLException(
056: "Port number cannot be less than zero");
057:
058: setProtocol(protocol);
059: setHost(host);
060: setPort(port);
061: setURLPath(urlPath);
062: }
063:
064: private String resolveHost() throws MalformedURLException {
065: try {
066: return InetAddress.getLocalHost().getHostName();
067: } catch (UnknownHostException x) {
068: throw new MalformedURLException(
069: "Cannot resolve local host name");
070: }
071: }
072:
073: public String getProtocol() {
074: return protocol;
075: }
076:
077: private void setProtocol(String protocol) {
078: if (protocol != null)
079: this .protocol = protocol.toLowerCase();
080: else
081: this .protocol = "jmxmp"; // Default required by the spec
082: }
083:
084: public String getHost() {
085: return host;
086: }
087:
088: private void setHost(String host) throws MalformedURLException {
089: if (host != null)
090: this .host = host.toLowerCase();
091: else
092: this .host = resolveHost().toLowerCase(); // Default required by the spec
093: }
094:
095: public int getPort() {
096: return port;
097: }
098:
099: private void setPort(int port) {
100: this .port = port;
101: }
102:
103: public String getURLPath() {
104: return urlPath;
105: }
106:
107: private void setURLPath(String urlPath) {
108: if (urlPath != null) {
109: if (urlPath.length() > 0 && !urlPath.startsWith("/"))
110: urlPath = "/" + urlPath;
111: this .urlPath = urlPath;
112: } else {
113: this .urlPath = ""; // Default required by the spec
114: }
115: }
116:
117: public int hashCode() {
118: if (hash == 0) {
119: hash = getProtocol().hashCode();
120: String host = getHost();
121: hash = 29 * hash + (host != null ? host.hashCode() : 0);
122: hash = 29 * hash + getPort();
123: String path = getURLPath();
124: hash = 29 * hash + (path != null ? path.hashCode() : 0);
125: }
126: return hash;
127: }
128:
129: public boolean equals(Object obj) {
130: if (obj == this )
131: return true;
132: if (!(obj instanceof JMXServiceURL))
133: return false;
134:
135: JMXServiceURL other = (JMXServiceURL) obj;
136:
137: if (!getProtocol().equalsIgnoreCase(other.getProtocol()))
138: return false;
139:
140: String host = getHost();
141: String otherHost = other.getHost();
142: if (host != null ? !host.equalsIgnoreCase(otherHost)
143: : otherHost != null)
144: return false;
145:
146: if (getPort() != other.getPort())
147: return false;
148:
149: String path = getURLPath();
150: String otherPath = other.getURLPath();
151: if (path != null ? !path.equals(otherPath) : otherPath != null)
152: return false;
153:
154: return true;
155: }
156:
157: public String toString() {
158: StringBuffer buffer = new StringBuffer("service:jmx:");
159: buffer.append(getProtocol()).append("://");
160: buffer.append(getHost());
161: int port = getPort();
162: if (port > 0)
163: buffer.append(":").append(port);
164: String path = getURLPath();
165: if (path != null) {
166: if (!path.startsWith("/"))
167: buffer.append("/");
168: buffer.append(path);
169: }
170: return buffer.toString();
171: }
172:
173: private void parse(String url) throws MalformedURLException {
174: String prefix = "service:jmx:";
175: if (url.length() <= prefix.length())
176: throw new MalformedURLException("JMXServiceURL " + url
177: + " must start with " + prefix);
178: String servicejmx = url.substring(0, prefix.length());
179: if (!servicejmx.equalsIgnoreCase(prefix))
180: throw new MalformedURLException("JMXServiceURL " + url
181: + " must start with " + prefix);
182:
183: String parse = url.substring(prefix.length());
184:
185: String hostSeparator = "://";
186: int index = parse.indexOf(hostSeparator);
187: if (index < 0)
188: throw new MalformedURLException(
189: "No protocol defined for JMXServiceURL " + url);
190: String protocol = parse.substring(0, index);
191: checkProtocol(url, protocol);
192: setProtocol(protocol);
193:
194: String hostAndMore = parse.substring(index
195: + hostSeparator.length());
196: index = hostAndMore.indexOf('/');
197: if (index < 0) {
198: parseHostAndPort(url, hostAndMore);
199: setURLPath(null);
200: } else {
201: String hostAndPort = hostAndMore.substring(0, index);
202: parseHostAndPort(url, hostAndPort);
203:
204: String pathAndMore = hostAndMore.substring(index);
205: if (pathAndMore.length() > 0) {
206: checkURLPath(url, pathAndMore);
207: String path = "/".equals(pathAndMore) ? ""
208: : pathAndMore; // Special case
209: setURLPath(path);
210: }
211: }
212: }
213:
214: private void parseHostAndPort(String url, String hostAndPort)
215: throws MalformedURLException {
216: if (hostAndPort.length() == 0) {
217: setHost(null);
218: setPort(0);
219: return;
220: }
221:
222: int colon = hostAndPort.indexOf(':');
223: if (colon == 0)
224: throw new MalformedURLException(
225: "No host defined for JMXServiceURL " + url);
226:
227: if (colon > 0) {
228: String host = hostAndPort.substring(0, colon);
229: checkHost(url, host);
230: setHost(host);
231: String portString = hostAndPort.substring(colon + 1);
232: try {
233: int port = Integer.parseInt(portString);
234: setPort(port);
235: } catch (NumberFormatException x) {
236: throw new MalformedURLException("Invalid port "
237: + portString + " for JMXServiceURL " + url);
238: }
239: } else {
240: checkHost(url, hostAndPort);
241: setHost(hostAndPort);
242: setPort(0);
243: }
244: }
245:
246: private void checkProtocol(String url, String protocol)
247: throws MalformedURLException {
248: if (protocol.length() == 0)
249: throw new MalformedURLException(
250: "No protocol defined for JMXServiceURL " + url);
251: if (!protocol.trim().equals(protocol))
252: throw new MalformedURLException(
253: "No leading or trailing white space allowed in protocol for JMXServiceURL "
254: + url);
255: }
256:
257: private void checkHost(String url, String host)
258: throws MalformedURLException {
259: if (host.length() == 0)
260: throw new MalformedURLException(
261: "No host defined for JMXServiceURL " + url);
262: if (!host.trim().equals(host))
263: throw new MalformedURLException(
264: "No leading or trailing white space allowed in host for JMXServiceURL "
265: + url);
266: }
267:
268: private void checkURLPath(String url, String path)
269: throws MalformedURLException {
270: if (!path.startsWith("/"))
271: throw new MalformedURLException(
272: "Invalid path for JMXServiceURL " + url);
273: if (!path.trim().equals(path))
274: throw new MalformedURLException(
275: "No leading or trailing white space allowed in path for JMXServiceURL "
276: + url);
277: }
278: }
|