1/*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.org.jvnet.mimepull;
27
28import java.io.BufferedReader;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.InputStreamReader;
32import java.nio.charset.StandardCharsets;
33import java.util.logging.Level;
34import java.util.logging.Logger;
35
36class FactoryFinder {
37
38    private static ClassLoader cl = FactoryFinder.class.getClassLoader();
39
40    static Object find(String factoryId) throws ClassNotFoundException,
41            InstantiationException, IllegalAccessException {
42        String systemProp = System.getProperty(factoryId);
43        if (systemProp != null) {
44            return newInstance(systemProp);
45        }
46
47        String providerName = findJarServiceProviderName(factoryId);
48        if (providerName != null && providerName.trim().length() > 0) {
49            return newInstance(providerName);
50        }
51
52        return null;
53    }
54
55    static Object newInstance(String className) throws ClassNotFoundException,
56            InstantiationException, IllegalAccessException {
57        Class providerClass = cl.loadClass(className);
58        Object instance = providerClass.newInstance();
59        return instance;
60    }
61
62    private static String findJarServiceProviderName(String factoryId) {
63        String serviceId = "META-INF/services/" + factoryId;
64        InputStream is;
65        is = cl.getResourceAsStream(serviceId);
66
67        if (is == null) {
68            return null;
69        }
70
71        String factoryClassName;
72        BufferedReader rd = null;
73        try {
74            rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
75            try {
76                factoryClassName = rd.readLine();
77            } catch (IOException x) {
78                return null;
79            }
80        } finally {
81            if (rd != null) {
82                try {
83                    rd.close();
84                } catch (IOException ex) {
85                    Logger.getLogger(FactoryFinder.class.getName()).log(Level.INFO, null, ex);
86                }
87            }
88        }
89
90        return factoryClassName;
91    }
92
93}
94