KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > api > jaxws > project > config > JaxWsModel


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 package org.netbeans.modules.websvc.api.jaxws.project.config;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.api.project.FileOwnerQuery;
29 import org.netbeans.api.project.Project;
30 //retouche:
31
//import org.netbeans.modules.j2ee.metadata.JAXWSMetadataProvider;
32
//import org.netbeans.modules.j2ee.metadata.MetadataUnit;
33
import org.netbeans.modules.schema2beans.BaseBean;
34 import org.netbeans.modules.websvc.jaxwsmodel.project.metadata.JAXWSNNMergerProvider;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileLock;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileSystem.AtomicAction;
39
40 /** Provides information about web services and clients in a project
41  * Provides information used for build-impl generation
42  * Working over nbproject/jax-ws.xml file
43  */

44 public final class JaxWsModel {
45     private org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.JaxWs jaxws;
46     private FileObject fo;
47     private boolean nninitialized = false;
48     private Object JavaDoc initLock = new Object JavaDoc();
49     private List JavaDoc<ServiceListener> serviceListeners;
50     
51     JaxWsModel(org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.JaxWs jaxws) {
52         this(jaxws,null);
53     }
54     
55     JaxWsModel(org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.JaxWs jaxws, FileObject fo) {
56         this.jaxws=jaxws;
57         this.fo=fo;
58         serviceListeners = new ArrayList JavaDoc<ServiceListener>();
59     }
60     
61     public Service[] getServices() {
62         //init annotations provider
63
initNNProvider();
64         
65         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Services services = jaxws.getServices();
66         if (services==null) return new Service[]{};
67         else {
68             org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service[] org = services.getService();
69             if (org==null) return new Service[]{};
70             Service[] newServices = new Service[org.length];
71             for (int i=0;i<org.length;i++) newServices[i] = new Service(org[i]);
72             return newServices;
73         }
74     }
75     
76     public void setJsr109(Boolean JavaDoc jsr109){
77         jaxws.setJsr109(jsr109);
78     }
79     
80     public Boolean JavaDoc getJsr109(){
81         return jaxws.getJsr109();
82     }
83     
84     private void initNNProvider() {
85         synchronized (initLock) {
86             if(!nninitialized) {
87                 if(fo != null) {
88                     //retouche:
89
// try {
90
// Project project = FileOwnerQuery.getOwner(fo);
91
// MetadataUnit mu = findJAXWSMetadata(project);
92
// if(mu != null) {
93
// //init the NN listener if there is JAXWSMetadataProvider instance in the project lookup
94
// JAXWSNNMergerProvider.getDefault().initNNMerger(this, mu);
95
// }
96
//set nninitialized to true even there is no JAXWSMetadataProvider in the project lookup
97
//so next time initNNProvider is called it will not try to find it - which cannot happen
98
nninitialized = true;
99                         //retouche:
100
// }catch(IOException e) {
101
// ErrorManager.getDefault().notify(e);
102
// }
103
}
104             }
105         }
106     }
107     
108     public Service findServiceByName(String JavaDoc name) {
109         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = findService(name);
110         return (service==null?null:new Service(service));
111     }
112     
113     public Service findServiceByImplementationClass(String JavaDoc wsClassName) {
114         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = _findServiceByClass(wsClassName);
115         return (service==null?null:new Service(service));
116     }
117     
118     private org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service findService(String JavaDoc name) {
119         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Services services = jaxws.getServices();
120         if (services==null) return null;
121         else {
122             org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service[] org = services.getService();
123             if (org==null) return null;
124             for (int i=0;i<org.length;i++) {
125                 if (name.equals(org[i].getName())) return org[i];
126             }
127             return null;
128         }
129     }
130     
131     private org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service _findServiceByClass(String JavaDoc wsClassName) {
132         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Services services = jaxws.getServices();
133         if (services==null) return null;
134         else {
135             org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service[] org = services.getService();
136             if (org==null) return null;
137             for (int i=0;i<org.length;i++) {
138                 if (wsClassName.equals(org[i].getImplementationClass())) return org[i];
139             }
140             return null;
141         }
142     }
143     
144     public boolean removeService(String JavaDoc name) {
145         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = findService(name);
146         if (name==null) return false;
147         else {
148             jaxws.getServices().removeService(service);
149             fireServiceRemoved(name);
150             return true;
151         }
152     }
153     
154     public boolean removeServiceByClassName(String JavaDoc webserviceClassName) {
155         if (webserviceClassName != null) {
156             org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = _findServiceByClass(webserviceClassName);
157             if (service != null) {
158                 fireServiceRemoved(service.getName());
159                 jaxws.getServices().removeService(service);
160                 return true;
161             }
162         }
163         return false;
164     }
165     
166     public Service addService(String JavaDoc name, String JavaDoc implementationClass)
167     throws ServiceAlreadyExistsExeption {
168         
169         if (findService(name)!=null) throw new ServiceAlreadyExistsExeption(name);
170         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = jaxws.getServices().newService();
171         service.setName(name);
172         service.setImplementationClass(implementationClass);
173         jaxws.getServices().addService(service);
174         fireServiceAdded(name, implementationClass);
175         return new Service(service);
176     }
177     
178     public Service addService(String JavaDoc name, String JavaDoc implementationClass, String JavaDoc wsdlUrl, String JavaDoc serviceName, String JavaDoc portName, String JavaDoc packageName)
179     throws ServiceAlreadyExistsExeption {
180         
181         if (findService(name)!=null) throw new ServiceAlreadyExistsExeption(name);
182         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = jaxws.getServices().newService();
183         service.setName(name);
184         service.setImplementationClass(implementationClass);
185         service.setWsdlUrl(wsdlUrl);
186         service.setServiceName(serviceName);
187         service.setPortName(portName);
188         service.setPackageName(packageName);
189         jaxws.getServices().addService(service);
190         return new Service(service);
191     }
192     
193     public Client[] getClients() {
194         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Clients clients = jaxws.getClients();
195         if (clients==null) return new Client[]{};
196         else {
197             org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client[] org = clients.getClient();
198             if (org==null) return new Client[]{};
199             Client[] newClients = new Client[org.length];
200             for (int i=0;i<org.length;i++) newClients[i] = new Client(org[i]);
201             return newClients;
202         }
203     }
204     
205     public Client findClientByName(String JavaDoc name) {
206         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client client = findClient(name);
207         return (client==null?null:new Client(client));
208     }
209     
210     public Client findClientByWsdlUrl(String JavaDoc wsdlUrl) {
211         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Clients clients = jaxws.getClients();
212         if (clients==null) return null;
213         else {
214             org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client[] org = clients.getClient();
215             if (org==null) return null;
216             for (int i=0;i<org.length;i++) {
217                 if (wsdlUrl.equals(org[i].getWsdlUrl())) return new Client(org[i]);
218             }
219             return null;
220         }
221     }
222     
223     private org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client findClient(String JavaDoc name) {
224         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Clients clients = jaxws.getClients();
225         if (clients==null) return null;
226         else {
227             org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client[] org = clients.getClient();
228             if (org==null) return null;
229             for (int i=0;i<org.length;i++) {
230                 if (name.equals(org[i].getName())) return org[i];
231             }
232             return null;
233         }
234     }
235     
236     public boolean removeClient(String JavaDoc name) {
237         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client client = findClient(name);
238         if (client==null) return false;
239         else {
240             jaxws.getClients().removeClient(client);
241             return true;
242         }
243     }
244     
245     public Client addClient(String JavaDoc name, String JavaDoc wsdlUrl, String JavaDoc packageName)
246     throws ClientAlreadyExistsExeption {
247         
248         if (findClient(name)!=null) throw new ClientAlreadyExistsExeption(name);
249         org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client client = jaxws.getClients().newClient();
250         client.setName(name);
251         client.setWsdlUrl(wsdlUrl);
252         if (packageName!=null) {
253             client.setPackageName(packageName);
254             client.setPackageNameForceReplace("true");
255         }
256         jaxws.getClients().addClient(client);
257         return new Client(client);
258     }
259     
260     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
261         jaxws.addPropertyChangeListener(l);
262     }
263     
264     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
265         jaxws.removePropertyChangeListener(l);
266     }
267     
268     public void merge(JaxWsModel newJaxWs) {
269         if (newJaxWs.jaxws!=null)
270             jaxws.merge(newJaxWs.jaxws,BaseBean.MERGE_UPDATE);
271     }
272     
273     public void write(OutputStream JavaDoc os) throws IOException JavaDoc {
274         jaxws.write(os);
275     }
276     
277     public FileObject getJaxWsFile() {
278         return fo;
279     }
280     
281     public void setJaxWsFile(FileObject fo) {
282         this.fo=fo;
283     }
284     
285     public void write() throws IOException JavaDoc {
286         if (fo!=null) {
287             fo.getFileSystem().runAtomicAction(new AtomicAction() {
288             public void run() {
289                 FileLock lock=null;
290                 try {
291                     lock = fo.lock();
292                     OutputStream JavaDoc os = fo.getOutputStream(lock);
293                     write(os);
294                     os.close();
295                 } catch (IOException JavaDoc ex) {
296                     ErrorManager.getDefault().notify(ex);
297                 } finally {
298                     if (lock!=null) lock.releaseLock();
299                 }
300             }
301         });
302         } else throw new IOException JavaDoc("No FileObject for writing specified"); //NOI18N
303
}
304     
305     //retouche:
306
// private static MetadataUnit findJAXWSMetadata(Project project) {
307
// JAXWSMetadataProvider metadataProvider = (JAXWSMetadataProvider)project.getLookup().lookup(JAXWSMetadataProvider.class);
308
// if(metadataProvider != null) {
309
// return metadataProvider.getJAXWSMetadata();
310
// } else {
311
// return null;
312
// }
313
// }
314

315     public synchronized void addServiceListener(ServiceListener listener) {
316         if (listener!=null)
317             serviceListeners.add(listener);
318     }
319     
320     public synchronized void removeServiceListener(ServiceListener listener) {
321         serviceListeners.remove(listener);
322     }
323     
324     void fireServiceAdded(String JavaDoc name, String JavaDoc implementationClass) {
325         Iterator JavaDoc<ServiceListener> it = serviceListeners.iterator();
326         while (it.hasNext()) it.next().serviceAdded(name, implementationClass);
327     }
328     
329     void fireServiceRemoved(String JavaDoc name) {
330         Iterator JavaDoc<ServiceListener> it = serviceListeners.iterator();
331         while (it.hasNext()) it.next().serviceRemoved(name);
332     }
333     
334     public static interface ServiceListener {
335         
336         public void serviceAdded(String JavaDoc name, String JavaDoc implementationClass);
337         
338         public void serviceRemoved(String JavaDoc name);
339         
340     }
341     
342 }
343
Popular Tags