3 Replies Latest reply: Apr 14, 2011 1:46 PM by user502964 RSS

    Java Print Service API in Java Stored Procedure (Linux)

    373910
      Hi

      We are running an Oracle 10g database on Linux and I am in the proces of developing a java stored procedure which should utilize the Java Print Service API.
      I have made a simple stored procedure to list all available printers and the DocFlavors they support (se the code below).
      My problem is that no printers are listed. I have made a standalone java app. with the same code and executed it directly on the OS level of the Linux box through the Sun JDK 1.4.2 and here I get two printers.
      Is there any specific configuration I need to do to make it work?
      I am wondering if I need to grant specific authorisations through dbms_java for it to work...?
      Any help is greatlyh appreciated.

      ************************************** CODE BEGIN *************************************
      import javax.print.DocFlavor;
      import javax.print.PrintService;
      import javax.print.PrintServiceLookup;
      import javax.print.attribute.HashPrintRequestAttributeSet;
      import javax.print.attribute.PrintRequestAttributeSet;


      public class TestPrintService {
      public TestPrintService() {
      }

      public static void listPrinters() {
      // Get all available printers and their supported DocFlavors
      PrintService[] pservices =
      PrintServiceLookup.lookupPrintServices(null, null);
      System.out.println("Printer services: " + pservices.length);
      for (int i = 0; i < pservices.length; i++) {
      PrintService pservice = pservices;
      System.err.println("Printer: " + pservice);
      DocFlavor[] docFlavors = pservice.getSupportedDocFlavors();
      for (int j = 0; j < docFlavors.length; j++) {
      DocFlavor docFlavor = docFlavors[j];
      System.err.println("DocFlavor " + docFlavor.toString());
      }
      System.err.println();
      }
      }

      }
      ************************************** CODE END *************************************

      Cheers,
      Jacob Vennervald
        • 1. Re: Java Print Service API in Java Stored Procedure (Linux)
          Dallag
          Hi,
          I know you posted this 3 years ago but I am facing exactly the same problem in a windows envirenment.
          All prints fine from the prompt but give a "Exception ee java.lang.NullPointerException" when ran from an Oracle Function.
          Did you ever get it working ?

          Thank you
          Dallag

          Edited by: user7434881 on 11-Mar-2010 07:44
          • 2. Re: Java Print Service API in Java Stored Procedure (Linux)
            Dallag
            Found this on Oracle support:

            Cannot List Available Printers From The Database Using A Java Stored Procedure [ID 372694.1]


            Applies to:
            Oracle Server - Enterprise Edition - Version: 10.1.0.4.0
            This problem can occur on any platform.
            Symptoms
            Able to list available printers on a machine when running Java code outside the Database.

            When running the same Java code inside the Database as a Java Stored Procedure, no printers are found.
            Cause
            Due to security restrictions, this is expected results.

            The Java Docs state:
            "Services which are registered by registerService(PrintService) will not be included in lookup
            results if a security manager is installed and its CheckPrintJobAccess() method denies access."

            Also from the documentation it states:
            "A PrintServiceLookup implementor is recommended to check for the SecurityManager.checkPrintJobAccess() to deny access to untrusted code. Following this recommended policy means that untrusted code may not be able to locate any print services. Downloaded applets are the most common example of untrusted code."

            Using the checkPrintJobAccess(); call, it does produce a Security Exception when run inside the Database but not when run outside. The exception can be viewed within the log file found in the UDUMP directory.

            .
            Solution

            Run the following code to confirm obtaining available __printers can not be done...__


            This is the code to create the Java Stored Procedure

            CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "ListPrinters" AS
            import javax.print.*;
            public class ListPrinters {
            public static String AvailablePrinters(){
            String strList;
            PrintService[] pservices =PrintServiceLookup.lookupPrintServices(null,null);
            if (pservices.length > 0 )
            strList = pservices[0].getName();
            else
            strList = "No printers found";
            return strList;
            }

            public static String listprinters() throws Exception {
            String listofprinters;
            try {
            SecurityManager sm = System.getSecurityManager();
            if (sm != null) sm.checkPrintJobAccess();
            }
            catch (SecurityException ex) {
            System.err.println("Sorry. Printing is not allowed.");
            }
            listofprinters = AvailablePrinters();
            return listofprinters;
            }
            }
            /



            This is the PL/SQL Wrapper

            CREATE OR REPLACE FUNCTION Get_Printer_Test RETURN VARCHAR2 IS
            LANGUAGE JAVA
            NAME 'ListPrinters.listprinters() return String';
            /



            This executes the code

            SQL> SELECT Get_Printer_Test FROM DUAL;

            GET_PRINTER_TEST
            --------------------------------------------
            No printers found


            This is the output found in the trace file in the UDUMP directory

            *** SESSION ID:(144.28) 2006-07-08 09:02:25.518
            Sorry. Printing is not allowed.
            • 3. Re: Java Print Service API in Java Stored Procedure (Linux)
              user502964
              Hello good afternoon. I'm just plumb with the problem, you could solve the case?
              Thank you.
              Roberto Fernandez