1 package org.codehaus.xfire.plexus.java;
2
3 import javax.xml.namespace.QName;
4
5 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
6 import org.codehaus.plexus.configuration.PlexusConfiguration;
7 import org.codehaus.plexus.configuration.PlexusConfigurationException;
8 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Configurable;
9 import org.codehaus.plexus.personality.plexus.lifecycle.phase.ServiceLocator;
10 import org.codehaus.xfire.SOAPConstants;
11 import org.codehaus.xfire.XFireRuntimeException;
12 import org.codehaus.xfire.fault.FaultHandler;
13 import org.codehaus.xfire.fault.SOAP11FaultHandler;
14 import org.codehaus.xfire.fault.SOAP12FaultHandler;
15 import org.codehaus.xfire.handler.Handler;
16 import org.codehaus.xfire.handler.SoapHandler;
17 import org.codehaus.xfire.java.DefaultJavaService;
18 import org.codehaus.xfire.java.JavaService;
19 import org.codehaus.xfire.java.JavaServiceHandler;
20 import org.codehaus.xfire.java.mapping.TypeMapping;
21 import org.codehaus.xfire.java.mapping.TypeMappingRegistry;
22 import org.codehaus.xfire.java.wsdl.JavaWSDLBuilder;
23 import org.codehaus.xfire.service.ServiceRegistry;
24 import org.codehaus.xfire.transport.TransportManager;
25
26 /***
27 * A service that is created from an XML configuration within Plexus.
28 *
29 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
30 */
31 public class PlexusJavaService
32 extends DefaultJavaService
33 implements Configurable
34 {
35 private PlexusConfiguration[] types;
36
37 private PlexusConfiguration[] handlers;
38
39 private String faultHandlerHint;
40
41 private ServiceLocator manager;
42
43 private Handler serviceHandler;
44
45 public FaultHandler getFaultHandler()
46 {
47 try
48 {
49 return (FaultHandler) getServiceLocator().lookup( FaultHandler.ROLE, faultHandlerHint );
50 }
51 catch (ComponentLookupException e)
52 {
53 throw new XFireRuntimeException( "Couldn't find service provider!", e );
54 }
55 }
56
57 public String getFaultHandlerHint()
58 {
59 return faultHandlerHint;
60 }
61
62 public void setFaultHandlerHint(String faultHandlerHint)
63 {
64 this.faultHandlerHint = faultHandlerHint;
65 }
66
67
68 /***
69 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
70 */
71 public void configure(PlexusConfiguration config) throws PlexusConfigurationException
72 {
73 configureService(config);
74
75 configureTypes(config);
76
77 SoapHandler handler = new SoapHandler( new JavaServiceHandler() );
78 setServiceHandler( handler );
79 }
80
81 /***
82 * @param config
83 * @throws PlexusConfigurationException
84 */
85 private void configureService(PlexusConfiguration config) throws PlexusConfigurationException
86 {
87 setName( config.getChild("name").getValue() );
88
89 setDefaultNamespace( config.getChild( "namespace" ).getValue("") );
90
91 setWSDLURL( config.getChild("wsdlUrl").getValue("") );
92
93 setUse( config.getChild("use").getValue("literal") );
94
95 setStyle( config.getChild("style").getValue("wrapped") );
96
97 try
98 {
99 setServiceClass( config.getChild( SERVICE_CLASS ).getValue() );
100 }
101 catch (ClassNotFoundException e)
102 {
103 throw new PlexusConfigurationException( "Couldn't find service class.", e );
104 }
105
106
107 setProperty( ALLOWED_METHODS, config.getChild( ALLOWED_METHODS ).getValue("") );
108
109 String soapNS = config.getChild( "soapVersion" ).getValue("1.1");
110
111 if ( soapNS.equals("1.1") )
112 {
113 setSoapVersion( SOAPConstants.SOAP11_ENVELOPE_NS );
114 setFaultHandlerHint( SOAP11FaultHandler.NAME );
115 }
116 else if ( soapNS.equals("1.2") )
117 {
118 setFaultHandlerHint( SOAP12FaultHandler.NAME );
119 setSoapVersion( SOAPConstants.SOAP12_ENVELOPE_NS );
120 }
121 else
122 throw new PlexusConfigurationException("Invalid soap version. Must be 1.1 or 1.2.");
123
124 String scope = config.getChild("scope").getValue("application");
125 if ( scope.equals("application") )
126 setScope(JavaService.SCOPE_APPLICATION);
127 else if ( scope.equals("session") )
128 setScope(JavaService.SCOPE_SESSION);
129 else if ( scope.equals("request") )
130 setScope(JavaService.SCOPE_REQUEST);
131
132 setFaultHandlerHint( soapNS );
133
134 setAutoTyped( Boolean.valueOf(config.getChild( "autoTyped" ).getValue("false")).booleanValue() );
135 }
136
137 /***
138 * @param config
139 * @throws PlexusConfigurationException
140 */
141 private void configureTypes(PlexusConfiguration config) throws PlexusConfigurationException
142 {
143 types = config.getChild("types").getChildren("type");
144 }
145
146 public void initialize() throws Exception
147 {
148 TypeMappingRegistry tmr = getTypeMappingRegistry();
149 TypeMapping tm = tmr.createTypeMapping(SOAPConstants.XSD, isAutoTyped());
150 tmr.register(getDefaultNamespace(), tm);
151 setTypeMapping(tm);
152
153 for ( int i = 0; i < types.length; i++ )
154 {
155 initializeType( types[i], getTypeMapping() );
156 }
157
158 setWSDLBuilder( new JavaWSDLBuilder( getTransportManager() ) );
159
160 getServiceRegistry().register( this );
161 }
162
163 private void initializeType(PlexusConfiguration configuration, TypeMapping tm) throws PlexusConfigurationException
164 {
165 try
166 {
167 String ns = configuration.getAttribute( "namespace", getDefaultNamespace() );
168 String name = configuration.getAttribute("name");
169
170 tm.register( loadClass( configuration.getAttribute("class") ),
171 new QName( ns, name ),
172 loadClass( configuration.getAttribute("type") ) );
173 }
174 catch (Exception e)
175 {
176 if ( e instanceof PlexusConfigurationException )
177 throw (PlexusConfigurationException) e;
178
179 throw new PlexusConfigurationException( "Could not configure type.", e );
180 }
181 }
182
183 protected ServiceRegistry getServiceRegistry()
184 {
185 ServiceRegistry registry = null;
186
187 try
188 {
189 registry = (ServiceRegistry) getServiceLocator().lookup( ServiceRegistry.ROLE );
190 }
191 catch (ComponentLookupException e)
192 {
193 throw new RuntimeException( "Couldn't find the ServiceRegistry!", e );
194 }
195
196 return registry;
197 }
198
199 protected TransportManager getTransportManager()
200 {
201 TransportManager transMan = null;
202
203 try
204 {
205 transMan = (TransportManager) getServiceLocator().lookup( TransportManager.ROLE );
206 }
207 catch (ComponentLookupException e)
208 {
209 throw new RuntimeException( "Couldn't find the TransportManager!", e );
210 }
211
212 return transMan;
213 }
214
215
216 public TypeMappingRegistry getTypeMappingRegistry()
217 {
218 TypeMappingRegistry registry = null;
219
220 try
221 {
222 registry = (TypeMappingRegistry) getServiceLocator().lookup( TypeMappingRegistry.ROLE );
223 }
224 catch (ComponentLookupException e)
225 {
226 throw new RuntimeException( "Couldn't find the TypeMappingRegistry!", e );
227 }
228
229 return registry;
230 }
231
232 public void service( ServiceLocator manager )
233 {
234 this.manager = manager;
235 }
236
237 /***
238 * @return Returns the service manager.
239 */
240 protected ServiceLocator getServiceLocator()
241 {
242 return manager;
243 }
244 }