KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > encoding > ser > MetaDataTypeDesc


1 /**
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.webservice.encoding.ser;
8
9 // $Id: MetaDataTypeDesc.java,v 1.6.2.8 2005/06/15 22:59:29 jgreene Exp $
10

11 import org.jboss.axis.description.AttributeDesc;
12 import org.jboss.axis.description.ElementDesc;
13 import org.jboss.axis.description.FieldDesc;
14 import org.jboss.axis.description.TypeDesc;
15 import org.jboss.axis.utils.BeanPropertyDescriptor;
16 import org.jboss.logging.Logger;
17 import org.jboss.mx.util.MBeanServerLocator;
18 import org.jboss.webservice.deployment.BeanXMLMetaData;
19 import org.jboss.webservice.deployment.MetaDataRegistry;
20
21 import javax.management.MBeanServer JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23 import javax.xml.namespace.QName JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * An Axis TypeDesc that can get additional information from the ws4ee-deployment.xml
30  * <p/>
31  * Type <typeMapping> element has the same basic structure as defined by the wsdd schema.
32  * <p/>
33  * A additionally you can have a <typeDesc> child element which more information on every field
34  * <p/>
35  * Initially the TypeDesc is initalized through introspection from a java type, all fields will be encoded
36  * xsd style in a random order.
37  * <p/>
38  * Here you can specify the name and encoding style for every element.
39  * <p/>
40  * The <elementOrder> is optional, but when present must contain all fields from the java type. This is
41  * usefull when the schema complexType specifies <sequence> instead of <all>.
42  * <p/>
43  * Here is an example:
44  * <p/>
45  * <typeMapping
46  * qname='ns1:SequenceStruct2' xmlns:ns1='http://MarshallTest.org/xsd'
47  * type='java:org.jboss.ws4ee.test.marshalltest.rpcenc.SequenceStruct2'
48  * serializer='org.jboss.webservice.encoding.ser.MetaDataBeanSerializerFactory'
49  * deserializer='org.jboss.webservice.encoding.ser.MetaDataBeanDeserializerFactory'
50  * encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
51  * <typeDesc>
52  * <elementDesc fieldName="varBase64Binary" xmlName="varBase64Binary" xmlType="xsd:base64Binary"/>
53  * <elementDesc fieldName="varHexBinary" xmlName="varHexBinary" xmlType="xsd:hexBinary"/>
54  * <elementDesc fieldName="varSoapString" xmlName="varSoapString" xmlType="soapenc:string"/>
55  * <elementDesc fieldName="varSoapBoolean" xmlName="varSoapBoolean" xmlType="soapenc:boolean"/>
56  * <elementDesc fieldName="varSoapFloat" xmlName="varSoapFloat" xmlType="soapenc:float"/>
57  * <elementDesc fieldName="varSoapDouble" xmlName="varSoapDouble" xmlType="soapenc:double"/>
58  * <elementDesc fieldName="varSoapDecimal" xmlName="varSoapDecimal" xmlType="soapenc:decimal"/>
59  * <elementDesc fieldName="varSoapInt" xmlName="varSoapInt" xmlType="soapenc:int"/>
60  * <elementDesc fieldName="varSoapShort" xmlName="varSoapShort" xmlType="soapenc:short"/>
61  * <elementDesc fieldName="varSoapByte" xmlName="varSoapByte" xmlType="soapenc:byte"/>
62  * <elementDesc fieldName="varSoapBase64" xmlName="varSoapBase64" xmlType="soapenc:base64"/>
63  * <elementDesc fieldName="varDateTimeArray" xmlName="varDateTimeArray" itemXmlType="xsd:dateTime"/>
64  * <elementOrder>
65  * <element name="varString"/>
66  * <element name="varInteger"/>
67  * <element name="varInt"/>
68  * <element name="varLong"/>
69  * <element name="varShort"/>
70  * <element name="varDecimal"/>
71  * <element name="varFloat"/>
72  * <element name="varDouble"/>
73  * <element name="varBoolean"/>
74  * <element name="varByte"/>
75  * <element name="varQName"/>
76  * <element name="varDateTime"/>
77  * <element name="varSoapString"/>
78  * <element name="varSoapBoolean"/>
79  * <element name="varSoapFloat"/>
80  * <element name="varSoapDouble"/>
81  * <element name="varSoapDecimal"/>
82  * <element name="varSoapInt"/>
83  * <element name="varSoapShort"/>
84  * <element name="varSoapByte"/>
85  * <element name="varBase64Binary"/>
86  * <element name="varHexBinary"/>
87  * <element name="varSoapBase64"/>
88  * <element name="varSequenceStruct"/>
89  * <element name="varDateTimeArray"/>
90  * </elementOrder>
91  * </typeDesc>
92  * </typeMapping>
93  *
94  * @author thomas.diesler@jboss.org
95  * @since 18-June-2004
96  */

