javax.print.PrintServiceLookup under Karaf

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

javax.print.PrintServiceLookup under Karaf

Castor
Hello! I'm having a problem with PrintServiceLookup under Karaf, it seems that when i run a PrintServiceLookup.lookupPrintServices(null, null) it always return empty, after some investigation i found that this method searchs for a javax.print.PrintServiceLookup under /META-INF/services , the file contains the following: 

# Provider for Java Print Service
sun.print.Win32PrintServiceLookup

It seems that it looks to the running JRE to define which printService to use, so i created the required folder in my bundle and copied the javax.print.PrintServiceLookup file to it, with that it worked. But i can't rely on that in a production server, since the application can be runned on linux or windows. 

How can i fix that?
Reply | Threaded
Open this post in threaded view
|

Re: javax.print.PrintServiceLookup under Karaf

Guillaume Nodet-2
It looks like the code is using:
  ServiceLoader.load(PrintServiceLookup.class)
If those services are provided by the JRE, then you need to make sure that the first time the service list is looked up, the thread context class loader is set to null, so that it will use the system classloader to load those services.
That's really a bad idea to initialize a static variable with something dependent on the context, but there's not much that can be done about it.  So whenever you use lookupPrintServices make sure you do it with a block like:

public static final PrintService[] lookupPrintServices(DocFlavor flavor, AttributeSet attributes) {
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
  try {
  Thread.currentThread().setContextClassLoader(null);
  return PrintServiceLookup.lookupPrintServices(flavor, attributes);
  } finally {
  Thread.currentThread().setContextClassLoader(tccl);
  }
}

Guillaume

2017-10-13 20:07 GMT+02:00 Ygor Castor <[hidden email]>:
Hello! I'm having a problem with PrintServiceLookup under Karaf, it seems that when i run a PrintServiceLookup.lookupPrintServices(null, null) it always return empty, after some investigation i found that this method searchs for a javax.print.PrintServiceLookup under /META-INF/services , the file contains the following: 

# Provider for Java Print Service
sun.print.Win32PrintServiceLookup

It seems that it looks to the running JRE to define which printService to use, so i created the required folder in my bundle and copied the javax.print.PrintServiceLookup file to it, with that it worked. But i can't rely on that in a production server, since the application can be runned on linux or windows. 

How can i fix that?



--
------------------------
Guillaume Nodet