This commit is contained in:
Redkale
2017-06-02 14:00:14 +08:00
parent dc3f318949
commit 5f2c2a9f2c
5 changed files with 48 additions and 76 deletions

View File

@@ -5,6 +5,7 @@
*/
package org.redkale.boot;
import org.redkale.util.RedkaleClassLoader;
import org.redkale.net.TransportGroupInfo;
import java.io.*;
import java.lang.reflect.*;
@@ -136,7 +137,7 @@ public final class Application {
private final CountDownLatch serversLatch;
//根ClassLoader
private final NodeClassLoader classLoader;
private final RedkaleClassLoader classLoader;
private Application(final AnyValue config) {
this(false, config);
@@ -267,7 +268,7 @@ public final class Application {
}
}
this.transportFactory = new TransportFactory(transportExec, transportPool, transportGroup);
this.classLoader = new NodeClassLoader((URLClassLoader)Thread.currentThread().getContextClassLoader());
this.classLoader = new RedkaleClassLoader(Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(this.classLoader);
}
@@ -279,7 +280,7 @@ public final class Application {
return transportFactory;
}
public NodeClassLoader getNodeClassLoader() {
public RedkaleClassLoader getClassLoader() {
return classLoader;
}
@@ -310,7 +311,7 @@ public final class Application {
logger.log(Level.INFO, RESNAME_APP_HOME + "= " + homepath + "\r\n" + RESNAME_APP_ADDR + "= " + this.localAddress.getHostAddress());
String lib = config.getValue("lib", "").trim().replace("${APP_HOME}", homepath);
lib = lib.isEmpty() ? (homepath + "/conf") : (lib + ";" + homepath + "/conf");
Server.loadLib(logger, lib);
Server.loadLib(classLoader, logger, lib);
//------------------------------------------------------------------------
final AnyValue resources = config.getAnyValue("resources");

View File

@@ -1,54 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.boot;
import java.lang.reflect.Method;
import java.net.*;
/**
*
* @author zhangjx
*/
public class NodeClassLoader extends URLClassLoader {
private static final Method addURLMethod;
static {
Method m = null;
try {
m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
m.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
addURLMethod = m;
}
public NodeClassLoader(URLClassLoader parent) {
super(new URL[0], parent);
}
public Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
@Override
public void addURL(URL url) {
try {
addURLMethod.invoke(getParent(), url);
} catch (RuntimeException re) {
throw re;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public URL[] getURLs() {
return ((URLClassLoader) getParent()).getURLs();
}
}

View File

@@ -5,6 +5,7 @@
*/
package org.redkale.boot;
import org.redkale.util.RedkaleClassLoader;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
@@ -54,7 +55,7 @@ public abstract class NodeServer {
protected final Server server;
//ClassLoader
protected final NodeClassLoader classLoader;
protected final RedkaleClassLoader classLoader;
//当前Server的SNCP协议的组
protected String sncpGroup = null;
@@ -89,7 +90,7 @@ public abstract class NodeServer {
this.resourceFactory = application.getResourceFactory().createChild();
this.server = server;
this.logger = Logger.getLogger(this.getClass().getSimpleName());
this.classLoader = new NodeClassLoader((URLClassLoader)Thread.currentThread().getContextClassLoader());
this.classLoader = new RedkaleClassLoader(Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(this.classLoader);
}
@@ -158,7 +159,7 @@ public abstract class NodeServer {
final String homepath = myroot.getCanonicalPath();
//加入指定的classpath
Server.loadLib(logger, this.serverConf.getValue("lib", "").replace("${APP_HOME}", homepath) + ";" + homepath + "/lib/*;" + homepath + "/classes");
Server.loadLib(classLoader, logger, this.serverConf.getValue("lib", "").replace("${APP_HOME}", homepath) + ";" + homepath + "/lib/*;" + homepath + "/classes");
}
//必须要进行初始化, 构建Service时需要使用Context中的ExecutorService
server.init(this.serverConf);
@@ -597,7 +598,7 @@ public abstract class NodeServer {
return resourceFactory;
}
public NodeClassLoader getNodeClassLoader() {
public RedkaleClassLoader getClassLoader() {
return classLoader;
}

View File

@@ -6,7 +6,6 @@
package org.redkale.net;
import java.io.*;
import java.lang.reflect.Method;
import java.net.*;
import java.nio.charset.Charset;
import java.text.*;
@@ -14,7 +13,7 @@ import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
import org.redkale.util.AnyValue;
import org.redkale.util.*;
/**
*
@@ -217,7 +216,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
return new DecimalFormat(sf);
}
public static URL[] loadLib(final Logger logger, final String lib) throws Exception {
public static URL[] loadLib(final RedkaleClassLoader classLoader, final Logger logger, final String lib) throws Exception {
if (lib == null || lib.isEmpty()) return new URL[0];
final Set<URL> set = new HashSet<>();
for (String s : lib.split(";")) {
@@ -235,17 +234,8 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
}
}
if (set.isEmpty()) return new URL[0];
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl instanceof URLClassLoader) {
URLClassLoader loader = (URLClassLoader) cl;
final Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
for (URL url : set) {
method.invoke(loader, url);
//if (logger != null) logger.log(Level.INFO, "Server found ClassPath({0})", url);
}
} else {
Thread.currentThread().setContextClassLoader(new URLClassLoader(set.toArray(new URL[set.size()]), cl));
for (URL url : set) {
classLoader.addURL(url);
}
List<URL> list = new ArrayList<>(set);
Collections.sort(list, (URL o1, URL o2) -> o1.getFile().compareTo(o2.getFile()));

View File

@@ -0,0 +1,34 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.util;
import java.net.*;
/**
*
* @author zhangjx
*/
public class RedkaleClassLoader extends URLClassLoader {
public RedkaleClassLoader(ClassLoader parent) {
super(new URL[0], parent);
}
public Class<?> loadClass(String name, byte[] b) {
return defineClass(name, b, 0, b.length);
}
@Override
public void addURL(URL url) {
super.addURL(url);
}
@Override
public URL[] getURLs() {
return super.getURLs();
}
}