2929import org .apache .commons .io .FileUtils ;
3030import org .apache .commons .io .IOUtils ;
3131import org .apache .commons .lang3 .StringUtils ;
32+ import org .apache .commons .lang3 .exception .ExceptionUtils ;
3233
3334import java .io .File ;
3435import java .io .IOException ;
@@ -75,11 +76,15 @@ public static boolean initialize() {
7576 loadNativeLibrary ();
7677 } catch (ThreadDeath td ) {
7778 throw td ;
79+ } catch (JNIPerformanceCounterConnectorException e ) {
80+ if (InternalLogger .INSTANCE .isErrorEnabled ()) {
81+ InternalLogger .INSTANCE .error ("Error initializing JNI Performance Counter library. Windows performance counters will not be used.: " + ExceptionUtils .getStackTrace (e ));
82+ }
7883 } catch (Throwable e ) {
7984 try {
80- InternalLogger .INSTANCE .error (
81- "Failed to load native dll, Windows performance counters will not be used. " +
82- "Please make sure that Visual C++ Redistributable is properly installed: %s." , e . toString ());
85+ if ( InternalLogger .INSTANCE .isErrorEnabled ()) {
86+ InternalLogger . INSTANCE . error ( "Unexpected error initialiing JNI Performance Counter library. Windows performance counters will not be used: " + ExceptionUtils . getStackTrace ( e ));
87+ }
8388
8489 return false ;
8590 } catch (ThreadDeath td ) {
@@ -176,21 +181,39 @@ private static void initNativeCode() {
176181 * @throws IOException If there are errors in opening/writing/reading/closing etc.
177182 * Note that the method might throw RuntimeExceptions due to critical issues
178183 */
179- private static void loadNativeLibrary () throws IOException {
180- String model = System .getProperty ("sun.arch.data.model" );
181- String libraryToLoad = BITS_MODEL_64 .equals (model ) ? NATIVE_LIBRARY_64 : NATIVE_LIBRARY_32 ;
184+ private static void loadNativeLibrary () throws JNIPerformanceCounterConnectorException {
185+ final File dllOnDisk ;
186+ final String libraryToLoad ;
187+ try {
188+ String model = System .getProperty ("sun.arch.data.model" );
189+ libraryToLoad = BITS_MODEL_64 .equals (model ) ? NATIVE_LIBRARY_64 : NATIVE_LIBRARY_32 ;
182190
183- File dllPath = buildDllLocalPath ();
191+ File dllPath = buildDllLocalPath ();
184192
185- File dllOnDisk = new File (dllPath , libraryToLoad );
193+ dllOnDisk = new File (dllPath , libraryToLoad );
186194
187- if (!dllOnDisk .exists ()) {
188- extractToLocalFolder (dllOnDisk , libraryToLoad );
195+ if (!dllOnDisk .exists ()) {
196+ extractToLocalFolder (dllOnDisk , libraryToLoad );
197+ } else if (InternalLogger .INSTANCE .isTraceEnabled ()) {
198+ InternalLogger .INSTANCE .trace ("Found existing DLL: " + dllOnDisk .getAbsolutePath ());
199+ }
200+ } catch (Exception e ) {
201+ throw new JNIPerformanceCounterConnectorException ("Error extracting DLL to disk" , e );
189202 }
190203
191- System .load (dllOnDisk .toString ());
204+ try {
205+ System .load (dllOnDisk .toString ());
206+ } catch (Exception e ) {
207+ throw new JNIPerformanceCounterConnectorException ("Error loading DLL. Please make sure that Visual C++ 2015 Redistributable is properly installed" , e );
208+ }
192209
193- initNativeCode ();
210+ try {
211+ initNativeCode ();
212+ } catch (NumberFormatException e ) {
213+ throw new JNIPerformanceCounterConnectorException ("Could not parse PID as int" , e );
214+ } catch (Exception e ) {
215+ throw new JNIPerformanceCounterConnectorException ("Unexpected error initializing performance counter DLL library" , e );
216+ }
194217
195218 InternalLogger .INSTANCE .trace ("Successfully loaded library '%s'" , libraryToLoad );
196219 }
@@ -246,4 +269,14 @@ private static File buildDllLocalPath() {
246269
247270 return dllPath ;
248271 }
272+
273+ private static class JNIPerformanceCounterConnectorException extends Exception {
274+ public JNIPerformanceCounterConnectorException (String s ) {
275+ super (s );
276+ }
277+
278+ public JNIPerformanceCounterConnectorException (String s , Throwable throwable ) {
279+ super (s , throwable );
280+ }
281+ }
249282}
0 commit comments