Skip to content

Commit

Permalink
Use reflection for all JAI interaction, added -DLOGGING_TRACE=true co…
Browse files Browse the repository at this point in the history
…mmand line option for feedback

Author: Jody Garnett <[email protected]>
Author: Jim Hughes <[email protected]>

A slight update to the JAI Logging to consistently use reflection for all JAI interaction, added -DLOGGING_TRACE=true command line option for feedback
  • Loading branch information
jodygarnett committed Apr 23, 2015
1 parent c0eb434 commit c585b48
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ target
*.pyc
nb-configuration.xml
*.*~
.DS_Store
.DS_Store
.idea/**
*.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

import javax.media.jai.JAI;
import javax.media.jai.util.ImagingListener;

import org.geotools.resources.XArray;
import org.geotools.resources.Classes;
import org.geotools.resources.i18n.Errors;
Expand Down Expand Up @@ -67,7 +64,7 @@ public int compare(final Object o1, final Object o2) {
};

/**
* An empty array of loggings. Also used for locks.
* An empty array of logging. Also used for locks.
*/
private static final Logging[] EMPTY = new Logging[0];

Expand Down Expand Up @@ -111,22 +108,35 @@ public int compare(final Object o1, final Object o2) {

// Register LoggingImagingListener if JAI is available
static {
final boolean LOGGING_TRACE = Boolean.getBoolean("LOGGING_TRACE");
try {
Class.forName("javax.media.jai.JAI");
JAI jai = JAI.getDefaultInstance();
ImagingListener imagingListener = jai.getImagingListener();
final Class<?> JAI = Class.forName("javax.media.jai.JAI");
final Class<?> IMAGING_LISTENER = Class.forName("javax.media.jai.util.ImagingListener");
Method getDefaultInstance = JAI.getMethod("getDefaultInstance");
Method getImagingListener = JAI.getMethod("getImagingListener");
Method setImagingListener = JAI.getMethod("setImagingListener", IMAGING_LISTENER);

Object jai = getDefaultInstance.invoke(null, null);
Object imagingListener = getImagingListener.invoke(jai, null);

if (imagingListener == null
|| imagingListener.getClass().getName().contains("ImagingListenerImpl")) {
// Client code has not provided an ImagingListener so we can use our own
// Custom GeoTools ImagingListener used to ignore common warnings
jai.setImagingListener(new LoggingImagingListener());
// System.out.println("Logging JAI messages: javax.media.jai logger redirected");
setImagingListener.invoke(jai, new LoggingImagingListener());
if( LOGGING_TRACE ){
System.out.println("Logging JAI messages: javax.media.jai logger redirected");
}
} else {
// System.out.println("Logging JAI messages: ImagingListener already in use: "+ imagingListener);
if( LOGGING_TRACE ){
System.out.println("Logging JAI messages: ImagingListener already in use: "+ imagingListener);
}
}
} catch (Throwable ignore) {
// JAI not available so no need to redirect logging messages
// System.out.println("Logging JAI messages: Unable to redirect to javax.media.jai");
if( LOGGING_TRACE ){
System.out.println("Logging JAI messages: Unable to redirect to javax.media.jai");
}
}
}

Expand Down

0 comments on commit c585b48

Please sign in to comment.