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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.api.jaxws.project.config;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.IOException;
047: import java.io.OutputStream;
048: import java.util.ArrayList;
049: import java.util.Iterator;
050: import java.util.List;
051: import javax.swing.event.ChangeEvent;
052: import javax.swing.event.ChangeListener;
053: import org.netbeans.modules.schema2beans.BaseBean;
054: import org.openide.ErrorManager;
055: import org.openide.filesystems.FileLock;
056: import org.openide.filesystems.FileObject;
057: import org.openide.filesystems.FileSystem.AtomicAction;
058: import org.openide.util.ChangeSupport;
059:
060: /** Provides information about web services and clients in a project
061: * Provides information used for build-impl generation
062: * Working over nbproject/jax-ws.xml file
063: */
064: public final class JaxWsModel {
065: private org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.JaxWs jaxws;
066: private FileObject fo;
067: private Object initLock = new Object();
068: private List<ServiceListener> serviceListeners;
069: private List<PropertyChangeListener> propertyChangeListeners;
070: private ChangeSupport changeSupport;
071:
072: JaxWsModel(
073: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.JaxWs jaxws) {
074: this (jaxws, null);
075: }
076:
077: JaxWsModel(
078: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.JaxWs jaxws,
079: FileObject fo) {
080: this .jaxws = jaxws;
081: this .fo = fo;
082: propertyChangeListeners = new ArrayList<PropertyChangeListener>();
083: serviceListeners = new ArrayList<ServiceListener>();
084: changeSupport = new ChangeSupport(this );
085: }
086:
087: public Service[] getServices() {
088:
089: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Services services = jaxws
090: .getServices();
091: if (services == null)
092: return new Service[] {};
093: else {
094: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service[] org = services
095: .getService();
096: if (org == null)
097: return new Service[] {};
098: Service[] newServices = new Service[org.length];
099: for (int i = 0; i < org.length; i++)
100: newServices[i] = new Service(org[i]);
101: return newServices;
102: }
103: }
104:
105: public void setJsr109(Boolean jsr109) {
106: jaxws.setJsr109(jsr109);
107: }
108:
109: public Boolean getJsr109() {
110: return jaxws.getJsr109();
111: }
112:
113: public Service findServiceByName(String name) {
114: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = findService(name);
115: return (service == null ? null : new Service(service));
116: }
117:
118: public Service findServiceByImplementationClass(String wsClassName) {
119: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = _findServiceByClass(wsClassName);
120: return (service == null ? null : new Service(service));
121: }
122:
123: private org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service findService(
124: String name) {
125: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Services services = jaxws
126: .getServices();
127: if (services == null)
128: return null;
129: else {
130: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service[] org = services
131: .getService();
132: if (org == null)
133: return null;
134: for (int i = 0; i < org.length; i++) {
135: if (name.equals(org[i].getName()))
136: return org[i];
137: }
138: return null;
139: }
140: }
141:
142: private org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service _findServiceByClass(
143: String wsClassName) {
144: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Services services = jaxws
145: .getServices();
146: if (services == null)
147: return null;
148: else {
149: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service[] org = services
150: .getService();
151: if (org == null)
152: return null;
153: for (int i = 0; i < org.length; i++) {
154: if (wsClassName.equals(org[i].getImplementationClass()))
155: return org[i];
156: }
157: return null;
158: }
159: }
160:
161: public boolean removeService(String name) {
162: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = findService(name);
163: if (name == null)
164: return false;
165: else {
166: fireServiceRemoved(name);
167: jaxws.getServices().removeService(service);
168: return true;
169: }
170: }
171:
172: public boolean removeServiceByClassName(String webserviceClassName) {
173: if (webserviceClassName != null) {
174: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = _findServiceByClass(webserviceClassName);
175: if (service != null) {
176: fireServiceRemoved(service.getName());
177: jaxws.getServices().removeService(service);
178: return true;
179: }
180: }
181: return false;
182: }
183:
184: public Service addService(String name, String implementationClass)
185: throws ServiceAlreadyExistsExeption {
186:
187: if (findService(name) != null)
188: throw new ServiceAlreadyExistsExeption(name);
189: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = jaxws
190: .getServices().newService();
191: service.setName(name);
192: service.setImplementationClass(implementationClass);
193: jaxws.getServices().addService(service);
194: fireServiceAdded(name, implementationClass);
195: return new Service(service);
196: }
197:
198: public Service addService(String name, String implementationClass,
199: String wsdlUrl, String serviceName, String portName,
200: String packageName) throws ServiceAlreadyExistsExeption {
201:
202: if (findService(name) != null)
203: throw new ServiceAlreadyExistsExeption(name);
204: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service service = jaxws
205: .getServices().newService();
206: service.setName(name);
207: service.setImplementationClass(implementationClass);
208: service.setWsdlUrl(wsdlUrl);
209: service.setServiceName(serviceName);
210: service.setPortName(portName);
211: service.setPackageName(packageName);
212: jaxws.getServices().addService(service);
213: return new Service(service);
214: }
215:
216: public Client[] getClients() {
217: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Clients clients = jaxws
218: .getClients();
219: if (clients == null)
220: return new Client[] {};
221: else {
222: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client[] org = clients
223: .getClient();
224: if (org == null)
225: return new Client[] {};
226: Client[] newClients = new Client[org.length];
227: for (int i = 0; i < org.length; i++)
228: newClients[i] = new Client(org[i]);
229: return newClients;
230: }
231: }
232:
233: public Client findClientByName(String name) {
234: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client client = findClient(name);
235: return (client == null ? null : new Client(client));
236: }
237:
238: public Client findClientByWsdlUrl(String wsdlUrl) {
239: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Clients clients = jaxws
240: .getClients();
241: if (clients == null)
242: return null;
243: else {
244: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client[] org = clients
245: .getClient();
246: if (org == null)
247: return null;
248: for (int i = 0; i < org.length; i++) {
249: if (wsdlUrl.equals(org[i].getWsdlUrl()))
250: return new Client(org[i]);
251: }
252: return null;
253: }
254: }
255:
256: private org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client findClient(
257: String name) {
258: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Clients clients = jaxws
259: .getClients();
260: if (clients == null)
261: return null;
262: else {
263: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client[] org = clients
264: .getClient();
265: if (org == null)
266: return null;
267: for (int i = 0; i < org.length; i++) {
268: if (name.equals(org[i].getName()))
269: return org[i];
270: }
271: return null;
272: }
273: }
274:
275: public boolean removeClient(String name) {
276: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client client = findClient(name);
277: if (client == null)
278: return false;
279: else {
280: jaxws.getClients().removeClient(client);
281: return true;
282: }
283: }
284:
285: public Client addClient(String name, String wsdlUrl,
286: String packageName) throws ClientAlreadyExistsExeption {
287:
288: if (findClient(name) != null)
289: throw new ClientAlreadyExistsExeption(name);
290: org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client client = jaxws
291: .getClients().newClient();
292: client.setName(name);
293: client.setWsdlUrl(wsdlUrl);
294: if (packageName != null) {
295: client.setPackageName(packageName);
296: client.setPackageNameForceReplace("true");
297: }
298: jaxws.getClients().addClient(client);
299: return new Client(client);
300: }
301:
302: public synchronized void addPropertyChangeListener(
303: PropertyChangeListener l) {
304: JaxWsPCL jaxWsPcl = new JaxWsPCL(l);
305: propertyChangeListeners.add(jaxWsPcl);
306: jaxws.addPropertyChangeListener(jaxWsPcl);
307: }
308:
309: public synchronized void removePropertyChangeListener(
310: PropertyChangeListener l) {
311: for (PropertyChangeListener pcl : propertyChangeListeners) {
312: if (l == ((JaxWsPCL) pcl).getOriginalListener()) {
313: jaxws.removePropertyChangeListener(pcl);
314: propertyChangeListeners.remove(pcl);
315: break;
316: }
317: }
318: }
319:
320: public void merge(JaxWsModel newJaxWs) {
321: if (newJaxWs.jaxws != null)
322: jaxws.merge(newJaxWs.jaxws, BaseBean.MERGE_UPDATE);
323: }
324:
325: public void write(OutputStream os) throws IOException {
326: jaxws.write(os);
327: }
328:
329: public FileObject getJaxWsFile() {
330: return fo;
331: }
332:
333: public void setJaxWsFile(FileObject fo) {
334: this .fo = fo;
335: changeSupport.fireChange();
336: }
337:
338: public void write() throws IOException {
339: if (fo != null) {
340: fo.getFileSystem().runAtomicAction(new AtomicAction() {
341: public void run() {
342: FileLock lock = null;
343: try {
344: lock = fo.lock();
345: OutputStream os = fo.getOutputStream(lock);
346: write(os);
347: os.close();
348: } catch (IOException ex) {
349: ErrorManager.getDefault().notify(ex);
350: } finally {
351: if (lock != null)
352: lock.releaseLock();
353: }
354: }
355: });
356: } else
357: throw new IOException("No FileObject for writing specified"); //NOI18N
358: }
359:
360: public synchronized void addServiceListener(ServiceListener listener) {
361: if (listener != null)
362: serviceListeners.add(listener);
363: }
364:
365: public synchronized void removeServiceListener(
366: ServiceListener listener) {
367: serviceListeners.remove(listener);
368: }
369:
370: void fireServiceAdded(String name, String implementationClass) {
371: Iterator<ServiceListener> it = serviceListeners.iterator();
372: while (it.hasNext())
373: it.next().serviceAdded(name, implementationClass);
374: }
375:
376: void fireServiceRemoved(String name) {
377: Iterator<ServiceListener> it = serviceListeners.iterator();
378: while (it.hasNext())
379: it.next().serviceRemoved(name);
380: }
381:
382: public static interface ServiceListener {
383:
384: public void serviceAdded(String name, String implementationClass);
385:
386: public void serviceRemoved(String name);
387:
388: }
389:
390: private class JaxWsPCL implements PropertyChangeListener {
391:
392: PropertyChangeListener originalListener;
393:
394: JaxWsPCL(PropertyChangeListener originalListener) {
395: this .originalListener = originalListener;
396:
397: }
398:
399: public void propertyChange(PropertyChangeEvent evt) {
400: Object oldValue = evt.getOldValue();
401: if (oldValue != null) {
402: if (oldValue instanceof org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client) {
403: oldValue = new Client(
404: (org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client) oldValue);
405: }
406: if (oldValue instanceof org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service) {
407: oldValue = new Service(
408: (org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service) oldValue);
409: }
410: }
411: Object newValue = evt.getNewValue();
412: if (newValue != null) {
413: if (newValue instanceof org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client) {
414: newValue = new Client(
415: (org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Client) newValue);
416: }
417: if (newValue instanceof org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service) {
418: newValue = new Service(
419: (org.netbeans.modules.websvc.jaxwsmodel.project_config1_0.Service) newValue);
420: }
421: }
422: originalListener.propertyChange(new PropertyChangeEvent(evt
423: .getSource(), evt.getPropertyName(), oldValue,
424: newValue));
425: }
426:
427: PropertyChangeListener getOriginalListener() {
428: return originalListener;
429: }
430:
431: }
432:
433: /** Registers ChangeListener for JaxWsModel object.
434: * The listener fires the ChangeEvent when FileObject is set for JaxWsModel
435: * (For projects, this occurs when jax-ws.xml is physicaly created in nbproject directory)
436: *
437: * @param listener ChangeListener instance
438: */
439: public void addChangeListener(ChangeListener listener) {
440: changeSupport.addChangeListener(listener);
441: }
442:
443: /** Unregisters ChangeListener from JaxWsModel object.
444: *
445: * @param listener ChangeListener instance
446: */
447: public void removeChangeListener(ChangeListener listener) {
448: changeSupport.removeChangeListener(listener);
449: }
450:
451: }
|