1 19 20 package org.netbeans.modules.websvc.registry.model; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.beans.PropertyChangeSupport ; 28 import javax.swing.SwingUtilities ; 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 java.util.HashMap ; 38 import java.util.Collections ; 39 import java.util.HashSet ; 40 import java.util.Map ; 41 import java.util.Random ; 42 import java.util.Set ; 43 import java.util.Date ; 44 import org.netbeans.modules.websvc.registry.WebServicePersistenceManager; 45 50 public class WebServiceListModel { 51 52 private static Random serviceRandom = new Random (System.currentTimeMillis()); 53 private static Random serviceGroupRandom = new Random (System.currentTimeMillis()); 54 55 Set listeners = new HashSet (); 56 61 private Map webservices = Collections.synchronizedMap(new HashMap ()); 62 private Map webserviceGroups = Collections.synchronizedMap(new HashMap ()); 63 64 private static int websvcCounter=0; 65 private static int groupCounter=0; 66 67 public static String MODEL_SERVICE_ADDED = "ServiceAdded"; 68 public static String MODEL_SERVICE_REMOVED = "ServiceRemoved"; 69 70 private static String EVENT_TYPE_ADD = "ADD"; 71 private static String 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; 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 101 public String getUniqueWebServiceId(){ 102 String uniqueId = "webservice" + serviceRandom.nextLong(); 103 while(webservices.containsKey(uniqueId)){ 104 uniqueId = "webservice" + serviceRandom.nextLong(); 105 } 106 return uniqueId; 107 } 108 109 112 public String getUniqueWebServiceGroupId(){ 113 String uniqueId = "webserviceGroup" + serviceGroupRandom.nextLong(); 114 while(webserviceGroups.containsKey(uniqueId)){ 115 uniqueId = "webserviceGroup" + serviceGroupRandom.nextLong(); 116 } 117 return uniqueId; 118 } 119 120 121 122 public void addWebService(WebServiceData webService) { 123 if (!webservices.containsKey(webService.getId())) { 125 webservices.put(webService.getId(), webService); 126 fireServiceAdded(webService); 127 } 128 } 129 130 131 public WebServiceData getWebService(String webServiceId){ 132 return (WebServiceData) webservices.get(webServiceId); 133 } 134 135 136 public void removeWebService(String webServiceId) { 137 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 Iterator iter = webservices.keySet().iterator(); 153 while(iter.hasNext()){ 154 String 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 169 public boolean webServiceExists(WebServiceData webService){ 170 Iterator 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 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 192 public boolean webServiceExists(String serviceName) { 193 WebServiceData wsKey = new WebServiceData(); 195 wsKey.setDisplayName(serviceName); 196 197 Iterator 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 209 public Set getWebServiceSet() { 210 Set websvcs = new HashSet (); 211 Iterator 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 220 public void addWebServiceGroup(WebServiceGroup group){ 221 223 if (!webserviceGroups.containsKey(group.getId())) { 224 webserviceGroups.put(group.getId(), group); 225 Iterator 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 234 public void removeWebServiceGroup(String groupId){ 235 if (webserviceGroups.containsKey(groupId)) { 237 244 String [] webserviceIds = (String [])getWebServiceGroup(groupId).getWebServiceIds().toArray(new String [0]); 245 for(int ii=0; null != webserviceIds && ii < webserviceIds.length; ii++) { 246 removeWebService(webserviceIds[ii]); 247 } 248 webserviceGroups.remove(groupId); 249 Iterator iter = listeners.iterator(); 250 while(iter.hasNext()) { 251 WebServiceListModelEvent evt = new WebServiceListModelEvent(groupId); 252 ((WebServiceListModelListener)iter.next()).webServiceGroupRemoved(evt); 253 } 254 } 255 } 256 257 258 public WebServiceGroup getWebServiceGroup(String groupId){ 259 WebServiceGroup group = (WebServiceGroup) webserviceGroups.get(groupId); 260 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 getWebServiceGroupSet() { 277 Set wsGroups = new HashSet (); 278 Iterator 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 groupName) { 287 Iterator 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 303 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport (this); 304 305 public void addPropertyChangeListener(PropertyChangeListener listener) { 306 propertyChangeSupport.addPropertyChangeListener(listener); 307 } 308 309 public void removePropertyChangeListener(PropertyChangeListener listener) { 310 propertyChangeSupport.removePropertyChangeListener(listener); 311 } 312 313 private void fireServiceAdded(final WebServiceData wsData) { 314 SwingUtilities.invokeLater(new Runnable () { 318 public void run() { 319 propertyChangeSupport.firePropertyChange(MODEL_SERVICE_ADDED, null, wsData); 320 } 321 }); 322 } 323 324 private void fireServiceRemoved(final WebServiceData wsData) { 325 SwingUtilities.invokeLater(new Runnable () { 329 public void run() { 330 propertyChangeSupport.firePropertyChange(MODEL_SERVICE_REMOVED, wsData, null); 331 } 332 }); 333 } 334 335 } | Popular Tags |