1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
|
import java.rmi.RemoteException;
import java.util.Locale;
import javax.print.attribute.Attribute;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintJobAttributeSet;
import javax.print.attribute.PrintRequestAttribute;
import javax.print.attribute.standard.DocumentName;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.JobState;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaColor;
import javax.print.attribute.standard.MediaKind;
import javax.print.attribute.standard.MediaOpacity;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.exception.PrintException;
import net.jini.core.entry.Entry;
import net.jini.core.lease.Lease;
import net.jini.core.lease.UnknownLeaseException;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.lease.LeaseRenewalManager;
import net.jini.print.job.PrintJob;
import net.jini.print.job.PrintJobAndLease;
import net.jini.print.job.PrintRequest;
import net.jini.print.lookup.AttributeEntry;
import net.jini.print.lookup.FlavorEntry;
import net.jini.print.lookup.MediaEntry;
import net.jini.print.service.PrintService;
import org.jpwg.jini.print.data.StringDoc;
. . .
try
{
LeaseRenewalManager theLRM = new LeaseRenewalManager();
Media theMedia = new Media
(new Attribute[]
{MediaSize.NA.LETTER,
MediaColor.PINK,
MediaOpacity.OPAQUE,
MediaKind.SHEET});
StringDoc theDoc =
new StringDoc
("Hello, world!",
new HashDocAttributeSet
(new DocumentName
("Test String",
Locale.getDefault())));
ServiceRegistrar theJLUS = // Obtain a Jini Lookup Service (JLUS) proxy
ServiceTemplate theServiceTemplate =
new ServiceTemplate
(null,
new Class[] {PrintService.class},
new Entry[]
{new FlavorEntry (theDoc.getDocFlavor()),
new MediaEntry (theMedia),
new AttributeEntry (OrientationRequested.LANDSCAPE)});
PrintService thePrintService =
(PrintService) theJLUS.lookup (theServiceTemplate);
if (thePrintService != null)
{
PrintRequest thePrintRequest =
thePrintService.createDocPrintRequest
(theDoc,
new HashPrintRequestAttributeSet
(new PrintRequestAttribute[]
{theMedia,
OrientationRequested.LANDSCAPE,
new JobName
("Test Job",
Locale.getDefault())}));
PrintJobAndLease theJobAndLease =
thePrintRequest.print (Lease.ANY);
PrintJob theJob = theJobAndLease.printJob;
theLRM.renewUntil (theJobAndLease.lease, Lease.ANY, null);
for (;;)
{
PrintJobAttributeSet theJobAttributes = theJob.getAttributes();
JobState theJobState =
(JobState) theJobAttributes.get (JobState.class);
System.out.println ("Job state = " + theJobState);
if (theJobState == JobState.ABORTED ||
theJobState == JobState.CANCELED ||
theJobState == JobState.COMPLETED)
{
break;
}
Thread.currentThread().sleep (1000L);
}
theLRM.cancel (theJobAndLease.lease);
}
else
{
// This code executed if the JLUS couldn't find any print services
}
}
catch (PrintException exc)
{
// This code executed if a printing error occurred
// Thrown by any of the above
}
catch (RemoteException exc)
{
// This code executed if a remote error occurred
// Thrown by any of the above
}
catch (InterruptedException exc)
{
// This code executed if the current thread is interrupted
// Thrown by Thread.currentThread().sleep()
}
catch (UnknownLeaseException exc)
{
// This code executed if the print job's lease is unknown
// Thrown by theLRM.cancel()
}
|