MCPcopy Index your code
hub / github.com/apache/tomcat / WebappServiceLoader

Class WebappServiceLoader

java/org/apache/catalina/startup/WebappServiceLoader.java:59–239  ·  view source on GitHub ↗

A variation of Java's JAR ServiceLoader that respects exclusion rules for web applications. Primarily intended for use loading ServletContainerInitializers as defined by Servlet 8.2.4. This implementation does not attempt lazy loading as the container is required to introspect all implementation

Source from the content-addressed store, hash-verified

57 * @see java.util.ServiceLoader
58 */
59public class WebappServiceLoader<T> {
60 private static final String CLASSES = "/WEB-INF/classes/";
61 private static final String LIB = "/WEB-INF/lib/";
62 private static final String SERVICES = "META-INF/services/";
63
64 private final Context context;
65 private final ServletContext servletContext;
66 private final Pattern containerSciFilterPattern;
67
68
69 /**
70 * Construct a loader to load services from a ServletContext.
71 *
72 * @param context the context to use
73 */
74 public WebappServiceLoader(Context context) {
75 this.context = context;
76 this.servletContext = context.getServletContext();
77 String containerSciFilter = context.getContainerSciFilter();
78 if (containerSciFilter != null && !containerSciFilter.isEmpty()) {
79 containerSciFilterPattern = Pattern.compile(containerSciFilter);
80 } else {
81 containerSciFilterPattern = null;
82 }
83 }
84
85
86 /**
87 * Load the providers for a service type. Container defined services will be loaded before application defined
88 * services in case the application depends on a Container provided service. Note that services are always loaded
89 * via the Context (web application) class loader so it is possible for an application to provide an alternative
90 * implementation of what would normally be a Container provided service.
91 *
92 * @param serviceType the type of service to load
93 *
94 * @return an unmodifiable collection of service providers
95 *
96 * @throws IOException if there was a problem loading any service
97 */
98 public List<T> load(Class<T> serviceType) throws IOException {
99 String configFile = SERVICES + serviceType.getName();
100
101 // Obtain the Container provided service configuration files.
102 ClassLoader loader = context.getParentClassLoader();
103 Enumeration<URL> containerResources;
104 if (loader == null) {
105 containerResources = ClassLoader.getSystemResources(configFile);
106 } else {
107 containerResources = loader.getResources(configFile);
108 }
109
110 // Extract the Container provided service class names. Each
111 // configuration file may list more than one service class name. This
112 // uses a LinkedHashSet so if a service class name appears more than
113 // once in the configuration files, only the first one found is used.
114 LinkedHashSet<String> containerServiceClassNames = new LinkedHashSet<>();
115 Set<URL> containerServiceConfigFiles = new HashSet<>();
116 while (containerResources.hasMoreElements()) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected