Codehaus XFire

Documentation

Quicklinks

Developers

Sponsors

Sometimes you'll want to read/write your own custom types. This will give you direct access to the xml streams.

Writing a Type class

There are many reasons why you may want to write your own Type. Your java class may not map well to your xml. Or sometimes you'll want to return large amounts of data which you don't want loaded into memory. Here a couple steps to help you get going.

1. Return or receive some kind of class which gives a reference to the data

public ReferenceToData doSomething(...) {}

2. Create a org.codehaus.xfire.aegis.type.Type for ReferenceToData

public class ReferenceToDataType extends Type
{
  public ReferenceToDataType() {
    setTypeClass(ReferenceToData.class);
    setSchemaType(new QName(.. the QName of the type you're returning ..));
  }

  public void writeObject(Object value, XMLStreamWriter writer, MessageContext context)
  {
    ReferenceToData data = (ReferenceToData) value;
    ... do you're writing to the writer
  }
 
  public Object readObject( MessageReader reader, MessageContext context )
  {
    // If you're reading you can read in a reference to the data
    XMLStreamReader reader = context.getInMessage().getXMLStreamReader();

    ReferenceToData data = read(reader);
    return data;
  }

  public void writeSchema(Element schemaRoot)
  {
    // override this to write out your schema
    // if you have it in DOM form you can convert it to YOM via DOMConverter
  }
}

3. Register the ReferenceToDataType

ServiceRegistry serviceRegistry  = ....; // get this from the XFire instance
Serivce service = serviceRegistry.getService("serviceName");

TypeMapping tm = ((AegisBindingProvider) service.getBindingProvider).getTypeMapping(service);
tm.register(new ReferenceToDataType());

Configuration via services.xml

<beans xmlns="http://xfire.codehaus.org/config/1.0">
  <!-- This calls initializeTypes before your service is created -->
  <bean id="TypeRegistrar" init-method="initializeTypes" class="foo.TypeRegistrar ">
    <property name="typeMappingRegistry" ref="xfire.typeMappingRegistry"/>
  </bean>

  <service id="MyService">
    <serviceClass>foo.MyService</serviceClass>
  </service>
</beans>

and a bean which registers your types:

public class TypeRegistrar {
  private TypeMappingRegistry typeMappingRegistry;

  public void setTypeMappingRegistry (TypeMappingRegistry typeMappingRegistry) {
    this.typeMappingRegistry= typeMappingRegistry;
  }

  public void initializeTypes() {
    TypeMapping tm = typeMappingRegistry.getDefaultTypeMapping();
    tm.register(new ReferenceToDataType());
  }
}