View Javadoc

1   package org.codehaus.xfire.java;
2   
3   import org.codehaus.plexus.configuration.PlexusConfiguration;
4   import org.codehaus.plexus.configuration.PlexusConfigurationException;
5   import org.codehaus.plexus.personality.plexus.lifecycle.phase.Configurable;
6   import org.codehaus.xfire.SOAPConstants;
7   import org.codehaus.xfire.fault.SOAP11FaultHandler;
8   import org.codehaus.xfire.fault.SOAP12FaultHandler;
9   import org.codehaus.xfire.handler.dom.DOMHandler;
10  import org.codehaus.xfire.handler.dom.DOMPipelineHandler;
11  import org.codehaus.xfire.java.mapping.TypeMapping;
12  import org.codehaus.xfire.wsdl.WSDLBuilder;
13  import org.dom4j.QName;
14  
15  /***
16   * A service that is created from an XML configuration within Plexus.
17   * 
18   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
19   */
20  public class XmlJavaService
21  	extends AbstractJavaService
22  	implements Configurable
23  {
24      private PlexusConfiguration[] types;
25      
26      private PlexusConfiguration[] handlers;
27      
28      /***
29       * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
30       */
31      public void configure(PlexusConfiguration config) throws PlexusConfigurationException
32      {
33          configureService(config);
34          
35          configureTypes(config);
36          
37          DOMPipelineHandler handler = new DOMPipelineHandler();
38          setServiceHandler( handler );
39          
40          handlers = config.getChild("handlers").getChildren("handler");
41      }
42  
43      /***
44       * @param config
45       * @throws PlexusConfigurationException
46       */
47      private void configureService(PlexusConfiguration config) throws PlexusConfigurationException
48      {
49          setName( config.getChild("name").getValue() );
50          
51          setDefaultNamespace( config.getChild( "namespace" ).getValue("") );
52      
53          setWSDLURL( config.getChild("wsdlURL").getValue("") );
54          
55          setUse( config.getChild("use").getValue("literal") );
56          
57          setStyle( config.getChild("style").getValue("wrapped") );
58          
59          try
60          {
61              setServiceClass( config.getChild( SERVICE_CLASS ).getValue() );
62          }
63          catch (ClassNotFoundException e)
64          {
65              throw new PlexusConfigurationException( "Couldn't find service class.", e );
66          }
67          
68          // TODO use allowed methods attribute
69          setProperty( ALLOWED_METHODS, config.getChild( ALLOWED_METHODS ).getValue("") );
70          
71          String soapNS = config.getChild( "soapVersion" ).getValue("1.1");
72          
73          if ( soapNS.equals("1.1") )
74          {
75              setSoapVersion( SOAPConstants.SOAP11_ENVELOPE_NS );
76              setFaultHandlerHint( SOAP11FaultHandler.NAME );
77          }
78          else if ( soapNS.equals("1.2") )
79          {
80              setFaultHandlerHint( SOAP12FaultHandler.NAME );
81              setSoapVersion( SOAPConstants.SOAP12_ENVELOPE_NS );
82          }
83          else
84              throw new PlexusConfigurationException("Invalid soap version.  Must be 1.1 or 1.2.");
85              
86          setFaultHandlerHint( soapNS );
87          
88          setAutoTyped( Boolean.valueOf(config.getChild( "autoTyped" ).getValue("false")).booleanValue() );
89      }
90  
91      /***
92       * @param config
93       * @throws PlexusConfigurationException
94       */
95      private void configureTypes(PlexusConfiguration config) throws PlexusConfigurationException
96      {
97         types  = config.getChild("types").getChildren("type");
98      }
99      
100     public void initialize() throws Exception
101     {
102         super.initialize();
103         
104         DOMPipelineHandler pipe = (DOMPipelineHandler) getServiceHandler();
105 
106         if ( handlers != null && handlers.length > 0 )
107         {
108             for ( int i = 0; i < handlers.length; i++ )
109             {
110                 String hId = handlers[i].getValue();
111                 
112                 DOMHandler handler = (DOMHandler) getServiceLocator().lookup( DOMHandler.ROLE, hId );
113                 
114                 pipe.getHandlers().add( handler );
115             }
116         }
117         else
118         {
119             pipe.getHandlers().add( new JavaServiceHandler() );
120         }
121         
122         for ( int i = 0; i < types.length; i++ )
123         {
124             initializeType( types[i], getTypeMapping() );   
125         }
126         
127         
128         setWSDLBuilder( (WSDLBuilder) getServiceLocator().lookup(WSDLBuilder.ROLE, "java") );
129     }
130 
131     private void initializeType(PlexusConfiguration configuration, TypeMapping tm) throws PlexusConfigurationException
132     {
133         try
134         {
135             String ns = configuration.getAttribute( "namespace", getDefaultNamespace() );
136             String name = configuration.getAttribute("name");
137             
138             tm.register( loadClass( configuration.getAttribute("class") ),
139                          QName.get( name, ns ),
140                          loadClass( configuration.getAttribute("type") ) );
141         }
142         catch (Exception e)
143         {
144             if ( e instanceof PlexusConfigurationException )
145                 throw (PlexusConfigurationException) e;
146             
147             throw new PlexusConfigurationException( "Could not configure type.", e );
148         }                     
149     }
150 }