| 1 | |
package com.ctrip.apollo.core.utils; |
| 2 | |
|
| 3 | |
import org.slf4j.Logger; |
| 4 | |
import org.slf4j.LoggerFactory; |
| 5 | |
|
| 6 | |
import java.io.UnsupportedEncodingException; |
| 7 | |
import java.net.URL; |
| 8 | |
import java.net.URLDecoder; |
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | 0 | public class ClassLoaderUtil { |
| 14 | 1 | private static final Logger logger = LoggerFactory.getLogger(ClassLoaderUtil.class); |
| 15 | |
|
| 16 | 1 | private static ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 17 | 1 | private static String classPath = ""; |
| 18 | |
|
| 19 | |
static { |
| 20 | 1 | if (loader == null) { |
| 21 | 0 | logger.info("Using system class loader"); |
| 22 | 0 | loader = ClassLoader.getSystemClassLoader(); |
| 23 | |
} |
| 24 | |
|
| 25 | |
try { |
| 26 | 1 | URL url = loader.getResource(""); |
| 27 | |
|
| 28 | 1 | classPath = url.getPath(); |
| 29 | 1 | classPath = URLDecoder.decode(classPath, "utf-8"); |
| 30 | |
|
| 31 | |
|
| 32 | 1 | if (classPath.contains(".jar!")) { |
| 33 | 0 | logger.warn("using config file inline jar!"); |
| 34 | 0 | classPath = System.getProperty("user.dir"); |
| 35 | |
} |
| 36 | 0 | } catch (UnsupportedEncodingException e) { |
| 37 | 0 | e.printStackTrace(); |
| 38 | 1 | } |
| 39 | 1 | } |
| 40 | |
|
| 41 | |
public static ClassLoader getLoader() { |
| 42 | 1 | return loader; |
| 43 | |
} |
| 44 | |
|
| 45 | |
|
| 46 | |
public static String getClassPath() { |
| 47 | 0 | return classPath; |
| 48 | |
} |
| 49 | |
} |