/* * This file is modified by Ivan Maidanski * Project name: JCGO-SUNAWT (http://www.ivmaisoft.com/jcgo/) */ /* * @(#)UnixPrintServiceLookup.java 1.13 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package sun.print; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.util.ArrayList; import java.util.Vector; import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import javax.print.DocFlavor; import javax.print.MultiDocPrintService; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.attribute.Attribute; import javax.print.attribute.AttributeSet; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.HashPrintServiceAttributeSet; import javax.print.attribute.PrintRequestAttribute; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.PrintServiceAttribute; import javax.print.attribute.PrintServiceAttributeSet; import javax.print.attribute.standard.PrinterName; import java.io.File; import java.io.FileReader; /* * Remind: This class uses solaris commands. We also need a linux * version */ public class UnixPrintServiceLookup extends PrintServiceLookup implements BackgroundServiceLookup, Runnable { /* Remind: the current implementation is static, as its assumed * its preferable to minimise creation of PrintService instances. * Later we should add logic to add/remove services on the fly which * will take a hit of needing to regather the list of services. */ private String defaultPrinter; private PrintService defaultPrintService; private String[] printers; /* excludes the default printer */ private PrintService[] printServices; /* includes the default printer */ private Vector lookupListeners = null; static final String osname = (String)java.security.AccessController.doPrivileged( new sun.security.action.GetPropertyAction("os.name")); static boolean isSysV() { return osname.indexOf("SunOS") != -1 || osname.indexOf("Solaris") != -1; } static boolean isBSD() { return osname.indexOf("Linux") != -1 || osname.equals("FreeBSD") || osname.equals("NetBSD") || osname.equals("OpenBSD") || osname.equals("DragonFly") || osname.equals("BSD/OS"); } static final int UNINITIALIZED = -1; static final int BSD_LPD = 0; static final int BSD_LPD_NG = 1; static int cmdIndex = UNINITIALIZED; String[] lpcFirstCom = { "/usr/sbin/lpc status | grep : | sed -ne '1,1 s/://p'", "/usr/sbin/lpc status | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'" }; String[] lpcAllCom = { "/usr/sbin/lpc status | grep : | sed -e 's/://'", "/usr/sbin/lpc -a status | grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'" }; String[] lpcNameCom = { "| grep : | sed -ne 's/://p'", "| grep -E '^[ 0-9a-zA-Z_-]*@' | awk -F'@' '{print $1}'" }; static int getBSDCommandIndex() { String command = "/usr/sbin/lpc status | grep @"; String[] names = execCmd(command); if ((names == null) || (names.length == 0)) { return BSD_LPD; } else { return BSD_LPD_NG; } } /* Want the PrintService which is default print service to have * equality of reference with the equivalent in list of print services * This isn't required by the API and there's a risk doing this will * lead people to assume its guaranteed. */ public synchronized PrintService[] getPrintServices() { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPrintJobAccess(); } if (printServices == null) { getDefaultPrintService(); if (isSysV()) { printers = getAllPrinterNamesSysV(); printServices = new PrintService[printers.length+1]; printServices[0] = getDefaultPrintService(); for (int p=0;p 0) { return null; } else { return new UnixPrintService(name); } } private String[] getAllPrinterNamesSysV() { String defaultPrinter = "lp"; String command = "/usr/bin/lpstat -v|/usr/bin/expand|/usr/bin/cut -f3 -d' ' |/usr/bin/cut -f1 -d':'"; String [] names = execCmd(command); ArrayList printerNames = new ArrayList(); for (int i=0; i < names.length; i++) { if (!names[i].equals("_default") && !names[i].equals(defaultPrinter)) { printerNames.add(names[i]); } } return (String[])printerNames.toArray(new String[printerNames.size()]); } static String[] execCmd(String command) { ArrayList results = new ArrayList(); try { final String[] cmd = new String[3]; if (isSysV()) { cmd[0] = "/usr/bin/sh"; cmd[1] = "-c"; cmd[2] = "env LC_ALL=C " + command; } else { cmd[0] = "/bin/sh"; cmd[1] = "-c"; cmd[2] = command; } BufferedReader bufferedReader = (BufferedReader)AccessController.doPrivileged( new PrivilegedExceptionAction() { public Object run() throws IOException { File f = File.createTempFile("prn","xc"); try { cmd[2] = cmd[2]+">"+f.getAbsolutePath(); Process lpstat = Runtime.getRuntime().exec(cmd); try { lpstat.waitFor(); } catch (InterruptedException e) { } if (lpstat.exitValue() == 0) { FileReader reader = new FileReader(f); return new BufferedReader(reader); } } finally { f.delete(); } return null; } }); if (bufferedReader != null) { String line; while((line = bufferedReader.readLine()) != null) { results.add(line); } } } catch (IOException e) { } catch (PrivilegedActionException e) { } return (String[])results.toArray(new String[results.size()]); } }