Inconsistent Results from javax.print library

本秂侑毒 提交于 2019-12-23 16:34:29

问题


I have a section of code within a Web App, running in Tomcat 5.0, that makes a call to the javax.print.PrintServiceLookup method lookupPrintServices(null, null). Previously, this code returned an array of substantial size, listing all the printers on the server as expected. Rather suddenly one day recently, it started behaving differently, now returning a zero-size array of no printers instead. Checking rather thoroughly, I was not able to determine what might have changed to cause this method to behave differently now than it did before.

I made a small, stand-alone test program that contained this same method call.

    PrintService[] printers = PrintServiceLookup.lookupPrintServices(null, null);

    System.out.println("Java Version: " + System.getProperty("java.version"));
    System.out.println("Printers found:");
    if (printers != null) {
        for (PrintService printer : printers) {
            if (printer != null) {
                System.out.println("    " + printer.toString());
            }
        }
    }
    System.out.println("End");

Running this program, it reacted differently, returning the full list of printers. Double-checking, I put the same code (using logging statements instead of System.print statements) in the context initialization method of the Web App, and it still returns zero printers. The method returns different results depending on whether it is run from the web app war or the stand-alone jar.

Some of my colleagues suggested that it might have to do with the Security Manager, and indeed, the documentation for the PrintService class says that certain properties of a Security Manager can alter results from the method call. However, after adding some code to my test to retrieve and view the Security Manager, it appears that there is none in either case.

    try {
        if (sec != null) {
            System.out.println(sec.toString());
            sec.checkPrintJobAccess();
        }
        System.out.println("*-*-*-*-*Printer Access allowed!!");
    }
    catch (SecurityException e) {
        System.out.println("*-*-*-*-*Printer NOT Access allowed!!");
    }

The result is that the Security Manager is null in both cases.

Trying it on a different server, both the web app and the stand-alone jar versions of doing things return no printers. There is no consistency that I can find.

What is going on here? What is causing this javax method call to return different results in different situations? What could have changed about the web app to alter its behavior between one day and the next?


回答1:


Try starting the server with this option -DUseSunHttpHandler=true to initiate the HTTP URL Connection with JDK API instead of server API. Hope this works for you too.



来源:https://stackoverflow.com/questions/17727041/inconsistent-results-from-javax-print-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!