blob: f609e66df2f28ce2678fc6c4bcca603499a2c8ab [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.print;
27
28import java.util.Vector;
29
30import javax.print.PrintService;
31import javax.print.attribute.PrintServiceAttributeSet;
32import javax.print.attribute.HashPrintServiceAttributeSet;
33import javax.print.event.PrintServiceAttributeEvent;
34import javax.print.event.PrintServiceAttributeListener;
35
36/*
37 * A utility class usable by all print services for managing listeners
38 * The services create an instance and delegate the listener callback
39 * management to this class. The ServiceNotifier calls back to the service
40 * to obtain the state of the attributes and notifies the listeners of
41 * any changes.
42 */
43class ServiceNotifier extends Thread {
44
45 private PrintService service;
46 private Vector listeners;
47 private boolean stop = false;
48 private PrintServiceAttributeSet lastSet;
49
50 ServiceNotifier(PrintService service) {
51 super(service.getName() + " notifier");
52 this.service = service;
53 listeners = new Vector();
54 try {
55 setPriority(Thread.NORM_PRIORITY-1);
56 setDaemon(true);
57 start();
58 } catch (SecurityException e) {
59 }
60 }
61
62 void addListener(PrintServiceAttributeListener listener) {
63 synchronized (this) {
64 if (listener == null || listeners == null) {
65 return;
66 }
67 listeners.add(listener);
68 }
69 }
70
71 void removeListener(PrintServiceAttributeListener listener) {
72 synchronized (this) {
73 if (listener == null || listeners == null) {
74 return;
75 }
76 listeners.remove(listener);
77 }
78 }
79
80 boolean isEmpty() {
81 return (listeners == null || listeners.isEmpty());
82 }
83
84 void stopNotifier() {
85 stop = true;
86 }
87
88 /* If a service submits a job it may call this method which may prompt
89 * immediate notification of listeners.
90 */
91 void wake() {
92 try {
93 interrupt();
94 } catch (SecurityException e) {
95 }
96 }
97
98 /* A heuristic is used to calculate sleep time.
99 * 10 times the time taken to loop through all the listeners, with
100 * a minimum of 15 seconds. Ensures this won't take more than 10%
101 * of available time.
102 */
103 public void run() {
104
105 long minSleepTime = 15000;
106 long sleepTime = 2000;
107 HashPrintServiceAttributeSet attrs;
108 PrintServiceAttributeEvent attrEvent;
109 PrintServiceAttributeListener listener;
110 PrintServiceAttributeSet psa;
111
112 while (!stop) {
113 try {
114 Thread.sleep(sleepTime);
115 } catch (InterruptedException e) {
116 }
117 synchronized (this) {
118 if (listeners == null) {
119 continue;
120 }
121 long startTime = System.currentTimeMillis();
122 if (listeners != null) {
123 if (service instanceof AttributeUpdater) {
124 psa =
125 ((AttributeUpdater)service).getUpdatedAttributes();
126 } else {
127 psa = service.getAttributes();
128 }
129 if (psa != null && !psa.isEmpty()) {
130 for (int i = 0; i < listeners.size() ; i++) {
131 listener = (PrintServiceAttributeListener)
132 listeners.elementAt(i);
133 attrs =
134 new HashPrintServiceAttributeSet(psa);
135 attrEvent =
136 new PrintServiceAttributeEvent(service, attrs);
137 listener.attributeUpdate(attrEvent);
138 }
139 }
140 }
141 sleepTime = (System.currentTimeMillis()-startTime)*10;
142 if (sleepTime < minSleepTime) {
143 sleepTime = minSleepTime;
144 }
145 }
146 }
147 }
148
149}