Skip to content

Package: FactoryFinder

FactoryFinder

nameinstructionbranchcomplexitylinemethod
FactoryFinder()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
find(Class, String, String, Properties)
M: 68 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%
getFactoryClassName(String)
M: 36 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
newInstance(String, ClassLoader, Properties)
M: 61 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: * Copyright 2004 The Apache Software Foundation
5: *
6: * Licensed under the Apache License, Version 2.0 (the "License");
7: * you may not use this file except in compliance with the License.
8: * You may obtain a copy of the License at
9: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package jakarta.el;
20:
21: import static java.io.File.separator;
22:
23: import java.io.File;
24: import java.io.FileInputStream;
25: import java.lang.reflect.Constructor;
26: import java.util.Iterator;
27: import java.util.Properties;
28: import java.util.ServiceLoader;
29:
30: class FactoryFinder {
31:
32: /**
33: * Creates an instance of the specified class using the specified <code>ClassLoader</code> object.
34: *
35: * @exception ELException if the given class could not be found or could not be instantiated
36: */
37: private static Object newInstance(String className, ClassLoader classLoader, Properties properties) {
38: try {
39: Class<?> spiClass;
40:• if (classLoader == null) {
41: spiClass = Class.forName(className);
42: } else {
43: spiClass = classLoader.loadClass(className);
44: }
45:
46:• if (properties != null) {
47: Constructor<?> constr = null;
48: try {
49: constr = spiClass.getConstructor(Properties.class);
50: } catch (Exception ex) {
51: }
52:
53:• if (constr != null) {
54: return constr.newInstance(properties);
55: }
56: }
57:
58: return spiClass.getDeclaredConstructor().newInstance();
59:
60: } catch (ClassNotFoundException x) {
61: throw new ELException("Provider " + className + " not found", x);
62: } catch (Exception x) {
63: throw new ELException("Provider " + className + " could not be instantiated: " + x, x);
64: }
65: }
66:
67: /**
68: * Finds the implementation <code>Class</code> object for the given factory. The following search order is used:
69: * <ol>
70: * <li>{@link ServiceLoader} lookup using <code>serviceClass</code></li>
71: * <li>Property file located as <code>$java.home/lib/el.properties</code></li>
72: * <li>System property lookup using <code>factoryId</code></li>
73: * <li>Create an instance of <code>fallbackClassName</code></li>
74: * </ol>
75: * This method is package private so that this code can be shared.
76: *
77: * @return the <code>Class</code> object of the specified message factory; may not be <code>null</code>
78: *
79: * @param serviceClass The class to use when searching for the factory using the ServiceLoader mechanism
80: * @param factoryId the name of the factory to find, which is a system property
81: * @param fallbackClassName the implementation class name, which is to be used only if nothing else is found;
82: * <code>null</code> to indicate that there is no fallback class name
83: * @exception ELException if there is an error
84: */
85: static Object find(Class<?> serviceClass, String factoryId, String fallbackClassName, Properties properties) {
86: ClassLoader classLoader;
87: try {
88: classLoader = Thread.currentThread().getContextClassLoader();
89: } catch (Exception x) {
90: throw new ELException(x.toString(), x);
91: }
92:
93: // try to find services in CLASSPATH
94: try {
95: ServiceLoader<?> serviceLoader = ServiceLoader.load(serviceClass, classLoader);
96: Iterator<?> iter = serviceLoader.iterator();
97:• while (iter.hasNext()) {
98: Object service = iter.next();
99:• if (service != null) {
100: return service;
101: }
102: }
103: } catch (Exception ex) {
104: }
105:
106: // Try to read from $java.home/lib/el.properties
107: try {
108: String factoryClassName = getFactoryClassName(factoryId);
109:• if (factoryClassName != null) {
110: return newInstance(factoryClassName, classLoader, properties);
111: }
112: } catch (Exception ex) {
113: }
114:
115: // Use the system property
116: String systemProp = System.getProperty(factoryId);
117:• if (systemProp != null) {
118: return newInstance(systemProp, classLoader, properties);
119: }
120:
121:• if (fallbackClassName == null) {
122: throw new ELException("Provider for " + factoryId + " cannot be found", null);
123: }
124:
125: return newInstance(fallbackClassName, classLoader, properties);
126: }
127:
128: private static String getFactoryClassName(String factoryId) {
129: String factoryClass = null;
130: String javah = System.getProperty("java.home");
131: String configFileName = javah + separator + "lib" + separator + "el.properties";
132:
133: File configFile = new File(configFileName);
134:• if (configFile.exists()) {
135: Properties props = new Properties();
136: try {
137: props.load(new FileInputStream(configFile));
138: } catch(Exception e) {}
139: factoryClass = props.getProperty(factoryId);
140: }
141: return factoryClass;
142: }
143: }