1 7 package com.sun.jmx.remote.util; 8 9 import java.io.BufferedReader ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.InputStreamReader ; 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 import java.util.Enumeration ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.NoSuchElementException ; 19 import java.util.Set ; 20 import java.util.TreeSet ; 21 22 23 import java.security.AccessController ; 24 import java.security.PrivilegedAction ; 25 26 110 111 public final class Service { 112 113 private static final String prefix = "META-INF/services/"; 114 115 private Service() { } 116 117 private static void fail(Class service, String msg, Throwable cause) 118 throws IllegalArgumentException 119 { 120 IllegalArgumentException sce 121 = new IllegalArgumentException (service.getName() + ": " + msg); 122 123 throw (IllegalArgumentException ) EnvHelp.initCause(sce, cause); 124 } 125 126 private static void fail(Class service, String msg) 127 throws IllegalArgumentException 128 { 129 throw new IllegalArgumentException (service.getName() + ": " + msg); 130 } 131 132 private static void fail(Class service, URL u, int line, String msg) 133 throws IllegalArgumentException 134 { 135 fail(service, u + ":" + line + ": " + msg); 136 } 137 138 143 private static int parseLine(Class service, URL u, BufferedReader r, int lc, 144 List names, Set returned) 145 throws IOException , IllegalArgumentException 146 { 147 String ln = r.readLine(); 148 if (ln == null) { 149 return -1; 150 } 151 int ci = ln.indexOf('#'); 152 if (ci >= 0) ln = ln.substring(0, ci); 153 ln = ln.trim(); 154 int n = ln.length(); 155 if (n != 0) { 156 if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) 157 fail(service, u, lc, "Illegal configuration-file syntax"); 158 if (!Character.isJavaIdentifierStart(ln.charAt(0))) 159 fail(service, u, lc, "Illegal provider-class name: " + ln); 160 for (int i = 1; i < n; i++) { 161 char c = ln.charAt(i); 162 if (!Character.isJavaIdentifierPart(c) && (c != '.')) 163 fail(service, u, lc, "Illegal provider-class name: " + ln); 164 } 165 if (!returned.contains(ln)) { 166 names.add(ln); 167 returned.add(ln); 168 } 169 } 170 return lc + 1; 171 } 172 173 196 private static Iterator parse(Class service, URL u, Set returned) 197 throws IllegalArgumentException 198 { 199 InputStream in = null; 200 BufferedReader r = null; 201 ArrayList names = new ArrayList (); 202 try { 203 in = u.openStream(); 204 r = new BufferedReader (new InputStreamReader (in, "utf-8")); 205 int lc = 1; 206 while ((lc = parseLine(service, u, r, lc, names, returned)) >= 0); 207 } catch (IOException x) { 208 fail(service, ": " + x); 209 } finally { 210 try { 211 if (r != null) r.close(); 212 if (in != null) in.close(); 213 } catch (IOException y) { 214 fail(service, ": " + y); 215 } 216 } 217 return names.iterator(); 218 } 219 220 221 224 private static class LazyIterator implements Iterator { 225 226 Class service; 227 ClassLoader loader; 228 Enumeration configs = null; 229 Iterator pending = null; 230 Set returned = new TreeSet (); 231 String nextName = null; 232 233 private LazyIterator(Class service, ClassLoader loader) { 234 this.service = service; 235 this.loader = loader; 236 } 237 238 public boolean hasNext() throws IllegalArgumentException { 239 if (nextName != null) { 240 return true; 241 } 242 if (configs == null) { 243 try { 244 String fullName = prefix + service.getName(); 245 if (loader == null) 246 configs = ClassLoader.getSystemResources(fullName); 247 else 248 configs = loader.getResources(fullName); 249 } catch (IOException x) { 250 fail(service, ": " + x); 251 } 252 } 253 while ((pending == null) || !pending.hasNext()) { 254 if (!configs.hasMoreElements()) { 255 return false; 256 } 257 pending = parse(service, (URL )configs.nextElement(), returned); 258 } 259 nextName = (String )pending.next(); 260 return true; 261 } 262 263 public Object next() throws IllegalArgumentException { 264 if (!hasNext()) { 265 throw new NoSuchElementException (); 266 } 267 String cn = nextName; 268 nextName = null; 269 try { 270 return Class.forName(cn, true, loader).newInstance(); 271 } catch (ClassNotFoundException x) { 272 fail(service, 273 "Provider " + cn + " not found"); 274 } catch (Exception x) { 275 fail(service, 276 "Provider " + cn + " could not be instantiated: " + x, 277 x); 278 } 279 return null; 280 } 281 282 public void remove() { 283 throw new UnsupportedOperationException (); 284 } 285 286 } 287 288 289 325 public static Iterator providers(Class service, ClassLoader loader) 326 throws IllegalArgumentException 327 { 328 return new LazyIterator(service, loader); 329 } 330 } 331 | Popular Tags |