1 16 17 18 package org.apache.axis.transport.http; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.axis.AxisEngine; 33 import org.apache.axis.AxisFault; 34 import org.apache.axis.AxisProperties; 35 import org.apache.axis.EngineConfiguration; 36 import org.apache.axis.components.logger.LogFactory; 37 import org.apache.axis.configuration.EngineConfigurationFactoryFinder; 38 import org.apache.axis.server.AxisServer; 39 import org.apache.axis.utils.JavaUtils; 40 import org.apache.commons.logging.Log; 41 42 50 51 public class AxisServletBase extends HttpServlet { 52 53 56 protected AxisServer axisServer = null; 57 58 private static Log log = 59 LogFactory.getLog(AxisServlet.class.getName()); 60 61 private static boolean isDebug = false; 62 63 66 private static int loadCounter = 0; 67 68 71 private static Object loadCounterLock = new Object (); 72 73 76 protected static final String ATTR_AXIS_ENGINE = 77 "AxisEngine" ; 78 79 82 private String webInfPath = null; 83 84 87 private String homeDir = null; 88 89 92 private boolean isDevelopment; 93 94 97 private static final String INIT_PROPERTY_DEVELOPMENT_SYSTEM= 98 "axis.development.system"; 99 100 101 104 public void init() throws javax.servlet.ServletException { 105 ServletContext context = getServletConfig().getServletContext(); 106 107 webInfPath = context.getRealPath("/WEB-INF"); 108 homeDir = context.getRealPath("/"); 109 110 isDebug = log.isDebugEnabled(); 111 if(log.isDebugEnabled()) log.debug("In AxisServletBase init"); 112 isDevelopment= JavaUtils.isTrueExplicitly(getOption(context, 113 INIT_PROPERTY_DEVELOPMENT_SYSTEM, null)); 114 115 } 116 117 126 public void destroy() { 127 super.destroy(); 128 129 if (axisServer != null) { 131 synchronized(axisServer) { 133 if (axisServer != null) { 134 axisServer.cleanup(); 136 axisServer =null; 138 storeEngine(this,null); 139 } 140 } 141 } 142 } 143 144 149 public AxisServer getEngine() throws AxisFault { 150 if (axisServer == null) 151 axisServer = getEngine(this); 152 return axisServer; 153 } 154 155 156 162 public static AxisServer getEngine(HttpServlet servlet) throws AxisFault 163 { 164 AxisServer engine = null; 165 if (isDebug) 166 log.debug("Enter: getEngine()"); 167 168 ServletContext context = servlet.getServletContext(); 169 synchronized (context) { 170 engine = retrieveEngine(servlet); 171 if (engine == null) { 172 Map environment = getEngineEnvironment(servlet); 173 174 engine = AxisServer.getServer(environment); 186 engine.setName(servlet.getServletName()); 188 storeEngine(servlet, engine); 189 } 190 } 191 192 if (isDebug) 193 log.debug("Exit: getEngine()"); 194 195 return engine; 196 } 197 198 203 private static void storeEngine(HttpServlet servlet, AxisServer engine) { 204 ServletContext context = servlet.getServletContext(); 205 String axisServletName = servlet.getServletName(); 206 if (engine == null) { 207 context.removeAttribute(axisServletName + ATTR_AXIS_ENGINE); 208 AxisServer server = (AxisServer) context.getAttribute(ATTR_AXIS_ENGINE); 210 211 if (server != null && servlet.getServletName().equals(server.getName())) { 213 context.removeAttribute(ATTR_AXIS_ENGINE); 214 } 215 } else { 216 if (context.getAttribute(ATTR_AXIS_ENGINE) == null) { 217 context.setAttribute(ATTR_AXIS_ENGINE, engine); 220 } 221 context.setAttribute(axisServletName + ATTR_AXIS_ENGINE, engine); 222 } 223 } 224 225 234 private static AxisServer retrieveEngine(HttpServlet servlet) { 235 Object contextObject = servlet.getServletContext().getAttribute(servlet.getServletName() + ATTR_AXIS_ENGINE); 236 if (contextObject == null) { 237 contextObject = servlet.getServletContext().getAttribute(ATTR_AXIS_ENGINE); 240 } 241 if (contextObject instanceof AxisServer) { 242 AxisServer server = (AxisServer) contextObject; 243 if (server != null && servlet.getServletName().equals(server.getName())) { 245 return server; 246 } 247 return null; 248 } else { 249 return null; 250 } 251 } 252 253 258 protected static Map getEngineEnvironment(HttpServlet servlet) { 259 Map environment = new HashMap (); 260 261 String attdir= servlet.getInitParameter(AxisEngine.ENV_ATTACHMENT_DIR); 262 if (attdir != null) 263 environment.put(AxisEngine.ENV_ATTACHMENT_DIR, attdir); 264 265 ServletContext context = servlet.getServletContext(); 266 environment.put(AxisEngine.ENV_SERVLET_CONTEXT, context); 267 268 String webInfPath = context.getRealPath("/WEB-INF"); 269 if (webInfPath != null) 270 environment.put(AxisEngine.ENV_SERVLET_REALPATH, 271 webInfPath + File.separator + "attachments"); 272 273 EngineConfiguration config = 274 EngineConfigurationFactoryFinder.newFactory(servlet) 275 .getServerEngineConfig(); 276 277 if (config != null) { 278 environment.put(EngineConfiguration.PROPERTY_NAME, config); 279 } 280 281 return environment; 282 } 283 284 285 291 292 public static int getLoadCounter() { 293 return loadCounter; 294 } 295 296 299 protected static void incLockCounter() { 300 synchronized(loadCounterLock) { 301 loadCounter++; 302 } 303 } 304 305 308 protected static void decLockCounter() { 309 synchronized(loadCounterLock) { 310 loadCounter--; 311 } 312 } 313 314 323 protected void service(HttpServletRequest req, HttpServletResponse resp) 324 throws ServletException , IOException { 325 incLockCounter(); 326 try { 327 super.service(req, resp); 328 } 329 finally { 330 decLockCounter(); 331 } 332 } 333 334 340 protected String getWebappBase(HttpServletRequest request) { 341 StringBuffer baseURL=new StringBuffer (128); 342 baseURL.append(request.getScheme()); 343 baseURL.append("://"); 344 baseURL.append(request.getServerName()); 345 if(request.getServerPort()!=80) { 346 baseURL.append(":"); 347 baseURL.append(request.getServerPort()); 348 } 349 baseURL.append(request.getContextPath()); 350 return baseURL.toString(); 351 } 352 353 357 public ServletContext getServletContext() { 358 return getServletConfig().getServletContext(); 359 } 360 361 365 protected String getWebInfPath() { 366 return webInfPath; 367 } 368 369 373 protected String getHomeDir() { 374 return homeDir; 375 } 376 377 384 protected String getOption(ServletContext context, 385 String param, 386 String dephault) 387 { 388 String value = AxisProperties.getProperty(param); 389 390 if (value == null) 391 value = getInitParameter(param); 392 393 if (value == null) 394 value = context.getInitParameter(param); 395 try { 396 AxisServer engine = getEngine(this); 397 if (value == null && engine != null) 398 value = (String ) engine.getOption(param); 399 } catch (AxisFault axisFault) { 400 } 401 402 return (value != null) ? value : dephault; 403 } 404 405 409 public boolean isDevelopment() { 410 return isDevelopment; 411 } 412 413 } 414 | Popular Tags |