Java2Schema is a tool for mapping Java classes and structures to their XML Schema representations. This is used mainly for serializations to SOAP messages. Each type in a particular language (Java in this case) has a representation in XML. The generated Schema describes the way the type should be encoded.
Although there is probably no direct use of Java2Schema as a standalone tool unless you want to give your service advanced capabilities, it is a very important part of the Java2WSDL tool and other related parts of WSO2 SOA Enablement Server.
Java2Schema is included in the Core-Tools component (see Components chapter). It is accessible through the Java2Schema script in the bin subdirectory of the WSO2 SOA Enablement Server distribution.
All the primitive types and common data types like int, String and QName are predefined.
For more information about primitive type mapping, see the section Primitive Types.
Extra support is dedicated to Java containers. The Schemas for these data structures are pre-generated. If usage of some of these types is required, XML Schema describing these types is inserted into the target file.
For more information about containers, see Java Collections.
Schemas for Java containers can be found in WSO2 SOA Enablement Server Built-in XML Schema Files.
For information about containers compatible with Microsoft .NET for RPC/encoded style, please see .NET-Compatible Containers for SOAP Encoded.
For information about containers for document/literal style, please see .NET-Compatible Containers for SOAP Literal.
WSO2 SOA Enablement Server fully supports remote references, which are classes that implement the java.rmi.Remote interface. Such classes are mapped to QName {http://idoox.com/interface}serviceReference.
More information about references can be found in Remote References.
WSO2 SOA Enablement Server security features are also supported by the Java2Schema tool. There are two classes, java.security.Key and java.security.cert.Certificate, which are mapped to special structures in the namespace http://idoox.com/security.
Extra support is given to message attachments. The following classes are treated as attachments by WSO2 SOA Enablement Server:
org.idoox.wasp.types.MessageAttachment
org.idoox.wasp.types.RequestMessageAttachment
org.idoox.wasp.types.ResponseMessageAttachment
Each attachment is mapped as a {http://www.w3.org/2001/XMLSchema}base64Binary type and transmitted via HTTP using Multi-part /related or DIME encoding.
More about attachments can be found in Service Using Attachments and Processing Attachments.
Java2Schema can handle a number of Java types which have extra representations in generated XML Schema.
These are enumeration, choice and any.
For more information, please see Advanced XML Schema Constructs.
This section covers features of Java2Schema which should be explained in more detail.
The previous section described those types that have special mapping support. A different situation occurs for classes with a more complex structure. A complex structure may contain public attributes, getter/setter functions, or other elements. Mapping of these structures follows the type mapping described in Complex Types and Structures.
The inheritance and polymorphism solution is an important part of the mapping algorithm. While mapping, type inheritance in the XML Schema follows the same structure as in Java. For example, if class A inherits attribute M from class B, the XML Schema will contain structure A extending structure B, which has member M. Resolution of the multiple inheritance problem can be found in the section Multiple Inheritance.
For each generated schema structure, a target namespace is first chosen. The namespace is constructed from a default prefix (http://wso2.com/wsdl/) and the Java type's package name. When a type is primitive or has no package, the suffix default/ is used. If an array is generated, the namespace is chosen from the data type that comprises the array. In document/literal, each type used in an operation must be mapped to an element. Each element is stored to the namespace http://wso2.com/xsd/SchemaTypes/.
Table 1 shows a few examples of how mapping is provided.
Table 20. Target Namespace Mapping Examples
Java Type | XML Schema Type Namespace | XML Schema Element Namespace |
---|---|---|
int | http://www.w3.org/2001/XMLSchema | http://wso2.com/xsd/SchemaTypes/ |
int [] | http://wso2.com/xsd/SchemaTypes/ | http://wso2.com/xsd/SchemaTypes/ |
java.lang.String | http://www.w3.org/2001/XMLSchema | http://wso2.com/xsd/SchemaTypes/ |
java.lang.String [] | http://wso2.com/wsdl/java/lang/ | http://wso2.com/xsd/SchemaTypes/ |
examples.Struct | http://wso2.com/wsdl/examples/ | http://wso2.com/xsd/SchemaTypes/ |
examples.Struct [] | http://wso2.com/wsdl/examples/ | http://wso2.com/xsd/SchemaTypes/ |
To use custom mapping for a specific data type, you can give a custom XML Schema to the compiler. Each generated type can be annotated in the following way:
<xsd:complexType name="SimpleStruct"> <xsd:annotation> <xsd:appinfo> <map:java-type name="examples.SimpleStruct" xmlns:map="http://systinet.com/mapping/"/> </xsd:appinfo> </xsd:annotation> ... </xsd:complexType>
For example, if the examples.SimpleStruct class has to be mapped, no type will be created. Instead, a pregenerated type will be referenced. The given schema will be imported everywhere it is used. Finally, it will be directly inserted or referenced in the target WSDL.
Consider a class which contains secret attributes and is extended by a public class. The secret attributes will appear in any mappings that result from using the Java2Schema tool. To prevent this, exclude such secret classes from the mapping.
Java2Schema can exclude a single class or a whole package. If a majority of the classes in a package must be excluded, exclude all classes in the package and include the individual classes from the package that are needed. Keep in mind that inclusion always has higher priority than exclusion.
For example, the following command excludes all classes in the package java.util except java.util.Set, which is specifically included:
Java2Schema --include-class java.util.Set --exclude-class java.util.*
Another problem arises in the case of multiple inheritance because XML Schema supports only simple inheritance. The algorithm for choosing the inheritance path is:
If class A extends class B (other than java.lang.Object, choose A-B.
If class A extends java.lang.Object and implements the interface C, choose A-C.
In other cases, class A is not derived from any class.
The default inheritance path can be changed. If class A extends B and implements interface C, while B implements interface D, it can be said that A follows these inheritance paths: A-C or A-D.
The following command tells class A to follow inheritance path A-C:
Java2Schema --inheritance "A-C" A
Java type mapping can be customized using deployment descriptors (please see Deployment Descriptors). If you already have a descriptor, simply tell Java2Schema to use it, as shown in the following command:
Java2Schema --deployment-descriptor package.xml --processing Processing example.service.MyService
Note that both deployment descriptor and processing options must be set at the start. The first option causes Java2Schema to parse the deployment descriptor file package.xml. The processing node named Processing inside the descriptor is taken and put into the mapping context. If the descriptor contains additional information--about type mapping, for example--WSO2 SOA Enablement Server will change its behavior to hold new settings.
Java2Schema uses several built-in Default XML Schemas. The conversion from Java classes to XML schema definition (.xsd) is driven by mapping information that is contained in the WSO2 SOA Enablement Server configuration. If this mapping points to a built-in schema, part of the type declaration is taken from the built-in schema and included in the generated .xsd file.
Each class that will be mapped, or that will be used in mapping, must be in the classpath. Java2Schema can extend the classpath for custom purposes, as shown in the following command:
Java2Schema --classpath c:\temp classname
Java2Schema provides several optional parameters for customizing compilation. At least one mandatory argument must be set. This contains the class names with which XML Schema are generated. Class names must be fully qualified. Their names are entered without any option. Every class must be in the classpath.
Usage of Java2Schema is:
Java2Schema [option...] class...
Options are listed in Table 21, “Java2Schema Command Options”.
Table 21. Java2Schema Command Options
Parameter --full-name -abbreviation | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|
--add-type class | Generates a schema type for the given class to output definition(s). Useful for polymorphism. | ||||||||
--classpath classpath --cp classpath | Classpath for loading class and source when these classes are not in the system classpath. | ||||||||
--deployment-descriptor file --processing localName | Specifies deployment descriptor file name and its processing. Both parameters must be set at the same time. | ||||||||
--exclude-class class -x class | Excludes all methods of a given class from the output definition. (You can use Java notation, for example java.lang.*, to exclude all classes.) | ||||||||
--force | Overwrites existing file when a file with same path as the file being generated exists already. This prevents File already exists-type errors. | ||||||||
--generate-wsdl -w | Generates a single WSDL file instead of multiple XML Schema files. | ||||||||
--help -h | Prints help screen with the commands. | ||||||||
--include-class class -y class | Includes all methods of a given class in the output definition. (Class inclusion has higher priority than exclusion.) | ||||||||
--inherit class-superclass | Specifies the inheritance path. | ||||||||
--insert xmlschema-or-wsdl-file | Takes a given XML Schema or WSDL file and uses types defined therein as the basis for compilation. Inserts file to target WSDL. | ||||||||
--no-java-mapping-extension | Disables generation of extensibility elements into XML Schema. | ||||||||
--output-directory dir -d dir | Directory where output definition(s) will be stored (default is current directory). | ||||||||
--output-filename filename -f filename | Sets the name of the main output file. The name is relative to the output directory. | ||||||||
--package-mapping package=targetNamespace -p package=targetNamespace | Maps a Java package to target namespace. For Java classes with no package (default package), use default as package name. | ||||||||
--singledim-arrays | Disallows the generation of SOAP Sec. 5 arrays as multidimensional. For example, a 2-dimensional array will be generated as an [][] array, not as an [,] array. | ||||||||
--soap-encoding-style style --ses style | Specifies SOAP encoding style (use attribute). Style can be either literal or encoded. The default value is read from WSO2 SOA Enablement Server configuration files and it is set during the installation process. The default during installation is literal. Please see SOAP Binding and Encoding Styles. | ||||||||
--use-nillable[true|false] | Sets which attribute should be generated for Java reference data types. If true, nillable=true is generated. If false, minOccurs=0 is generated. The default setting is 'true.' This default can be changed in the defaultUseNillable element in serverconf.xml or clientconf.xml. This switch can be useful if you are experiencing trouble communicating with .NET, as C# prefers optional elements to be present rather than nillable. | ||||||||
| |||||||||
--verbose[=length] | Additional information during processing will be printed to stderr. Optional length attribute sets max length of output message. | ||||||||
--version -v | Prints version information. |
Java2Schema can be partially configured through the client's configuration file in WSO2 SOA Enablement Server, the contents of which are shown in Example 183 .
Example 183. The Java2Schema Configuration File
<?xml version="1.0" encoding="UTF-8"?> <java2Type name="main"> <excludedClass>fullClassName-or-packageName-to-exclude</excludedClass> <includedClass>fullClassName-to-include</includedClass> <internalSchema fileName="schema-file-name" targetNamespace="namespace-to-include-schema-on"/> </java2Type>
Both server and client side WSO2 SOA Enablement Server configuration files preset Java2Schema to:
Insert XML Schema to generated WSDL if appropriate namespace is used (see Table 22, “Default Configuration of Java2Schema” ) --insert MyType.xsd
Exclude these classes and packages from compilation:
java.io.* java.lang.ClassLoader java.lang.Compiler java.lang.ThreadLocal java.lang.Process java.lang.Runtime java.lang.RuntimePermission java.lang.SecurityManager java.lang.System java.lang.Thread java.lang.ThreadGroup java.lang.Throwable java.net.* java.text.* org.systinet.wasp.webservice.*
Table 22. Default Configuration of Java2Schema
Namespace | XML Schema File |
---|---|
http://idoox.com/interface | com/idoox/wasp/interrefs/interrefs.xsd |
http://idoox.com/interface | com/idoox/wasp/interrefs/interrefs.literal.xsd |
http://idoox.com/security | com/idoox/wasp/security/security.xsd |
http://idoox.com/containers | com/idoox/wasp/serialization/javabuiltin/idooxContainers.xsd |
http://idoox.com/containers | com/idoox/wasp/serialization/javabuiltin/apacheContainers.xsd |
http://xml.apache.org/xml-soap | com/idoox/wasp/serialization/javabuiltin/apacheContainers.xsd |
http://wso2.com/containers/encoded/ms.net | com/idoox/wasp/serialization/javabuiltin/msnet/msnetEncodedContainers.xsd |
http://wso2.com/containers/literal/ms.net | com/idoox/wasp/serialization/xml/msnet/msnetLiteralContainers.xsd |
http://idoox.com/sparse-array | com/idoox/wasp/serialization/idooxbuiltin/array.xsd |
All examples can be run from the src/example/tools/java2schema directory.
The Java2Schema tools can be used in a simplified way by passing a class in the classpath as an argument on the command line, as in this command for passing the SimpleStructure class:
Java2Schema example.tools.java2schema.SimpleStructure.SimpleStructure
Example 184. SimpleStructure
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.tools.java2schema.SimpleStructure; public class SimpleStructure { public int i; public String s; }
A WSDL file named Schema_example_tools_java2schema_SimpleStructure.xs --insert MyType.xsdd is generated for direct use.
Example 185. WSDL Produced by Simple Usage of Java2Schema
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema elementFormDefault="qualified" targetNamespace="http://systinet.com/wsdl/example/tools/java2schema/SimpleStructure/" xmlns:map="http://systinet.com/mapping/" xmlns:tns="http://systinet.com/wsdl/example/tools/java2schema/SimpleStructure/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:complexType name="SimpleStructure"> <xsd:annotation> <xsd:appinfo> <map:java-type name="example.tools.java2schema.SimpleStructure.SimpleStructure"/> </xsd:appinfo> </xsd:annotation> <xsd:sequence> <xsd:element name="i" type="xsd:int"/> <xsd:element name="s" nillable="true" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
Data types can be generated either in RPC/encoded or document/literal style. If in RPC/encoded style, the types are generated according to the SOAP 1.1 encoding rules. Use the --soap-encoding-style command to set encoding styles, as shown below for the RPCEncoded class.
Java2Schema example.tools.java2schema.RPCEncoded.SoapStructure --soap-encoding-style encoded
The default behavior of WSO2 SOA Enablement Server is to generate a service in document/literal style. To make certain of this, use the --soap-encoding-style or --ses option, as shown here for the SoapStructure class:
Java2Schema example.tools.java2schema.DocumentLiteral.SoapStructure --soap-encoding-style literal
Example 187. SoapStructure
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.tools.java2schema.DocumentLiteral; public class SoapStructure { public int[] ints; public long[] longs; public String[][] strings; }
The generated XML Schema will differ from the previous example if some of attributes are arrays or Java containers. See Literal vs. Encoded Serialization
To include or exclude a class, or all of the package, simply name it on the command line. Do not forget to take care of the default included and excluded classes named in the WSO2 SOA Enablement Server configuration file. If class A extends from B, but B has secret attributes, you can simply deny B, as shown in the following command. No prohibited extension will be written into the target Schema and only non-excluded attributes will occur in the final XML structure.
Java2Schema example.tools.java2schema.ClassExclusion.A --insert MyType.xsd --exclude-class src.example.tools.java2schema.ClassExclusion.B
A problem can occur due to multiple inheritance, as it is not supported by XML Schema. It can be partially solved by setting an explicit inheritance path. If class A extends class B and implements interfaces C and D, you can use the --inherit parameter to change the inheritance to A-C or A-D, as shown in this command:
Java2Schema example.tools.java2schema.Inheritance.A --inherit example.tools.java2schema.Inheritance.A - example.tools.java2schema.Inheritance.C
See Example 190.
Example 190. Specifying Inheritance of an Interface
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.tools.java2schema.Inheritance; public class A extends B implements C { } // Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.tools.java2schema.Inheritance; public class B implements D { } // Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.tools.java2schema.Inheritance; public interface C { } // Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.tools.java2schema.Inheritance; public interface D { }
Choosing another inheritance, where Class A derives Class D, you use this command:
Java2Schema example.tools.java2schema.Inheritance.A --inherit example.tools.java2schema.Inheritance.A-example.tools.java2schema.Inheritance.D
If a type should be managed in some way other than the default, you can deliver a custom XML Schema that describes how the type should be mapped. To hold references, this Schema can be either inserted into the target WSDL or just referenced by the import element as the following command shows for the Structure and MyType classes:
Java2Schema example.tools.java2schema.OwnMapping.Structure --insert MyType.xsd
Example 191. Java code used for custom mapping
// Copyright WSO2 Inc. All rights reserved. // Use is subject to license terms. package example.tools.java2schema.OwnMapping; public class Structure { public MyType myType; public int otherType; } class MyType { public int[] arr; public String s; }
MyType.xsd contains the custom mapping of class MyType:
Example 192. Schema for the custom type
<?xml version='1.0'?> <xsd:schema targetNamespace="http://systinet.com/wsdl/SchemaTypes/" xmlns:tns="http://systinet.com/wsdl/SchemaTypes/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:complexType name="MyType"> <xsd:annotation> <xsd:appinfo> <map:java-type name="example.tools.java2schema.OwnMapping.MyType" xmlns:map="http://systinet.com/mapping/"/> </xsd:appinfo> </xsd:annotation> <xsd:sequence> <xsd:element maxOccurs="unbounded" minOccurs="0" name="string" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
WSO2 SOA Enablement Server includes some pregenerated XML Schemas that define various structures which WSO2 SOA Enablement Server supports. Each Schema is tied to a namespace (usually its target namespace). If this namespace is used or referenced, an appropriate Schema will be included in the generated file.
See WSO2 SOA Enablement Server Built-in XML Schema Files for an overview of all pre-generated XML Schemas.