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.io.File;
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.net.URLClassLoader;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
28 import org.mybatis.generator.logging.Log;
29 import org.mybatis.generator.logging.LogFactory;
30
31
32
33
34
35
36 public class ClassloaderUtility {
37
38 private static final Log LOG = LogFactory.getLog(ClassloaderUtility.class);
39
40
41
42
43 private ClassloaderUtility() {
44 }
45
46 public static ClassLoader getCustomClassloader(Collection<String> entries) {
47 List<URL> urls = new ArrayList<>();
48 File file;
49
50 if (entries != null) {
51 for (String classPathEntry : entries) {
52 file = new File(classPathEntry);
53 if (!file.exists()) {
54 LOG.warn(getString("Warning.31", classPathEntry));
55 continue;
56 }
57
58 try {
59 urls.add(file.toURI().toURL());
60 } catch (MalformedURLException e) {
61
62 throw new RuntimeException(getString(
63 "RuntimeError.9", classPathEntry));
64 }
65 }
66 }
67
68 ClassLoader parent = Thread.currentThread().getContextClassLoader();
69
70 return new URLClassLoader(urls.toArray(new URL[urls.size()]), parent);
71 }
72 }