KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > server > jetty > CustomHttpContext


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.server.jetty;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.mortbay.http.HttpContext;
31 import org.mortbay.http.HttpServer;
32 import org.mortbay.http.ResourceCache;
33 import org.mortbay.util.Resource;
34
35 import com.sslexplorer.boot.ContextHolder;
36 import com.sslexplorer.boot.RequestHandler;
37 import com.sslexplorer.server.Main;
38
39 /**
40  * <p>An extension to the standard Jetty {@link org.mortbay.http.HttpContext}
41  * that allows resources to be loaded from multiple {@link org.mortbay.http.ResourceCache}s.
42  *
43  * <p>This is necessary for the plugin architecture so that plugins may register
44  * their own <b>webapp</b> directories which can then be overlaid onto the
45  * namespace of the main SSL-Explorer webapp.
46  *
47  * @author Brett Smith <brett@3sp.com>
48  */

49 public class CustomHttpContext extends HttpContext {
50
51     // Package protected statics
52
final static Log log = LogFactory.getLog(CustomHttpContext.class);
53
54     // Private statics
55
private static final long serialVersionUID = -4556775842230104865L;
56     
57     // Private instance variables
58

59     private List JavaDoc resourceCaches = new ArrayList JavaDoc();
60     private RequestHandlerAdapter requestHandlerAdapater;
61
62     /**
63      * Constructor
64      *
65      * @param server
66      * @param path
67      */

68     public CustomHttpContext(HttpServer server, String JavaDoc path, boolean useDevConfig) throws Exception JavaDoc{
69         super(server, path);
70         addHandler(requestHandlerAdapater = new RequestHandlerAdapter());
71         if (useDevConfig) {
72             setClassLoader(Main.class.getClassLoader());
73             setClassLoaderJava2Compliant(true);
74         }
75         setTempDirectory(ContextHolder.getContext().getTempDirectory());
76         setWelcomeFiles(new String JavaDoc[] { "showHome.do" });
77         resourceCaches = new ArrayList JavaDoc();
78     }
79
80     /**
81      * <p>
82      * Add a new {@link RequestHandler}. Every time a request is received that
83      * is not serviced by the main webapp, each registered handler will be
84      * invoked until one deals with the request.
85      *
86      * <p>
87      * This shouldn't be called directly, but through
88      * {@link com.sslexplorer.boot.Context#registerRequestHandler(RequestHandler)}
89      *
90      * @param requestHandler handler to add
91      */

92     public void registerRequestHandler(RequestHandler requestHandler) {
93         requestHandlerAdapater.registerRequestHandler(requestHandler);
94     }
95
96     /**
97      * <p>
98      * Remove a {@link RequestHandler} so that is no longer received unhandled
99      * requests. See
100      * {@link com.sslexplorer.boot.Context#deregisterRequestHandler(RequestHandler)}.
101      *
102      * <p>
103      * This shouldn't be called directly, but through
104      * {@link com.sslexplorer.boot.Context#registerRequestHandler(RequestHandler)}
105      *
106      * @param requestHandler handler to remove
107      */

108     public void deregisterRequestHandler(RequestHandler requestHandler) {
109         requestHandlerAdapater.deregisterRequestHandler(requestHandler);
110     }
111     
112     /**
113      * <p>Add a new Resource Cache. Whenever a resource is requested, this
114      * handler will search all registered resource caches until one can
115      * locate it.
116      *
117      * <p>This shouldn't be called directly, but through
118      * {@link com.sslexplorer.boot.Context#addResourceBase(URL)}
119      *
120      * @param cache cache to add
121      */

122     public void addResourceCache(ResourceCache cache) {
123         resourceCaches.add(cache);
124     }
125
126     /**
127      * <p>Remove a Resource Cache. Whenever a resource is requested, this
128      * handler will no longer use this cache.
129      *
130      * <p>This shouldn't be called directly, but through
131      * {@link com.sslexplorer.boot.Context#removeResourceBase(URL)}
132      *
133      * @param cache cache to remove
134      */

135     public void removeResourceCache(ResourceCache cache) {
136         resourceCaches.remove(cache);
137     }
138
139     /* (non-Javadoc)
140      * @see org.mortbay.http.ResourceCache#getResource(java.lang.String)
141      */

142     public Resource getResource(String JavaDoc pathInContext) throws IOException JavaDoc {
143         if (log.isDebugEnabled())
144             log.debug("Getting resource " + pathInContext + ", checking in plugins");
145         // First try all the plugins with the path for an overidden resource
146
for (Iterator JavaDoc i = resourceCaches.iterator(); i.hasNext();) {
147             ResourceCache cache = (ResourceCache) i.next();
148             Resource r = cache.getResource(pathInContext);
149             if (r != null && r.exists() && !r.isDirectory()) {
150                 if (log.isDebugEnabled())
151                     log.debug("Found in " + cache.getBaseResource().toString());
152                 return r;
153             }
154         }
155         if (log.isDebugEnabled())
156             log.debug("Checking for alias");
157
158         // Get from the main webapp
159
if (log.isDebugEnabled())
160             log.debug("Passsing to main webapp");
161         return super.getResource(pathInContext);
162     }
163 }
164
Popular Tags