-----
Castor
-----
XFire User's Guide
-----
Castor
Castor is a flexible XML binding tool that provides run-time marshalling and unmarshalling of XML and Java objects.
One strength of Castor when compared to most (not all) other Java XML binding frameworks is that re-compilation of
the Java code is not required if the mapping definition changes. Therefore, systems where the web service layer is
being developed independently from the business layer can benefit from using Castor. XFire support for Castor is
currently available in the latest XFire release. Two approaches to developing a Web service using Castor with XFire
are presented below: top-down (schema first) and bottom-up (code first). Before proceeding, check the Dependency
Guide for required castor module dependencies.
* Assumptions about Reader
* Competence with Java and XML
* Basic knowledge of Castor XML binding framework
* Experience configuring Java webapp and deploying
* Nominal familiarity with Spring framework
[]
Top-down Approach (starting with XML schema):
Firstly, the XML schema that defines the structure of your web service messages must be developed. For the purposes
of this guide, we'll borrow a schema from http://www.webservicex.net/WeatherForecast.asmx?WSDL which defines a
pre-existing weather forecast service.
The borrowed schema below should be saved under META-INF/schema/ in the classpath:
+---------------------------------------------------------------------------------------------------------------------+
+---------------------------------------------------------------------------------------------------------------------+
Next, we'll use castor to generate POJO classes from the service schema. First, define the source generator task and
a goal for generation. The example below is for a maven 1.x configuration. Go to Using the Source Code Generator for
a full reference on generating java classes from XML schema.
+---------------------------------------------------------------------------------------------------------------------+
+---------------------------------------------------------------------------------------------------------------------+
After running the generate-source goal, the supporting classes will be in the generated-src directory under the
net.webservicex package. Along with each class is an accompanying Descriptor class file (e.g.
WeatherDataDescriptor.java) that contains XML binding information.
Next, write a Web service with the support of the generated classes. Note, the example below is a trivial
implementation.
+---------------------------------------------------------------------------------------------------------------------+
package foo.bar;
import net.webservicex.*;
public class WeatherService
{
public GetWeatherByZipCodeResponse GetWeatherByZipCode(GetWeatherByZipCode body)
{
GetWeatherByZipCodeResponse res = new GetWeatherByZipCodeResponse();
String zipCode = body.getZipCode();
if (!zipCode.equals("1050"))
throw new RuntimeException("Parameter isnt passed correctly. expected: 1050, got "
+ zipCode);
GetWeatherByZipCodeResult weather = new GetWeatherByZipCodeResult();
weather.setLatitude(1);
weather.setLongitude(1);
weather.setPlaceName("Vienna, AT");
weather.setAllocationFactor(1);
res.setGetWeatherByZipCodeResult(weather);
return res;
}
}
+---------------------------------------------------------------------------------------------------------------------+
After this, configure the service in Xfire. The example below takes the XML configuration approach (which uses Spring
integration). In addition, XFire supports integration of configuration into containers such as Plexus and
PicoContainer. For more control over the Web service definition, JSR 181 Annotations should be used in the service
code.
The configuration goes in the services.xml descriptor. This file goes in META-INF/xfire/ on the classpath:
+---------------------------------------------------------------------------------------------------------------------+
foo.bar.WeatherService
META-INF/schema/WeatherForecast.xsd
#castorServiceFactory
+---------------------------------------------------------------------------------------------------------------------+
The service definition specifies the web service implementation class with the property. A list of
<<< >>> can be provided to be included in WSDL generation. In this case, the borrowed schema definition in
the classpath at META-INF/schema/WeatherForecast.xsd has been included.
The request and response messages were defined in our schema and were included as a single parameter and return type,
respectively, in our service method. This lends itself to a bare (or unwrapped) parameter-style. The service
definition specifies a bare parameter-style by setting the