jdk/src/solaris/classes/sun/print/CUPSPrinter.java
author jgodinez
Mon, 14 Oct 2013 09:15:21 -0700
changeset 21224 58a31574ac84
parent 14342 8435a30053c1
child 22584 eed64ee05369
permissions -rw-r--r--
8022536: closed/javax/print/TextFlavorTest.java fails Reviewed-by: prr, jchen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12997
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1737
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.print;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.print.IPPPrintService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.print.CustomMediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.print.CustomMediaTray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.print.attribute.standard.Media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.print.attribute.standard.MediaSizeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.print.attribute.standard.MediaSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.print.attribute.standard.MediaTray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.print.attribute.standard.MediaPrintableArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.print.attribute.Size2DSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.print.attribute.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.print.attribute.EnumSyntax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.print.attribute.standard.PrinterName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class CUPSPrinter  {
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
    49
    private static final String debugPrefix = "CUPSPrinter>> ";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final double PRINTER_DPI = 72.0;
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
    51
    private boolean initialized;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static native String getCupsServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static native int getCupsPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static native boolean canConnect(String server, int port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static native boolean initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // These functions need to be synchronized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // CUPS does not support multi-threading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static synchronized native String[] getMedia(String printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static synchronized native float[] getPageSizes(String printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    //public static boolean useIPPMedia = false; will be used later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private MediaPrintableArea[] cupsMediaPrintables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private MediaSizeName[] cupsMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private CustomMediaSizeName[] cupsCustomMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private MediaTray[] cupsMediaTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public  int nPageSizes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public  int nTrays = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private  String[] media;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private  float[] pageSizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private String printer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static boolean libFound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static String cupsServer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static int cupsPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // load awt library to access native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    80
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    81
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    82
                    System.loadLibrary("awt");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    83
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    84
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 5506
diff changeset
    85
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        libFound = initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (libFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
           cupsServer = getCupsServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
           cupsPort = getCupsPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    CUPSPrinter (String printerName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (printerName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new IllegalArgumentException("null printer name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        printer = printerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        cupsMediaSNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        cupsMediaPrintables = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        cupsMediaTrays = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (!libFound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new RuntimeException("cups lib not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            // get page + tray names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            media =  getMedia(printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (media == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                // either PPD file is not found or printer is unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new RuntimeException("error getting PPD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // get sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            pageSizes = getPageSizes(printer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (pageSizes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                nPageSizes = pageSizes.length/6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                nTrays = media.length/2-nPageSizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                assert (nTrays >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Returns array of MediaSizeNames derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public MediaSizeName[] getMediaSizeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return cupsMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Returns array of Custom MediaSizeNames derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public CustomMediaSizeName[] getCustomMediaSizeNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return cupsCustomMediaSNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns array of MediaPrintableArea derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public MediaPrintableArea[] getMediaPrintableArea() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return cupsMediaPrintables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns array of MediaTrays derived from PPD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public MediaTray[] getMediaTrays() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        initMedia();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return cupsMediaTrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Initialize media by translating PPD info to PrintService attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
   164
    private synchronized void initMedia() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (pageSizes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        cupsMediaPrintables = new MediaPrintableArea[nPageSizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        cupsMediaSNames = new MediaSizeName[nPageSizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        cupsCustomMediaSNames = new CustomMediaSizeName[nPageSizes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        CustomMediaSizeName msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        MediaPrintableArea mpa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        float length, width, x, y, w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // initialize names and printables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        for (int i=0; i<nPageSizes; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // media width and length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            width = (float)(pageSizes[i*6]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            length = (float)(pageSizes[i*6+1]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // media printable area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            x = (float)(pageSizes[i*6+2]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            h = (float)(pageSizes[i*6+3]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            w = (float)(pageSizes[i*6+4]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            y = (float)(pageSizes[i*6+5]/PRINTER_DPI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            msn = new CustomMediaSizeName(media[i*2], media[i*2+1],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                          width, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // add to list of standard MediaSizeNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if ((cupsMediaSNames[i] = msn.getStandardMedia()) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                // add custom if no matching standard media
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                cupsMediaSNames[i] = msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                // add this new custom msn to MediaSize array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if ((width > 0.0) && (length > 0.0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    new MediaSize(width, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                  Size2DSyntax.INCH, msn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // add to list of custom MediaSizeName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            // for internal use of IPPPrintService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            cupsCustomMediaSNames[i] = msn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            mpa = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                mpa = new MediaPrintableArea(x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                             MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (width > 0 && length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    mpa = new MediaPrintableArea(0, 0, width, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                             MediaPrintableArea.INCH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            cupsMediaPrintables[i] = mpa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // initialize trays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        cupsMediaTrays = new MediaTray[nTrays];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        MediaTray mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        for (int i=0; i<nTrays; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            mt = new CustomMediaTray(media[(nPageSizes+i)*2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                     media[(nPageSizes+i)*2+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            cupsMediaTrays[i] = mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Get CUPS default printer using IPP.
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   240
     * Returns 2 values - index 0 is printer name, index 1 is the uri.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   242
    static String[] getDefaultPrinter() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            URL url = new URL("http", getServer(), getPort(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            final HttpURLConnection urlConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                IPPPrintService.getIPPConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (urlConnection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                OutputStream os = (OutputStream)java.security.AccessController.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    doPrivileged(new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                return urlConnection.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (os == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                AttributeClass attCl[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    AttributeClass.ATTRIBUTES_CHARSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    new AttributeClass("requested-attributes",
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   268
                                       AttributeClass.TAG_URI,
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   269
                                       "printer-uri")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if (IPPPrintService.writeIPPRequest(os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                        IPPPrintService.OP_CUPS_GET_DEFAULT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                        attCl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    HashMap defaultMap = null;
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   277
                    String[] printerInfo = new String[2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    InputStream is = urlConnection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    HashMap[] responseMap = IPPPrintService.readIPPResponse(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                         is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   283
                    if (responseMap != null && responseMap.length > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        defaultMap = responseMap[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    if (defaultMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        urlConnection.disconnect();
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   290
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   291
                        /* CUPS on OS X, as initially configured, considers the
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   292
                         * default printer to be the last one used that's
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   293
                         * presently available. So if no default was
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   294
                         * reported, exec lpstat -d which has all the Apple
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   295
                         * special behaviour for this built in.
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   296
                         */
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   297
                         if (UnixPrintServiceLookup.isMac()) {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   298
                             printerInfo[0] = UnixPrintServiceLookup.
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   299
                                                   getDefaultPrinterNameSysV();
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   300
                             printerInfo[1] = null;
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   301
                             return (String[])printerInfo.clone();
12997
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   302
                         } else {
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   303
                             return null;
7ad469d89bed 7124536: [macosx] PrintServiceLookup.lookupDefaultPrintService() return null
prr
parents: 12559
diff changeset
   304
                         }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   307
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    AttributeClass attribClass = (AttributeClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        defaultMap.get("printer-name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    if (attribClass != null) {
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   312
                        printerInfo[0] = attribClass.getStringValue();
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   313
                        attribClass = (AttributeClass)defaultMap.get("device-uri");
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   314
                        if (attribClass != null) {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   315
                            printerInfo[1] = attribClass.getStringValue();
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   316
                        } else {
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   317
                            printerInfo[1] = null;
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   318
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        urlConnection.disconnect();
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   321
                        return (String [])printerInfo.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Get list of all CUPS printers using IPP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
21224
58a31574ac84 8022536: closed/javax/print/TextFlavorTest.java fails
jgodinez
parents: 14342
diff changeset
   336
    static String[] getAllPrinters() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            URL url = new URL("http", getServer(), getPort(), "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            final HttpURLConnection urlConnection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                IPPPrintService.getIPPConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (urlConnection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                OutputStream os = (OutputStream)java.security.AccessController.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    doPrivileged(new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                return urlConnection.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                if (os == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                AttributeClass attCl[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    AttributeClass.ATTRIBUTES_CHARSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    new AttributeClass("requested-attributes",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                       AttributeClass.TAG_KEYWORD,
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   364
                                       "printer-uri-supported")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if (IPPPrintService.writeIPPRequest(os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                IPPPrintService.OP_CUPS_GET_PRINTERS, attCl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    InputStream is = urlConnection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    HashMap[] responseMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        IPPPrintService.readIPPResponse(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    if (responseMap == null || responseMap.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    ArrayList printerNames = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    for (int i=0; i< responseMap.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        AttributeClass attribClass = (AttributeClass)
542
eb75700cdf75 6678161: Printing to remote non-Postscript printer does not work in Linux
jgodinez
parents: 2
diff changeset
   385
                            responseMap[i].get("printer-uri-supported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        if (attribClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                            String nameStr = attribClass.getStringValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                            printerNames.add(nameStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    return (String[])printerNames.toArray(new String[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    urlConnection.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Returns CUPS server name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public static String getServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return cupsServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Returns CUPS port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public static int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return cupsPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Detects if CUPS is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public static boolean isCupsRunning() {
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
   423
        IPPPrintService.debug_println(debugPrefix+"libFound "+libFound);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (libFound) {
1737
90d4fe987b09 6653384: Variable "initialized" in class CUPSPrinter is static by mistake
jgodinez
parents: 715
diff changeset
   425
            IPPPrintService.debug_println(debugPrefix+"CUPS server "+getServer()+
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                          " port "+getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return canConnect(getServer(), getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
}