001 // This file is part of AceWiki. 002 // Copyright 2011, AceWiki developers. 003 // 004 // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU 005 // Lesser General Public License as published by the Free Software Foundation, either version 3 of 006 // the License, or (at your option) any later version. 007 // 008 // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 010 // Lesser General Public License for more details. 011 // 012 // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If 013 // not, see http://www.gnu.org/licenses/. 014 015 package ch.uzh.ifi.attempto.acewiki; 016 017 import java.util.Enumeration; 018 import java.util.HashMap; 019 import java.util.Map; 020 021 import javax.servlet.ServletConfig; 022 import javax.servlet.ServletContext; 023 import javax.servlet.ServletException; 024 import javax.servlet.http.HttpServlet; 025 026 import ch.uzh.ifi.attempto.base.APE; 027 028 /** 029 * This class is a servlet that creates a Backend object and shares it with the AceWiki servlet. 030 * 031 * To use a Backend object from a particular BackendServlet, add a "backend" 032 * parameter to the AceWiki servlet configuration and set its value to the BackendServlet 033 * name. 034 * <servlet> 035 * <servlet-name>acewiki1<servlet-name> 036 * <servlet-class>ch.uzh.ifi.attempto.acewiki.AceWikiServlet</servlet-class> 037 * <load-on-startup>2</load-on-startup> 038 * <init-param> 039 * <param-name>backend</param-name> 040 * <param-value>backend1</param-value> 041 * </init-param> 042 * </servlet> 043 * <servlet> 044 * <servlet-name>backend1<servlet-name> 045 * <servlet-class>ch.uzh.ifi.attempto.acewiki.BackendServlet</servlet-class> 046 * <load-on-startup>1</load-on-startup> 047 * <!-- other parameter --> 048 * </servlet> 049 * 050 * This servlet accepts all parameters that the AceWiki servlet accepts. 051 * 052 * @author Yu Changyuan 053 */ 054 public class BackendServlet extends HttpServlet { 055 private Backend backend; 056 private static final long serialVersionUID = 1358039576597838L; 057 058 @SuppressWarnings("rawtypes") 059 private Map<String, String> getInitParameters(ServletConfig config) { 060 061 Map<String, String> initParameters = new HashMap<String, String>(); 062 Enumeration paramEnum = config.getInitParameterNames(); 063 while (paramEnum.hasMoreElements()) { 064 String n = paramEnum.nextElement().toString(); 065 initParameters.put(n, config.getInitParameter(n)); 066 } 067 Enumeration contextParamEnum = config.getServletContext().getInitParameterNames(); 068 while (contextParamEnum.hasMoreElements()) { 069 String n = contextParamEnum.nextElement().toString(); 070 initParameters.put("context:" + n, config.getServletContext().getInitParameter(n)); 071 } 072 return initParameters; 073 } 074 075 public void init(ServletConfig config) throws ServletException { 076 Map<String, String> parameters = getInitParameters(config); 077 String name = config.getServletName(); 078 079 setDefaultValues(parameters); 080 081 APE.setParameters(parameters); 082 083 backend = new Backend(parameters); 084 085 ServletContext ctx = config.getServletContext(); 086 ctx.setAttribute(name, backend); 087 088 super.init(config); 089 } 090 091 /** 092 * Sets some default values for the given parameter map. 093 * 094 * @param parameters The parameter map. 095 */ 096 public static void setDefaultValues(Map<String, String> parameters) { 097 if (parameters.get("context:apecommand") == null) { 098 parameters.put("context:apecommand", "ape.exe"); 099 } 100 101 if (parameters.get("context:logdir") == null) { 102 parameters.put("context:logdir", "logs"); 103 } 104 105 if (parameters.get("context:datadir") == null) { 106 parameters.put("context:datadir", "data"); 107 } 108 } 109 } 110