/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
public class CUPSPrinter {
private boolean initialized;
private static native int getCupsPort();
private static native boolean initIDs();
// These functions need to be synchronized as
// CUPS does not support multi-threading.
//public static boolean useIPPMedia = false; will be used later
private float[] pageSizes;
private static boolean libFound;
static {
// load awt library to access native code
if (libFound) {
cupsServer = getCupsServer();
cupsPort = getCupsPort();
}
}
if (printerName == null) {
throw new IllegalArgumentException("null printer name");
}
initialized = false;
if (!libFound) {
throw new RuntimeException("cups lib not found");
} else {
// get page + tray names
// either PPD file is not found or printer is unknown
throw new RuntimeException("error getting PPD");
}
// get sizes
assert (nTrays >= 0);
}
}
}
/**
* Returns array of MediaSizeNames derived from PPD.
*/
initMedia();
return cupsMediaSNames;
}
/**
* Returns array of Custom MediaSizeNames derived from PPD.
*/
initMedia();
return cupsCustomMediaSNames;
}
/**
* Returns array of MediaPrintableArea derived from PPD.
*/
initMedia();
return cupsMediaPrintables;
}
/**
* Returns array of MediaTrays derived from PPD.
*/
initMedia();
return cupsMediaTrays;
}
/**
* Initialize media by translating PPD info to PrintService attributes.
*/
private synchronized void initMedia() {
if (initialized) {
return;
} else {
initialized = true;
}
return;
}
// initialize names and printables
for (int i=0; i<nPageSizes; i++) {
// media width and length
// media printable area
// add to list of standard MediaSizeNames
// add custom if no matching standard media
cupsMediaSNames[i] = msn;
// add this new custom msn to MediaSize array
}
}
// add to list of custom MediaSizeName
// for internal use of IPPPrintService
cupsCustomMediaSNames[i] = msn;
try {
mpa = new MediaPrintableArea(x, y, w, h,
} catch (IllegalArgumentException e) {
}
}
cupsMediaPrintables[i] = mpa;
}
// initialize trays
for (int i=0; i<nTrays; i++) {
cupsMediaTrays[i] = mt;
}
}
/**
* Get CUPS default printer using IPP.
*/
try {
final HttpURLConnection urlConnection =
if (urlConnection != null) {
try {
return urlConnection.getOutputStream();
} catch (Exception e) {
}
return null;
}
});
return null;
}
AttributeClass attCl[] = {
new AttributeClass("requested-attributes",
"printer-name")
};
attCl)) {
is);
}
if (defaultMap == null) {
/* CUPS on OS X, as initially configured, considers the
* default printer to be the last one used that's
* presently available. So if no default was
* reported, exec lpstat -d which has all the Apple
* special behaviour for this built in.
*/
if (UnixPrintServiceLookup.isMac()) {
return UnixPrintServiceLookup.
} else {
return null;
}
}
if (attribClass != null) {
return nameStr;
}
}
}
} catch (Exception e) {
}
return null;
}
/**
* Get list of all CUPS printers using IPP.
*/
try {
final HttpURLConnection urlConnection =
if (urlConnection != null) {
try {
return urlConnection.getOutputStream();
} catch (Exception e) {
}
return null;
}
});
return null;
}
AttributeClass attCl[] = {
new AttributeClass("requested-attributes",
"printer-uri-supported")
};
HashMap[] responseMap =
return null;
}
if (attribClass != null) {
}
}
} else {
}
}
} catch (Exception e) {
}
return null;
}
/**
* Returns CUPS server name.
*/
return cupsServer;
}
/**
* Returns CUPS port number.
*/
public static int getPort() {
return cupsPort;
}
/**
* Detects if CUPS is running.
*/
public static boolean isCupsRunning() {
if (libFound) {
" port "+getPort());
} else {
return false;
}
}
}