private static String findFactory()

in src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java [73:133]


    private static String findFactory(final String property, final String defaultFactory) {
        // Use the factory ID system property first
        try {
            final String systemProp = System.getProperty(property);
            if (systemProp != null) {
                if (debug) {
                    System.err.println("JXPath: found system property" + systemProp);
                }
                return systemProp;
            }
        } catch (final SecurityException ignore) { // NOPMD
            // Ignore
        }
        // try to read from $java.home/lib/xml.properties
        try {
            final Path javaHome = Paths.get(System.getProperty("java.home"));
            final Path configFile = javaHome.resolve(Paths.get("lib", "jxpath.properties"));
            if (Files.exists(configFile)) {
                final Properties props = new Properties();
                try (InputStream fis = Files.newInputStream(configFile)) {
                    props.load(fis);
                }
                final String factory = props.getProperty(property);
                if (factory != null) {
                    if (debug) {
                        System.err.println("JXPath: found java.home property " + factory);
                    }
                    return factory;
                }
            }
        } catch (final IOException ex) {
            if (debug) {
                ex.printStackTrace();
            }
        }
        final String serviceId = "META-INF/services/" + property;
        // try to find services in CLASSPATH
        try {
            final ClassLoader cl = JXPathContextFactory.class.getClassLoader();
            try (InputStream is = cl == null ? ClassLoader.getSystemResourceAsStream(serviceId) : cl.getResourceAsStream(serviceId)) {
                if (is != null) {
                    if (debug) {
                        System.err.println("JXPath: found  " + serviceId);
                    }
                    final BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    final String factory = rd.readLine();
                    if (factory != null && !"".equals(factory)) {
                        if (debug) {
                            System.err.println("JXPath: loaded from services: " + factory);
                        }
                        return factory;
                    }
                }
            }
        } catch (final Exception ex) {
            if (debug) {
                ex.printStackTrace();
            }
        }
        return defaultFactory;
    }