001: /*
002: Copyright (c) 2007, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.ws.wsdl;
030:
031: import java.util.ArrayList;
032: import java.util.HashMap;
033: import java.util.List;
034: import java.util.Map;
035:
036: import org.jibx.binding.generator.IApply;
037: import org.jibx.binding.generator.SharedNestingBase;
038: import org.jibx.binding.model.IClassLocator;
039: import org.jibx.runtime.IUnmarshallingContext;
040:
041: /**
042: * Global customization information for WSDL generation. This extends the
043: * binding customization model to include the information used for service
044: * definitions.
045: */
046: public class WsdlCustom extends NestingBase implements IApply {
047: /** Customization value from unmarshalling. */
048: private String m_wsdlNamespace;
049:
050: /** List of Fault definitions. */
051: private final ArrayList m_faultList;
052:
053: /** Map from fully-qualified class name to Fault information. */
054: private final Map m_faultMap;
055:
056: /** List of services, in order added. */
057: private final ArrayList m_serviceList;
058:
059: /** Map from fully-qualified class name to service information. */
060: private final Map m_serviceMap;
061:
062: /** Class locator. */
063: private IClassLocator m_locator;
064:
065: /**
066: * Constructor.
067: *
068: * @param parent
069: */
070: public WsdlCustom(SharedNestingBase parent) {
071: super (parent);
072: m_faultList = new ArrayList();
073: m_faultMap = new HashMap();
074: m_serviceList = new ArrayList();
075: m_serviceMap = new HashMap();
076: }
077:
078: /**
079: * Get the namespace for WSDL definitions of services.
080: *
081: * @return WSDL namespace (<code>null</code> if unspecified)
082: */
083: public String getWsdlNamespace() {
084: return m_wsdlNamespace;
085: }
086:
087: /**
088: * Get list of Faults.
089: *
090: * @return fault list
091: */
092: public List getFaults() {
093: return m_faultList;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.jibx.binding.generator.SharedNestingBase#getNameStyle()
098: */
099: public int getNameStyle() {
100: return CAMEL_CASE_NAMES;
101: }
102:
103: /**
104: * Get fault customization information. This method should only be used
105: * after the {@link #apply(IClassLocator)} method is called.
106: *
107: * @param type fully qualified class name
108: * @return fault customization (<code>null</code> if none)
109: */
110: public FaultCustom getFaultCustomization(String type) {
111: return (FaultCustom) m_faultMap.get(type);
112: }
113:
114: /**
115: * Force fault customization information. This method should only be used
116: * after the {@link #apply(IClassLocator)} method is called. If the fault
117: * customization information has not previously been created, it will be
118: * created by this call.
119: *
120: * @param type fully qualified exception class name
121: * @return fault customization (<code>null</code> if none)
122: */
123: public FaultCustom forceFaultCustomization(String type) {
124: FaultCustom fault = (FaultCustom) m_faultMap.get(type);
125: if (fault == null) {
126: fault = new FaultCustom(this , type);
127: fault.apply(m_locator);
128: m_faultMap.put(type, fault);
129: }
130: return fault;
131: }
132:
133: /**
134: * Get list of services.
135: *
136: * @return service list
137: */
138: public List getServices() {
139: return m_serviceList;
140: }
141:
142: /**
143: * Get service customization information. This method should only be used
144: * after the {@link #apply(IClassLocator)} method is called.
145: *
146: * @param type fully qualified class name
147: * @return service customization (<code>null</code> if none)
148: */
149: public ServiceCustom getServiceCustomization(String type) {
150: return (ServiceCustom) m_serviceMap.get(type);
151: }
152:
153: /**
154: * Add new service customization. This creates the service customization,
155: * using defaults, and adds it to the internal structures. This method
156: * should only be used after first calling {@link
157: * #getServiceCustomization(String)} and obtaining a <code>null</code>
158: * result.
159: *
160: * @param type fully qualified class name
161: * @return service customization
162: */
163: public ServiceCustom addServiceCustomization(String type) {
164: ServiceCustom service = new ServiceCustom(this , type);
165: service.apply(m_locator);
166: m_serviceList.add(service);
167: m_serviceMap.put(type, service);
168: return service;
169: }
170:
171: /**
172: * Unmarshalling factory. This gets the containing element and the name so
173: * that the standard constructor can be used.
174: *
175: * @param ictx
176: * @return created instance
177: */
178: private static WsdlCustom factory(IUnmarshallingContext ictx) {
179: return new WsdlCustom(getContainingClass(ictx));
180: }
181:
182: /**
183: * Apply customizations to services to fill out members.
184: *
185: * @param icl class locator
186: */
187: public void apply(IClassLocator icl) {
188:
189: // save locator for later use (when services are added)
190: m_locator = icl;
191:
192: // fix Faults and create map
193: for (int i = 0; i < m_faultList.size(); i++) {
194: FaultCustom fault = (FaultCustom) m_faultList.get(i);
195: fault.apply(icl);
196: m_faultMap.put(fault.getExceptionType(), fault);
197: }
198:
199: // register services with names supplied (priority over generated names)
200: for (int i = 0; i < m_serviceList.size(); i++) {
201: ServiceCustom service = (ServiceCustom) m_serviceList
202: .get(i);
203: String name = service.getServiceName();
204: if (name != null) {
205: if (registerName(name, service) != name) {
206: throw new IllegalStateException(
207: "Duplicate service name " + name);
208: }
209: }
210: }
211:
212: // fix services and create map
213: for (int i = 0; i < m_serviceList.size(); i++) {
214: ServiceCustom service = (ServiceCustom) m_serviceList
215: .get(i);
216: service.apply(icl);
217: m_serviceMap.put(service.getClassName(), service);
218: }
219: }
220: }
|