KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > http > HTTPServerILService


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.il.http;
23
24 import java.net.InetAddress JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.jboss.mq.il.ServerIL;
28 import org.jboss.mq.il.ServerILJMXService;
29 import org.jboss.system.server.ServerConfigUtil;
30
31 /**
32  * Implements the ServerILJMXService which is used to manage the HTTP/S IL.
33  *
34  * @author Nathan Phelps (nathan@jboss.org)
35  * @version $Revision: 37459 $
36  * @created January 15, 2003
37  * @jmx:mbean extends="org.jboss.mq.il.ServerILJMXServiceMBean"
38  */

39 public class HTTPServerILService extends ServerILJMXService implements HTTPServerILServiceMBean
40 {
41
42    private HTTPServerIL serverIL;
43    private String JavaDoc url = null;
44    private String JavaDoc urlPrefix = "http://";
45    private int urlPort = 8080;
46    private String JavaDoc urlSuffix = "jbossmq-httpil/HTTPServerILServlet";
47    private String JavaDoc urlHostName = null;
48    private boolean useHostName = false;
49    private long timeout = 60 * 1000;
50    private long restInterval = 0;
51
52    public HTTPServerILService()
53    {
54    }
55
56    public String JavaDoc getName()
57    {
58       return "JBossMQ-HTTPServerIL";
59    }
60
61    public ServerIL getServerIL()
62    {
63       return this.serverIL;
64    }
65
66    public Properties JavaDoc getClientConnectionProperties()
67    {
68       Properties JavaDoc properties = super.getClientConnectionProperties();
69       properties.setProperty(HTTPServerILFactory.CLIENT_IL_SERVICE_KEY, HTTPServerILFactory.CLIENT_IL_SERVICE);
70       properties.setProperty(HTTPServerILFactory.SERVER_URL_KEY, this.url);
71       properties.setProperty(HTTPServerILFactory.TIMEOUT_KEY, String.valueOf(this.timeout));
72       properties.setProperty(HTTPServerILFactory.REST_INTERVAL_KEY, String.valueOf(this.restInterval));
73       return properties;
74    }
75
76    public void startService() throws Exception JavaDoc
77    {
78       super.startService();
79       if (this.url == null)
80       {
81          this.url = this.getConstructedURL();
82       }
83       this.serverIL = new HTTPServerIL(this.url);
84       super.bindJNDIReferences();
85    }
86
87    public void stopService()
88    {
89       try
90       {
91          unbindJNDIReferences();
92       }
93       catch (Exception JavaDoc e)
94       {
95          e.printStackTrace();
96       }
97    }
98
99    /**
100     * Set the HTTPIL default timeout--the number of seconds that the ClientILService
101     * HTTP requests will wait for messages. This can be overridden on the client
102     * by setting the system property org.jboss.mq.il.http.timeout to an int value
103     * of the number of seconds. NOTE: This value should be in seconds, not millis.
104     *
105     * @jmx:managed-attribute
106     */

107    public void setTimeOut(int timeout)
108    {
109       this.timeout = timeout * 1000; // provided in seconds so turn it into Millis
110
}
111
112    /**
113     * Get the HTTPIL default timeout
114     *
115     * @jmx:managed-attribute
116     */

117    public int getTimeOut()
118    {
119       return (int) this.timeout / 1000; // stored in Millis, but return it in seconds
120
}
121
122    /**
123     * Set the HTTPIL default RestInterval--the number of seconds the ClientILService
124     * will sleep after each client request. The default is 0, but you can set this
125     * value in conjunction with the TimeOut value to implement a pure timed based
126     * polling mechanism. For example, you could simply do a short lived request by
127     * setting the TimeOut value to 0 and then setting the RestInterval to 60. This
128     * would cause the ClientILService to send a single non-blocking request to the
129     * server, return any messages if available, then sleep for 60 seconds, before
130     * issuing another request. Like the TimeOut value, this can be explicitly
131     * overriden on a given client by specifying the org.jboss.mq.il.http.restinterval
132     * with the number of seconds you wish to wait between requests.
133     *
134     * @jmx:managed-attribute
135     */

136    public void setRestInterval(int restInterval)
137    {
138       this.restInterval = restInterval * 1000; // provided in seconds so turn it into Millis
139
}
140
141    /**
142     * Get the HTTPIL default RestInterval
143     *
144     * @jmx:managed-attribute
145     */

146    public int getRestInterval()
147    {
148       return (int) this.restInterval / 1000; // stored in Millis, but return it in seconds
149
}
150
151    /**
152     * Set the HTTPIL URL. This value takes precedence over any individual values
153     * set (i.e. the URLPrefix, URLSuffix, URLPort, etc.) It my be a actual
154     * URL or a propertyname which will be used on the client side to resolve the
155     * proper URL by calling System.getProperty(propertyname).
156     *
157     * @jmx:managed-attribute
158     */

159    public void setURL(String JavaDoc url)
160    {
161       this.url = url;
162       // Set all specific url properties to null values. I know we could parse
163
// the URL and set these, but the url might be a property name. Besides
164
// letting them have value in this case might mislead people into thinking
165
// that the value mattered. As the Javadoc states, when the URL is set
166
// all these value are irrelivant.
167
this.urlPrefix = null;
168       this.urlHostName = null;
169       this.urlPort = 0;
170       this.urlSuffix = null;
171       this.useHostName = false;
172    }
173
174    /**
175     * Get the HTTPIL URL. This value takes precedence over any individual values
176     * set (i.e. the URLPrefix, URLSuffix, URLPort, etc.) It my be a actual
177     * URL or a propertyname which will be used on the client side to resolve the
178     * proper URL by calling System.getProperty(propertyname).
179     *
180     * @jmx:managed-attribute
181     */

182    public String JavaDoc getURL()
183    {
184       return this.url;
185    }
186
187    /**
188     * Set the HTTPIL URLPrefix. I.E. "http://" or "https://"
189     * The default is "http://"
190     *
191     * @jmx:managed-attribute
192     */

193    public void setURLPrefix(String JavaDoc prefix)
194    {
195       this.urlPrefix = prefix;
196    }
197
198    /**
199     * Get the HTTPIL URLPrefix. I.E. "http://" or "https://"
200     * The default is "http://"
201     *
202     * @jmx:managed-attribute
203     */

204    public String JavaDoc getURLPrefix()
205    {
206       return this.urlPrefix;
207    }
208
209    /**
210     * Set the HTTPIL URLName.
211     *
212     * @jmx:managed-attribute
213     */

214    public void setURLHostName(String JavaDoc hostname)
215    {
216       this.urlHostName = hostname;
217    }
218
219    /**
220     * Get the HTTPIL URLHostName.
221     *
222     * @jmx:managed-attribute
223     */

224    public String JavaDoc getURLHostName()
225    {
226       return this.urlHostName;
227    }
228
229    /**
230     * Set the HTTPIL URLPort.
231     * The default is 8080
232     *
233     * @jmx:managed-attribute
234     */

235    public void setURLPort(int port)
236    {
237       this.urlPort = port;
238    }
239
240    /**
241     * Get the HTTPIL URLPort.
242     * The default is 8080
243     *
244     * @jmx:managed-attribute
245     */

246    public int getURLPort()
247    {
248       return this.urlPort;
249    }
250
251    /**
252     * Set the HTTPIL URLSuffix. I.E. The section of the URL after the port
253     * The default is "jbossmq-httpil/HTTPServerILServlet"
254     *
255     * @jmx:managed-attribute
256     */

257    public void setURLSuffix(String JavaDoc suffix)
258    {
259       this.urlSuffix = suffix;
260    }
261
262    /**
263     * Get the HTTPIL URLSuffix. I.E. The section of the URL after the port
264     * The default is "jbossmq-httpil/HTTPServerILServlet"
265     *
266     * @jmx:managed-attribute
267     */

268    public String JavaDoc getURLSuffix()
269    {
270       return this.urlSuffix;
271    }
272
273
274    /**
275     * Set the HTTPIL UseHostName flag.
276     * if true the default URL will include a hostname, if false it will include
277     * an IP adddress. The default is false
278     *
279     * @jmx:managed-attribute
280     */

281    public void setUseHostName(boolean value)
282    {
283       this.useHostName = value;
284    }
285
286    /**
287     * Get the HTTPIL UseHostName flag.
288     * if true the default URL will include a hostname, if false it will include
289     * an IP adddress. The default is false
290     *
291     * @jmx:managed-attribute
292     */

293    public boolean getUseHostName()
294    {
295       return this.useHostName;
296    }
297
298
299    /** Build the connection url from the org.jboss.mq.il.http.url system
300     * property if specified, else, build it from the
301     * urlPrefix + urlHostName + urlPort + urlSuffix. If urlHostName is null,
302     * it will be taken from the jboss.bind.address system property if its a
303     * valid address, InetAddress.getLocalHost otherwise.
304     */

305    private String JavaDoc getConstructedURL() throws Exception JavaDoc
306    {
307       if (System.getProperty(HTTPServerILFactory.SERVER_URL_KEY) != null)
308       {
309          return System.getProperty(HTTPServerILFactory.SERVER_URL_KEY);
310       }
311       else
312       {
313          String JavaDoc hostName = this.urlHostName;
314          if (hostName == null)
315          {
316             // First check for a global bind address
317
hostName = ServerConfigUtil.getSpecificBindAddress();
318          }
319          if (hostName == null)
320          {
321             // Else use the InetAddress.getLocalHost
322
if (this.useHostName)
323             {
324                hostName = InetAddress.getLocalHost().getHostName();
325             }
326             else
327             {
328                hostName = InetAddress.getLocalHost().getHostAddress();
329             }
330          }
331          return this.urlPrefix + hostName + ":" + String.valueOf(this.urlPort)
332             + "/" + this.urlSuffix;
333       }
334    }
335 }
336
337
Popular Tags