1 19 20 package org.netbeans.core; 21 22 import java.io.IOException ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Map.Entry; 35 import java.util.logging.Level ; 36 import java.util.logging.Logger ; 37 import org.openide.ServiceType; 38 import org.openide.cookies.InstanceCookie; 39 import org.openide.filesystems.FileObject; 40 import org.openide.loaders.DataFolder; 41 import org.openide.loaders.DataObject; 42 import org.openide.loaders.DataObjectNotFoundException; 43 import org.openide.loaders.InstanceDataObject; 44 import org.openide.util.Lookup; 45 import org.openide.util.LookupEvent; 46 import org.openide.util.LookupListener; 47 import org.openide.util.io.NbMarshalledObject; 48 49 54 @SuppressWarnings ({"deprecation","unchecked"}) 55 public final class Services extends ServiceType.Registry implements LookupListener { 56 57 static final long serialVersionUID =-7558069607307508327L; 58 59 60 private Lookup.Result<ServiceType> allTypes; 61 62 63 private Map <String , ServiceType> name2Service; 64 65 66 public static Services getDefault () { 67 return (Services)Lookup.getDefault ().lookup (org.openide.ServiceType.Registry.class); 68 } 69 70 public Services() { 71 name2Service = new HashMap <String , ServiceType>(); 72 } 73 74 public ServiceType find(Class clazz) { 75 return (ServiceType)Lookup.getDefault().lookup(clazz); 76 } 77 78 79 public ServiceType find (String name) { 80 Map <String , ServiceType> lookupMap = name2Service; 81 ServiceType ret; 82 synchronized (lookupMap) { 83 ret = lookupMap.get(name); 84 } 85 86 if (ret == null) { 87 ret = super.find(name); 88 synchronized (lookupMap) { 89 lookupMap.put(name, ret); 90 } 91 } 92 93 return ret; 94 } 95 96 97 private Lookup.Result<ServiceType> getTypesResult() { 98 boolean init = false; 99 synchronized (this) { 100 if (allTypes == null) { 101 allTypes = Lookup.getDefault().lookupResult(ServiceType.class); 102 allTypes.addLookupListener(this); 103 init = true; 104 } 105 } 106 if (init) resultChanged(null); 107 return allTypes; 108 } 109 110 113 public void resultChanged(LookupEvent ev) { 114 synchronized (name2Service) { 115 name2Service.clear(); 116 } 117 } 118 119 122 public java.util.List <ServiceType> getServiceTypes () { 123 return new ArrayList <ServiceType>(getTypesResult().allInstances()); 124 } 125 126 132 public synchronized void setServiceTypes (java.util.List arr0) { 133 if (arr0 == null) { 134 throw new NullPointerException (); 135 } 136 137 @SuppressWarnings ("unchecked") java.util.List <ServiceType> arr = ensureSingleness((java.util.List <ServiceType>) arr0); 138 139 HashMap <ServiceType,DataObject> services = new HashMap <ServiceType,DataObject>(20); searchServices(NbPlaces.getDefault().findSessionFolder("Services").getPrimaryFile(), services); 142 HashMap <DataFolder,List <DataObject>> order = new HashMap <DataFolder,List <DataObject>>(10); Iterator <ServiceType> it = arr.iterator(); 145 while (it.hasNext()) { 146 ServiceType st = it.next(); 147 DataObject dobj = services.get(st); 148 149 if (dobj != null) { 150 try { 152 dobj = InstanceDataObject.create(dobj.getFolder(), dobj.getPrimaryFile().getName(), st, null); 153 } catch (IOException ex) { 154 Logger.getLogger(Services.class.getName()).log(Level.WARNING, null, ex); 155 } 156 services.remove(st); 157 } else { 158 dobj = storeNewServiceType(st); 159 } 160 161 if (dobj != null) { 163 DataFolder parent = dobj.getFolder(); 164 List <DataObject> orderedFiles = order.get(parent); 165 if (orderedFiles == null) { 166 orderedFiles = new ArrayList <DataObject>(6); 167 order.put(parent, orderedFiles); 168 } 169 orderedFiles.add(dobj); 170 } 171 } 172 173 for (Entry<DataFolder,List <DataObject>> entry: order.entrySet()) { 175 DataObject parent = entry.getKey(); 176 List <DataObject> orderedFiles = entry.getValue(); 177 if (orderedFiles.size() < 2) continue; 178 179 Iterator <DataObject> files = orderedFiles.iterator(); 180 StringBuffer orderAttr = new StringBuffer (64); 181 while (files.hasNext()) { 182 DataObject file = files.next(); 183 orderAttr.append(file.getPrimaryFile().getNameExt()).append('/'); 184 } 185 orderAttr.deleteCharAt(orderAttr.length() - 1); 186 try { 187 parent.getPrimaryFile(). 188 setAttribute("OpenIDE-Folder-Order", orderAttr.toString()); } catch (IOException ex) { 190 Logger.getLogger(Services.class.getName()).log(Level.WARNING, null, ex); 191 } 192 } 193 194 for (DataObject dobj: services.values()) { 196 try { 197 dobj.delete(); 198 } catch (IOException ex) { 199 Logger.getLogger(Services.class.getName()).log(Level.WARNING, null, ex); 200 } 201 } 202 203 } 204 205 private DataObject storeNewServiceType(ServiceType st) { 206 Class stype = st.getClass (); 207 while (stype.getSuperclass () != ServiceType.class) { 209 stype = stype.getSuperclass(); 210 } 211 212 try{ 213 String folder = org.openide.util.Utilities.getShortClassName(stype); 214 215 DataFolder dfServices = NbPlaces.getDefault().findSessionFolder("Services"); DataFolder dfTarget = DataFolder.create(dfServices, folder); 217 218 return InstanceDataObject.create(dfTarget, null, st, null); 219 } catch (Exception ex) { 220 Logger.getLogger(Services.class.getName()).log(Level.WARNING, null, ex); 221 return null; 222 } 223 } 224 225 227 private List <ServiceType> ensureSingleness(List <ServiceType> l) { 228 List <ServiceType> newList = new ArrayList <ServiceType>(l.size()); 229 230 for (ServiceType stype: l) { 231 if (newList.contains(stype)) { 232 continue; 233 } else { 234 newList.add(stype); 235 } 236 } 237 238 return newList; 239 } 240 241 242 private void searchServices(FileObject folder, Map <ServiceType,DataObject> services) { 243 FileObject[] fobjs = folder.getChildren(); 244 for (int i = 0; i < fobjs.length; i++) { 245 if (!fobjs[i].isValid()) 246 continue; 247 if (fobjs[i].isFolder()) { 248 searchServices(fobjs[i], services); 249 } else { 250 try { 251 DataObject dobj = DataObject.find(fobjs[i]); 252 InstanceCookie inst = (InstanceCookie) dobj.getCookie(InstanceCookie.class); 253 254 if (inst == null) 255 continue; 256 if (instanceOf(inst, ServiceType.class)) { 257 ServiceType ser = (ServiceType) inst.instanceCreate(); 258 259 services.put(ser, dobj); 260 } 261 } 262 catch (DataObjectNotFoundException ex) { 263 } 264 catch (Exception ex) { 265 Logger.getLogger(Services.class.getName()).log(Level.WARNING, null, ex); 266 } 267 } 268 } 269 } 270 271 272 private static boolean instanceOf(InstanceCookie inst, Class clazz) { 273 if (inst instanceof InstanceCookie.Of) { 274 return ((InstanceCookie.Of) inst).instanceOf(clazz); 275 } else { 276 try { 277 return clazz.isAssignableFrom(inst.instanceClass()); 278 } catch (Exception ex) { 279 Logger.getLogger(Services.class.getName()).log(Level.WARNING, null, ex); 280 return false; 281 } 282 } 283 } 284 285 286 public Enumeration <ServiceType> services () { 287 return Collections.enumeration (getServiceTypes ()); 288 } 289 290 295 public <T extends ServiceType> Enumeration <T> services(Class <T> clazz) { 296 if (clazz == null) return org.openide.util.Enumerations.empty(); 297 Collection res = Lookup.getDefault().lookupAll(clazz); 298 return Collections.enumeration(res); 299 } 300 301 303 private void writeObject (ObjectOutputStream oos) throws IOException { 304 Enumeration en = services (); 305 while (en.hasMoreElements ()) { 306 ServiceType s = (ServiceType)en.nextElement (); 307 308 NbMarshalledObject obj; 309 try { 310 obj = new NbMarshalledObject (s); 311 } catch (IOException ex) { 312 Logger.getLogger(Services.class.getName()).log(Level.WARNING, null, ex); 313 obj = null; 315 } 316 if (obj != null) { 317 oos.writeObject (obj); 318 } 319 } 320 321 oos.writeObject (null); 322 } 323 324 326 private void readObject (ObjectInputStream oos) 327 throws IOException , ClassNotFoundException { 328 final LinkedList <ServiceType> ll = new LinkedList <ServiceType> (); 329 for (;;) { 330 NbMarshalledObject obj = (NbMarshalledObject) oos.readObject(); 331 332 if (obj == null) { 333 break; 334 } 335 try { 336 ServiceType s = (ServiceType) obj.get(); 337 338 ll.add(s); 339 } 340 catch (IOException ex) { 341 Logger.getLogger(Services.class.getName()).log(Level.WARNING, null, ex); 342 } 343 catch (ClassNotFoundException ex) { 344 Logger.getLogger(Services.class.getName()).log(Level.WARNING, null, ex); 345 } 346 } 347 348 getDefault ().setServiceTypes (ll); 349 } 350 351 352 private Object readResolve () { 353 return getDefault (); 354 } 355 } 356 | Popular Tags |