KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > axis > builder > LightweightTypeInfoBuilder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.axis.builder;
18
19 import java.beans.IntrospectionException JavaDoc;
20 import java.beans.Introspector JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.xml.namespace.QName JavaDoc;
30
31 import org.apache.axis.description.ElementDesc;
32 import org.apache.axis.description.FieldDesc;
33 import org.apache.axis.encoding.ser.ArrayDeserializerFactory;
34 import org.apache.axis.encoding.ser.ArraySerializerFactory;
35 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
36 import org.apache.axis.encoding.ser.BeanSerializerFactory;
37 import org.apache.geronimo.axis.client.TypeInfo;
38 import org.apache.geronimo.common.DeploymentException;
39 import org.apache.geronimo.kernel.ClassLoading;
40 import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingType;
41 import org.apache.geronimo.webservices.builder.SchemaTypeKey;
42 import org.apache.geronimo.webservices.builder.WSDescriptorParser;
43 import org.apache.xmlbeans.SchemaParticle;
44 import org.apache.xmlbeans.SchemaType;
45
46 /**
47  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
48  */

49 public class LightweightTypeInfoBuilder implements TypeInfoBuilder {
50     private final ClassLoader JavaDoc cl;
51     private final Map JavaDoc schemaTypeKeyToSchemaTypeMap;
52     private final Set JavaDoc wrapperElementQNames;
53
54     public LightweightTypeInfoBuilder(ClassLoader JavaDoc cl, Map JavaDoc schemaTypeKeyToSchemaTypeMap, Set JavaDoc wrapperElementQNames) {
55         this.cl = cl;
56         this.schemaTypeKeyToSchemaTypeMap = schemaTypeKeyToSchemaTypeMap;
57         this.wrapperElementQNames = wrapperElementQNames;
58     }
59
60     public List JavaDoc buildTypeInfo(JavaWsdlMappingType mapping) throws DeploymentException {
61         List JavaDoc typeInfoList = new ArrayList JavaDoc();
62
63         for (Iterator JavaDoc iterator = schemaTypeKeyToSchemaTypeMap.keySet().iterator(); iterator.hasNext();) {
64             SchemaTypeKey key = (SchemaTypeKey) iterator.next();
65             if (!key.isElement() && !key.isAnonymous()) {
66                 //default settings
67
QName JavaDoc typeQName = key.getqName();
68                 String JavaDoc namespace = typeQName.getNamespaceURI();
69                 String JavaDoc packageName = WSDescriptorParser.getPackageFromNamespace(namespace, mapping);
70                 String JavaDoc classShortName = typeQName.getLocalPart();
71                 String JavaDoc className = packageName + "." + classShortName;
72
73                 Class JavaDoc clazz = null;
74                 try {
75                     clazz = ClassLoading.loadClass(className, cl);
76                 } catch (ClassNotFoundException JavaDoc e) {
77                     throw new DeploymentException("Could not load java type", e);
78                 }
79
80                 Class JavaDoc serializerFactoryClass = BeanSerializerFactory.class;
81                 Class JavaDoc deserializerFactoryClass = BeanDeserializerFactory.class;
82
83                 if (clazz.isArray()) {
84                     serializerFactoryClass = ArraySerializerFactory.class;
85                     deserializerFactoryClass = ArrayDeserializerFactory.class;
86                 }
87
88                 TypeInfo.UpdatableTypeInfo internalTypeInfo = new TypeInfo.UpdatableTypeInfo();
89                 internalTypeInfo.setClazz(clazz);
90                 internalTypeInfo.setQName(typeQName);
91                 internalTypeInfo.setSerializerClass(serializerFactoryClass);
92                 internalTypeInfo.setDeserializerClass(deserializerFactoryClass);
93
94                 populateInternalTypeInfo(clazz, typeQName, key, internalTypeInfo);
95
96                 typeInfoList.add(internalTypeInfo.buildTypeInfo());
97             }
98         }
99
100         return typeInfoList;
101     }
102
103     private void populateInternalTypeInfo(Class JavaDoc javaClass, QName JavaDoc typeQName, SchemaTypeKey key, TypeInfo.UpdatableTypeInfo typeInfo) throws DeploymentException {
104         SchemaType schemaType = (SchemaType) schemaTypeKeyToSchemaTypeMap.get(key);
105         if (schemaType == null) {
106             throw new DeploymentException("Schema type key " + key + " not found in analyzed schema: " + schemaTypeKeyToSchemaTypeMap);
107         }
108         typeInfo.setCanSearchParents(schemaType.getDerivationType() == SchemaType.DT_RESTRICTION);
109
110         Map JavaDoc nameToType = new HashMap JavaDoc();
111         if (null == schemaType.getContentModel()) {
112             ;
113         } else if (SchemaParticle.SEQUENCE == schemaType.getContentModel().getParticleType()
114                 || SchemaParticle.ALL == schemaType.getContentModel().getParticleType()) {
115             SchemaParticle[] properties = schemaType.getContentModel().getParticleChildren();
116             for (int i = 0; i < properties.length; i++) {
117                 SchemaParticle parameter = properties[i];
118 // if (SchemaParticle.ELEMENT != parameter.getType().getContentModel().getParticleType()) {
119
// throw new DeploymentException(parameter.getName() + " is not an element in schema " + schemaType.getName());
120
// }
121
nameToType.put(parameter.getName(), parameter);
122             }
123         } else if (SchemaParticle.ELEMENT == schemaType.getContentModel().getParticleType()) {
124             SchemaParticle parameter = schemaType.getContentModel();
125             nameToType.put(parameter.getName(), parameter);
126         } else {
127             throw new DeploymentException("Only all, choice and sequence particle types are supported." +
128                     " SchemaType name =" + schemaType.getName());
129         }
130
131         PropertyDescriptor JavaDoc[] descriptors;
132         try {
133             descriptors = Introspector.getBeanInfo(javaClass).getPropertyDescriptors();
134         } catch (IntrospectionException JavaDoc e) {
135             throw new DeploymentException("Class " + javaClass + " is not a valid javabean", e);
136         }
137         Map JavaDoc nameToClass = new HashMap JavaDoc();
138         for (int i = 0; i < descriptors.length; i++) {
139             nameToClass.put(descriptors[i].getName(), descriptors[i].getPropertyType());
140         }
141
142         int idx = 0;
143         FieldDesc[] fields = new FieldDesc[nameToType.size()];
144         typeInfo.setFields(fields);
145         for (Iterator JavaDoc iter = nameToType.entrySet().iterator(); iter.hasNext();) {
146             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
147             QName JavaDoc fieldQName = (QName JavaDoc) entry.getKey();
148             String JavaDoc fieldName = fieldQName.getLocalPart();
149             SchemaParticle particle = (SchemaParticle) entry.getValue();
150
151             ElementDesc elementDesc = new ElementDesc();
152             elementDesc.setFieldName(fieldName);
153
154             Class JavaDoc javaType = (Class JavaDoc) nameToClass.get(fieldName);
155             if (null == javaType) {
156                 throw new DeploymentException("Field " + fieldName + " is not defined by class " + javaClass.getName());
157             }
158             elementDesc.setNillable(particle.isNillable());
159             elementDesc.setXmlName(fieldQName);
160             elementDesc.setXmlType(particle.getType().getName());
161
162             if (javaType.isArray()) {
163                 elementDesc.setMinOccurs(particle.getIntMinOccurs());
164                 elementDesc.setMaxOccurs(particle.getIntMaxOccurs());
165                 //TODO axis seems to have the wrong name for this property based on how it is used
166
elementDesc.setMaxOccursUnbounded(particle.getIntMaxOccurs() > 1);
167             }
168
169             fields[idx++] = elementDesc;
170         }
171     }
172 }
173
Popular Tags