1 19 20 package org.openide; 21 22 import java.beans.Introspector ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.io.IOException ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.io.Serializable ; 29 import java.util.Collection ; 30 import java.util.Enumeration ; 31 import java.util.List ; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import org.openide.util.Enumerations; 35 import org.openide.util.Exceptions; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.Lookup; 38 39 48 @Deprecated 49 public abstract class ServiceType extends Object implements Serializable , HelpCtx.Provider { 50 51 private static final long serialVersionUID = -7573598174423654252L; 52 53 54 public static final String PROP_NAME = "name"; private static final Logger err = Logger.getLogger("org.openide.ServiceType"); 57 58 private String name; 59 60 61 private transient PropertyChangeSupport supp; 62 63 68 protected String displayName() { 69 try { 70 return Introspector.getBeanInfo(getClass()).getBeanDescriptor().getDisplayName(); 71 } catch (Exception e) { 72 Logger.getLogger(ServiceType.class.getName()).log(Level.WARNING, null, e); 74 75 return getClass().getName(); 76 } 77 } 78 79 90 @Deprecated 91 public final ServiceType createClone() { 92 Exception anEx; 93 94 if (this instanceof Cloneable ) { 95 try { 96 return (ServiceType) clone(); 97 } catch (CloneNotSupportedException ex) { 98 anEx = ex; 99 } 100 } else { 101 try { 102 org.openide.util.io.NbMarshalledObject m = new org.openide.util.io.NbMarshalledObject(this); 103 104 return (ServiceType) m.get(); 105 } catch (IOException ex) { 106 anEx = ex; 107 } catch (ClassNotFoundException ex) { 108 anEx = ex; 109 } 110 } 111 112 IllegalStateException ex = new IllegalStateException (); 115 116 ex.initCause(anEx); 117 Exceptions.attachLocalizedMessage(ex, "Cannot createClone for " + this); 119 throw ex; 120 } 121 122 129 @Deprecated 130 protected Object clone() throws CloneNotSupportedException { 131 ServiceType t = (ServiceType) super.clone(); 132 133 t.supp = null; 135 136 t.name = null; 138 139 return t; 140 } 141 142 147 public void setName(String name) { 148 String old = this.name; 149 this.name = name; 150 151 if (supp != null) { 152 supp.firePropertyChange(PROP_NAME, old, name); 153 } 154 } 155 156 160 public String getName() { 161 return (name == null) ? displayName() : name; 162 } 163 164 167 public abstract HelpCtx getHelpCtx(); 168 169 172 public final synchronized void addPropertyChangeListener(PropertyChangeListener l) { 173 if (supp == null) { 174 supp = new PropertyChangeSupport (this); 175 } 176 177 supp.addPropertyChangeListener(l); 178 } 179 180 183 public final void removePropertyChangeListener(PropertyChangeListener l) { 184 if (supp != null) { 185 supp.removePropertyChangeListener(l); 186 } 187 } 188 189 194 protected final void firePropertyChange(String name, Object o, Object n) { 195 if (supp != null) { 196 supp.firePropertyChange(name, o, n); 197 } 198 } 199 200 208 @Deprecated 209 public static abstract class Registry implements Serializable { 210 211 final static long serialVersionUID = 8721000770371416481L; 212 213 216 public abstract Enumeration <ServiceType> services(); 217 218 222 public <T extends ServiceType> Enumeration <T> services(final Class <T> clazz) { 223 class IsInstance implements Enumerations.Processor<ServiceType,T> { 224 public T process(ServiceType obj, Collection ignore) { 225 return clazz.isInstance(obj) ? clazz.cast(obj) : null; 226 } 227 } 228 229 return Enumerations.filter(services(), new IsInstance()); 230 } 231 232 235 public abstract List getServiceTypes(); 236 237 245 @Deprecated 246 public abstract void setServiceTypes(List arr); 247 248 259 @Deprecated 260 public ServiceType find(Class clazz) { 261 Enumeration en = services(); 262 263 while (en.hasMoreElements()) { 264 Object o = en.nextElement(); 265 266 if (o.getClass() == clazz) { 267 return (ServiceType) o; 268 } 269 } 270 271 return null; 272 } 273 274 283 public ServiceType find(String name) { 284 Enumeration en = services(); 285 286 while (en.hasMoreElements()) { 287 ServiceType o = (ServiceType) en.nextElement(); 288 289 if (name.equals(o.getName())) { 290 return o; 291 } 292 } 293 294 return null; 295 } 296 } 297 298 304 @Deprecated 305 public static final class Handle extends Object implements java.io.Serializable { 306 307 static final long serialVersionUID = 7233109534462148872L; 308 309 310 private String name; 311 312 313 private String className; 314 315 316 private transient ServiceType serviceType; 317 318 321 public Handle(ServiceType ex) { 322 name = ex.getName(); 323 className = ex.getClass().getName(); 324 serviceType = ex; 325 } 326 327 330 public ServiceType getServiceType() { 331 if (serviceType == null) { 332 Class <? extends ServiceType> clazz; 334 335 Class <?> serviceTypeClass; 337 338 try { 340 serviceTypeClass = Class.forName(className, true, Lookup.getDefault().lookup(ClassLoader .class)); 341 clazz = serviceTypeClass.asSubclass(ServiceType.class); 342 343 while (serviceTypeClass.getSuperclass() != ServiceType.class) { 344 serviceTypeClass = serviceTypeClass.getSuperclass(); 345 } 346 } catch (ClassNotFoundException ex) { 347 err.log(Level.FINE, "Service not found", ex); 352 clazz = ServiceType.class; 354 serviceTypeClass = ServiceType.class; 355 } 356 357 ServiceType.Registry r = Lookup.getDefault().lookup(ServiceType.Registry.class); 359 Enumeration en = r.services(clazz); 360 ServiceType some = r.find(clazz); 361 362 while (en.hasMoreElements()) { 363 ServiceType t = (ServiceType) en.nextElement(); 364 365 if (!serviceTypeClass.isInstance(t)) { 366 continue; 368 } 369 370 String n = t.getName(); 371 372 if ((n != null) && n.equals(name)) { 373 return t; 374 } 375 376 if ((some == null) || ((some.getClass() != clazz) && (t.getClass() == clazz))) { 378 some = t; 380 } 381 } 382 383 if (serviceTypeClass == ServiceType.class) { 385 return null; 386 } 387 388 return some; 389 } 390 391 return serviceType; 392 } 393 394 396 private void readObject(ObjectInputStream ois) 397 throws IOException , ClassNotFoundException { 398 name = (String ) ois.readObject(); 399 400 String clazz = (String ) ois.readObject(); 401 className = (clazz == null) ? null : org.openide.util.Utilities.translate(clazz); 402 } 403 404 406 private void writeObject(ObjectOutputStream oos) 407 throws IOException { 408 oos.writeObject(name); 409 oos.writeObject(className); 410 } 411 412 public String toString() { 414 return "Handle[" + className + ":" + name + "]"; } 416 } 417 } 418 | Popular Tags |