private static String findFactory()

in src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java [152:257]


    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 String javah = System.getProperty("java.home");
            final String configFile =
                javah
                    + File.separator
                    + "lib"
                    + File.separator
                    + "jxpath.properties";
            final File f = new File(configFile);
            if (f.exists()) {
                final Properties props = new Properties();
                final FileInputStream fis = new FileInputStream(f);
                try {
                    props.load(fis);
                }
                finally {
                    if (fis != null) {
                        try {
                            fis.close();
                        }
                        catch (final IOException ignore) { // NOPMD
                            //swallow
                        }
                    }
                }
                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();
            InputStream is;
            if (cl == null) {
                is = ClassLoader.getSystemResourceAsStream(serviceId);
            }
            else {
                is = cl.getResourceAsStream(serviceId);
            }

            if (is != null) {
                if (debug) {
                    System.err.println("JXPath: found  " + serviceId);
                }
                final BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));

                String factory = null;
                try {
                    factory = rd.readLine();
                }
                finally {
                    try {
                        rd.close();
                    }
                    catch (final IOException ignore) { // NOPMD
                        // Ignore
                    }
                }

                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;
    }