1 54 55 56 package org.jboss.axis.transport.http; 57 58 import org.jboss.axis.AxisEngine; 59 import org.jboss.axis.AxisFault; 60 import org.jboss.axis.AxisProperties; 61 import org.jboss.axis.EngineConfiguration; 62 import org.jboss.axis.configuration.EngineConfigurationFactoryFinder; 63 import org.jboss.axis.server.AxisServer; 64 import org.jboss.axis.utils.JavaUtils; 65 import org.jboss.logging.Logger; 66 67 import javax.servlet.ServletContext ; 68 import javax.servlet.ServletException ; 69 import javax.servlet.http.HttpServlet ; 70 import javax.servlet.http.HttpServletRequest ; 71 import javax.servlet.http.HttpServletResponse ; 72 import java.io.File ; 73 import java.io.IOException ; 74 import java.util.HashMap ; 75 import java.util.Map ; 76 77 86 87 public class AxisServletBase extends HttpServlet 88 { 89 90 93 protected AxisServer axisServer = null; 94 95 private static Logger log = Logger.getLogger(AxisServlet.class.getName()); 96 97 private static boolean isDebug = false; 98 99 102 private static int loadCounter = 0; 103 104 107 private static Object loadCounterLock = new Object (); 108 109 112 protected static final String ATTR_AXIS_ENGINE = 113 "AxisEngine"; 114 115 118 private String webInfPath = null; 119 120 123 private String homeDir = null; 124 125 128 private boolean isDevelopment; 129 130 133 private static final String INIT_PROPERTY_DEVELOPMENT_SYSTEM = 134 "axis.development.system"; 135 136 137 140 public void init() 141 { 142 ServletContext context = getServletConfig().getServletContext(); 143 144 webInfPath = context.getRealPath("/WEB-INF"); 145 homeDir = context.getRealPath("/"); 146 147 isDebug = log.isDebugEnabled(); 148 if (log.isDebugEnabled()) log.debug("In AxisServletBase init"); 149 isDevelopment = JavaUtils.isTrueExplicitly(getOption(context, 150 INIT_PROPERTY_DEVELOPMENT_SYSTEM, null)); 151 152 } 153 154 164 public void destroy() 165 { 166 super.destroy(); 167 168 if (axisServer != null) 170 { 171 synchronized (axisServer) 173 { 174 if (axisServer != null) 175 { 176 axisServer.cleanup(); 178 axisServer = null; 180 storeEngine(getServletContext(), null); 181 } 182 } 183 } 184 } 185 186 192 public AxisServer getEngine() throws AxisFault 193 { 194 if (axisServer == null) 195 axisServer = getEngine(this); 196 return axisServer; 197 } 198 199 200 207 public static AxisServer getEngine(HttpServlet servlet) throws AxisFault 208 { 209 AxisServer engine = null; 210 if (isDebug) 211 log.debug("Enter: getEngine()"); 212 213 ServletContext context = servlet.getServletContext(); 214 synchronized (context) 215 { 216 engine = retrieveEngine(context); 217 if (engine == null) 218 { 219 Map environment = getEngineEnvironment(servlet); 220 221 engine = AxisServer.getServer(environment); 233 storeEngine(context, engine); 234 } 235 } 236 237 if (isDebug) 238 log.debug("Exit: getEngine()"); 239 240 return engine; 241 } 242 243 249 private static void storeEngine(ServletContext context, AxisServer engine) 250 { 251 if (engine == null) 252 { 253 context.removeAttribute(ATTR_AXIS_ENGINE); 254 } 255 else 256 { 257 context.setAttribute(ATTR_AXIS_ENGINE, engine); 258 } 259 } 260 261 271 private static AxisServer retrieveEngine(ServletContext context) 272 { 273 Object contextObject = context.getAttribute(ATTR_AXIS_ENGINE); 274 if (contextObject instanceof AxisServer) 275 { 276 return (AxisServer)contextObject; 277 } 278 else 279 { 280 return null; 281 } 282 } 283 284 285 291 protected static Map getEngineEnvironment(HttpServlet servlet) 292 { 293 Map environment = new HashMap (); 294 295 String attdir = servlet.getInitParameter(AxisEngine.ENV_ATTACHMENT_DIR); 296 if (attdir != null) 297 environment.put(AxisEngine.ENV_ATTACHMENT_DIR, attdir); 298 299 ServletContext context = servlet.getServletContext(); 300 environment.put(AxisEngine.ENV_SERVLET_CONTEXT, context); 301 302 String webInfPath = context.getRealPath("/WEB-INF"); 303 if (webInfPath != null) 304 environment.put(AxisEngine.ENV_SERVLET_REALPATH, 305 webInfPath + File.separator + "attachments"); 306 307 EngineConfiguration config = 308 EngineConfigurationFactoryFinder.newFactory(context) 309 .getServerEngineConfig(); 310 311 if (config != null) 312 { 313 environment.put(EngineConfiguration.PROPERTY_NAME, config); 314 } 315 316 return environment; 317 } 318 319 320 326 327 public static int getLoadCounter() 328 { 329 return loadCounter; 330 } 331 332 335 protected static void incLockCounter() 336 { 337 synchronized (loadCounterLock) 338 { 339 loadCounter++; 340 } 341 } 342 343 346 protected static void decLockCounter() 347 { 348 synchronized (loadCounterLock) 349 { 350 loadCounter--; 351 } 352 } 353 354 364 protected void service(HttpServletRequest req, HttpServletResponse resp) 365 throws ServletException , IOException 366 { 367 incLockCounter(); 368 try 369 { 370 super.service(req, resp); 371 } 372 finally 373 { 374 decLockCounter(); 375 } 376 } 377 378 384 protected String getWebappBase(HttpServletRequest request) 385 { 386 StringBuffer baseURL = new StringBuffer (128); 387 baseURL.append(request.getScheme()); 388 baseURL.append("://"); 389 baseURL.append(request.getServerName()); 390 if (request.getServerPort() != 80) 391 { 392 baseURL.append(":"); 393 baseURL.append(request.getServerPort()); 394 } 395 baseURL.append(request.getContextPath()); 396 return baseURL.toString(); 397 } 398 399 404 public ServletContext getServletContext() 405 { 406 return getServletConfig().getServletContext(); 407 } 408 409 414 protected String getWebInfPath() 415 { 416 return webInfPath; 417 } 418 419 424 protected String getHomeDir() 425 { 426 return homeDir; 427 } 428 429 436 protected String getOption(ServletContext context, 437 String param, 438 String dephault) 439 { 440 String value = AxisProperties.getProperty(param); 441 442 if (value == null) 443 value = getInitParameter(param); 444 445 if (value == null) 446 value = context.getInitParameter(param); 447 448 return (value != null) ? value : dephault; 449 } 450 451 456 public boolean isDevelopment() 457 { 458 return isDevelopment; 459 } 460 461 } 462 | Popular Tags |