KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > model > WebServiceListModel


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.registry.model;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.beans.PropertyChangeSupport JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29
30 import org.openide.loaders.DataObject;
31 import org.openide.filesystems.FileObject;
32 import org.openide.loaders.DataFolder;
33 import org.openide.loaders.DataObjectNotFoundException;
34
35 import org.netbeans.modules.websvc.registry.wsdl.*;
36 ////import org.netbeans.modules.websvc.registry.NotFoundException;
37
import java.util.HashMap JavaDoc;
38 import java.util.Collections JavaDoc;
39 import java.util.HashSet JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Random JavaDoc;
42 import java.util.Set JavaDoc;
43 import java.util.Date JavaDoc;
44 import org.netbeans.modules.websvc.registry.WebServicePersistenceManager;
45 /**
46  * A model to keep track of web service data and their group
47  * Nodes are created using this model
48  * @author Winston Prakash
49  */

50 public class WebServiceListModel {
51
52     private static Random JavaDoc serviceRandom = new Random JavaDoc(System.currentTimeMillis());
53     private static Random JavaDoc serviceGroupRandom = new Random JavaDoc(System.currentTimeMillis());
54
55     Set JavaDoc listeners = new HashSet JavaDoc();
56     /**
57      * Fix for Bug#: 5039378
58      * Netbeans can potentially use multiple threads to maintain a Node's data model.
59      *- David Botterill 5/6/2004
60      */

61     private Map JavaDoc webservices = Collections.synchronizedMap(new HashMap JavaDoc());
62     private Map JavaDoc webserviceGroups = Collections.synchronizedMap(new HashMap JavaDoc());
63
64     private static int websvcCounter=0;
65     private static int groupCounter=0;
66
67     public static String JavaDoc MODEL_SERVICE_ADDED = "ServiceAdded";
68     public static String JavaDoc MODEL_SERVICE_REMOVED = "ServiceRemoved";
69
70     private static String JavaDoc EVENT_TYPE_ADD = "ADD";
71     private static String JavaDoc EVENT_TYPE_REMOVE = "REMOVE";
72
73     private static WebServiceListModel websvcNodeModel = new WebServiceListModel();
74     private static boolean initialized=false;
75     
76
77
78     private WebServiceListModel() {
79     }
80
81     public static WebServiceListModel getInstance() {
82         if (initialized==false){
83             initialized=true;//avoid conflicts in threads.
84
WebServicePersistenceManager pm = new WebServicePersistenceManager();
85             pm.load(pm.getClass().getClassLoader());
86         }
87         return websvcNodeModel;
88     }
89
90
91     public void addWebServiceListModelListener(WebServiceListModelListener listener){
92         listeners.add(listener);
93     }
94
95     public void removeWebServiceListModelListener(WebServiceListModelListener listener){
96         listeners.remove(listener);
97     }
98     /** Get a unique Id for the webservice data
99      * Unique Id is "webservice" + a random number.
100      */

101     public String JavaDoc getUniqueWebServiceId(){
102         String JavaDoc uniqueId = "webservice" + serviceRandom.nextLong();
103         while(webservices.containsKey(uniqueId)){
104             uniqueId = "webservice" + serviceRandom.nextLong();
105         }
106         return uniqueId;
107     }
108
109     /** Get a unique Id for the webservice data group
110      * Unique Id is "webserviceGroup" + a random number.
111      */

112     public String JavaDoc getUniqueWebServiceGroupId(){
113         String JavaDoc uniqueId = "webserviceGroup" + serviceGroupRandom.nextLong();
114         while(webserviceGroups.containsKey(uniqueId)){
115             uniqueId = "webserviceGroup" + serviceGroupRandom.nextLong();
116         }
117         return uniqueId;
118     }
119
120
121     /** Add the webservice data with a unique Id */
122     public void addWebService(WebServiceData webService) {
123         //System.out.println("WebServiceNodeModel Webservice add called - " + webService.getId());
124
if (!webservices.containsKey(webService.getId())) {
125             webservices.put(webService.getId(), webService);
126             fireServiceAdded(webService);
127         }
128     }
129
130     /** Get the webservice data based on unique Id */
131     public WebServiceData getWebService(String JavaDoc webServiceId){
132         return (WebServiceData) webservices.get(webServiceId);
133     }
134
135     /** Get the webservice data from the model */
136     public void removeWebService(String JavaDoc webServiceId) {
137         //System.out.println("WebServiceNodeModel Webservice remove called - " + webServiceId);
138
WebServiceData wsData = (WebServiceData) webservices.get(webServiceId);
139         if(wsData != null) {
140             WebServiceGroup group = getWebServiceGroup(getWebService(webServiceId).getGroupId());
141             if(group != null) {
142                 group.remove(webServiceId);
143             }
144             webservices.remove(webServiceId);
145             fireServiceRemoved(wsData);
146         }
147     }
148
149     public void removeWebService(WebServiceData ws) {
150         // !PW Note the web service passed in is not necessarily the same data object
151
// as the service to be removed. It is merely a key to find a match in the list model
152
Iterator JavaDoc iter = webservices.keySet().iterator();
153         while(iter.hasNext()){
154             String JavaDoc key = iter.next().toString();
155             WebServiceData wsData = (WebServiceData) webservices.get(key);
156             if(wsData.equals(ws)) {
157                 WebServiceGroup group = getWebServiceGroup(wsData.getGroupId());
158                 if(group != null) {
159                     group.remove(key);
160                 }
161                 webservices.remove(key);
162                 fireServiceRemoved(wsData);
163                 break;
164             }
165         }
166     }
167
168     /** Check if the model contains the webservice data*/
169     public boolean webServiceExists(WebServiceData webService){
170         Iterator JavaDoc iter = webservices.keySet().iterator();
171         while(iter.hasNext()){
172             WebServiceData wsData = (WebServiceData) webservices.get(iter.next());
173             if(wsData.equals(webService)) {
174                 return true;
175             }
176         }
177         return false;
178     }
179     
180     public WebServiceData findService(WebServiceData webService) {
181         Iterator JavaDoc iter = webservices.keySet().iterator();
182         while(iter.hasNext()){
183             WebServiceData wsData = (WebServiceData) webservices.get(iter.next());
184             if(wsData.equals(webService)) {
185                 return wsData;
186             }
187         }
188         return null;
189     }
190
191     /** Check if the model contains this service by name */
192     public boolean webServiceExists(String JavaDoc serviceName) {
193         // Create key from serviceName in form of WebServiceData
194
WebServiceData wsKey = new WebServiceData();
195         wsKey.setDisplayName(serviceName);
196
197         Iterator JavaDoc iter = webservices.keySet().iterator();
198         while(iter.hasNext()){
199             WebServiceData wsData = (WebServiceData) webservices.get(iter.next());
200             if(wsData.equals(wsKey)) {
201                 return true;
202             }
203         }
204
205         return false;
206     }
207
208     /** Get all the webservice data added to this model*/
209     public Set JavaDoc getWebServiceSet() {
210         Set JavaDoc websvcs = new HashSet JavaDoc();
211         Iterator JavaDoc iter = webservices.keySet().iterator();
212         while(iter.hasNext()){
213             WebServiceData wsData = (WebServiceData) webservices.get(iter.next());
214             websvcs.add(wsData);
215         }
216         return websvcs;
217     }
218
219     /** Add a webservice group to the model*/
220     public void addWebServiceGroup(WebServiceGroup group){
221         //System.out.println("WebServiceNodeModel add group called - " + group.getId());
222

223         if (!webserviceGroups.containsKey(group.getId())) {
224             webserviceGroups.put(group.getId(), group);
225             Iterator JavaDoc iter = listeners.iterator();
226             while(iter.hasNext()) {
227                 WebServiceListModelEvent evt = new WebServiceListModelEvent(group.getId());
228                 ((WebServiceListModelListener)iter.next()).webServiceGroupAdded(evt);
229             }
230         }
231     }
232
233     /** Remove the webservice group from the model*/
234     public void removeWebServiceGroup(String JavaDoc groupId){
235         //System.out.println("WebServiceNodeModel remove group called - " + groupId);
236
if (webserviceGroups.containsKey(groupId)) {
237             /**
238              * Fix bug:
239              * We need to get an array of the web services instead of using the Iterator because a
240              * Set iterator is fail-safe and will throw a ConcurrentModificationException if you're using
241              * it and the set is modified.
242              * - David Botterill 5/6/2004.
243              */

244             String JavaDoc [] webserviceIds = (String JavaDoc [])getWebServiceGroup(groupId).getWebServiceIds().toArray(new String JavaDoc[0]);
245            for(int ii=0; null != webserviceIds && ii < webserviceIds.length; ii++) {
246                 removeWebService(webserviceIds[ii]);
247             }
248             webserviceGroups.remove(groupId);
249             Iterator JavaDoc iter = listeners.iterator();
250             while(iter.hasNext()) {
251                 WebServiceListModelEvent evt = new WebServiceListModelEvent(groupId);
252                 ((WebServiceListModelListener)iter.next()).webServiceGroupRemoved(evt);
253             }
254         }
255     }
256
257     /** Get a webservice group by its Id*/
258     public WebServiceGroup getWebServiceGroup(String JavaDoc groupId){
259         WebServiceGroup group = (WebServiceGroup) webserviceGroups.get(groupId);
260         // !PW IZ 57542 - deferred initialization changes have made default group initialization
261
// difficult so move it here. The default group should always exist. The only time
262
// it does not is generally when the user is running the IDE for the first time with
263
// a new user directory.
264
if(group == null && "default".equals(groupId)) {
265             synchronized (this) {
266                 group = (WebServiceGroup) webserviceGroups.get(groupId);
267                 if(group == null) {
268                     group = new WebServiceGroup("default");
269                     addWebServiceGroup(group);
270                 }
271             }
272         }
273         return group;
274     }
275
276     public Set JavaDoc getWebServiceGroupSet() {
277         Set JavaDoc wsGroups = new HashSet JavaDoc();
278         Iterator JavaDoc iter = webserviceGroups.keySet().iterator();
279         while(iter.hasNext()){
280             WebServiceGroup group = (WebServiceGroup) webserviceGroups.get(iter.next());
281             wsGroups.add(group);
282         }
283         return wsGroups;
284     }
285     
286     public WebServiceGroup findWebServiceGroup(String JavaDoc groupName) {
287         Iterator JavaDoc iter = webserviceGroups.keySet().iterator();
288         while(iter.hasNext()) {
289             WebServiceGroup group = (WebServiceGroup) webserviceGroups.get(iter.next());
290             if (groupName.equals(group.getName()))
291                 return group;
292         }
293         return null;
294     }
295
296     /** Using property change object to signify service adds and removes.
297      * !PW FIXME Should flesh out the ListModel events and move them to the API
298      * in lieu of this code. EA2 item.
299      * Note also, should provide ability for listener to only listen for adds or
300      * removes of a particular name before sending out event. Otherwise, this
301      * won't scale.
302      */

303     private PropertyChangeSupport JavaDoc propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
304
305     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
306         propertyChangeSupport.addPropertyChangeListener(listener);
307     }
308
309     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
310         propertyChangeSupport.removePropertyChangeListener(listener);
311     }
312
313     private void fireServiceAdded(final WebServiceData wsData) {
314         // Make sure these events are fired on the Swing event thread in case the
315
// respondants update UI (which they likely will because this event is
316
// intended for node updates.)
317
SwingUtilities.invokeLater(new Runnable JavaDoc() {
318             public void run() {
319                 propertyChangeSupport.firePropertyChange(MODEL_SERVICE_ADDED, null, wsData);
320             }
321         });
322     }
323
324     private void fireServiceRemoved(final WebServiceData wsData) {
325         // Make sure these events are fired on the Swing event thread in case the
326
// respondants update UI (which they likely will because this event is
327
// intended for node updates.)
328
SwingUtilities.invokeLater(new Runnable JavaDoc() {
329             public void run() {
330                 propertyChangeSupport.firePropertyChange(MODEL_SERVICE_REMOVED, wsData, null);
331             }
332         });
333     }
334
335 // public static class WebServiceEvent {
336
// private final WebServiceData webServiceData;
337
//
338
// private WebServiceEvent(final WebServiceData wsData) {
339
// this.webServiceData = wsData;
340
// }
341
//
342
// public WebServiceData getWebServiceData() {
343
// return webServiceData;
344
// }
345
// };
346
}
Popular Tags