001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.core;
016:
017: import java.io.IOException;
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.Map;
021: import javax.servlet.ServletConfig;
022: import javax.servlet.ServletContext;
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServlet;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.araneaframework.AraneaVersion;
030: import org.araneaframework.Environment;
031: import org.araneaframework.core.StandardEnvironment;
032: import org.araneaframework.http.ServletServiceAdapterComponent;
033:
034: /**
035: * Aranea's main servlet. Routes GET & POST methods to the root component's
036: * <code>service(HttpServletRequest, HttpServletResponse)</code> method.
037: * <p>
038: * Enriches the environment with objects of ServletContext and ServletConfig. They
039: * can be accessed later from the environment under the keys ServletContext.class
040: * and ServletConfig.class respectively.
041: * </p>
042: *
043: * @author "Toomas Römer" <toomas@webmedia.ee>
044: */
045: public abstract class BaseAraneaDispatcherServlet extends HttpServlet {
046: private static final Log log = LogFactory
047: .getLog(BaseAraneaDispatcherServlet.class);
048: private ServletServiceAdapterComponent serviceAdapter;
049:
050: public void init() throws ServletException {
051: serviceAdapter = buildRootComponent();
052: buildAlternateRootComponents();
053:
054: Environment env = new StandardEnvironment(null,
055: getServletEnvironmentMap());
056: try {
057: serviceAdapter._getComponent().init(null, env);
058: } catch (Exception e) {
059: log.info("Unable to start " + AraneaVersion.getTitle()
060: + " " + AraneaVersion.getVersion(), e);
061: throw new ServletException(e.getMessage(), e);
062: }
063:
064: log.info(AraneaVersion.getTitle() + " "
065: + AraneaVersion.getVersion() + " started");
066: }
067:
068: protected void doGet(HttpServletRequest req,
069: HttpServletResponse resp) throws ServletException,
070: IOException {
071: try {
072: getServiceAdapter(req).service(req, resp);
073: } catch (Exception e) {
074: throw new ServletException(e.getMessage(), e);
075: }
076: }
077:
078: protected void doPost(HttpServletRequest req,
079: HttpServletResponse resp) throws ServletException,
080: IOException {
081: try {
082: getServiceAdapter(req).service(req, resp);
083: } catch (Exception e) {
084: throw new ServletException(e.getMessage(), e);
085: }
086: }
087:
088: /**
089: * Returns {@link ServletServiceAdapterComponent} that should service the given request.
090: * Default implementation returns component built by {@link BaseAraneaDispatcherServlet#buildRootComponent()}.
091: * @since 1.0.7
092: * @return ServletServiceAdapterComponent that should service the given request
093: */
094: protected ServletServiceAdapterComponent getServiceAdapter(
095: HttpServletRequest req) {
096: return serviceAdapter;
097: }
098:
099: /**
100: * Builds the default (and in most cases only) root component of this servlet.
101: * All the requests (GET & POST) through some root component, when no alternate
102: * root components are defined, requests will always go through the root component
103: * built here.
104: *
105: * @return default root component
106: */
107: protected abstract ServletServiceAdapterComponent buildRootComponent();
108:
109: /**
110: * Should build all alternate root component hierarchies, if applicable.
111: * Base implementation does not build any. If this is overridden, also
112: * {@link BaseAraneaDispatcherServlet#getServiceAdapter(HttpServletRequest)}
113: * should be overriden to choose the correct component hierarchy for
114: * processing the request.
115: *
116: * @since 1.0.7
117: */
118: protected void buildAlternateRootComponents() {
119: }
120:
121: protected Map getEnvironmentEntries() {
122: return Collections.EMPTY_MAP;
123: }
124:
125: /**
126: * @return map with same entries as {@link BaseAraneaDispatcherServlet#getEnvironmentEntries()} plus servlet
127: * container specific entries (ServletContext, ServletConfig)
128: * @since 1.0.7
129: */
130: protected Map getServletEnvironmentMap() {
131: Map entries = new HashMap();
132: entries.put(ServletContext.class, getServletContext());
133: entries.put(ServletConfig.class, getServletConfig());
134: entries.putAll(getEnvironmentEntries());
135: return entries;
136: }
137: }
|