public void print(Doc userDoc, PrintRequestAttributeSet printRequestAS)
throws PrintException {
synchronized (this) {
if (busyFlag) {
throw new PrintException(
"Already printed. Need to create new DocPrintJob.");
}
busyFlag = true;
}
DocFlavor userDocDF = userDoc.getDocFlavor();
/*
* Checking if doc.DocFlavor is supported by the current
* PrintService
*/
if (!printService.isDocFlavorSupported(userDocDF)) {
throw new PrintException("Doc flavor \'" + userDocDF
+ "\' is not supported");
}
/*
* Checking if doc.DocFlavor is supported directly by osClent. If it
* is not: - get StereamPrintServiceFactory for doc.DocFlavor -
* instantiate StreamPrintService - get PrintJob from it - run this
* PrintJob in separate thread
*/
printClient = printService.getPrintClient();
if (printService.isDocFlavorSupportedByClient(userDocDF)) {
printClient.print(userDoc, printRequestAS);
} else {
try {
Doc clientDoc = userDoc;
PipedOutputStream spsOutStream = new PipedOutputStream();
PipedInputStream clientInputStream = new PipedInputStream(
spsOutStream);
DocFlavor newFlavor = null;
StreamPrintServiceFactory spsf = null;
DocFlavor clientFlavors[] = printClient
.getSupportedDocFlavors();
for (int i = 0; i < clientFlavors.length; i++) {
StreamPrintServiceFactory[] factories = StreamPrintServiceFactory
.lookupStreamPrintServiceFactories(userDocDF,
clientFlavors[i].getMimeType());
if (factories.length > 0
&& Class.forName(
clientFlavors[i]
.getRepresentationClassName())
.isInstance(clientInputStream)) {
spsf = factories[0];
newFlavor = clientFlavors[i];
break;
}
}
if (spsf != null) {
StreamPrintService sps = spsf.getPrintService(spsOutStream);
/*
* Constructing new Doc object for client: - connecting
* InputStream of client with OutputStream of
* StreamPrintSrevice - constructing DocFlavor for
* StreamPrintSrevice output - creating new SimpleDoc
* for client
*/
clientDoc = new SimpleDoc(clientInputStream, newFlavor,
userDoc.getAttributes());
PrintJobThread printThread = new PrintJobThread(this,
userDoc, printRequestAS, sps);
printThread.start();
printClient.print(clientDoc, printRequestAS);
if (printThread.exceptionOccured()) {
throw new PrintException(printThread
.getPrintException());
}
} else {
throw new PrintException("Doc flavor "
+ userDocDF.getRepresentationClassName()
+ " is not supported");
}
} catch (ClassNotFoundException e) {
throw new PrintException(e);
} catch (IOException e) {
throw new PrintException(e);
} catch (PrintException e) {
throw e;
}
}
}