List of usage examples for java.util ServiceLoader loadInstalled
@CallerSensitive public static <S> ServiceLoader<S> loadInstalled(Class<S> service)
From source file:org.cellprofiler.preferences.CellProfilerPreferences.java
/** * Get the preferences factory supplied by the JRE or * provided as a service./*from www . j av a 2 s . c o m*/ * * @return the default preferences factory. */ static private PreferencesFactory getJREPreferencesFactory() { synchronized (lock) { if (delegatePreferencesFactory == null) { do { /* * First, see if there is a PreferencesFactory * provided as a service. */ final ServiceLoader<PreferencesFactory> pfServiceLoader = ServiceLoader .loadInstalled(PreferencesFactory.class); final Iterator<PreferencesFactory> pfIter = pfServiceLoader.iterator(); if (pfIter.hasNext()) { delegatePreferencesFactory = pfIter.next(); break; } /* * Next, try the WindowsPreferencesFactory if OS is Windows. */ String pfName = (SystemUtils.IS_OS_WINDOWS) ? "java.util.prefs.WindowsPreferencesFactory" : "java.util.prefs.FilePreferencesFactory"; try { Class<?> pfClass = Class.forName("java.util.prefs.WindowsPreferencesFactory", false, null); Class<?> pfFuckYou = Class.forName("java.util.prefs.WindowsPreferences", true, null); Constructor<?>[] pfConstructors = pfClass.getDeclaredConstructors(); for (Constructor<?> c : pfConstructors) { if (c.getParameterTypes().length == 0) { /* * Bad boy - it's package-private AND I CALL IT ANYWAY BAH HA HA HA HA HA HA */ c.setAccessible(true); delegatePreferencesFactory = (PreferencesFactory) c.newInstance(new Object[0]); break; } } break; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * And as a last resort, there's always our headless * preferences factory. */ delegatePreferencesFactory = new HeadlessPreferencesFactory(); } while (false); } } return delegatePreferencesFactory; }