1 16 package org.apache.cocoon.components.axis; 17 18 import java.io.File ; 19 import java.io.InputStreamReader ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.avalon.framework.activity.Initializable; 31 import org.apache.avalon.framework.activity.Startable; 32 import org.apache.avalon.framework.component.Component; 33 import org.apache.avalon.framework.component.ComponentException; 34 import org.apache.avalon.framework.component.ComponentManager; 35 import org.apache.avalon.framework.component.Composable; 36 import org.apache.avalon.framework.configuration.Configurable; 37 import org.apache.avalon.framework.configuration.Configuration; 38 import org.apache.avalon.framework.configuration.ConfigurationException; 39 import org.apache.avalon.framework.context.Context; 40 import org.apache.avalon.framework.context.ContextException; 41 import org.apache.avalon.framework.context.Contextualizable; 42 import org.apache.avalon.framework.logger.AbstractLogEnabled; 43 import org.apache.avalon.framework.thread.ThreadSafe; 44 import org.apache.axis.AxisEngine; 45 import org.apache.axis.Constants; 46 import org.apache.axis.EngineConfiguration; 47 import org.apache.axis.MessageContext; 48 import org.apache.axis.configuration.FileProvider; 49 import org.apache.axis.deployment.wsdd.WSDDDeployment; 50 import org.apache.axis.deployment.wsdd.WSDDDocument; 51 import org.apache.axis.deployment.wsdd.WSDDService; 52 import org.apache.axis.security.servlet.ServletSecurityProvider; 53 import org.apache.axis.server.AxisServer; 54 import org.apache.axis.transport.http.HTTPConstants; 55 import org.apache.axis.transport.http.HTTPTransport; 56 import org.apache.axis.transport.http.ServletEndpointContextImpl; 57 import org.apache.axis.utils.XMLUtils; 58 import org.apache.cocoon.components.axis.providers.AvalonProvider; 59 import org.apache.cocoon.util.IOUtils; 60 import org.apache.commons.lang.BooleanUtils; 61 import org.apache.excalibur.source.Source; 62 import org.apache.excalibur.source.SourceResolver; 63 import org.apache.excalibur.xml.dom.DOMParser; 64 import org.w3c.dom.Document ; 65 import org.xml.sax.InputSource ; 66 67 93 public class SoapServerImpl extends AbstractLogEnabled 94 implements SoapServer, Composable, Configurable, Contextualizable, Initializable, 95 Startable, ThreadSafe { 96 97 100 public static final String DEFAULT_SERVER_CONFIG 101 = "resource://org/apache/axis/server/server-config.wsdd"; 102 103 private String m_transportName; 105 106 private ServletSecurityProvider m_securityProvider; 108 109 private String m_jwsClassDir; 111 112 private AxisServer m_axisServer; 114 115 private FileProvider m_engineConfig; 117 118 private String m_attachmentDir; 120 121 private Source m_serverWSDD; 123 124 private WSDDDocument[] m_descriptors; 126 127 private Context context; 129 130 private ComponentManager manager; 132 133 136 public void contextualize(final Context context) 137 throws ContextException { 138 this.context = context; 139 } 140 141 144 public void compose(ComponentManager manager) 145 throws ComponentException { 146 this.manager = manager; 147 } 148 149 185 public void configure(final Configuration config) 186 throws ConfigurationException { 187 try { 188 setServerConfig(config); 189 setAttachmentDir(config); 190 setJWSDir(config); 191 setSecurityProvider(config); 192 setTransportName(config); 193 setManagedServices(config); 194 195 if (getLogger().isDebugEnabled()) { 196 getLogger().debug("SoapServerImpl.configure() complete"); 197 } 198 } catch (final Exception e) { 199 throw new ConfigurationException("Error during configuration", e); 200 } 201 } 202 203 209 public void setServerConfig(final Configuration config) 210 throws Exception { 211 final Configuration wsdd = config.getChild("server-wsdd"); 212 SourceResolver resolver = null; 213 214 try { 215 resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 216 m_serverWSDD = 217 resolver.resolveURI( 218 wsdd.getAttribute("src", DEFAULT_SERVER_CONFIG) 219 ); 220 } finally { 221 this.manager.release(resolver); 222 } 223 } 224 225 233 private void setAttachmentDir(final Configuration config) 234 throws ConfigurationException, ContextException { 235 final Configuration dir = config.getChild("attachment-dir"); 236 m_attachmentDir = dir.getAttribute("src", null); 237 238 if (m_attachmentDir == null) { 239 File workDir = 240 (File ) this.context.get(org.apache.cocoon.Constants.CONTEXT_WORK_DIR); 241 File attachmentDir = 242 IOUtils.createFile(workDir, "attachments" + File.separator); 243 m_attachmentDir = IOUtils.getFullFilename(attachmentDir); 244 } 245 246 if (getLogger().isDebugEnabled()) { 247 getLogger().debug("attachment directory = " + m_attachmentDir); 248 } 249 } 250 251 259 private void setJWSDir(final Configuration config) 260 throws ConfigurationException, ContextException { 261 final Configuration dir = config.getChild("jws-dir"); 262 m_jwsClassDir = dir.getAttribute("src", null); 263 264 if (m_jwsClassDir == null) { 265 File workDir = 266 (File ) this.context.get(org.apache.cocoon.Constants.CONTEXT_WORK_DIR); 267 File jwsClassDir = 268 IOUtils.createFile(workDir, "axis-jws" + File.separator); 269 m_jwsClassDir = IOUtils.getFullFilename(jwsClassDir); 270 } 271 272 if (getLogger().isDebugEnabled()) { 273 getLogger().debug("jws class directory = " + m_jwsClassDir); 274 } 275 } 276 277 283 private void setSecurityProvider(final Configuration config) 284 throws ConfigurationException { 285 final Configuration secProvider = 286 config.getChild("security-provider", false); 287 288 if (secProvider != null) { 289 final String attr = secProvider.getAttribute("enabled"); 290 final boolean providerIsEnabled = BooleanUtils.toBoolean(attr); 291 292 if (providerIsEnabled) { 293 m_securityProvider = new ServletSecurityProvider(); 294 } 295 } 296 297 if (getLogger().isDebugEnabled()) { 298 getLogger().debug("security provider = " + m_securityProvider); 299 } 300 } 301 302 308 private void setTransportName(final Configuration config) 309 throws ConfigurationException { 310 final Configuration name = config.getChild("transport"); 311 m_transportName = 312 name.getAttribute("name", HTTPTransport.DEFAULT_TRANSPORT_NAME); 313 } 314 315 323 private void setManagedServices(final Configuration config) 324 throws Exception { 325 final Configuration m = config.getChild("managed-services", false); 326 final List descriptors = new ArrayList (); 327 328 if (m != null) { 329 SourceResolver resolver = null; 330 DOMParser parser = null; 331 332 try { 333 final Configuration[] services = m.getChildren("descriptor"); 334 resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE); 335 parser = (DOMParser) this.manager.lookup(DOMParser.ROLE); 336 337 for (int i = 0; i < services.length; ++i) { 338 final String location = services[i].getAttribute("src"); 339 Source source = resolver.resolveURI(location); 340 341 final Document d = 342 parser.parseDocument( 343 new InputSource ( 344 new InputStreamReader (source.getInputStream()) 345 ) 346 ); 347 348 descriptors.add(new WSDDDocument(d)); 349 } 350 } finally { 351 this.manager.release(resolver); 352 this.manager.release((Component)parser); 353 } 354 } 355 356 m_descriptors = 358 (WSDDDocument[]) descriptors.toArray(new WSDDDocument[]{}); 359 } 360 361 364 public void initialize() 365 throws Exception { 366 m_axisServer = createEngine(); 367 368 if (getLogger().isDebugEnabled()) { 369 getLogger().debug("SoapServerImpl.initialize() complete"); 370 } 371 } 372 373 379 public void start() 380 throws Exception { 381 for (int i = 0; i < m_descriptors.length; ++i) { 383 WSDDDeployment deployment = m_engineConfig.getDeployment(); 384 m_descriptors[i].deploy(deployment); 385 386 if (getLogger().isDebugEnabled()) { 387 getLogger().debug( 388 "Deployed Descriptor:\n" + 389 XMLUtils.DocumentToString(m_descriptors[i].getDOMDocument()) 390 ); 391 } 392 } 393 394 if (getLogger().isDebugEnabled()) { 395 getLogger().debug("SoapServerImpl.start() complete"); 396 } 397 } 398 399 406 public void stop() 407 throws Exception { 408 WSDDDeployment deployment = m_engineConfig.getDeployment(); 409 WSDDService[] services = deployment.getServices(); 410 411 for (int i = 0; i < services.length; ++i) { 413 deployment.undeployService(services[i].getQName()); 414 415 if (getLogger().isDebugEnabled()) { 416 getLogger().debug("Undeployed: " + services[i].toString()); 417 } 418 } 419 420 if (getLogger().isDebugEnabled()) { 421 getLogger().debug("SoapServerImpl.stop() complete"); 422 } 423 } 424 425 428 public void invoke(MessageContext message) 429 throws Exception { 430 m_axisServer.invoke(message); 431 } 432 433 440 public MessageContext createMessageContext( 441 HttpServletRequest req, 442 HttpServletResponse res, 443 ServletContext con) { 444 445 MessageContext msgContext = new MessageContext(m_axisServer); 446 String webInfPath = con.getRealPath("/WEB-INF"); 447 String homeDir = con.getRealPath("/"); 448 449 msgContext.setTransportName(m_transportName); 451 452 msgContext.setProperty(LOGGER, getLogger()); 454 msgContext.setProperty(AvalonProvider.COMPONENT_MANAGER, this.manager); 455 456 msgContext.setProperty(Constants.MC_JWS_CLASSDIR, m_jwsClassDir); 458 msgContext.setProperty(Constants.MC_HOME_DIR, homeDir); 459 msgContext.setProperty(Constants.MC_RELATIVE_PATH, req.getServletPath()); 460 msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLET, this ); 461 msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST, req ); 462 msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE, res ); 463 msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLETLOCATION, webInfPath); 464 msgContext.setProperty(HTTPConstants.MC_HTTP_SERVLETPATHINFO, 465 req.getPathInfo() ); 466 msgContext.setProperty(HTTPConstants.HEADER_AUTHORIZATION, 467 req.getHeader(HTTPConstants.HEADER_AUTHORIZATION)); 468 msgContext.setProperty(Constants.MC_REMOTE_ADDR, req.getRemoteAddr()); 469 470 471 ServletEndpointContextImpl sec = new ServletEndpointContextImpl(); 473 msgContext.setProperty(Constants.MC_SERVLET_ENDPOINT_CONTEXT, sec); 474 475 String realpath = con.getRealPath(req.getServletPath()); 477 478 if (realpath != null) { 479 msgContext.setProperty(Constants.MC_REALPATH, realpath); 480 } 481 482 msgContext.setProperty(Constants.MC_CONFIGPATH, webInfPath); 483 484 if (m_securityProvider != null) { 485 msgContext.setProperty("securityProvider", m_securityProvider); 486 } 487 488 if (getLogger().isDebugEnabled()) { 490 debugMessageContext(msgContext); 491 } 492 493 return msgContext; 494 } 495 496 501 private void debugMessageContext(final MessageContext context) { 502 for (final Iterator i = context.getPropertyNames(); i.hasNext(); ) { 503 final String key = (String ) i.next(); 504 getLogger().debug( 505 "MessageContext: Key:" + key + ": Value: " + context.getProperty(key) 506 ); 507 } 508 } 509 510 511 515 public AxisServer createEngine() 516 throws Exception { 517 AxisServer engine = AxisServer.getServer(getEngineEnvironment()); 518 519 if (getLogger().isDebugEnabled()) { 520 getLogger().debug("Axis engine created"); 521 } 522 523 return engine; 524 } 525 526 protected Map getEngineEnvironment() 527 throws Exception { 528 Map env = new HashMap (); 529 530 m_engineConfig = new FileProvider(m_serverWSDD.getInputStream()); 533 534 env.put(EngineConfiguration.PROPERTY_NAME, m_engineConfig); 535 env.put(AxisEngine.ENV_ATTACHMENT_DIR, m_attachmentDir); 536 539 return env; 540 } 541 } 542 | Popular Tags |