001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)BindingImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.wsdl2.impl;
030:
031: import java.io.StringWriter;
032:
033: import java.util.Map;
034:
035: import javax.xml.namespace.QName;
036:
037: import org.apache.xmlbeans.XmlOptions;
038:
039: import org.w3.ns.wsdl.BindingFaultType;
040: import org.w3.ns.wsdl.BindingType;
041:
042: import org.w3c.dom.DocumentFragment;
043:
044: /**
045: * Implementation of WSDL 2.0 Binding component.
046: *
047: * @author Sun Microsystems, Inc.
048: */
049: public final class BindingImpl extends Binding {
050: /** The definitions component this binding belongs to */
051: private DescriptionImpl mContainer;
052:
053: /**
054: * Get the container for this component.
055: *
056: * @return The component for this component
057: */
058: protected DescriptionImpl getContainer() {
059: return this .mContainer;
060: }
061:
062: /**
063: * Construct a binding component implementation object from the given XML
064: * bean.
065: *
066: * @param bean The binding XML bean to use to construct this component.
067: * @param defs The container for this binding component.
068: */
069: private BindingImpl(BindingType bean, DescriptionImpl defs) {
070: super (bean);
071:
072: this .mContainer = defs;
073: }
074:
075: /** Map of WSDL-defined attribute QNames. Keyed by QName.toString value */
076: private static java.util.Map sWsdlAttributeQNames = null;
077:
078: /**
079: * Worker class method for {@link #getWsdlAttributeNameMap()}.
080: *
081: * @return Map of WSDL-defined attribute QNames for this component,
082: * indexed by QName.toString()
083: */
084: static synchronized java.util.Map getAttributeNameMap() {
085: if (sWsdlAttributeQNames == null) {
086: sWsdlAttributeQNames = XmlBeansUtil
087: .getAttributesMap(BindingType.type);
088: }
089:
090: return sWsdlAttributeQNames;
091: }
092:
093: /**
094: * Get map of WSDL-defined attribute QNames for this component, indexed by
095: * canonical QName string (see {@link javax.xml.namespace.QName#toString()}.
096: * <p>
097: * Implementation note: since this method is declared in the public API
098: * <code>interface</code>, it has to be a member, not static. We delegate
099: * to a class method to do the actual work.
100: *
101: * @return Map of WSDL-defined attribute QNames for this component,
102: * indexed by QName.toString()
103: */
104: public java.util.Map getWsdlAttributeNameMap() {
105: return getAttributeNameMap();
106: }
107:
108: /**
109: * Get the target namespace of this binding.
110: *
111: * @return The target namespace of this binding's container.
112: */
113: public String getTargetNamespace() {
114: return this .mContainer.getTargetNamespace();
115: }
116:
117: /**
118: * Get the qualified name of this binding component.
119: *
120: * @return The qualified name of this binding component.
121: */
122: public QName getQName() {
123: return new QName(this .mContainer.getTargetNamespace(),
124: getName());
125: }
126:
127: /**
128: * Get interface being bound by this binding definition, if any.
129: *
130: * @return Interface being bound by this binding definition, if any
131: */
132: public com.sun.jbi.wsdl2.Interface getInterface() {
133: return this .mContainer.findInterface(getBean().getInterface());
134: }
135:
136: /**
137: * Set interface being bound by this binding definition, if any.
138: *
139: * @param theInterface Interface being bound by this binding definition, if any
140: */
141: public void setInterface(com.sun.jbi.wsdl2.Interface theInterface) {
142: if (theInterface != null) {
143: getBean().setInterface(theInterface.getQName());
144: } else {
145: getBean().setInterface(null);
146: }
147: }
148:
149: /**
150: * Get the number of BindingFault items in faults.
151: *
152: * @return The number of BindingFault items in faults
153: */
154: public int getFaultsLength() {
155: return getBean().sizeOfFaultArray();
156: }
157:
158: /**
159: * Get faults bound by this binding definition by indexed position.
160: *
161: * @param index Indexed position value 0..length-1
162: * @return Faults bound by this binding definition at given
163: * <code>index</code> position.
164: */
165: public com.sun.jbi.wsdl2.BindingFault getFault(int index) {
166: return BindingFaultImpl.Factory.getInstance(getBean()
167: .getFaultArray(index), this .mContainer);
168: }
169:
170: /**
171: * Set faults bound by this binding definition by indexed position.
172: *
173: * @param index Indexed position value (0..length-1) of the item to set
174: * @param theFault Item to add at position <code>index</code>.
175: */
176: public void setFault(int index,
177: com.sun.jbi.wsdl2.BindingFault theFault) {
178: getBean().setFaultArray(
179: index,
180: theFault != null ? ((BindingFaultImpl) theFault)
181: .getBean() : null);
182: }
183:
184: /**
185: * Append an item to faults bound by this binding definition.
186: *
187: * @param theFault Item to append to faults
188: */
189: public void appendFault(com.sun.jbi.wsdl2.BindingFault theFault) {
190: synchronized (getBean().monitor()) {
191: setFault(getFaultsLength(), theFault);
192: }
193: }
194:
195: /**
196: * Remove faults bound by this binding definition by index position.
197: *
198: * @param index The index position of the fault to remove
199: * @return The BindingFault removed, if any.
200: */
201: public com.sun.jbi.wsdl2.BindingFault removeFault(int index) {
202: com.sun.jbi.wsdl2.BindingFault result;
203:
204: synchronized (getBean().monitor()) {
205: result = getFault(index);
206: getBean().removeFault(index);
207: }
208:
209: return result;
210: }
211:
212: /**
213: * Get the number of BindingOperation items in operations.
214: *
215: * @return The number of BindingOperation items in operations
216: */
217: public int getOperationsLength() {
218: return getBean().sizeOfOperationArray();
219: }
220:
221: /**
222: * Get named binding operations, if any, by indexed position.
223: *
224: * @param index Indexed position value 0..length-1
225: * @return Named binding operations, if any at given <code>index</code> position.
226: */
227: public com.sun.jbi.wsdl2.BindingOperation getOperation(int index) {
228: return BindingOperationImpl.Factory.getInstance(getBean()
229: .getOperationArray(index), this .mContainer);
230: }
231:
232: /**
233: * Set named binding operations, if any by indexed position.
234: *
235: * @param index Indexed position value (0..length-1) of the item to set
236: * @param theOperation Item to add at position <code>index</code>.
237: */
238: public void setOperation(int index,
239: com.sun.jbi.wsdl2.BindingOperation theOperation) {
240: getBean()
241: .setOperationArray(
242: index,
243: theOperation != null ? ((BindingOperationImpl) theOperation)
244: .getBean()
245: : null);
246: }
247:
248: /**
249: * Append an item to named binding operations, if any.
250: *
251: * @param theOperation Item to append to operations
252: */
253: public void appendOperation(
254: com.sun.jbi.wsdl2.BindingOperation theOperation) {
255: synchronized (getBean().monitor()) {
256: final int length = getOperationsLength();
257:
258: setOperation(length, theOperation);
259: }
260: }
261:
262: /**
263: * Remove named binding operations, if any, by index position.
264: *
265: * @param index The index position of the operation to remove
266: * @return The BindingOperation removed, if any.
267: */
268: public com.sun.jbi.wsdl2.BindingOperation removeOperation(int index) {
269: com.sun.jbi.wsdl2.BindingOperation result = getOperation(index);
270:
271: getBean().removeOperation(index);
272:
273: return result;
274: }
275:
276: /**
277: * Create a new operation, appending it to this binding's operations list.
278: *
279: * @return Newly created operation, appended to the operations list.
280: */
281: public com.sun.jbi.wsdl2.BindingOperation addNewOperation() {
282: return BindingOperationImpl.Factory.getInstance(getBean()
283: .addNewOperation(), mContainer);
284: }
285:
286: /**
287: * Create a new binding fault, appending it to this binding's faults
288: * list.
289: *
290: * @param ref Interface fault to which the new binding fault adds binding
291: * information
292: * @return Newly created binding fault, appended to the faults list.
293: */
294: public com.sun.jbi.wsdl2.BindingFault addNewBindingFault(
295: com.sun.jbi.wsdl2.InterfaceFault ref) {
296: BindingFaultType bindingFaultBean = getBean().addNewFault();
297:
298: if (ref != null) {
299: bindingFaultBean.setRef(ref.getQualifiedName());
300: }
301:
302: return BindingFaultImpl.Factory.getInstance(bindingFaultBean,
303: this .mContainer);
304: }
305:
306: /**
307: * Return this WSDL binding as an XML string.
308: *
309: * @return This binding, serialized as an XML string.
310: */
311: public String toXmlString() {
312: String result;
313: StringWriter sw = new StringWriter();
314: XmlOptions options = new XmlOptions();
315:
316: options.setSavePrettyPrint();
317: options
318: .setSavePrettyPrintIndent(Constants.XML_PRETTY_PRINT_INDENT);
319: options.setSaveOuter();
320:
321: try {
322: getBean().save(sw, options);
323: sw.close();
324: } catch (java.io.IOException ex) {
325: sw.write("\n<!-- IO error: ");
326: sw.write(ex.getMessage());
327: sw.write("\n Document fragment truncated. -->\n");
328: // $TODO: log error
329: }
330:
331: return sw.getBuffer().toString();
332: }
333:
334: /**
335: * Return this binding as a DOM document fragment.
336: *
337: * @return This binding, as a DOM document fragment.
338: */
339: public DocumentFragment toXmlDocumentFragment() {
340: XmlOptions options = new XmlOptions();
341:
342: options.setSaveOuter();
343: return (DocumentFragment) getBean().newDomNode(options);
344: }
345:
346: /**
347: * A factory class for creating / finding components for given XML beans.
348: * <p>
349: * This factory guarantees that there will only be one component for each
350: * XML bean instance.
351: */
352: static class Factory {
353: /**
354: * Find the WSDL binding component associated with the given XML
355: * bean, creating a new component if necessary.
356: * <p>
357: * This is thread-safe.<p>
358: * Note: don't feel tempted to use the old double-checked lock trick to
359: * avoid the cost of always locking the read from the map. It doesn't work
360: * in Java 1.4 and earlier. See <a href =
361: * "http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html">
362: * The "Double-Checked Locking is Broken" Declaration</a>
363: * for the details.
364: *
365: * @param bean The XML bean to find the component for.
366: * @param defs The container for the component.
367: * @return The WSDL binding component for the given <code>bean</code>
368: * (null if the <code>bean</code> is null).
369: */
370: static BindingImpl getInstance(BindingType bean,
371: DescriptionImpl defs) {
372: BindingImpl result = null;
373:
374: if (bean != null) {
375: Map map = defs.getBindingMap();
376:
377: synchronized (map) {
378: result = (BindingImpl) map.get(bean);
379:
380: if (result == null) {
381: result = new BindingImpl(bean, defs);
382: map.put(bean, result);
383: }
384: }
385: }
386:
387: return result;
388: }
389: }
390: }
391:
392: // End-of-file: BindingImpl.java
|