javax.print库的结果不一致 [英] Inconsistent Results from javax.print library

查看:125
本文介绍了javax.print库的结果不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Tomcat 5.0中运行的Web应用程序中有一段代码,它调用 javax.print.PrintServiceLookup 方法 lookupPrintServices(null,null) >.以前,此代码返回了一个很大的数组,并按预期列出了服务器上的所有打印机.相反,最近一天突然,它的行为开始有所不同,现在返回的是没有打印机的零尺寸数组.经过相当彻底的检查,我无法确定是什么原因导致此方法现在的行为与以前有所不同.

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");

运行该程序,它有不同的反应,并返回打印机的完整列表.仔细检查,我将相同的代码(使用日志记录语句而不是System.print语句)放在Web App的上下文初始化方法中,它仍然返回零打印机.方法返回不同的结果,具体取决于它是从Web应用程序战争中运行还是从独立jar中运行.

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.

在其他服务器上进行尝试,Web应用程序和独立的jar版本的服务器都不会返回打印机.我找不到一致性.

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.

这是怎么回事?是什么导致此javax方法调用在不同情况下返回不同的结果?在一天到第二天之间更改Web应用程序的行为可能会发生什么变化?

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?

推荐答案

尝试使用此选项 -DUseSunHttpHandler = true 启动服务器,以使用JDK API而非服务器API初始化HTTP URL连接. 希望这对您也有用.

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.

这篇关于javax.print库的结果不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