Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/print/ServiceNotifier.java
38829 views
/*1* Copyright (c) 2000, 2001, 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 listeners;46private boolean stop = false;47private PrintServiceAttributeSet lastSet;4849ServiceNotifier(PrintService service) {50super(service.getName() + " notifier");51this.service = service;52listeners = new Vector();53try {54setPriority(Thread.NORM_PRIORITY-1);55setDaemon(true);56start();57} catch (SecurityException e) {58}59}6061void addListener(PrintServiceAttributeListener listener) {62synchronized (this) {63if (listener == null || listeners == null) {64return;65}66listeners.add(listener);67}68}6970void removeListener(PrintServiceAttributeListener listener) {71synchronized (this) {72if (listener == null || listeners == null) {73return;74}75listeners.remove(listener);76}77}7879boolean isEmpty() {80return (listeners == null || listeners.isEmpty());81}8283void stopNotifier() {84stop = true;85}8687/* If a service submits a job it may call this method which may prompt88* immediate notification of listeners.89*/90void wake() {91try {92interrupt();93} catch (SecurityException e) {94}95}9697/* A heuristic is used to calculate sleep time.98* 10 times the time taken to loop through all the listeners, with99* a minimum of 15 seconds. Ensures this won't take more than 10%100* of available time.101*/102public void run() {103104long minSleepTime = 15000;105long sleepTime = 2000;106HashPrintServiceAttributeSet attrs;107PrintServiceAttributeEvent attrEvent;108PrintServiceAttributeListener listener;109PrintServiceAttributeSet psa;110111while (!stop) {112try {113Thread.sleep(sleepTime);114} catch (InterruptedException e) {115}116synchronized (this) {117if (listeners == null) {118continue;119}120long startTime = System.currentTimeMillis();121if (listeners != null) {122if (service instanceof AttributeUpdater) {123psa =124((AttributeUpdater)service).getUpdatedAttributes();125} else {126psa = service.getAttributes();127}128if (psa != null && !psa.isEmpty()) {129for (int i = 0; i < listeners.size() ; i++) {130listener = (PrintServiceAttributeListener)131listeners.elementAt(i);132attrs =133new HashPrintServiceAttributeSet(psa);134attrEvent =135new PrintServiceAttributeEvent(service, attrs);136listener.attributeUpdate(attrEvent);137}138}139}140sleepTime = (System.currentTimeMillis()-startTime)*10;141if (sleepTime < minSleepTime) {142sleepTime = minSleepTime;143}144}145}146}147148}149150151