@@ -54,6 +54,7 @@ public class CUPSPrinter {
5454 private static native String getCupsServer ();
5555 private static native int getCupsPort ();
5656 private static native String getCupsDefaultPrinter ();
57+ private static native String [] getCupsDefaultPrinters ();
5758 private static native boolean canConnect (String server , int port );
5859 private static native boolean initIDs ();
5960 // These functions need to be synchronized as
@@ -78,6 +79,7 @@ public class CUPSPrinter {
7879
7980 private static boolean libFound ;
8081 private static String cupsServer = null ;
82+ private static String domainSocketPathname = null ;
8183 private static int cupsPort = 0 ;
8284
8385 static {
@@ -92,6 +94,13 @@ public Void run() {
9294 libFound = initIDs ();
9395 if (libFound ) {
9496 cupsServer = getCupsServer ();
97+ // Is this a local domain socket pathname?
98+ if (cupsServer != null && cupsServer .startsWith ("/" )) {
99+ if (isSandboxedApp ()) {
100+ domainSocketPathname = cupsServer ;
101+ }
102+ cupsServer = "localhost" ;
103+ }
95104 cupsPort = getCupsPort ();
96105 }
97106 }
@@ -376,6 +385,20 @@ public OutputStream run() {
376385 * Get list of all CUPS printers using IPP.
377386 */
378387 static String [] getAllPrinters () {
388+
389+ if (getDomainSocketPathname () != null ) {
390+ String [] printerNames = getCupsDefaultPrinters ();
391+ if (printerNames != null && printerNames .length > 0 ) {
392+ String [] printerURIs = new String [printerNames .length ];
393+ for (int i =0 ; i < printerNames .length ; i ++) {
394+ printerURIs [i ] = String .format ("ipp://%s:%d/printers/%s" ,
395+ getServer (), getPort (), printerNames [i ]);
396+ }
397+ return printerURIs ;
398+ }
399+ return null ;
400+ }
401+
379402 try {
380403 URL url = new URL ("http" , getServer (), getPort (), "" );
381404
@@ -458,15 +481,39 @@ public static int getPort() {
458481 return cupsPort ;
459482 }
460483
484+ /**
485+ * Returns CUPS domain socket pathname.
486+ */
487+ private static String getDomainSocketPathname () {
488+ return domainSocketPathname ;
489+ }
490+
491+ @ SuppressWarnings ("removal" )
492+ private static boolean isSandboxedApp () {
493+ if (PrintServiceLookupProvider .isMac ()) {
494+ return java .security .AccessController
495+ .doPrivileged ((java .security .PrivilegedAction <Boolean >) () ->
496+ System .getenv ("APP_SANDBOX_CONTAINER_ID" ) != null );
497+ }
498+ return false ;
499+ }
500+
501+
461502 /**
462503 * Detects if CUPS is running.
463504 */
464505 public static boolean isCupsRunning () {
465506 IPPPrintService .debug_println (debugPrefix +"libFound " +libFound );
466507 if (libFound ) {
467- IPPPrintService .debug_println (debugPrefix +"CUPS server " +getServer ()+
468- " port " +getPort ());
469- return canConnect (getServer (), getPort ());
508+ String server = getDomainSocketPathname () != null
509+ ? getDomainSocketPathname ()
510+ : getServer ();
511+ IPPPrintService .debug_println (debugPrefix +"CUPS server " +server +
512+ " port " +getPort ()+
513+ (getDomainSocketPathname () != null
514+ ? " use domain socket pathname"
515+ : "" ));
516+ return canConnect (server , getPort ());
470517 } else {
471518 return false ;
472519 }
0 commit comments