1 16 package org.apache.commons.chain.web.servlet; 17 18 19 import java.io.IOException ; 20 import javax.servlet.ServletException ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 23 import org.apache.commons.chain.Catalog; 24 import org.apache.commons.chain.CatalogFactory; 25 import org.apache.commons.chain.Command; 26 import org.apache.commons.chain.web.ChainServlet; 27 28 29 54 55 public class ChainProcessor extends ChainServlet { 56 57 58 60 61 65 public static final String CATALOG = 66 "org.apache.commons.chain.CATALOG"; 67 68 69 73 public static final String CATALOG_DEFAULT = 74 "org.apache.commons.chain.CATALOG"; 75 76 77 82 public static final String COMMAND = 83 "org.apache.commons.chain.COMMAND"; 84 85 86 89 private static final String COMMAND_DEFAULT = "command"; 90 91 92 94 95 102 private String attribute = null; 103 104 105 110 private String catalog = null; 111 112 113 117 private String command = null; 118 119 120 123 private CatalogFactory factory = null; 124 125 126 128 129 132 public void destroy() { 133 134 super.destroy(); 135 attribute = null; 136 catalog = null; 137 command = null; 138 factory = null; 139 140 } 141 142 143 148 public void init() throws ServletException { 149 150 super.init(); 151 attribute = getServletConfig().getInitParameter(CONFIG_ATTR); 152 catalog = getServletConfig().getInitParameter(CATALOG); 153 command = getServletConfig().getInitParameter(COMMAND); 154 if (command == null) { 155 command = COMMAND_DEFAULT; 156 } 157 factory = CatalogFactory.getInstance(); 158 159 } 160 161 162 173 public void service(HttpServletRequest request, 174 HttpServletResponse response) 175 throws IOException , ServletException { 176 177 ServletWebContext context = 178 new ServletWebContext(getServletContext(), request, response); 179 Catalog theCatalog = null; 180 if (attribute != null) { 181 theCatalog = (Catalog) getServletContext().getAttribute 182 (this.attribute); 183 } else if (catalog != null) { 184 theCatalog = factory.getCatalog(catalog); 185 } else { 186 theCatalog = factory.getCatalog(); 187 } 188 if (attribute == null) { 189 request.setAttribute(CATALOG_DEFAULT, theCatalog); 190 } 191 Command command = theCatalog.getCommand(this.command); 192 try { 193 command.execute(context); 194 } catch (Exception e) { 195 throw new ServletException (e); 196 } 197 198 } 199 200 201 } 202 | Popular Tags |