166 printServices = newServices;
167 }
168
169
170 public synchronized PrintService getPrintServiceByName(String name) {
171
172 if (name == null || name.equals("")) {
173 return null;
174 } else {
175 /* getPrintServices() is now very fast. */
176 PrintService[] printServices = getPrintServices();
177 for (int i=0; i<printServices.length; i++) {
178 if (printServices[i].getName().equals(name)) {
179 return printServices[i];
180 }
181 }
182 return null;
183 }
184 }
185
186 boolean matchingService(PrintService service,
187 PrintServiceAttributeSet serviceSet) {
188 if (serviceSet != null) {
189 Attribute [] attrs = serviceSet.toArray();
190 Attribute serviceAttr;
191 for (int i=0; i<attrs.length; i++) {
192 serviceAttr
193 = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
194 if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
195 return false;
196 }
197 }
198 }
199 return true;
200 }
201
202 public PrintService[] getPrintServices(DocFlavor flavor,
203 AttributeSet attributes) {
204
205 SecurityManager security = System.getSecurityManager();
229 * (by name) then we can save time by getting just that service
230 * to check against the rest of the specified attributes.
231 */
232 PrintService[] services = null;
233 if (serviceSet != null && serviceSet.get(PrinterName.class) != null) {
234 PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
235 PrintService service = getPrintServiceByName(name.getValue());
236 if (service == null || !matchingService(service, serviceSet)) {
237 services = new PrintService[0];
238 } else {
239 services = new PrintService[1];
240 services[0] = service;
241 }
242 } else {
243 services = getPrintServices();
244 }
245
246 if (services.length == 0) {
247 return services;
248 } else {
249 ArrayList matchingServices = new ArrayList();
250 for (int i=0; i<services.length; i++) {
251 try {
252 if (services[i].
253 getUnsupportedAttributes(flavor, requestSet) == null) {
254 matchingServices.add(services[i]);
255 }
256 } catch (IllegalArgumentException e) {
257 }
258 }
259 services = new PrintService[matchingServices.size()];
260 return (PrintService[])matchingServices.toArray(services);
261 }
262 }
263
264 /*
265 * return empty array as don't support multi docs
266 */
267 public MultiDocPrintService[]
268 getMultiDocPrintServices(DocFlavor[] flavors,
269 AttributeSet attributes) {
270 SecurityManager security = System.getSecurityManager();
271 if (security != null) {
272 security.checkPrintJobAccess();
273 }
274 return new MultiDocPrintService[0];
275 }
276
277
278 public synchronized PrintService getDefaultPrintService() {
279 SecurityManager security = System.getSecurityManager();
280 if (security != null) {
|
166 printServices = newServices;
167 }
168
169
170 public synchronized PrintService getPrintServiceByName(String name) {
171
172 if (name == null || name.equals("")) {
173 return null;
174 } else {
175 /* getPrintServices() is now very fast. */
176 PrintService[] printServices = getPrintServices();
177 for (int i=0; i<printServices.length; i++) {
178 if (printServices[i].getName().equals(name)) {
179 return printServices[i];
180 }
181 }
182 return null;
183 }
184 }
185
186 @SuppressWarnings("unchecked") // Cast to Class<PrintServiceAttribute>
187 boolean matchingService(PrintService service,
188 PrintServiceAttributeSet serviceSet) {
189 if (serviceSet != null) {
190 Attribute [] attrs = serviceSet.toArray();
191 Attribute serviceAttr;
192 for (int i=0; i<attrs.length; i++) {
193 serviceAttr
194 = service.getAttribute((Class<PrintServiceAttribute>)attrs[i].getCategory());
195 if (serviceAttr == null || !serviceAttr.equals(attrs[i])) {
196 return false;
197 }
198 }
199 }
200 return true;
201 }
202
203 public PrintService[] getPrintServices(DocFlavor flavor,
204 AttributeSet attributes) {
205
206 SecurityManager security = System.getSecurityManager();
230 * (by name) then we can save time by getting just that service
231 * to check against the rest of the specified attributes.
232 */
233 PrintService[] services = null;
234 if (serviceSet != null && serviceSet.get(PrinterName.class) != null) {
235 PrinterName name = (PrinterName)serviceSet.get(PrinterName.class);
236 PrintService service = getPrintServiceByName(name.getValue());
237 if (service == null || !matchingService(service, serviceSet)) {
238 services = new PrintService[0];
239 } else {
240 services = new PrintService[1];
241 services[0] = service;
242 }
243 } else {
244 services = getPrintServices();
245 }
246
247 if (services.length == 0) {
248 return services;
249 } else {
250 ArrayList<PrintService> matchingServices = new ArrayList<>();
251 for (int i=0; i<services.length; i++) {
252 try {
253 if (services[i].
254 getUnsupportedAttributes(flavor, requestSet) == null) {
255 matchingServices.add(services[i]);
256 }
257 } catch (IllegalArgumentException e) {
258 }
259 }
260 services = new PrintService[matchingServices.size()];
261 return matchingServices.toArray(services);
262 }
263 }
264
265 /*
266 * return empty array as don't support multi docs
267 */
268 public MultiDocPrintService[]
269 getMultiDocPrintServices(DocFlavor[] flavors,
270 AttributeSet attributes) {
271 SecurityManager security = System.getSecurityManager();
272 if (security != null) {
273 security.checkPrintJobAccess();
274 }
275 return new MultiDocPrintService[0];
276 }
277
278
279 public synchronized PrintService getDefaultPrintService() {
280 SecurityManager security = System.getSecurityManager();
281 if (security != null) {
|