96 97 private static ArrayList<PrintService> getRegisteredServices() { 98 return getServicesForContext().registeredServices; 99 } 100 101 private static ArrayList<PrintService> initRegisteredServices() { 102 ArrayList<PrintService> registeredServices = new ArrayList<>(); 103 getServicesForContext().registeredServices = registeredServices; 104 return registeredServices; 105 } 106 107 /** 108 * Locates print services capable of printing the specified 109 * {@link DocFlavor}. 110 * 111 * @param flavor the flavor to print. If null, this constraint is not 112 * used. 113 * @param attributes attributes that the print service must support. 114 * If null this constraint is not used. 115 * 116 * @return array of matching <code>PrintService</code> objects 117 * representing print services that support the specified flavor 118 * attributes. If no services match, the array is zero-length. 119 */ 120 public static final PrintService[] 121 lookupPrintServices(DocFlavor flavor, 122 AttributeSet attributes) { 123 ArrayList<PrintService> list = getServices(flavor, attributes); 124 return list.toArray(new PrintService[list.size()]); 125 } 126 127 128 /** 129 * Locates MultiDoc print Services capable of printing MultiDocs 130 * containing all the specified doc flavors. 131 * <P> This method is useful to help locate a service that can print 132 * a <code>MultiDoc</code> in which the elements may be different 133 * flavors. An application could perform this itself by multiple lookups 134 * on each <code>DocFlavor</code> in turn and collating the results, 135 * but the lookup service may be able to do this more efficiently. 136 * 137 * @param flavors the flavors to print. If null or empty this 138 * constraint is not used. 139 * Otherwise return only multidoc print services that can print all 140 * specified doc flavors. 141 * @param attributes attributes that the print service must 142 * support. If null this constraint is not used. 143 * 144 * @return array of matching {@link MultiDocPrintService} objects. 145 * If no services match, the array is zero-length. 146 * 147 */ 148 public static final MultiDocPrintService[] 149 lookupMultiDocPrintServices(DocFlavor[] flavors, 150 AttributeSet attributes) { 151 ArrayList<MultiDocPrintService> list = getMultiDocServices(flavors, attributes); 152 return list.toArray(new MultiDocPrintService[list.size()]); 153 } 154 184 if (service != null) { 185 return service; 186 } 187 } catch (Exception e) { 188 } 189 } 190 return null; 191 } 192 193 194 /** 195 * Allows an application to explicitly register a class that 196 * implements lookup services. The registration will not persist 197 * across VM invocations. 198 * This is useful if an application needs to make a new service 199 * available that is not part of the installation. 200 * If the lookup service is already registered, or cannot be registered, 201 * the method returns false. 202 * 203 * @param sp an implementation of a lookup service. 204 * @return <code>true</code> if the new lookup service is newly 205 * registered; <code>false</code> otherwise. 206 */ 207 public static boolean registerServiceProvider(PrintServiceLookup sp) { 208 synchronized (PrintServiceLookup.class) { 209 Iterator<PrintServiceLookup> psIterator = 210 getAllLookupServices().iterator(); 211 while (psIterator.hasNext()) { 212 try { 213 Object lus = psIterator.next(); 214 if (lus.getClass() == sp.getClass()) { 215 return false; 216 } 217 } catch (Exception e) { 218 } 219 } 220 getListOfLookupServices().add(sp); 221 return true; 222 } 223 224 } 225 226 227 /** 228 * Allows an application to directly register an instance of a 229 * class which implements a print service. 230 * The lookup operations for this service will be 231 * performed by the PrintServiceLookup class using the attribute 232 * values and classes reported by the service. 233 * This may be less efficient than a lookup 234 * service tuned for that service. 235 * Therefore registering a <code>PrintServiceLookup</code> instance 236 * instead is recommended. 237 * The method returns true if this service is not previously 238 * registered and is now successfully registered. 239 * This method should not be called with StreamPrintService instances. 240 * They will always fail to register and the method will return false. 241 * @param service an implementation of a print service. 242 * @return <code>true</code> if the service is newly 243 * registered; <code>false</code> otherwise. 244 */ 245 246 public static boolean registerService(PrintService service) { 247 synchronized (PrintServiceLookup.class) { 248 if (service == null || service instanceof StreamPrintService) { 249 return false; 250 } 251 ArrayList<PrintService> registeredServices = getRegisteredServices(); 252 if (registeredServices == null) { 253 registeredServices = initRegisteredServices(); 254 } 255 else { 256 if (registeredServices.contains(service)) { 257 return false; 258 } 259 } 260 registeredServices.add(service); 261 return true; 262 } 263 } | 96 97 private static ArrayList<PrintService> getRegisteredServices() { 98 return getServicesForContext().registeredServices; 99 } 100 101 private static ArrayList<PrintService> initRegisteredServices() { 102 ArrayList<PrintService> registeredServices = new ArrayList<>(); 103 getServicesForContext().registeredServices = registeredServices; 104 return registeredServices; 105 } 106 107 /** 108 * Locates print services capable of printing the specified 109 * {@link DocFlavor}. 110 * 111 * @param flavor the flavor to print. If null, this constraint is not 112 * used. 113 * @param attributes attributes that the print service must support. 114 * If null this constraint is not used. 115 * 116 * @return array of matching {@code PrintService} objects 117 * representing print services that support the specified flavor 118 * attributes. If no services match, the array is zero-length. 119 */ 120 public static final PrintService[] 121 lookupPrintServices(DocFlavor flavor, 122 AttributeSet attributes) { 123 ArrayList<PrintService> list = getServices(flavor, attributes); 124 return list.toArray(new PrintService[list.size()]); 125 } 126 127 128 /** 129 * Locates MultiDoc print Services capable of printing MultiDocs 130 * containing all the specified doc flavors. 131 * <P> This method is useful to help locate a service that can print 132 * a {@code MultiDoc} in which the elements may be different 133 * flavors. An application could perform this itself by multiple lookups 134 * on each {@code DocFlavor} in turn and collating the results, 135 * but the lookup service may be able to do this more efficiently. 136 * 137 * @param flavors the flavors to print. If null or empty this 138 * constraint is not used. 139 * Otherwise return only multidoc print services that can print all 140 * specified doc flavors. 141 * @param attributes attributes that the print service must 142 * support. If null this constraint is not used. 143 * 144 * @return array of matching {@link MultiDocPrintService} objects. 145 * If no services match, the array is zero-length. 146 * 147 */ 148 public static final MultiDocPrintService[] 149 lookupMultiDocPrintServices(DocFlavor[] flavors, 150 AttributeSet attributes) { 151 ArrayList<MultiDocPrintService> list = getMultiDocServices(flavors, attributes); 152 return list.toArray(new MultiDocPrintService[list.size()]); 153 } 154 184 if (service != null) { 185 return service; 186 } 187 } catch (Exception e) { 188 } 189 } 190 return null; 191 } 192 193 194 /** 195 * Allows an application to explicitly register a class that 196 * implements lookup services. The registration will not persist 197 * across VM invocations. 198 * This is useful if an application needs to make a new service 199 * available that is not part of the installation. 200 * If the lookup service is already registered, or cannot be registered, 201 * the method returns false. 202 * 203 * @param sp an implementation of a lookup service. 204 * @return {@code true} if the new lookup service is newly 205 * registered; {@code false} otherwise. 206 */ 207 public static boolean registerServiceProvider(PrintServiceLookup sp) { 208 synchronized (PrintServiceLookup.class) { 209 Iterator<PrintServiceLookup> psIterator = 210 getAllLookupServices().iterator(); 211 while (psIterator.hasNext()) { 212 try { 213 Object lus = psIterator.next(); 214 if (lus.getClass() == sp.getClass()) { 215 return false; 216 } 217 } catch (Exception e) { 218 } 219 } 220 getListOfLookupServices().add(sp); 221 return true; 222 } 223 224 } 225 226 227 /** 228 * Allows an application to directly register an instance of a 229 * class which implements a print service. 230 * The lookup operations for this service will be 231 * performed by the PrintServiceLookup class using the attribute 232 * values and classes reported by the service. 233 * This may be less efficient than a lookup 234 * service tuned for that service. 235 * Therefore registering a {@code PrintServiceLookup} instance 236 * instead is recommended. 237 * The method returns true if this service is not previously 238 * registered and is now successfully registered. 239 * This method should not be called with StreamPrintService instances. 240 * They will always fail to register and the method will return false. 241 * @param service an implementation of a print service. 242 * @return {@code true} if the service is newly 243 * registered; {@code false} otherwise. 244 */ 245 246 public static boolean registerService(PrintService service) { 247 synchronized (PrintServiceLookup.class) { 248 if (service == null || service instanceof StreamPrintService) { 249 return false; 250 } 251 ArrayList<PrintService> registeredServices = getRegisteredServices(); 252 if (registeredServices == null) { 253 registeredServices = initRegisteredServices(); 254 } 255 else { 256 if (registeredServices.contains(service)) { 257 return false; 258 } 259 } 260 registeredServices.add(service); 261 return true; 262 } 263 } |