1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.internal.util;
17
18 import static org.mybatis.generator.internal.util.messages.Messages.getString;
19
20 import java.net.MalformedURLException;
21 import java.net.URL;
22 import java.net.URLClassLoader;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.mybatis.generator.exception.InternalException;
32
33
34
35
36
37
38 public class ClassloaderUtility {
39
40 private static final Log LOG = LogFactory.getLog(ClassloaderUtility.class);
41
42
43
44
45 private ClassloaderUtility() {
46 }
47
48 public static ClassLoader getCustomClassloader(Collection<String> entries) {
49 List<URL> urls = new ArrayList<>();
50 Path file;
51
52 for (String classPathEntry : entries) {
53 file = Path.of(classPathEntry);
54 if (Files.notExists(file)) {
55 LOG.warn(getString("Warning.31", classPathEntry));
56 continue;
57 }
58
59 try {
60 urls.add(file.toUri().toURL());
61 } catch (MalformedURLException e) {
62
63 throw new InternalException(getString("RuntimeError.9", classPathEntry));
64 }
65 }
66
67 ClassLoader parent = Thread.currentThread().getContextClassLoader();
68
69 return new URLClassLoader(urls.toArray(new URL[0]), parent);
70 }
71 }