ServiceLoaderUtils.java
01 /*
02  * Copyright 2008-2014 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package griffon.util;
17 
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 
21 import javax.annotation.Nonnull;
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URL;
25 import java.util.Enumeration;
26 import java.util.Scanner;
27 
28 import static griffon.core.GriffonExceptionHandler.sanitize;
29 import static griffon.util.GriffonNameUtils.isBlank;
30 import static griffon.util.GriffonNameUtils.requireNonBlank;
31 import static java.util.Objects.requireNonNull;
32 
33 /**
34  @author Andres Almiray
35  @since 2.0.0
36  */
37 public class ServiceLoaderUtils {
38     private static final Logger LOG = LoggerFactory.getLogger(ServiceLoaderUtils.class);
39 
40     private ServiceLoaderUtils() {
41 
42     }
43 
44     public static interface LineProcessor {
45         void process(@Nonnull ClassLoader classLoader, @Nonnull Class<?> type, @Nonnull String line);
46     }
47 
48     public static boolean load(@Nonnull ClassLoader classLoader, @Nonnull String path, @Nonnull Class<?> type, @Nonnull LineProcessor processor) {
49         requireNonNull(classLoader, "Argument 'classLoader' must not be null");
50         requireNonBlank(path, "Argument 'path' must not be blank");
51         requireNonNull(type, "Argument 'type' must not be null");
52         requireNonNull(processor, "Argument 'processor' must not be null");
53         String normalizedPath = path.endsWith(File.separator? path : path + File.separator;
54 
55         Enumeration<URL> urls;
56 
57         try {
58             urls = classLoader.getResources(normalizedPath + type.getName());
59         catch (IOException ioe) {
60             return false;
61         }
62 
63         if (urls == nullreturn false;
64 
65         while (urls.hasMoreElements()) {
66             URL url = urls.nextElement();
67             LOG.debug("Reading {} definitions from {}", type.getName(), url);
68 
69             try (Scanner scanner = new Scanner(url.openStream())) {
70                 while (scanner.hasNextLine()) {
71                     String line = scanner.nextLine();
72                     if (line.startsWith("#"|| isBlank(line)) continue;
73                     processor.process(classLoader, type, line);
74                 }
75             catch (IOException e) {
76                 LOG.warn("Could not load " + type.getName() " definitions from " + url, sanitize(e));
77             }
78         }
79 
80         return true;
81     }
82 }