97 public class MetaDataTypeDesc extends TypeDesc
98 {
99    // provide logging
100
private final Logger log = Logger.getLogger(MetaDataTypeDesc.class);
101
102    public MetaDataTypeDesc(Class JavaDoc javaClass, QName JavaDoc xmlType, BeanXMLMetaData metaData)
103    {
104       super(javaClass);
105
106       if (javaClass == null)
107          throw new IllegalArgumentException JavaDoc("javaClass cannot be null");
108       if (xmlType == null)
109          throw new IllegalArgumentException JavaDoc("xmlType cannot be null");
110
111       // On the client side this should never be null because the ServiceImpl installs the typemappings.
112
if (metaData == null)
113          metaData = getServerSideMetaData(xmlType);
114
115       try
116       {
117          if (metaData != null)
118          {
119             setXmlType(xmlType);
120             setupElementDescriptions(metaData);
121             setupElementOrder(metaData);
122          }
123       }
124       catch (RuntimeException JavaDoc e)
125       {
126          log.error("Instanciation error", e);
127          throw e;
128       }
129    }
130
131    /**
132     * The server side installs type mappings through the WSDD deployment, which constructs
133     * the MetaDataBean[Serializer|Deserializer]Factory without metaData.
134     *
135     * This hack calls back to the AxisServer to obtain the missing metaData from a registry.
136     * Ideally we would like to register the serializer/deserializer factories with axis complete
137     * with meta data, but I could not figure out how to get to the TypeMappingRegistry at deploy time.
138     */

139    private BeanXMLMetaData getServerSideMetaData(QName JavaDoc xmlType)
140    {
141       BeanXMLMetaData metaData = null;
142       try
143       {
144          MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
145          ObjectName JavaDoc objectName = new ObjectName JavaDoc("jboss.ws4ee:service=AxisService");
146          MetaDataRegistry registry = (MetaDataRegistry)server.getAttribute(objectName, "MetaDataRegistry");
147          metaData = registry.getTypeMappingMetaData(xmlType);
148       }
149       catch (Exception JavaDoc e)
150       {
151          log.warn("Cannot obtain metaData from AxisService: " + e);
152       }
153       return metaData;
154    }
155
156    private void setupElementDescriptions(BeanXMLMetaData metaData)
157    {
158       // Setup element descriptions
159
Iterator JavaDoc itElDesc = metaData.getElementMetaData();
160       while (itElDesc.hasNext())
161       {
162          BeanXMLMetaData.ElementMetaData elMetaData = (BeanXMLMetaData.ElementMetaData)itElDesc.next();
163          String JavaDoc fieldName = elMetaData.getFieldName();
164          QName JavaDoc xmlName = elMetaData.getXmlName();
165          QName JavaDoc xmlType = elMetaData.getXmlType();
166          boolean asAttribute = elMetaData.isAsAttribute();
167          boolean asContent = elMetaData.isAsContent();
168
169          FieldDesc fieldDesc = null;
170          if (asAttribute)
171             fieldDesc = new AttributeDesc();
172          else
173          {
174             fieldDesc = new ElementDesc();
175             ((ElementDesc)fieldDesc).setAsContent(asContent);
176             ((ElementDesc)fieldDesc).setItemXmlType(elMetaData.getItemXmlType());
177
178             Integer JavaDoc minOccurs = elMetaData.getMinOccurs();
179             if (minOccurs != null)
180             {
181                ((ElementDesc)fieldDesc).setMinOccurs(minOccurs.intValue());
182             }
183          }
184
185          fieldDesc.setFieldName(fieldName);
186          fieldDesc.setXmlName(xmlName);
187          fieldDesc.setXmlType(xmlType);
188          addFieldDesc(fieldDesc);
189       }
190    }
191
192    private void setupElementOrder(BeanXMLMetaData metaData)
193    {
194       // Setup element order
195
Iterator JavaDoc itElementOrder = metaData.getElementOrder();
196       if (itElementOrder.hasNext())
197       {
198          Map JavaDoc bpdMap = getPropertyDescriptorMap();
199          BeanPropertyDescriptor[] bpdArr = new BeanPropertyDescriptor[bpdMap.size()];
200
201          int index = 0;
202          if (bpdMap.size() > 0)
203          {
204             ArrayList JavaDoc keys = new ArrayList JavaDoc(bpdMap.keySet());
205
206             // The first one is fixed
207
BeanPropertyDescriptor bpd = (BeanPropertyDescriptor)bpdMap.get("class");
208             if (bpd != null)
209             {
210                bpdArr[index++] = bpd;
211                keys.remove("class");
212             }
213
214             // copy the BeanPropertyDescriptors according to the meta data elementOrder
215
while (itElementOrder.hasNext())
216             {
217                String JavaDoc fieldName = (String JavaDoc)itElementOrder.next();
218
219                bpd = (BeanPropertyDescriptor)bpdMap.get(fieldName);
220                if (bpd != null)
221                {
222                   bpdArr[index++] = bpd;
223                   keys.remove(fieldName);
224                }
225             }
226
227             // copy the remaining BeanPropertyDescriptors
228
Iterator JavaDoc it = keys.iterator();
229             while (it.hasNext())
230             {
231                String JavaDoc name = (String JavaDoc)it.next();
232                bpd = (BeanPropertyDescriptor)bpdMap.get(name);
233                if (bpd != null)
234                   bpdArr[index++] = bpd;
235             }
236
237             setPropertyDescriptors(bpdArr);
238          }
239       }
240    }
241 }
242
Popular Tags