KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > config > ServiceInformationImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * ServiceInformationImpl.java
21  *
22  * Created on April 26, 2006, 10:11 AM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.websvc.wsdl.config;
29
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.lang.ref.WeakReference JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.List JavaDoc;
35 import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
36 import org.netbeans.modules.websvc.jaxrpc.PortInformation;
37 import org.netbeans.modules.websvc.jaxrpc.ServiceInformation;
38 import org.netbeans.modules.websvc.jaxrpc.nodes.WsCompileConfigCookie;
39 import org.openide.filesystems.FileObject;
40 import org.openide.loaders.DataObject;
41 import org.openide.loaders.DataObjectNotFoundException;
42
43 /**
44  *
45  * @author rico
46  * Refactored methods from the WsdlDataObject
47  */

48 public class ServiceInformationImpl implements ServiceInformation {
49     
50     private WeakReference JavaDoc portInformationHandlerRef = null;
51     private DataObject wsdlDataObj;
52     
53     // If isClientWsdl is true, the WSDL file is in the WSDL folder of a web service
54
// client enabled module and thus will have operations and UI exposed that affect
55
// the service as it exists within the project. E.g. deleting such a file will
56
// actually remove the service from the project, not just delete the file on disk.
57
private boolean clientResolved, isClientWsdl;
58     
59     /** Typical data object constructor.
60      */

61     public ServiceInformationImpl(DataObject dobj) {
62         wsdlDataObj = dobj;
63     }
64     
65     public boolean isClientWsdl() {
66         if (!clientResolved) {
67             initClientWsdl();
68             clientResolved=true;
69         }
70         return isClientWsdl;
71     }
72     
73     private void initClientWsdl() {
74         isClientWsdl = false;
75         FileObject wsdlFO = wsdlDataObj.getPrimaryFile();
76         
77         // Check to make sure it has a non-null parent (can't be in WSDL folder if it does).
78
FileObject parentFO = wsdlFO.getParent();
79         if(parentFO != null) {
80             // Does this module support web service clients?
81
WebServicesClientSupport clientSupport = WebServicesClientSupport.getWebServicesClientSupport(wsdlFO);
82             if(clientSupport != null) {
83                 // Is this file object in the WSDL folder of the client?
84
FileObject wsdlFolder = clientSupport.getWsdlFolder();
85                 if(wsdlFolder != null && wsdlFolder.equals(parentFO)) {
86                     // If we get here, the following conditions should be true:
87
// The WSDL file is in a code module that supports webservice clients.
88
// The WSDL file is in the proper WSDL folder of that module.
89
isClientWsdl = true;
90                     
91                     // With the addition of "Create service from wsdl" feature,
92
// wsdl files in this folder could also be services (if the project
93
// in question supports service at least) so we need additional
94
// heuristics for this case.
95
// WebServicesSupport serviceSupport = WebServicesSupport.getWebServicesSupport(wsdlFO);
96
// if(serviceSupport != null) {
97
// List serviceList = serviceSupport.getServices();
98
// }
99

100                     // for now, just check and see if there is a mapping file in the web-inf/meta-inf folder (parent)
101
FileObject ddFolder = wsdlFolder.getParent();
102                     if(ddFolder != null) {
103                         FileObject mappingFile = ddFolder.getFileObject(wsdlDataObj.getName() + "-mapping", "xml"); // NOI18N
104
if(mappingFile != null) {
105                             isClientWsdl = false;
106                         }
107                     }
108                 }
109             }
110         }
111     }
112     
113     public String JavaDoc getServicePackageName() {
114         // locate config object and use cookie on that to get package name
115
String JavaDoc packageName = "unknown"; // NOI18N default to unknown package name
116

117         FileObject configFO = null;
118         FileObject wsdlFO = wsdlDataObj.getPrimaryFile();
119         FileObject parentFO = wsdlFO.getParent();
120         if(parentFO != null && parentFO.isFolder()) {
121             configFO = parentFO.getFileObject(wsdlFO.getName() + WsCompileConfigDataObject.WSCOMPILE_CONFIG_FILENAME_SUFFIX, "xml");
122         }
123         
124         if(configFO != null) {
125             WsCompileConfigCookie configCookie = null;
126             try {
127                 DataObject dobj = DataObject.find(configFO);
128                 if (dobj instanceof WsCompileConfigCookie)
129                     configCookie = (WsCompileConfigCookie) dobj;
130             } catch(DataObjectNotFoundException ex) {
131                 // Shouldn't happen, but it it does, we're screwed.
132
// !PW FIXME log this.
133
}
134             
135             if(configCookie != null) {
136                 packageName = configCookie.getServicePackageName();
137             }
138         }
139         
140         return packageName;
141         
142         // !PW FIXME rewrite this to use the DD provider API implemented for config files.
143
// // If it's cached, use that.
144
// synchronized (this) {
145
// if(packageHandlerRef != null) {
146
// PackageHandler handler = (PackageHandler) packageHandlerRef.get();
147
// if(handler != null) {
148
// return handler.getPackageName();
149
// }
150
// }
151
// }
152
//
153
// String result = null;
154
// FileObject configFO = null;
155
//
156
// // Locate the -config file for this service client.
157
// Set extraFiles = secondaryEntries();
158
// if(extraFiles.size() >= 1) {
159
// // !PW This section is a holdover from EA1 where config files are secondary entries in the loader.
160
// // It is probably obsolete. Remove just before or after EA2.
161
// FileEntry fe = (FileEntry) extraFiles.iterator().next();
162
// configFO = fe.getFile();
163
// } else {
164
// FileObject wsdlFO = getPrimaryFile();
165
// FileObject parentFO = wsdlFO.getParent();
166
// if(parentFO != null && parentFO.isFolder()) {
167
// configFO = parentFO.getFileObject(wsdlFO.getName() + WsCompileConfigDataObject.WSCOMPILE_CONFIG_FILENAME_SUFFIX, WSCOMPILE_CONFIG_EXTENSION);
168
// }
169
// }
170
//
171
// if(configFO != null) {
172
// // Invoke SAX parser on the WSDL config file to extract the package name
173
// PackageHandler handler = new PackageHandler();
174
//
175
// try {
176
// javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
177
// javax.xml.parsers.SAXParser saxParser = factory.newSAXParser();
178
// saxParser.parse(configFO.getInputStream(), handler);
179
//
180
// synchronized (this) {
181
// packageHandlerRef = new WeakReference(handler);
182
// }
183
//
184
// result = handler.getPackageName();
185
// } catch(javax.xml.parsers.ParserConfigurationException ex) {
186
// // !PW FIXME
187
// } catch(org.xml.sax.SAXException ex) {
188
// // !PW FIXME
189
// } catch(FileNotFoundException ex) {
190
// // !PW Should never happen.
191
// } catch(IOException ex) {
192
// // !PW FIXME
193
// }
194
// }
195
//
196
// return result;
197
}
198     
199     public PortInformation getPortInformation() {
200         return parseWsdl();
201     }
202     
203     public List JavaDoc getServicePorts(String JavaDoc serviceName) {
204         List JavaDoc portList;
205         PortInformationHandler handler = parseWsdl();
206         
207         if(handler != null) {
208             PortInformation.ServiceInfo serviceInfo = handler.getServiceInfo(serviceName);
209             portList = serviceInfo.getPorts();
210         } else {
211             portList = Collections.EMPTY_LIST;
212         }
213         
214         return portList;
215     }
216     
217     public String JavaDoc [] getServiceNames() {
218         String JavaDoc [] result;
219         PortInformationHandler handler = parseWsdl();
220         
221         if(handler != null) {
222             result = handler.getServiceNames();
223         } else {
224             result = new String JavaDoc [0];
225         }
226         
227         return result;
228     }
229     
230     public String JavaDoc getTargetNamespace() {
231         String JavaDoc result;
232         PortInformationHandler handler = parseWsdl();
233         
234         if(handler != null) {
235             result = handler.getTargetNamespace();
236         } else {
237             result = null; // !PW Should this be the default?
238
}
239         
240         return result;
241     }
242     
243     private PortInformationHandler parseWsdl() {
244         PortInformationHandler handler = null;
245         
246         // If it's cached, use that.
247
synchronized (this) {
248             if(portInformationHandlerRef != null) {
249                 handler = (PortInformationHandler) portInformationHandlerRef.get();
250                 if(handler != null) {
251                     return handler;
252                 }
253             }
254         }
255         
256         List JavaDoc result = Collections.EMPTY_LIST;
257         FileObject primaryFile = wsdlDataObj.getPrimaryFile();
258         // Invoke SAX parser on the WSDL to extract list of port bindings
259
handler = new PortInformationHandler();
260         
261         try {
262             org.xml.sax.XMLReader JavaDoc xmlReader = org.openide.xml.XMLUtil.createXMLReader(false,true);
263             xmlReader.setContentHandler(handler);
264             xmlReader.parse(new org.xml.sax.InputSource JavaDoc(primaryFile.getInputStream()));
265             // Extract service names and porttypes
266
synchronized (this) {
267                 portInformationHandlerRef = new WeakReference JavaDoc(handler);
268             }
269         } catch(org.xml.sax.SAXException JavaDoc ex) {
270             // !PW FIXME
271
handler = null;
272         } catch(FileNotFoundException JavaDoc ex) {
273             // !PW Should never happen.
274
handler = null;
275         } catch(IOException JavaDoc ex) {
276             // !PW FIXME
277
handler = null;
278         }
279         
280         return handler;
281     }
282 }
283
Popular Tags