1 package org.codehaus.xfire.aegis; 2 3 import java.io.InputStream; 4 import java.util.Collection; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 10 import org.codehaus.plexus.component.repository.exception.ComponentLookupException; 11 import org.codehaus.plexus.configuration.PlexusConfiguration; 12 import org.codehaus.plexus.configuration.PlexusConfigurationException; 13 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; 14 import org.codehaus.xfire.SOAPConstants; 15 import org.codehaus.xfire.XFireRuntimeException; 16 import org.codehaus.xfire.aegis.mapping.TypeRegistry; 17 import org.codehaus.xfire.aegis.operation.WrappedOperation; 18 import org.codehaus.xfire.aegis.wsdl.AegisWSDL; 19 import org.codehaus.xfire.fault.SOAP11FaultHandler; 20 import org.codehaus.xfire.fault.SOAP12FaultHandler; 21 import org.codehaus.xfire.handler.Handler; 22 import org.codehaus.xfire.handler.dom.DOMHandler; 23 import org.codehaus.xfire.handler.dom.DOMPipelineHandler; 24 import org.codehaus.xfire.plexus.simple.PlexusSimpleService; 25 import org.codehaus.xfire.wsdl.WSDLBuilder; 26 import org.dom4j.Document; 27 import org.dom4j.Element; 28 import org.dom4j.QName; 29 import org.dom4j.io.SAXReader; 30 31 /*** 32 * AegisService 33 * 34 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> 35 */ 36 public class AegisService 37 extends PlexusSimpleService 38 implements Initializable 39 { 40 private Document serviceDescriptor; 41 42 private Map operations; 43 44 private String serviceClassName; 45 46 private TypeRegistry typeRegistry; 47 48 private PlexusConfiguration[] handlers; 49 50 private Handler serviceHandler; 51 52 public AegisService() 53 { 54 operations = new HashMap(); 55 } 56 57 /*** 58 * @see org.apache.avalon.framework.activity.Initializable#initialize() 59 */ 60 public void initialize() throws Exception 61 { 62 super.initialize(); 63 64 DOMPipelineHandler pipe = (DOMPipelineHandler) getServiceHandler(); 65 66 if ( handlers != null && handlers.length > 0 ) 67 { 68 for ( int i = 0; i < handlers.length; i++ ) 69 { 70 String hId = handlers[i].getValue(); 71 72 DOMHandler handler = (DOMHandler) getServiceLocator().lookup( DOMHandler.ROLE, hId ); 73 74 pipe.getHandlers().add( handler ); 75 } 76 } 77 else 78 { 79 pipe.getHandlers().add( new AegisServiceHandler() ); 80 } 81 82 typeRegistry = (TypeRegistry) getServiceLocator().lookup(TypeRegistry.ROLE); 83 setWSDLBuilder((WSDLBuilder) getServiceLocator().lookup(WSDLBuilder.ROLE, "aegis")); 84 85 SAXReader xmlReader = new SAXReader(); 86 serviceDescriptor = xmlReader.read( getServiceDescriptorStream() ); 87 88 createService(); 89 90 AegisWSDL wsdl = (AegisWSDL) getWSDL(); 91 } 92 93 /*** 94 * Create a service and operations from the service descriptor. 95 */ 96 protected void createService() 97 { 98 Element root = serviceDescriptor.getRootElement(); 99 setDefaultNamespace( root.attribute("targetNamespace").getStringValue() ); 100 setSoapVersion( root.attribute("soapVersion").getStringValue() ); 101 102 if ( getSoapVersion().equals ( SOAPConstants.SOAP12_ENVELOPE_NS )) 103 { 104 setFaultHandlerHint( SOAP12FaultHandler.NAME ); 105 } 106 else 107 { 108 setFaultHandlerHint( SOAP11FaultHandler.NAME ); 109 } 110 111 List elements = root.elements("operation"); 112 for ( Iterator itr = elements.iterator(); itr.hasNext(); ) 113 { 114 Element opEl = (Element) itr.next(); 115 116 WrappedOperation op = new WrappedOperation(); 117 op.configure( opEl, this, getTypeRegistry() ); 118 119 QName opQ = op.getQName(); 120 121 operations.put( opQ, op ); 122 } 123 } 124 125 public Collection getOperations() 126 { 127 return operations.values(); 128 } 129 130 public WrappedOperation getOperation( QName name ) 131 { 132 return (WrappedOperation) operations.get( name ); 133 } 134 135 public TypeRegistry getTypeRegistry() 136 { 137 return typeRegistry; 138 } 139 140 /*** 141 * Returns an InputStream for the descriptor. 142 * @return InputStream representing the descriptor 143 */ 144 public InputStream getServiceDescriptorStream() 145 { 146 String name = getServiceDescriptorName(); 147 InputStream is = getClass().getResourceAsStream( name ); 148 149 if ( is == null ) 150 throw new XFireRuntimeException("No service descriptor found: " + name); 151 152 return is; 153 } 154 155 public String getServiceDescriptorName() 156 { 157 String full = getServiceClassName(); 158 full = "/" + full.replace('.', '/'); 159 160 return full + ".xml"; 161 } 162 163 public Document getServiceDescriptor() 164 { 165 return serviceDescriptor; 166 } 167 168 public String getServiceClassName() 169 { 170 return serviceClassName; 171 } 172 173 public void setServiceClassName(String serviceClassName) 174 { 175 this.serviceClassName = serviceClassName; 176 } 177 178 public Object createServiceObject() 179 { 180 if ( getServiceLocator().hasComponent(getServiceClassName()) ) 181 { 182 try 183 { 184 return getServiceLocator().lookup( getServiceClassName() ); 185 } 186 catch (ComponentLookupException e) 187 { 188 throw new XFireRuntimeException("Couldn't lookup service.", e); 189 } 190 } 191 else 192 { 193 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 194 195 Class clazz; 196 try 197 { 198 clazz = cl.loadClass(getServiceClassName()); 199 return clazz.newInstance(); 200 } 201 catch (Exception e) 202 { 203 throw new XFireRuntimeException("Couldn't create service class.", e); 204 } 205 206 } 207 } 208 209 /*** 210 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration) 211 */ 212 public void configure( PlexusConfiguration config ) throws PlexusConfigurationException 213 { 214 super.configure( config ); 215 216 setFaultHandlerHint( config.getChild("faultHandler").getValue("aegis") ); 217 218 setServiceClassName( config.getChild("serviceClass").getValue() ); 219 setStyle( SOAPConstants.STYLE_WRAPPED ); 220 setUse( SOAPConstants.USE_LITERAL ); 221 222 DOMPipelineHandler handler = new DOMPipelineHandler(); 223 setServiceHandler( handler ); 224 225 handlers = config.getChild("handlers").getChildren("handler"); 226 } 227 228 229 public Handler getServiceHandler() 230 { 231 return serviceHandler; 232 } 233 234 public void setServiceHandler( Handler serviceHandler ) 235 { 236 this.serviceHandler = serviceHandler; 237 } 238 }