1 22 23 package org.gjt.sp.jedit; 24 25 import java.io.*; 26 import java.net.URL ; 27 import java.util.*; 28 import org.gjt.sp.util.Log; 29 import org.gjt.sp.util.XMLUtilities; 30 31 85 public class ServiceManager 86 { 87 92 public static void loadServices(PluginJAR plugin, URL uri, 93 PluginJAR.PluginCacheEntry cache) 94 { 95 ServiceListHandler dh = new ServiceListHandler(plugin,uri); 96 try 97 { 98 if (!XMLUtilities.parseXML(uri.openStream(), dh) 99 && cache != null) 100 { 101 cache.cachedServices = dh.getCachedServices(); 102 } 103 } 104 catch (IOException ioe) 105 { 106 Log.log(Log.ERROR, ServiceManager.class, ioe); 107 } 108 } 110 116 public static void unloadServices(PluginJAR plugin) 117 { 118 Iterator<Descriptor> descriptors = serviceMap.keySet().iterator(); 119 while(descriptors.hasNext()) 120 { 121 Descriptor d = descriptors.next(); 122 if(d.plugin == plugin) 123 descriptors.remove(); 124 } 125 } 127 139 public static void registerService(String clazz, String name, 140 String code, PluginJAR plugin) 141 { 142 Descriptor d = new Descriptor(clazz,name,code,plugin); 143 serviceMap.put(d,d); 144 } 146 155 public static void unregisterService(String clazz, String name) 156 { 157 Descriptor d = new Descriptor(clazz,name); 158 serviceMap.remove(d); 159 } 161 167 public static String [] getServiceTypes() 168 { 169 Set<String > returnValue = new HashSet<String >(); 170 171 Set<Descriptor> keySet = serviceMap.keySet(); 172 for (Descriptor d : keySet) 173 returnValue.add(d.clazz); 174 175 return returnValue.toArray( 176 new String [returnValue.size()]); 177 } 179 189 public static String [] getServiceNames(String clazz) 190 { 191 List<String > returnValue = new ArrayList<String >(); 192 193 Set<Descriptor> keySet = serviceMap.keySet(); 194 for (Descriptor d : keySet) 195 if(d.clazz.equals(clazz)) 196 returnValue.add(d.name); 197 198 199 return returnValue.toArray( 200 new String [returnValue.size()]); 201 } 203 214 public static Object getService(String clazz, String name) 215 { 216 Descriptor key = new Descriptor(clazz,name); 218 Descriptor value = serviceMap.get(key); 219 if(value == null) 220 { 221 return null; 223 } 224 else 225 { 226 if(value.code == null) 227 { 228 loadServices(value.plugin, 229 value.plugin.getServicesURI(), 230 null); 231 value = serviceMap.get(key); 232 } 233 return value.getInstance(); 234 } 235 } 237 239 245 static void registerService(Descriptor d) 246 { 247 serviceMap.put(d,d); 248 } 250 252 private static final Map<Descriptor, Descriptor> serviceMap = new HashMap<Descriptor, Descriptor>(); 254 256 static class Descriptor 258 { 259 final String clazz; 260 final String name; 261 String code; 262 PluginJAR plugin; 263 Object instance; 264 boolean instanceIsNull; 265 266 Descriptor(String clazz, String name) 268 { 269 this.clazz = clazz; 270 this.name = name; 271 } 272 273 Descriptor(String clazz, String name, String code, 275 PluginJAR plugin) 276 { 277 this.clazz = clazz; 278 this.name = name; 279 this.code = code; 280 this.plugin = plugin; 281 } 282 283 Object getInstance() 284 { 285 if(instanceIsNull) 286 return null; 287 else if(instance == null) 288 { 289 instance = BeanShell.eval(null, 291 BeanShell.getNameSpace(), 292 code); 293 if(instance == null) 294 { 295 instanceIsNull = true; 298 } 299 } 300 301 return instance; 302 } 303 public int hashCode() 304 { 305 return name.hashCode(); 306 } 307 308 public boolean equals(Object o) 309 { 310 if(o instanceof Descriptor) 311 { 312 Descriptor d = (Descriptor)o; 313 return d.clazz.equals(clazz) 314 && d.name.equals(name); 315 } 316 else 317 return false; 318 } 319 } } 321 | Popular Tags |