1 7 8 9 package javax.print; 10 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import javax.print.attribute.AttributeSet ; 15 16 import sun.awt.AppContext; 17 import sun.misc.Service; 18 19 50 51 public abstract class PrintServiceLookup { 52 53 static class Services { 54 private ArrayList listOfLookupServices = null; 55 private ArrayList registeredServices = null; 56 } 57 58 private static Services getServicesForContext() { 59 Services services = 60 (Services)AppContext.getAppContext().get(Services.class); 61 if (services == null) { 62 services = new Services(); 63 AppContext.getAppContext().put(Services.class, services); 64 } 65 return services; 66 } 67 68 private static ArrayList getListOfLookupServices() { 69 return getServicesForContext().listOfLookupServices; 70 } 71 72 private static ArrayList initListOfLookupServices() { 73 ArrayList listOfLookupServices = new ArrayList (); 74 getServicesForContext().listOfLookupServices = listOfLookupServices; 75 return listOfLookupServices; 76 } 77 78 79 private static ArrayList getRegisteredServices() { 80 return getServicesForContext().registeredServices; 81 } 82 83 private static ArrayList initRegisteredServices() { 84 ArrayList registeredServices = new ArrayList (); 85 getServicesForContext().registeredServices = registeredServices; 86 return registeredServices; 87 } 88 89 102 public static final PrintService [] 103 lookupPrintServices(DocFlavor flavor, 104 AttributeSet attributes) { 105 ArrayList list = getServices(flavor, attributes); 106 return (PrintService [])(list.toArray(new PrintService [list.size()])); 107 } 108 109 110 130 public static final MultiDocPrintService [] 131 lookupMultiDocPrintServices(DocFlavor [] flavors, 132 AttributeSet attributes) { 133 ArrayList list = getMultiDocServices(flavors, attributes); 134 return (MultiDocPrintService []) 135 list.toArray(new MultiDocPrintService [list.size()]); 136 } 137 138 139 159 160 public static final PrintService lookupDefaultPrintService() { 161 162 Iterator psIterator = getAllLookupServices().iterator(); 163 while (psIterator.hasNext()) { 164 try { 165 PrintServiceLookup lus = (PrintServiceLookup )psIterator.next(); 166 PrintService service = lus.getDefaultPrintService(); 167 if (service != null) { 168 return service; 169 } 170 } catch (Exception e) { 171 } 172 } 173 return null; 174 } 175 176 177 191 public static boolean registerServiceProvider(PrintServiceLookup sp) { 192 synchronized (PrintServiceLookup .class) { 193 Iterator psIterator = getAllLookupServices().iterator(); 194 while (psIterator.hasNext()) { 195 try { 196 Object lus = psIterator.next(); 197 if (lus.getClass() == sp.getClass()) { 198 return false; 199 } 200 } catch (Exception e) { 201 } 202 } 203 getListOfLookupServices().add(sp); 204 return true; 205 } 206 207 } 208 209 210 228 229 public static boolean registerService(PrintService service) { 230 synchronized (PrintServiceLookup .class) { 231 if (service instanceof StreamPrintService ) { 232 return false; 233 } 234 ArrayList registeredServices = getRegisteredServices(); 235 if (registeredServices == null) { 236 registeredServices = initRegisteredServices(); 237 } 238 else { 239 if (registeredServices.contains(service)) { 240 return false; 241 } 242 } 243 registeredServices.add(service); 244 return true; 245 } 246 } 247 248 249 269 public abstract PrintService [] getPrintServices(DocFlavor flavor, 270 AttributeSet attributes); 271 272 279 public abstract PrintService [] getPrintServices() ; 280 281 282 298 public abstract MultiDocPrintService [] 299 getMultiDocPrintServices(DocFlavor [] flavors, 300 AttributeSet attributes); 301 302 309 public abstract PrintService getDefaultPrintService(); 310 311 private static ArrayList getAllLookupServices() { 312 synchronized (PrintServiceLookup .class) { 313 ArrayList listOfLookupServices = getListOfLookupServices(); 314 if (listOfLookupServices != null) { 315 return listOfLookupServices; 316 } else { 317 listOfLookupServices = initListOfLookupServices(); 318 } 319 try { 320 java.security.AccessController.doPrivileged( 321 new java.security.PrivilegedExceptionAction () { 322 public Object run() { 323 Iterator iterator = 324 Service.providers(PrintServiceLookup .class); 325 ArrayList los = getListOfLookupServices(); 326 while (iterator.hasNext()) { 327 try { 328 PrintServiceLookup lus = 329 (PrintServiceLookup )iterator.next(); 330 los.add(lus); 331 } catch (Exception e) { 332 } 333 } 334 return null; 335 } 336 }); 337 } catch (java.security.PrivilegedActionException e) { 338 } 339 340 return listOfLookupServices; 341 } 342 } 343 344 private static ArrayList getServices(DocFlavor flavor, 345 AttributeSet attributes) { 346 347 ArrayList listOfServices = new ArrayList (); 348 Iterator psIterator = getAllLookupServices().iterator(); 349 while (psIterator.hasNext()) { 350 try { 351 PrintServiceLookup lus = (PrintServiceLookup )psIterator.next(); 352 PrintService [] services=null; 353 if (flavor == null && attributes == null) { 354 try { 355 services = lus.getPrintServices(); 356 } catch (Throwable tr) { 357 } 358 } else { 359 services = lus.getPrintServices(flavor, attributes); 360 } 361 if (services == null) { 362 continue; 363 } 364 for (int i=0; i<services.length; i++) { 365 listOfServices.add(services[i]); 366 } 367 } catch (Exception e) { 368 } 369 } 370 371 ArrayList registeredServices = null; 372 try { 373 SecurityManager security = System.getSecurityManager(); 374 if (security != null) { 375 security.checkPrintJobAccess(); 376 } 377 registeredServices = getRegisteredServices(); 378 } catch (SecurityException se) { 379 } 380 if (registeredServices != null) { 381 PrintService [] services = (PrintService []) 382 registeredServices.toArray( 383 new PrintService [registeredServices.size()]); 384 for (int i=0; i<services.length; i++) { 385 if (!listOfServices.contains(services[i])) { 386 if (flavor == null && attributes == null) { 387 listOfServices.add(services[i]); 388 } else if (((flavor != null && 389 services[i].isDocFlavorSupported(flavor)) || 390 flavor == null) && 391 null == services[i].getUnsupportedAttributes( 392 flavor, attributes)) { 393 listOfServices.add(services[i]); 394 } 395 } 396 } 397 } 398 return listOfServices; 399 } 400 401 private static ArrayList getMultiDocServices(DocFlavor [] flavors, 402 AttributeSet attributes) { 403 404 405 ArrayList listOfServices = new ArrayList (); 406 Iterator psIterator = getAllLookupServices().iterator(); 407 while (psIterator.hasNext()) { 408 try { 409 PrintServiceLookup lus = (PrintServiceLookup )psIterator.next(); 410 MultiDocPrintService [] services = 411 lus.getMultiDocPrintServices(flavors, attributes); 412 if (services == null) { 413 continue; 414 } 415 for (int i=0; i<services.length; i++) { 416 listOfServices.add(services[i]); 417 } 418 } catch (Exception e) { 419 } 420 } 421 422 ArrayList registeredServices = null; 423 try { 424 SecurityManager security = System.getSecurityManager(); 425 if (security != null) { 426 security.checkPrintJobAccess(); 427 } 428 registeredServices = getRegisteredServices(); 429 } catch (Exception e) { 430 } 431 if (registeredServices != null) { 432 PrintService [] services = (PrintService []) 433 registeredServices.toArray( 434 new PrintService [registeredServices.size()]); 435 for (int i=0; i<services.length; i++) { 436 if (services[i] instanceof MultiDocPrintService && 437 !listOfServices.contains(services[i])) { 438 if (flavors == null || flavors.length == 0) { 439 listOfServices.add(services[i]); 440 } else { 441 boolean supported = true; 442 for (int f=0; f<flavors.length; f++) { 443 if (services[i].isDocFlavorSupported(flavors[f])) { 444 445 if (services[i].getUnsupportedAttributes( 446 flavors[f], attributes) != null) { 447 supported = false; 448 break; 449 } 450 } else { 451 supported = false; 452 break; 453 } 454 } 455 if (supported) { 456 listOfServices.add(services[i]); 457 } 458 } 459 } 460 } 461 } 462 return listOfServices; 463 } 464 465 } 466 467 | Popular Tags |