001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.wsif.providers;
059:
060: import java.io.Serializable;
061: import java.util.ArrayList;
062: import java.util.Iterator;
063: import java.util.Vector;
064:
065: import javax.xml.namespace.QName;
066:
067: import org.apache.wsif.logging.Trc;
068: import org.apache.wsif.util.WSIFUtils;
069:
070: /**
071: * Container for type mappings that can be used by dynamic providers.
072: *
073: * @author Alekander Slominski
074: * @author Owen Burroughs <owenb@apache.org>
075: */
076: public class WSIFDynamicTypeMap implements Serializable {
077:
078: private static final long serialVersionUID = 1L;
079:
080: protected Vector typeMapList = new Vector();
081: // Create a Vector of xmlTypes (QNames) for faster lookup of existing mappings
082: protected Vector xmlTypes = new Vector();
083: protected ArrayList allTypes;
084:
085: /**
086: * Default constructor
087: */
088: public WSIFDynamicTypeMap() {
089: Trc.entry(this );
090: allTypes = new ArrayList();
091: Trc.exit();
092: }
093:
094: /**
095: * Constructor
096: * @param alist An ArrayList of all custom types from the wsdl
097: */
098: public WSIFDynamicTypeMap(ArrayList aList) {
099: 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 xmlType, Class 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,
117: javaType));
118: xmlTypes.add(xmlType);
119: } else {
120: // Mapping for this type already exists so replace it.
121: typeMapList.setElementAt(new WSIFDynamicTypeMapping(
122: xmlType, javaType), i);
123: }
124: Trc.exit();
125: }
126:
127: /**
128: * Add new mapping between XML and Java type.
129: * @param xmlType The qualified xml name
130: * @param javaType The Java class
131: * @param force flag to indicate if mapping should override an existing one
132: * for the same xmlType
133: */
134: public void mapType(QName xmlType, Class javaType, boolean force) {
135: Trc.entry(this , xmlType, javaType);
136: int i = xmlTypes.indexOf(xmlType);
137: // Only add mapping if no mapping yet exists for this type
138: // or if force is set to true
139: if (force || (i == -1)) {
140: if (i == -1) {
141: // No mapping for this type exists so create one.
142: typeMapList.add(new WSIFDynamicTypeMapping(xmlType,
143: javaType));
144: xmlTypes.add(xmlType);
145: } else {
146: // Mapping for this type already exists so replace it.
147: typeMapList.setElementAt(new WSIFDynamicTypeMapping(
148: xmlType, javaType), i);
149: }
150: }
151: Trc.exit();
152: }
153:
154: /**
155: * Map a package name to a namespace URI
156: * @param namespace The wsdl namespace
157: * @param packageName The name of the Java package
158: */
159: public void mapPackage(String namespace, String packageName) {
160: Trc.entry(this , namespace, packageName);
161: Iterator it = allTypes.iterator();
162: while (it.hasNext()) {
163: QName qname = (QName) it.next();
164: String ns = qname.getNamespaceURI();
165: if (ns != null && ns.equals(namespace)) {
166: resolveMapping(qname, packageName);
167: }
168: }
169: Trc.exit();
170: }
171:
172: protected void resolveMapping(QName qn, String pn) {
173: Trc.entry(this , qn, pn);
174: try {
175: String xmlName = qn.getLocalPart();
176: if (xmlName != null) {
177: String javaName = WSIFUtils
178: .getJavaClassNameFromXMLName(xmlName);
179: String classname = pn + "." + javaName;
180: Class cl = Class.forName(classname, true, Thread
181: .currentThread().getContextClassLoader());
182: mapType(qn, cl);
183: }
184: } catch (Exception e) {
185: Trc.ignoredException(e);
186: //ignore
187: }
188: Trc.exit();
189: }
190:
191: /**
192: * Set the list of all custom types from the wsdl
193: * @param aList The list
194: */
195: public void setAllTypes(ArrayList aList) {
196: Trc.entry(this , aList);
197: allTypes = aList;
198: Trc.exit();
199: }
200:
201: /**
202: * Return iterator with all mappings.
203: * @return The iterator
204: */
205: public Iterator iterator() {
206: Trc.entry(this );
207: Trc.exit();
208: return typeMapList.iterator();
209: }
210:
211: /**
212: * Produce a copy of the WSIFDynamicTypeMap. This is not a clone;
213: * the copy will contain references to the same WSIFDynamicTypeMappings.
214: * This method contains synchronized code so that the type map cannot
215: * be altered whilst the copy takes place.
216: * @return The copy of the WSIFDynamicTypeMap
217: */
218: public WSIFDynamicTypeMap copy() {
219: // Obtain a lock on the hash map
220: synchronized (typeMapList) {
221: WSIFDynamicTypeMap tm = new WSIFDynamicTypeMap();
222: synchronized (allTypes) {
223: tm.setAllTypes((ArrayList) allTypes.clone());
224: }
225: Iterator it = typeMapList.iterator();
226: // Copy the mappings
227: while (it.hasNext()) {
228: WSIFDynamicTypeMapping temp = (WSIFDynamicTypeMapping) it
229: .next();
230: tm.mapType(temp.getXmlType(), temp.getJavaType());
231: }
232: return tm;
233: }
234: }
235: }
|