01: /*
02: * Copyright 2005-2007 Noelios Consulting.
03: *
04: * The contents of this file are subject to the terms of the Common Development
05: * and Distribution License (the "License"). You may not use this file except in
06: * compliance with the License.
07: *
08: * You can obtain a copy of the license at
09: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
10: * language governing permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL HEADER in each file and
13: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
14: * applicable, add the following below this CDDL HEADER, with the fields
15: * enclosed by brackets "[]" replaced with your own identifying information:
16: * Portions Copyright [yyyy] [name of copyright owner]
17: */
18:
19: package com.noelios.restlet.ext.servlet;
20:
21: import java.util.Enumeration;
22:
23: import javax.servlet.Servlet;
24: import javax.servlet.ServletContext;
25:
26: import org.restlet.Application;
27: import org.restlet.Context;
28:
29: import com.noelios.restlet.application.ApplicationContext;
30:
31: /**
32: * Context allowing access to the component's connectors, reusing the Servlet's
33: * logging mechanism and adding the Servlet's initialization parameters to the
34: * context's parameters.
35: *
36: * @author Jerome Louvel (contact@noelios.com)
37: */
38: public class ServletContextAdapter extends ApplicationContext {
39: /** The Servlet context. */
40: private ServletContext servletContext;
41:
42: /**
43: * Constructor.
44: *
45: * @param servlet
46: * The parent Servlet.
47: * @param parentContext
48: * The parent context.
49: * @param application
50: * The parent application.
51: */
52: @SuppressWarnings("unchecked")
53: public ServletContextAdapter(Servlet servlet,
54: Application application, Context parentContext) {
55: super (application, parentContext, new ServletLogger(servlet
56: .getServletConfig().getServletContext()));
57: this .servletContext = servlet.getServletConfig()
58: .getServletContext();
59:
60: // Set the special WAR client
61: setWarClient(new ServletWarClient(parentContext, servlet
62: .getServletConfig().getServletContext()));
63:
64: // Copy all the servlet parameters into the context
65: String initParam;
66:
67: // Copy all the Web component initialization parameters
68: javax.servlet.ServletConfig servletConfig = servlet
69: .getServletConfig();
70: for (Enumeration<String> enum1 = servletConfig
71: .getInitParameterNames(); enum1.hasMoreElements();) {
72: initParam = enum1.nextElement();
73: getParameters().add(initParam,
74: servletConfig.getInitParameter(initParam));
75: }
76:
77: // Copy all the Web Application initialization parameters
78: for (Enumeration<String> enum1 = getServletContext()
79: .getInitParameterNames(); enum1.hasMoreElements();) {
80: initParam = enum1.nextElement();
81: getParameters().add(initParam,
82: getServletContext().getInitParameter(initParam));
83: }
84: }
85:
86: /**
87: * Returns the Servlet context.
88: *
89: * @return The Servlet context.
90: */
91: public ServletContext getServletContext() {
92: return this.servletContext;
93: }
94:
95: }
|