KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > WSIFDynamicTypeMap


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.providers;
59
60 import java.io.Serializable JavaDoc;
61 import java.util.ArrayList JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.Vector JavaDoc;
64
65 import javax.xml.namespace.QName JavaDoc;
66
67 import org.apache.wsif.logging.Trc;
68 import org.apache.wsif.util.WSIFUtils;
69
70 /**
71  * Container for type mappings that can be used by dynamic providers.
72  *
73  * @author Alekander Slominski
74  * @author Owen Burroughs <owenb@apache.org>
75  */

76 public class WSIFDynamicTypeMap implements Serializable JavaDoc {
77     
78     private static final long serialVersionUID = 1L;
79     
80     protected Vector JavaDoc typeMapList = new Vector JavaDoc();
81     // Create a Vector of xmlTypes (QNames) for faster lookup of existing mappings
82
protected Vector JavaDoc xmlTypes = new Vector JavaDoc();
83     protected ArrayList JavaDoc allTypes;
84
85     /**
86      * Default constructor
87      */

88     public WSIFDynamicTypeMap() {
89         Trc.entry(this);
90         allTypes = new ArrayList JavaDoc();
91         Trc.exit();
92     }
93
94     /**
95      * Constructor
96      * @param alist An ArrayList of all custom types from the wsdl
97      */

98     public WSIFDynamicTypeMap(ArrayList JavaDoc aList) {
99         Trc.entry(this, aList);
100         allTypes = aList;
101         Trc.exit();
102     }
103
104     /**
105      * Add new mapping between XML and Java type. This method is equivalent to
106      * calling mapType(xmlType, javaType, true)
107      * @param xmlType The qualified xml name
108      * @param javaType The Java class
109      * @see #mapType(QName, Class, boolean)
110      */

111     public void mapType(QName JavaDoc xmlType, Class JavaDoc javaType) {
112         Trc.entry(this, xmlType, javaType);
113         int i = xmlTypes.indexOf(xmlType);
114         if (i == -1) {
115             // No mapping for this type exists so create one.
116
typeMapList.add(new WSIFDynamicTypeMapping(xmlType, javaType));
117             xmlTypes.add(xmlType);
118         } else {
119             // Mapping for this type already exists so replace it.
120
typeMapList.setElementAt(new WSIFDynamicTypeMapping(xmlType, javaType), i);
121         }
122         Trc.exit();
123     }
124
125     /**
126      * Add new mapping between XML and Java type.
127      * @param xmlType The qualified xml name
128      * @param javaType The Java class
129      * @param force flag to indicate if mapping should override an existing one
130      * for the same xmlType
131      */

132     public void mapType(QName JavaDoc xmlType, Class JavaDoc javaType, boolean force) {
133         Trc.entry(this, xmlType, javaType);
134         int i = xmlTypes.indexOf(xmlType);
135         // Only add mapping if no mapping yet exists for this type
136
// or if force is set to true
137
if (force || (i == -1)) {
138             if (i == -1) {
139                 // No mapping for this type exists so create one.
140
typeMapList.add(new WSIFDynamicTypeMapping(xmlType, javaType));
141                 xmlTypes.add(xmlType);
142             } else {
143                 // Mapping for this type already exists so replace it.
144
typeMapList.setElementAt(new WSIFDynamicTypeMapping(xmlType, javaType), i);
145             }
146         }
147         Trc.exit();
148     }
149
150     /**
151      * Map a package name to a namespace URI
152      * @param namespace The wsdl namespace
153      * @param packageName The name of the Java package
154      */

155     public void mapPackage(String JavaDoc namespace, String JavaDoc packageName) {
156         Trc.entry(this, namespace, packageName);
157         Iterator JavaDoc it = allTypes.iterator();
158         while (it.hasNext()) {
159             QName JavaDoc qname = (QName JavaDoc) it.next();
160             String JavaDoc ns = qname.getNamespaceURI();
161             if (ns != null && ns.equals(namespace)) {
162                 resolveMapping(qname, packageName);
163             }
164         }
165         Trc.exit();
166     }
167
168     protected void resolveMapping(QName JavaDoc qn, String JavaDoc pn) {
169         Trc.entry(this, qn, pn);
170         try {
171             String JavaDoc xmlName = qn.getLocalPart();
172             if (xmlName != null) {
173                 String JavaDoc javaName = WSIFUtils.getJavaClassNameFromXMLName(xmlName);
174                 String JavaDoc classname = pn + "." + javaName;
175                 Class JavaDoc cl =
176                     Class.forName(classname, true, Thread.currentThread().getContextClassLoader());
177                 mapType(qn, cl);
178             }
179         } catch (Exception JavaDoc e) {
180             Trc.ignoredException(e);
181             //ignore
182
}
183         Trc.exit();
184     }
185
186     /**
187      * Set the list of all custom types from the wsdl
188      * @param aList The list
189      */

190     public void setAllTypes(ArrayList JavaDoc aList) {
191         Trc.entry(this, aList);
192         allTypes = aList;
193         Trc.exit();
194     }
195
196     /**
197      * Return iterator with all mappings.
198      * @return The iterator
199      */

200     public Iterator JavaDoc iterator() {
201         Trc.entry(this);
202         Trc.exit();
203         return typeMapList.iterator();
204     }
205     /**
206      * Produce a copy of the WSIFDynamicTypeMap. This is not a clone;
207      * the copy will contain references to the same WSIFDynamicTypeMappings.
208      * This method contains synchronized code so that the type map cannot
209      * be altered whilst the copy takes place.
210      * @return The copy of the WSIFDynamicTypeMap
211      */

212     public WSIFDynamicTypeMap copy() {
213         // Obtain a lock on the hash map
214
synchronized (typeMapList) {
215             WSIFDynamicTypeMap tm = new WSIFDynamicTypeMap();
216             synchronized (allTypes) {
217                 tm.setAllTypes((ArrayList JavaDoc) allTypes.clone());
218             }
219             Iterator JavaDoc it = typeMapList.iterator();
220             // Copy the mappings
221
while (it.hasNext()) {
222                 WSIFDynamicTypeMapping temp =
223                     (WSIFDynamicTypeMapping) it.next();
224                 tm.mapType(temp.getXmlType(), temp.getJavaType());
225             }
226             return tm;
227         }
228     }}
Popular Tags