1 /**
2 * Copyright 2006-2016 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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 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.List;
26
27 /**
28 * This class holds methods useful for constructing custom classloaders.
29 *
30 * @author Jeff Butler
31 *
32 */
33 public class ClassloaderUtility {
34
35 /**
36 * Utility Class - No Instances
37 */
38 private ClassloaderUtility() {
39 }
40
41 public static ClassLoader getCustomClassloader(List<String> entries) {
42 List<URL> urls = new ArrayList<URL>();
43 File file;
44
45 if (entries != null) {
46 for (String classPathEntry : entries) {
47 file = new File(classPathEntry);
48 if (!file.exists()) {
49 throw new RuntimeException(getString(
50 "RuntimeError.9", classPathEntry)); //$NON-NLS-1$
51 }
52
53 try {
54 urls.add(file.toURI().toURL());
55 } catch (MalformedURLException e) {
56 // this shouldn't happen, but just in case...
57 throw new RuntimeException(getString(
58 "RuntimeError.9", classPathEntry)); //$NON-NLS-1$
59 }
60 }
61 }
62
63 ClassLoader parent = Thread.currentThread().getContextClassLoader();
64
65 URLClassLoader ucl = new URLClassLoader(urls.toArray(new URL[urls
66 .size()]), parent);
67
68 return ucl;
69 }
70 }