001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq.il.http;
023:
024: import java.net.InetAddress;
025: import java.util.Properties;
026:
027: import org.jboss.mq.il.ServerIL;
028: import org.jboss.mq.il.ServerILJMXService;
029: import org.jboss.system.server.ServerConfigUtil;
030:
031: /**
032: * Implements the ServerILJMXService which is used to manage the HTTP/S IL.
033: *
034: * @author Nathan Phelps (nathan@jboss.org)
035: * @version $Revision: 57198 $
036: * @created January 15, 2003
037: * @jmx:mbean extends="org.jboss.mq.il.ServerILJMXServiceMBean"
038: */
039: public class HTTPServerILService extends ServerILJMXService implements
040: HTTPServerILServiceMBean {
041:
042: private HTTPServerIL serverIL;
043: private String url = null;
044: private String urlPrefix = "http://";
045: private int urlPort = 8080;
046: private String urlSuffix = "jbossmq-httpil/HTTPServerILServlet";
047: private String urlHostName = null;
048: private boolean useHostName = false;
049: private long timeout = 60 * 1000;
050: private long restInterval = 0;
051:
052: public HTTPServerILService() {
053: }
054:
055: public String getName() {
056: return "JBossMQ-HTTPServerIL";
057: }
058:
059: public ServerIL getServerIL() {
060: return this .serverIL;
061: }
062:
063: public Properties getClientConnectionProperties() {
064: Properties properties = super .getClientConnectionProperties();
065: properties.setProperty(
066: HTTPServerILFactory.CLIENT_IL_SERVICE_KEY,
067: HTTPServerILFactory.CLIENT_IL_SERVICE);
068: properties.setProperty(HTTPServerILFactory.SERVER_URL_KEY,
069: this .url);
070: properties.setProperty(HTTPServerILFactory.TIMEOUT_KEY, String
071: .valueOf(this .timeout));
072: properties.setProperty(HTTPServerILFactory.REST_INTERVAL_KEY,
073: String.valueOf(this .restInterval));
074: return properties;
075: }
076:
077: public void startService() throws Exception {
078: super .startService();
079: if (this .url == null) {
080: this .url = this .getConstructedURL();
081: }
082: this .serverIL = new HTTPServerIL(this .url);
083: super .bindJNDIReferences();
084: }
085:
086: public void stopService() {
087: try {
088: unbindJNDIReferences();
089: } catch (Exception e) {
090: e.printStackTrace();
091: }
092: }
093:
094: /**
095: * Set the HTTPIL default timeout--the number of seconds that the ClientILService
096: * HTTP requests will wait for messages. This can be overridden on the client
097: * by setting the system property org.jboss.mq.il.http.timeout to an int value
098: * of the number of seconds. NOTE: This value should be in seconds, not millis.
099: *
100: * @jmx:managed-attribute
101: */
102: public void setTimeOut(int timeout) {
103: this .timeout = timeout * 1000; // provided in seconds so turn it into Millis
104: }
105:
106: /**
107: * Get the HTTPIL default timeout
108: *
109: * @jmx:managed-attribute
110: */
111: public int getTimeOut() {
112: return (int) this .timeout / 1000; // stored in Millis, but return it in seconds
113: }
114:
115: /**
116: * Set the HTTPIL default RestInterval--the number of seconds the ClientILService
117: * will sleep after each client request. The default is 0, but you can set this
118: * value in conjunction with the TimeOut value to implement a pure timed based
119: * polling mechanism. For example, you could simply do a short lived request by
120: * setting the TimeOut value to 0 and then setting the RestInterval to 60. This
121: * would cause the ClientILService to send a single non-blocking request to the
122: * server, return any messages if available, then sleep for 60 seconds, before
123: * issuing another request. Like the TimeOut value, this can be explicitly
124: * overriden on a given client by specifying the org.jboss.mq.il.http.restinterval
125: * with the number of seconds you wish to wait between requests.
126: *
127: * @jmx:managed-attribute
128: */
129: public void setRestInterval(int restInterval) {
130: this .restInterval = restInterval * 1000; // provided in seconds so turn it into Millis
131: }
132:
133: /**
134: * Get the HTTPIL default RestInterval
135: *
136: * @jmx:managed-attribute
137: */
138: public int getRestInterval() {
139: return (int) this .restInterval / 1000; // stored in Millis, but return it in seconds
140: }
141:
142: /**
143: * Set the HTTPIL URL. This value takes precedence over any individual values
144: * set (i.e. the URLPrefix, URLSuffix, URLPort, etc.) It my be a actual
145: * URL or a propertyname which will be used on the client side to resolve the
146: * proper URL by calling System.getProperty(propertyname).
147: *
148: * @jmx:managed-attribute
149: */
150: public void setURL(String url) {
151: this .url = url;
152: // Set all specific url properties to null values. I know we could parse
153: // the URL and set these, but the url might be a property name. Besides
154: // letting them have value in this case might mislead people into thinking
155: // that the value mattered. As the Javadoc states, when the URL is set
156: // all these value are irrelivant.
157: this .urlPrefix = null;
158: this .urlHostName = null;
159: this .urlPort = 0;
160: this .urlSuffix = null;
161: this .useHostName = false;
162: }
163:
164: /**
165: * Get the HTTPIL URL. This value takes precedence over any individual values
166: * set (i.e. the URLPrefix, URLSuffix, URLPort, etc.) It my be a actual
167: * URL or a propertyname which will be used on the client side to resolve the
168: * proper URL by calling System.getProperty(propertyname).
169: *
170: * @jmx:managed-attribute
171: */
172: public String getURL() {
173: return this .url;
174: }
175:
176: /**
177: * Set the HTTPIL URLPrefix. I.E. "http://" or "https://"
178: * The default is "http://"
179: *
180: * @jmx:managed-attribute
181: */
182: public void setURLPrefix(String prefix) {
183: this .urlPrefix = prefix;
184: }
185:
186: /**
187: * Get the HTTPIL URLPrefix. I.E. "http://" or "https://"
188: * The default is "http://"
189: *
190: * @jmx:managed-attribute
191: */
192: public String getURLPrefix() {
193: return this .urlPrefix;
194: }
195:
196: /**
197: * Set the HTTPIL URLName.
198: *
199: * @jmx:managed-attribute
200: */
201: public void setURLHostName(String hostname) {
202: this .urlHostName = hostname;
203: }
204:
205: /**
206: * Get the HTTPIL URLHostName.
207: *
208: * @jmx:managed-attribute
209: */
210: public String getURLHostName() {
211: return this .urlHostName;
212: }
213:
214: /**
215: * Set the HTTPIL URLPort.
216: * The default is 8080
217: *
218: * @jmx:managed-attribute
219: */
220: public void setURLPort(int port) {
221: this .urlPort = port;
222: }
223:
224: /**
225: * Get the HTTPIL URLPort.
226: * The default is 8080
227: *
228: * @jmx:managed-attribute
229: */
230: public int getURLPort() {
231: return this .urlPort;
232: }
233:
234: /**
235: * Set the HTTPIL URLSuffix. I.E. The section of the URL after the port
236: * The default is "jbossmq-httpil/HTTPServerILServlet"
237: *
238: * @jmx:managed-attribute
239: */
240: public void setURLSuffix(String suffix) {
241: this .urlSuffix = suffix;
242: }
243:
244: /**
245: * Get the HTTPIL URLSuffix. I.E. The section of the URL after the port
246: * The default is "jbossmq-httpil/HTTPServerILServlet"
247: *
248: * @jmx:managed-attribute
249: */
250: public String getURLSuffix() {
251: return this .urlSuffix;
252: }
253:
254: /**
255: * Set the HTTPIL UseHostName flag.
256: * if true the default URL will include a hostname, if false it will include
257: * an IP adddress. The default is false
258: *
259: * @jmx:managed-attribute
260: */
261: public void setUseHostName(boolean value) {
262: this .useHostName = value;
263: }
264:
265: /**
266: * Get the HTTPIL UseHostName flag.
267: * if true the default URL will include a hostname, if false it will include
268: * an IP adddress. The default is false
269: *
270: * @jmx:managed-attribute
271: */
272: public boolean getUseHostName() {
273: return this .useHostName;
274: }
275:
276: /** Build the connection url from the org.jboss.mq.il.http.url system
277: * property if specified, else, build it from the
278: * urlPrefix + urlHostName + urlPort + urlSuffix. If urlHostName is null,
279: * it will be taken from the jboss.bind.address system property if its a
280: * valid address, InetAddress.getLocalHost otherwise.
281: */
282: private String getConstructedURL() throws Exception {
283: if (System.getProperty(HTTPServerILFactory.SERVER_URL_KEY) != null) {
284: return System
285: .getProperty(HTTPServerILFactory.SERVER_URL_KEY);
286: } else {
287: String hostName = this .urlHostName;
288: if (hostName == null) {
289: // First check for a global bind address
290: hostName = ServerConfigUtil.getSpecificBindAddress();
291: }
292: if (hostName == null) {
293: // Else use the InetAddress.getLocalHost
294: if (this .useHostName) {
295: hostName = InetAddress.getLocalHost().getHostName();
296: } else {
297: hostName = InetAddress.getLocalHost()
298: .getHostAddress();
299: }
300: }
301: return this .urlPrefix + hostName + ":"
302: + String.valueOf(this .urlPort) + "/"
303: + this.urlSuffix;
304: }
305: }
306: }
|