KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > WebService


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.web;
23
24 import java.io.IOException JavaDoc;
25 import java.net.InetAddress JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.UnknownHostException JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 import org.jboss.system.MissingAttributeException;
35 import org.jboss.system.ServiceMBeanSupport;
36 import org.jboss.system.server.ServerConfigUtil;
37 import org.jboss.util.ThrowableHandler;
38 import org.jboss.util.threadpool.BasicThreadPoolMBean;
39
40 /**
41  * The WebService implementation. It configures a WebServer instance to
42  * perform dynamic class and resource loading.
43  *
44  * @jmx:mbean
45  * extends="org.jboss.system.ServiceMBean"
46  * name="jboss:service=WebService"
47  *
48  * @version <tt>$Revision: 37459 $</tt>
49  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard �berg</a>.
50  * @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
51  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
52  */

53 public class WebService extends ServiceMBeanSupport implements WebServiceMBean
54 {
55    //
56
// jason: WebService and WebServer classes should be merged into one
57
// WebService only provides a thin layer of JMX which WebServer
58
// can do just as well.
59
//
60
// Doing so will dramatically simplify this component.
61
//
62
// Rename to HTTPClassLoaderService to be clear that this is not
63
// a fully functional web server.
64
//
65

66    /**
67     * The web server instance which does the work.
68     *
69     * <p>
70     * Note: This value shadows the MBeanServer server value
71     * from ServiceMBeanSupport.
72     */

73    private WebServer server = new WebServer();
74
75    /**
76     * @jmx:managed-constructor
77     */

78    public WebService()
79    {
80       super();
81    }
82
83    /**
84     * @jmx:managed-operation
85     */

86    public URL JavaDoc addClassLoader(ClassLoader JavaDoc cl)
87    {
88       return server.addClassLoader(cl);
89    }
90
91    /**
92     * @jmx:managed-operation
93     */

94    public void removeClassLoader(ClassLoader JavaDoc cl)
95    {
96       server.removeClassLoader(cl);
97    }
98
99    /**
100     * Set the WebService listening port.
101     *
102     * @jmx:managed-attribute
103     *
104     * @param port The listening port; 0 for anonymous
105     */

106    public void setPort(int port)
107    {
108       server.setPort(port);
109    }
110
111    /**
112     * Get the WebService listening port.
113     *
114     * @jmx:managed-attribute
115     */

116    public int getPort()
117    {
118       return server.getPort();
119    }
120
121    /**
122     * Get the name of the interface to use for the host portion of the
123     * RMI codebase URL.
124     *
125     * @jmx:managed-attribute
126     */

127    public void setHost(final String JavaDoc hostname)
128    {
129       server.setBindAddress(hostname);
130    }
131
132    /**
133     * Set the name of the interface to use for the host portion of the
134     * RMI codebase URL.
135     *
136     * @jmx:managed-attribute
137     */

138    public String JavaDoc getHost()
139    {
140       return server.getBindHostname();
141    }
142
143    /**
144     * Get the specific address the WebService listens on.
145     *
146     * @jmx:managed-attribute
147     *
148     * @return the interface name or IP address the WebService binds to.
149     */

150    public String JavaDoc getBindAddress()
151    {
152       return server.getBindAddress();
153    }
154
155    /**
156     * Set the specific address the WebService listens on. This can be used on
157     * a multi-homed host for a ServerSocket that will only accept connect requests
158     * to one of its addresses.
159     *
160     * @jmx:managed-attribute
161     *
162     * @param host the interface name or IP address to bind. If host is null,
163     * connections on any/all local addresses will be allowed.
164     */

165    public void setBindAddress(String JavaDoc host) throws UnknownHostException JavaDoc
166    {
167       server.setBindAddress(host);
168    }
169
170    /**
171     * Get the WebService listen queue backlog limit. The maximum queue length
172     * for incoming connection indications (a request to connect) is set to the
173     * backlog parameter. If a connection indication arrives when the queue is
174     * full, the connection is refused.
175     *
176     * @jmx:managed-attribute
177     *
178     * @return the queue backlog limit.
179     */

180    public int getBacklog()
181    {
182       return server.getBacklog();
183    }
184
185    /**
186     * Set the WebService listen queue backlog limit. The maximum queue length
187     * for incoming connection indications (a request to connect) is set to the
188     * backlog parameter. If a connection indication arrives when the queue is
189     * full, the connection is refused.
190     *
191     * @jmx:managed-attribute
192     *
193     * @param backlog, the queue backlog limit.
194     */

195    public void setBacklog(int backlog)
196    {
197       server.setBacklog(backlog);
198    }
199
200    /** Set the thread pool used for the WebServer class loading.
201     * @jmx:managed-attribute
202     *
203     */

204    public void setThreadPool(BasicThreadPoolMBean threadPool)
205    {
206       server.setThreadPool(threadPool);
207    }
208
209    /**
210     * A flag indicating if the server should attempt to download classes from
211     * thread context class loader when a request arrives that does not have a
212     * class loader key prefix.
213     *
214     * @jmx:managed-attribute
215     */

216    public boolean getDownloadServerClasses()
217    {
218       return server.getDownloadServerClasses();
219    }
220
221    /**
222     * @jmx:managed-attribute
223     */

224    public void setDownloadServerClasses(boolean flag)
225    {
226       server.setDownloadServerClasses(flag);
227    }
228
229    /**
230     * A flag indicating if the server should attempt to download resources,
231     * i.e. resource paths that don't end in .class
232     *
233     * @jmx:managed-attribute
234     */

235    public boolean getDownloadResources()
236    {
237       return server.getDownloadResources();
238    }
239
240    /**
241     * @jmx:managed-attribute
242     */

243    public void setDownloadResources(boolean flag)
244    {
245       server.setDownloadResources(flag);
246    }
247
248    protected ObjectName JavaDoc getObjectName(MBeanServer JavaDoc server, ObjectName JavaDoc name)
249       throws javax.management.MalformedObjectNameException JavaDoc
250    {
251       return name == null ? OBJECT_NAME : name;
252    }
253
254    protected void createService() throws Exception JavaDoc
255    {
256       // Load the file mime.types into the mapping list
257
Properties JavaDoc mimeTypes = new Properties JavaDoc();
258
259       try
260       {
261          mimeTypes.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jboss/web/mime.types"));
262
263          Enumeration JavaDoc keys = mimeTypes.keys();
264          while (keys.hasMoreElements())
265          {
266             String JavaDoc extension = (String JavaDoc) keys.nextElement();
267             String JavaDoc type = mimeTypes.getProperty(extension);
268             server.addMimeType(extension, type);
269          }
270       }
271       catch (Exception JavaDoc e)
272       {
273          log.error("Failed to load org/jboss/web/mime.types; ignoring", e);
274       }
275
276       String JavaDoc hostname = server.getBindAddress();
277
278       // If not user specified hostname given, then try to determine what it should be
279
if (hostname == null)
280       {
281          // First look for the rmi server name
282
try
283          {
284             hostname = System.getProperty("java.rmi.server.hostname");
285          }
286          catch (SecurityException JavaDoc e)
287          {
288             // ignore, but don't be silent
289
ThrowableHandler.addWarning(e);
290          }
291
292          // else use the localhost name
293
if (hostname == null)
294          {
295             try
296             {
297                hostname = InetAddress.getLocalHost().getHostName();
298             }
299             catch (IOException JavaDoc e)
300             {
301                log.error("Failed to get localhost name; ignoring", e);
302             }
303          }
304
305          if (hostname != null)
306          {
307             setHost(hostname);
308          }
309       }
310       // Host must be set to continue (either by user or detection)
311
String JavaDoc address = getHost();
312       if (address == null)
313          throw new MissingAttributeException("Host");
314
315       // Set the rmi codebase if it is not already set
316
String JavaDoc codebase = System.getProperty("java.rmi.server.codebase");
317       if (codebase == null)
318       {
319          address = ServerConfigUtil.fixRemoteAddress(address);
320
321          codebase = "http://" + address + ":" + getPort() + "/";
322          System.setProperty("java.rmi.server.codebase", codebase);
323       }
324       log.info("Using RMI server codebase: " + codebase);
325    }
326
327    /**
328     * Start the web server for dynamic downloading of classes and resources.
329     *
330     * <p>
331     * The system <tt>java.rmi.server.codebase</tt> is also set to
332     * <em>http://<host>:<port>/</em> if the property has not been set.
333     */

334    protected void startService() throws Exception JavaDoc
335    {
336       // Start the WebServer running
337
server.start();
338       log.debug("Started WebServer with address: " + server.getBindAddress() + ":" + getPort());
339    }
340
341    protected void stopService() throws Exception JavaDoc
342    {
343       server.stop();
344    }
345 }
346
Popular Tags