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.registry.model;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Iterator;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049: import java.beans.PropertyChangeSupport;
050: import javax.swing.SwingUtilities;
051:
052: import org.openide.loaders.DataObject;
053: import org.openide.filesystems.FileObject;
054: import org.openide.loaders.DataFolder;
055: import org.openide.loaders.DataObjectNotFoundException;
056:
057: import org.netbeans.modules.websvc.registry.wsdl.*; ////import org.netbeans.modules.websvc.registry.NotFoundException;
058: import java.util.HashMap;
059: import java.util.Collections;
060: import java.util.HashSet;
061: import java.util.Map;
062: import java.util.Random;
063: import java.util.Set;
064: import java.util.Date;
065: import org.netbeans.modules.websvc.registry.WebServicePersistenceManager;
066:
067: /**
068: * A model to keep track of web service data and their group
069: * Nodes are created using this model
070: * @author Winston Prakash
071: */
072: public class WebServiceListModel {
073:
074: private static Random serviceRandom = new Random(System
075: .currentTimeMillis());
076: private static Random serviceGroupRandom = new Random(System
077: .currentTimeMillis());
078:
079: Set listeners = new HashSet();
080: /**
081: * Fix for Bug#: 5039378
082: * Netbeans can potentially use multiple threads to maintain a Node's data model.
083: *- David Botterill 5/6/2004
084: */
085: private Map webservices = Collections
086: .synchronizedMap(new HashMap());
087: private Map webserviceGroups = Collections
088: .synchronizedMap(new HashMap());
089:
090: private static int websvcCounter = 0;
091: private static int groupCounter = 0;
092:
093: public static String MODEL_SERVICE_ADDED = "ServiceAdded";
094: public static String MODEL_SERVICE_REMOVED = "ServiceRemoved";
095:
096: private static String EVENT_TYPE_ADD = "ADD";
097: private static String EVENT_TYPE_REMOVE = "REMOVE";
098:
099: private static WebServiceListModel websvcNodeModel = new WebServiceListModel();
100: private static boolean initialized = false;
101:
102: private WebServiceListModel() {
103: }
104:
105: public static WebServiceListModel getInstance() {
106: if (initialized == false) {
107: initialized = true;//avoid conflicts in threads.
108: WebServicePersistenceManager pm = new WebServicePersistenceManager();
109: pm.load(pm.getClass().getClassLoader());
110: }
111: return websvcNodeModel;
112: }
113:
114: public void addWebServiceListModelListener(
115: WebServiceListModelListener listener) {
116: listeners.add(listener);
117: }
118:
119: public void removeWebServiceListModelListener(
120: WebServiceListModelListener listener) {
121: listeners.remove(listener);
122: }
123:
124: /** Get a unique Id for the webservice data
125: * Unique Id is "webservice" + a random number.
126: */
127: public String getUniqueWebServiceId() {
128: String uniqueId = "webservice" + serviceRandom.nextLong();
129: while (webservices.containsKey(uniqueId)) {
130: uniqueId = "webservice" + serviceRandom.nextLong();
131: }
132: return uniqueId;
133: }
134:
135: /** Get a unique Id for the webservice data group
136: * Unique Id is "webserviceGroup" + a random number.
137: */
138: public String getUniqueWebServiceGroupId() {
139: String uniqueId = "webserviceGroup"
140: + serviceGroupRandom.nextLong();
141: while (webserviceGroups.containsKey(uniqueId)) {
142: uniqueId = "webserviceGroup"
143: + serviceGroupRandom.nextLong();
144: }
145: return uniqueId;
146: }
147:
148: /** Add the webservice data with a unique Id */
149: public void addWebService(WebServiceData webService) {
150: //System.out.println("WebServiceNodeModel Webservice add called - " + webService.getId());
151: if (webService != null
152: && !webservices.containsKey(webService.getId())) {
153: webservices.put(webService.getId(), webService);
154: fireServiceAdded(webService);
155: }
156: }
157:
158: /** Get the webservice data based on unique Id */
159: public WebServiceData getWebService(String webServiceId) {
160: return (WebServiceData) webservices.get(webServiceId);
161: }
162:
163: /** Get the webservice data from the model */
164: public void removeWebService(String webServiceId) {
165: //System.out.println("WebServiceNodeModel Webservice remove called - " + webServiceId);
166: WebServiceData wsData = (WebServiceData) webservices
167: .get(webServiceId);
168: if (wsData != null) {
169: WebServiceGroup group = getWebServiceGroup(getWebService(
170: webServiceId).getGroupId());
171: if (group != null) {
172: group.remove(webServiceId);
173: }
174: webservices.remove(webServiceId);
175: fireServiceRemoved(wsData);
176: }
177: }
178:
179: public void removeWebService(WebServiceData ws) {
180: // !PW Note the web service passed in is not necessarily the same data object
181: // as the service to be removed. It is merely a key to find a match in the list model
182: Iterator iter = webservices.keySet().iterator();
183: while (iter.hasNext()) {
184: String key = iter.next().toString();
185: WebServiceData wsData = (WebServiceData) webservices
186: .get(key);
187: if (wsData.equals(ws)) {
188: WebServiceGroup group = getWebServiceGroup(wsData
189: .getGroupId());
190: if (group != null) {
191: group.remove(key);
192: }
193: webservices.remove(key);
194: fireServiceRemoved(wsData);
195: break;
196: }
197: }
198: }
199:
200: /** Check if the model contains the webservice data*/
201: public boolean webServiceExists(WebServiceData webService) {
202: Iterator iter = webservices.keySet().iterator();
203: while (iter.hasNext()) {
204: WebServiceData wsData = (WebServiceData) webservices
205: .get(iter.next());
206: if (wsData.equals(webService)) {
207: return true;
208: }
209: }
210: return false;
211: }
212:
213: public WebServiceData findService(WebServiceData webService) {
214: Iterator iter = webservices.keySet().iterator();
215: while (iter.hasNext()) {
216: WebServiceData wsData = (WebServiceData) webservices
217: .get(iter.next());
218: if (wsData.equals(webService)) {
219: return wsData;
220: }
221: }
222: return null;
223: }
224:
225: /** Check if the model contains this service by name */
226: public boolean webServiceExists(String serviceName) {
227: // Create key from serviceName in form of WebServiceData
228: WebServiceData wsKey = new WebServiceData();
229: wsKey.setDisplayName(serviceName);
230:
231: Iterator iter = webservices.keySet().iterator();
232: while (iter.hasNext()) {
233: WebServiceData wsData = (WebServiceData) webservices
234: .get(iter.next());
235: if (wsData.equals(wsKey)) {
236: return true;
237: }
238: }
239:
240: return false;
241: }
242:
243: /** Get all the webservice data added to this model*/
244: public Set getWebServiceSet() {
245: Set websvcs = new HashSet();
246: Iterator iter = webservices.keySet().iterator();
247: while (iter.hasNext()) {
248: WebServiceData wsData = (WebServiceData) webservices
249: .get(iter.next());
250: websvcs.add(wsData);
251: }
252: return websvcs;
253: }
254:
255: /** Add a webservice group to the model*/
256: public void addWebServiceGroup(WebServiceGroup group) {
257: //System.out.println("WebServiceNodeModel add group called - " + group.getId());
258: if (webserviceGroups == null || group == null)
259: return;
260: if (!webserviceGroups.containsKey(group.getId())) {
261: webserviceGroups.put(group.getId(), group);
262: Iterator iter = listeners.iterator();
263: while (iter.hasNext()) {
264: WebServiceListModelEvent evt = new WebServiceListModelEvent(
265: group.getId());
266: ((WebServiceListModelListener) iter.next())
267: .webServiceGroupAdded(evt);
268: }
269: }
270: }
271:
272: /** Remove the webservice group from the model*/
273: public void removeWebServiceGroup(String groupId) {
274: //System.out.println("WebServiceNodeModel remove group called - " + groupId);
275: if (webserviceGroups.containsKey(groupId)) {
276: /**
277: * Fix bug:
278: * We need to get an array of the web services instead of using the Iterator because a
279: * Set iterator is fail-safe and will throw a ConcurrentModificationException if you're using
280: * it and the set is modified.
281: * - David Botterill 5/6/2004.
282: */
283: String[] webserviceIds = (String[]) getWebServiceGroup(
284: groupId).getWebServiceIds().toArray(new String[0]);
285: for (int ii = 0; null != webserviceIds
286: && ii < webserviceIds.length; ii++) {
287: removeWebService(webserviceIds[ii]);
288: }
289: webserviceGroups.remove(groupId);
290: Iterator iter = listeners.iterator();
291: while (iter.hasNext()) {
292: WebServiceListModelEvent evt = new WebServiceListModelEvent(
293: groupId);
294: ((WebServiceListModelListener) iter.next())
295: .webServiceGroupRemoved(evt);
296: }
297: }
298: }
299:
300: /** Get a webservice group by its Id*/
301: public WebServiceGroup getWebServiceGroup(String groupId) {
302: WebServiceGroup group = (WebServiceGroup) webserviceGroups
303: .get(groupId);
304: // !PW IZ 57542 - deferred initialization changes have made default group initialization
305: // difficult so move it here. The default group should always exist. The only time
306: // it does not is generally when the user is running the IDE for the first time with
307: // a new user directory.
308: if (group == null && "default".equals(groupId)) {
309: synchronized (this ) {
310: group = (WebServiceGroup) webserviceGroups.get(groupId);
311: if (group == null) {
312: group = new WebServiceGroup("default");
313: addWebServiceGroup(group);
314: }
315: }
316: }
317: return group;
318: }
319:
320: public Set getWebServiceGroupSet() {
321: Set wsGroups = new HashSet();
322: Iterator iter = webserviceGroups.keySet().iterator();
323: while (iter.hasNext()) {
324: WebServiceGroup group = (WebServiceGroup) webserviceGroups
325: .get(iter.next());
326: wsGroups.add(group);
327: }
328: return wsGroups;
329: }
330:
331: public WebServiceGroup findWebServiceGroup(String groupName) {
332: Iterator iter = webserviceGroups.keySet().iterator();
333: while (iter.hasNext()) {
334: WebServiceGroup group = (WebServiceGroup) webserviceGroups
335: .get(iter.next());
336: if (groupName.equals(group.getName()))
337: return group;
338: }
339: return null;
340: }
341:
342: /** Using property change object to signify service adds and removes.
343: * !PW FIXME Should flesh out the ListModel events and move them to the API
344: * in lieu of this code. EA2 item.
345: * Note also, should provide ability for listener to only listen for adds or
346: * removes of a particular name before sending out event. Otherwise, this
347: * won't scale.
348: */
349: private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
350: this );
351:
352: public void addPropertyChangeListener(
353: PropertyChangeListener listener) {
354: propertyChangeSupport.addPropertyChangeListener(listener);
355: }
356:
357: public void removePropertyChangeListener(
358: PropertyChangeListener listener) {
359: propertyChangeSupport.removePropertyChangeListener(listener);
360: }
361:
362: private void fireServiceAdded(final WebServiceData wsData) {
363: // Make sure these events are fired on the Swing event thread in case the
364: // respondants update UI (which they likely will because this event is
365: // intended for node updates.)
366: SwingUtilities.invokeLater(new Runnable() {
367: public void run() {
368: propertyChangeSupport.firePropertyChange(
369: MODEL_SERVICE_ADDED, null, wsData);
370: }
371: });
372: }
373:
374: private void fireServiceRemoved(final WebServiceData wsData) {
375: // Make sure these events are fired on the Swing event thread in case the
376: // respondants update UI (which they likely will because this event is
377: // intended for node updates.)
378: SwingUtilities.invokeLater(new Runnable() {
379: public void run() {
380: propertyChangeSupport.firePropertyChange(
381: MODEL_SERVICE_REMOVED, wsData, null);
382: }
383: });
384: }
385:
386: // public static class WebServiceEvent {
387: // private final WebServiceData webServiceData;
388: //
389: // private WebServiceEvent(final WebServiceData wsData) {
390: // this.webServiceData = wsData;
391: // }
392: //
393: // public WebServiceData getWebServiceData() {
394: // return webServiceData;
395: // }
396: // };
397: }
|