Unity Logo

Mapping collections

There is often a need to either return collections as a result of a web service call, or to take in collection parameters. SOAP supports this natively.

The problem with this mechanism however arises from the fact that collections in java are untyped. Thus, to support collections on Java 1.4, a little bit of extra work is required.

Java 5 & Generics

The first, and recommended approach is to use JDK5 and use generics. Generics enable you to specify type information for your collections in the source code, thus allowing xfire to automatically deduce the collection type and generate the correct wsdl and so on.

A brief example of how such a method would look is:

Java 1.4 & Collections

There times where it is not possible to use generics. For example, if your deployment environment uses JDK 1.4, or if you want to expose legacy services without modifying any of their source code or migrating it.

For such cases, you are required to create an xml mappings file which specifies the methods and collection types for them.

The file's name must be <className>.aegis.xml, where className is the unqualified class name of your service.

The format of this file is best illustrated through examples. The service we would like to expose has the following interface:

Since the collections in the source code are not typed, we are required to create an xml file to specify the required types. The file's location must be the same package as MyService1.class, and it must be called MyService1.aegis.xml

A minimal mapping file for this interface would be:

Note that the mapping file specifies exactly the information required and does not contain any redundancy. For example, the getFoo method is not specified, since it does not contain any collections and thus can be exposed without any additional mapping information.

Secondly, the setCollection method does not specify the parameter at index 0. Again, this is because that parameter is an int and thus does not require any additional mapping.

What if we had multiple methods that match the specified mapping? In that case, the mapping applies to all of the specified methods that match.

So if we added the following method to our interface:

Our mapping definition now for setList matches both setList methods. So in this case, we would not need to specify it twice for the additional parameter. The mapping file specifies 'all setList methods that have a List as the second parameter should assume that that list contains strings', in effect.

What if we wanted to specify that the list in the 3 argument method does not contain Strings, but actually contains Dates? In that case, a more specific mapping is required to override the more general one, so our mapping file would require this definition added:

Note the type attribute. The specified mapping will now apply to all methods that have a List as the second parameter, and a boolean for the third. In our interface, this uniquely identifies the method, and so the List parameter for that method will be resolved using this specific mapping.

In terms of precedence, the most specific mapping applicable always takes precedence over a more generic one.

Let us consider a more complex example:

The contents of the file are:

The format of the file should hopefully be self-explanatory. There are a number of things to note about it though.

Let us consider the first mapping (mapping 1). This mapping specifies that the collections returned for all getCollection methods contain java.lang.Doubles. If no other getCollection mappings were specified, this this mapping would apply to methods 1, 2, and 3.

However, the second mapping is more explicit about what it applies to. It specifies that the collection returned by getCollection contains a Float if the first parameter of the method is an int. Since this rule is more explicit, it will override the first mapping for method 2, which satisfies its mapping constraint criteria.

Using the rules above, it should be possible to deduce what the component types for each of the collections specified in methods 4 and 5 are.

Collections on Javabeans

The syntax is similar if you have java beans with collections. For instance, lets say we have a Company bean with a List of employees:

Instead of using the <method> & <parameter> elements, you would use the <property> element:

Handling Maps

Java Maps don't map well to XML Schema (no pun intended) because there is no Map concept in XML Schema so your clients. Maps are transformed to a collection of {key, value} tuples instead. In addition to providing the type of the value, you must also provide Aegis with the type of the key:

The mapping file should look like this:

This will generate the following type:

Collections of Collections of Collections of....

In some cases you may want to pass around Collections of Collections.Lets say that you have a service that returns a List of a List of Doubles (don't ask why you would do such a thing...):

To handle this we need to introduce a new <component> element. This is best shown with an example:

As you can see here, instead of the return type's componentType being a class, its a reference to a <component>. The componentType "#someDoubles" references to the <component> with the name "someDoubles".

Aegis will be automatically name these collections ArrayOfDouble and ArrayOfArrayOfDouble. You may want to change this. To set your own names, supply a "typeName" attribute:

© 2003-2008 Codehaus