KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > transport > http > ServiceMapper


1 package org.sapia.ubik.rmi.server.transport.http;
2
3 import java.io.File JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import simple.http.ProtocolHandler;
8 import simple.http.Request;
9 import simple.http.Response;
10 import simple.http.load.Service;
11 import simple.http.serve.Context;
12 import simple.http.serve.FileContext;
13 import simple.http.serve.FileEngine;
14 import simple.util.parse.URIParser;
15
16
17 /**
18  * An instance of this class maps context paths to Simple services (as specified by the Simple API).
19  *
20  * @author Yanick Duchesne
21  * <dl>
22  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
23  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
24  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
25  * </dl>
26  */

27 public class ServiceMapper implements ProtocolHandler {
28   private FileContext _context;
29   private FileEngine _engine = new FileEngine(_context = new FileContext(
30           new File JavaDoc(System.getProperty("user.dir"))));
31   private Map JavaDoc _services = new HashMap JavaDoc();
32
33   public ServiceMapper() {
34   }
35
36   /**
37    * @return this instance's <code>Context</code> object.
38    */

39   public Context getContext() {
40     return _context;
41   }
42
43   public void setBaseDir(File JavaDoc baseDir) {
44     _engine = new FileEngine(_context = new FileContext(baseDir));
45   }
46
47   /**
48    * Adds the given service to this object; internally maps it to the given
49    * context path.
50    *
51    * @param contextPath the path that follows the host:port in a HTTP URL.
52    * @param svc a <code>Service</code> instance, as specified by the Simple API.
53    */

54   public void addService(String JavaDoc contextPath, Service svc) {
55     if (contextPath == null) {
56       contextPath = "/";
57     }
58
59     if (!contextPath.startsWith("/")) {
60       contextPath = "/" + contextPath;
61     }
62
63     _services.put(contextPath, svc);
64   }
65
66   /**
67    * @see simple.http.ProtocolHandler#handle(simple.http.Request, simple.http.Response)
68    */

69   public void handle(Request req, Response res) {
70     URIParser uri = new URIParser();
71     uri.parse(req.getURI());
72
73     if (uri.getPath() != null) {
74       Service svc = (Service) _services.get(uri.getPath().getPath());
75
76       if (svc != null) {
77         svc.handle(req, res);
78       }
79     }
80   }
81 }
82
Popular Tags