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: * @(#)ServiceImpl.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.EndpointType;
040: import org.w3.ns.wsdl.ServiceType;
041:
042: import org.w3c.dom.DocumentFragment;
043:
044: /**
045: * Implementation of WSDL 2.0 Service component.
046: *
047: * @author Sun Microsystems, Inc.
048: */
049: final class ServiceImpl extends Service {
050: /** The container for this component */
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 Service component implementation from the given XML bean.
064: * @param bean The Service XML bean to use to construct this component.
065: * @param defs The container for this component.
066: */
067: private ServiceImpl(ServiceType bean, DescriptionImpl defs) {
068: super (bean);
069: this .mContainer = defs;
070: }
071:
072: /** Map of WSDL-defined attribute QNames. Keyed by QName.toString value */
073: private static java.util.Map sWsdlAttributeQNames = null;
074:
075: /**
076: * Worker class method for {@link #getWsdlAttributeNameMap()}.
077: *
078: * @return Map of WSDL-defined attribute QNames for this component,
079: * indexed by QName.toString()
080: */
081: static synchronized java.util.Map getAttributeNameMap() {
082: if (sWsdlAttributeQNames == null) {
083: sWsdlAttributeQNames = XmlBeansUtil
084: .getAttributesMap(ServiceType.type);
085: }
086:
087: return sWsdlAttributeQNames;
088: }
089:
090: /**
091: * Get map of WSDL-defined attribute QNames for this component, indexed by
092: * canonical QName string (see {@link javax.xml.namespace.QName#toString()}.
093: *
094: * @return Map of WSDL-defined attribute QNames for this component,
095: * indexed by QName.toString()
096: */
097: public java.util.Map getWsdlAttributeNameMap() {
098: return getAttributeNameMap();
099: }
100:
101: /**
102: * Get the qualified name of this service.
103: *
104: * @return The qualified name of this service.
105: */
106: public QName getQName() {
107: return new QName(this .mContainer.getTargetNamespace(),
108: getName());
109: }
110:
111: /**
112: * Get local name of this service component.
113: *
114: * @return Local name of this service component
115: */
116: public String getName() {
117: return getBean().getName();
118: }
119:
120: /**
121: * Set local name of this service component.
122: *
123: * @param theName Local name of this service component
124: */
125: public void setName(String theName) {
126: getBean().setName(theName);
127: }
128:
129: /**
130: * Get target namespace of this service.
131: *
132: * @return Target namespace of this service
133: */
134: public String getTargetNamespace() {
135: return this .mContainer.getTargetNamespace();
136: }
137:
138: /**
139: * Get interface provided by this service.
140: *
141: * @return Interface provided by this service
142: */
143: public com.sun.jbi.wsdl2.Interface getInterface() {
144: QName name = getBean().getInterface();
145:
146: return this .mContainer.findInterface(name);
147: }
148:
149: /**
150: * Set interface provided by this service.
151: *
152: * @param theInterface Interface provided by this service
153: */
154: public void setInterface(com.sun.jbi.wsdl2.Interface theInterface) {
155: QName name = null;
156:
157: if (theInterface != null) {
158: name = new QName(theInterface.getTargetNamespace(),
159: theInterface.getName());
160: }
161:
162: // assert (this.mContainer.getTargetNamespace() == name.getNamespaceUri());
163:
164: getBean().setInterface(name);
165: }
166:
167: /**
168: * Get the number of Endpoint items in endpoints.
169: *
170: * @return The number of Endpoint items in endpoints
171: */
172: public int getEndpointsLength() {
173: return getBean().sizeOfEndpointArray();
174: }
175:
176: /**
177: * Get endpoints for this service by indexed position.
178: *
179: * @param index Indexed position value 0..length-1
180: * @return Endpoints for this service at given <code>index</code> position.
181: */
182: public com.sun.jbi.wsdl2.Endpoint getEndpoint(int index) {
183: return EndpointImpl.Factory.getInstance(getBean()
184: .getEndpointArray(index), this .mContainer);
185: }
186:
187: /**
188: * Set endpoints for this service by indexed position.
189: *
190: * @param index Indexed position value (0..length-1) of the item to set
191: * @param theEndpoint Item to add at position <code>index</code>.
192: */
193: public void setEndpoint(int index,
194: com.sun.jbi.wsdl2.Endpoint theEndpoint) {
195: getBean().setEndpointArray(
196: index,
197: theEndpoint != null ? ((EndpointImpl) theEndpoint)
198: .getBean() : null);
199: }
200:
201: /**
202: * Append an item to endpoints for this service.
203: *
204: * @param theEndpoint Item to append to endpoints
205: */
206: public void appendEndpoint(com.sun.jbi.wsdl2.Endpoint theEndpoint) {
207: synchronized (getBean().monitor()) {
208: setEndpoint(getEndpointsLength(), theEndpoint);
209: }
210: }
211:
212: /**
213: * Remove endpoints for this service by index position.
214: *
215: * @param index The index position of the endpoint to remove
216: * @return The Endpoint removed, if any.
217: */
218: public com.sun.jbi.wsdl2.Endpoint removeEndpoint(int index) {
219: com.sun.jbi.wsdl2.Endpoint result;
220:
221: synchronized (getBean().monitor()) {
222: result = getEndpoint(index);
223: getBean().removeEndpoint(index);
224: }
225:
226: return result;
227: }
228:
229: /**
230: * Create a new end point component, appending it to this service's endpoint list.
231: *
232: * @param name NC name for the new endpoint.
233: * @param binding Binding to which the endpoint refers.
234: * @return The newly created endpoint, appended to this service's endpoint list.
235: */
236: public com.sun.jbi.wsdl2.Endpoint addNewEndpoint(String name,
237: com.sun.jbi.wsdl2.Binding binding) {
238: EndpointType epBean = getBean().addNewEndpoint();
239:
240: synchronized (epBean.monitor()) {
241: epBean.setName(name);
242:
243: if (binding != null) {
244: epBean.setBinding(((BindingImpl) binding).getQName());
245: }
246: }
247:
248: return EndpointImpl.Factory
249: .getInstance(epBean, this .mContainer);
250: }
251:
252: /**
253: * Return this WSDL service as an XML string.
254: *
255: * @return This service, serialized as an XML string.
256: */
257: public String toXmlString() {
258: String result;
259: StringWriter sw = new StringWriter();
260: XmlOptions options = new XmlOptions();
261:
262: options.setSavePrettyPrint();
263: options
264: .setSavePrettyPrintIndent(Constants.XML_PRETTY_PRINT_INDENT);
265: options.setSaveOuter();
266:
267: try {
268: getBean().save(sw, options);
269: sw.close();
270: } catch (java.io.IOException ex) {
271: sw.write("\n<!-- IO error: ");
272: sw.write(ex.getMessage());
273: sw.write("\n Document fragment truncated. -->\n");
274: // $TODO: log error
275: }
276:
277: return sw.getBuffer().toString();
278: }
279:
280: /**
281: * Return this service as a DOM document fragment.
282: *
283: * @return This service, as a DOM document fragment.
284: */
285: public DocumentFragment toXmlDocumentFragment() {
286: XmlOptions options = new XmlOptions();
287:
288: options.setSaveOuter();
289: return (DocumentFragment) getBean().newDomNode(options);
290: }
291:
292: /**
293: * A factory class for creating / finding components for given XML beans.
294: * <p>
295: * This factory guarantees that there will only be one component for each
296: * XML bean instance.
297: */
298: static class Factory {
299: /**
300: * Find the WSDL service component associated with the given XML
301: * bean, creating a new component if necessary.
302: * <p>
303: * This is thread-safe.<p>
304: *
305: * @param bean The XML bean to find the component for.
306: * @param defs The container for the component.
307: * @return The WSDL service component for the given <code>bean</code>
308: * (null if the <code>bean</code> is null).
309: */
310: static ServiceImpl getInstance(ServiceType bean,
311: DescriptionImpl defs) {
312: ServiceImpl result;
313:
314: if (bean != null) {
315: Map map = defs.getServiceMap();
316:
317: synchronized (map) {
318: result = (ServiceImpl) map.get(bean);
319:
320: if (result == null) {
321: result = new ServiceImpl(bean, defs);
322: map.put(bean, result);
323: }
324: }
325: } else {
326: result = null;
327: }
328:
329: return result;
330: }
331: }
332: }
333:
334: // End-of-file: ServiceImpl.java
|