Path: blob/master/src/java.desktop/share/classes/sun/print/ServiceNotifier.java
41153 views
/*1* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.print;2627import java.util.Vector;2829import javax.print.PrintService;30import javax.print.attribute.PrintServiceAttributeSet;31import javax.print.attribute.HashPrintServiceAttributeSet;32import javax.print.event.PrintServiceAttributeEvent;33import javax.print.event.PrintServiceAttributeListener;3435/*36* A utility class usable by all print services for managing listeners37* The services create an instance and delegate the listener callback38* management to this class. The ServiceNotifier calls back to the service39* to obtain the state of the attributes and notifies the listeners of40* any changes.41*/42class ServiceNotifier extends Thread {4344private PrintService service;45private Vector<PrintServiceAttributeListener> listeners;46private boolean stop = false;47private PrintServiceAttributeSet lastSet;4849/*50* If adding any other constructors, always call the 5-args51* super-class constructor passing "false" for inherit-locals.52*/53ServiceNotifier(PrintService service) {54super(null, null, service.getName() + " notifier", 0, false);55this.service = service;56listeners = new Vector<>();57try {58setPriority(Thread.NORM_PRIORITY-1);59setDaemon(true);60start();61} catch (SecurityException e) {62}63}6465void addListener(PrintServiceAttributeListener listener) {66synchronized (this) {67if (listener == null || listeners == null) {68return;69}70listeners.add(listener);71}72}7374void removeListener(PrintServiceAttributeListener listener) {75synchronized (this) {76if (listener == null || listeners == null) {77return;78}79listeners.remove(listener);80}81}8283boolean isEmpty() {84return (listeners == null || listeners.isEmpty());85}8687void stopNotifier() {88stop = true;89}9091/* If a service submits a job it may call this method which may prompt92* immediate notification of listeners.93*/94void wake() {95try {96interrupt();97} catch (SecurityException e) {98}99}100101/* A heuristic is used to calculate sleep time.102* 10 times the time taken to loop through all the listeners, with103* a minimum of 15 seconds. Ensures this won't take more than 10%104* of available time.105*/106public void run() {107108long minSleepTime = 15000;109long sleepTime = 2000;110HashPrintServiceAttributeSet attrs;111PrintServiceAttributeEvent attrEvent;112PrintServiceAttributeListener listener;113PrintServiceAttributeSet psa;114115while (!stop) {116try {117Thread.sleep(sleepTime);118} catch (InterruptedException e) {119}120synchronized (this) {121if (listeners == null) {122continue;123}124long startTime = System.currentTimeMillis();125if (listeners != null) {126if (service instanceof AttributeUpdater) {127psa =128((AttributeUpdater)service).getUpdatedAttributes();129} else {130psa = service.getAttributes();131}132if (psa != null && !psa.isEmpty()) {133for (int i = 0; i < listeners.size() ; i++) {134listener = listeners.elementAt(i);135attrs =136new HashPrintServiceAttributeSet(psa);137attrEvent =138new PrintServiceAttributeEvent(service, attrs);139listener.attributeUpdate(attrEvent);140}141}142}143sleepTime = (System.currentTimeMillis()-startTime)*10;144if (sleepTime < minSleepTime) {145sleepTime = minSleepTime;146}147}148}149}150151}152153154