001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.websvc.saas.model;
041:
042: import java.beans.PropertyChangeEvent;
043: import java.beans.PropertyChangeListener;
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.List;
048: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlPort;
049: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService;
050: import org.netbeans.modules.websvc.saas.model.jaxb.Method;
051: import org.netbeans.modules.websvc.saas.model.jaxb.SaasServices;
052: import org.netbeans.modules.websvc.saas.spi.websvcmgr.WsdlData;
053: import org.netbeans.modules.websvc.saas.util.WsdlUtil;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.util.Exceptions;
057: import org.openide.util.WeakListeners;
058:
059: /**
060: *
061: * @author nam
062: */
063: public class WsdlSaas extends Saas implements PropertyChangeListener {
064: private WsdlData wsData;
065:
066: private List<WsdlSaasPort> ports;
067:
068: public WsdlSaas(SaasGroup parentGroup, SaasServices services) {
069: super (parentGroup, services);
070: }
071:
072: public WsdlSaas(SaasGroup parentGroup, String displayName,
073: String url, String packageName) {
074: super (parentGroup, url, displayName, packageName);
075: getDelegate().setType(NS_WSDL);
076: }
077:
078: protected void setWsdlData(WsdlData data) {
079: wsData = data;
080: if (wsData.isReady()) {
081: setState(State.READY);
082: } else {
083: setState(State.UNINITIALIZED);
084: }
085: }
086:
087: public WsdlData getWsdlData() {
088: if (getState() == State.RETRIEVED || getState() == State.READY) {
089: return wsData;
090: }
091: throw new IllegalStateException("Current state: " + getState());
092: }
093:
094: @Override
095: protected void refresh() {
096: if (wsData == null || getState() == State.INITIALIZING) {
097: throw new IllegalStateException(
098: "Could not refresh null WSDL data or while it is initializing");
099: }
100: super .refresh();
101: ports = null;
102: WsdlUtil.refreshWsdlData(wsData);
103: }
104:
105: public String getDefaultServiceName() {
106: if (getMethods().size() > 0) {
107: return getMethods().get(0).getMethod().getServiceName();
108: }
109: return ""; //NOI18N
110: }
111:
112: public String getPackageName() {
113: String pname = getDelegate().getSaasMetadata().getCodeGen()
114: .getPackageName();
115: if (pname == null) {
116: pname = "";
117: }
118: return pname;
119: }
120:
121: @Override
122: public void toStateReady(boolean synchronous) {
123: if (wsData != null && wsData.isReady()) {
124: return;
125: }
126: String serviceName = getDefaultServiceName();
127: wsData = WsdlUtil.getWsdlData(getUrl(), serviceName,
128: synchronous); //NOI18N
129: // first-time the call will return null
130: if (wsData == null) {
131: wsData = WsdlUtil.addWsdlData(getUrl(), getPackageName());
132: if (wsData != null && synchronous) {
133: int count = 0;
134: while (!wsData.isReady() && count < 100) {
135: try {
136: Thread.sleep(100);
137: count++;
138: } catch (InterruptedException ex) {
139: }
140: }
141: }
142: }
143: if (wsData != null) {
144: wsData.addPropertyChangeListener(WeakListeners
145: .propertyChange(this , wsData));
146: if (wsData.isReady()) {
147: setState(State.READY);
148: } else {
149: setState(State.INITIALIZING);
150: }
151: } else {
152: setState(State.UNINITIALIZED);
153: }
154: }
155:
156: private List<WsdlPort> filterNonSoapPorts(List<WsdlPort> ports) {
157: List<WsdlPort> filterPorts = new java.util.ArrayList<WsdlPort>(
158: ports.size());
159:
160: for (WsdlPort port : ports) {
161: if (port.getAddress() != null) {
162: filterPorts.add(port);
163: }
164: }
165:
166: return filterPorts;
167: }
168:
169: public void propertyChange(PropertyChangeEvent evt) {
170: String property = evt.getPropertyName();
171: Object newValue = evt.getNewValue();
172:
173: // these are transitions out of the temporary state INITIALIZING
174: // we are only interested in transition to ready and retrieved states.
175: // when compile fail we fallback to retrieved to allow user examine the wsdl
176:
177: if (property.equals("resolved")
178: && getState() == State.INITIALIZING) { //NOI18N
179: if (Boolean.FALSE.equals(newValue)) {
180: setState(State.RETRIEVED);
181: } else if (wsData.isReady()) {
182: setState(State.READY); // compiled in previous IDE run
183: }
184: } else if (WsdlData.Status.WSDL_SERVICE_COMPILED
185: .equals(newValue)) {
186: setState(State.READY);
187: WsdlUtil.saveWsdlData(getWsdlData());
188: } else if (WsdlData.Status.WSDL_SERVICE_COMPILE_FAILED
189: .equals(newValue)) {
190: setState(State.RETRIEVED);
191: } else if (WsdlData.Status.WSDL_UNRETRIEVED.equals(newValue)) {
192: setState(State.UNINITIALIZED);
193: }
194: }
195:
196: public WsdlService getWsdlModel() {
197: return getWsdlData().getWsdlService();
198: }
199:
200: public FileObject getLocalWsdlFile() {
201: return FileUtil.toFileObject(new File(getWsdlData()
202: .getWsdlFile()));
203: }
204:
205: /**
206: * Either return methods, if filtering methods exists, or return ports.
207: *
208: * @return list of either all filtered methods or all ports.
209: */
210: public List<Object> getPortsOrMethods() {
211: List<Object> result = new ArrayList<Object>();
212: List<SaasMethod> methods = getMethods();
213: if (methods != null && methods.size() > 0) {
214: result.addAll(methods);
215: } else {
216: result.addAll(getPorts());
217: }
218: return result;
219: }
220:
221: public List<WsdlSaasPort> getPorts() {
222: if (ports == null) {
223: ports = new ArrayList<WsdlSaasPort>();
224: for (WsdlPort p : filterNonSoapPorts(getWsdlModel()
225: .getPorts())) {
226: ports.add(new WsdlSaasPort(this , p));
227: }
228: }
229: return ports;
230: }
231:
232: @Override
233: protected WsdlSaasMethod createSaasMethod(Method method) {
234: return new WsdlSaasMethod(this , method);
235: }
236:
237: @Override
238: public FileObject getSaasFolder() {
239: if (saasFolder == null) {
240: String folderName = WsdlUtil.getServiceDirName(getUrl());
241: FileObject websvcHome = SaasServicesModel
242: .getWebServiceHome();
243: saasFolder = websvcHome.getFileObject(folderName);
244: if (saasFolder == null) {
245: try {
246: saasFolder = websvcHome.createFolder(folderName);
247: } catch (IOException ex) {
248: Exceptions.printStackTrace(ex);
249: }
250: }
251: }
252: return saasFolder;
253: }
254: }
|