This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.boot;
|
package org.redkale.boot;
|
||||||
|
|
||||||
|
import org.redkale.util.RedkaleClassLoader;
|
||||||
import org.redkale.net.TransportGroupInfo;
|
import org.redkale.net.TransportGroupInfo;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
@@ -136,7 +137,7 @@ public final class Application {
|
|||||||
private final CountDownLatch serversLatch;
|
private final CountDownLatch serversLatch;
|
||||||
|
|
||||||
//根ClassLoader
|
//根ClassLoader
|
||||||
private final NodeClassLoader classLoader;
|
private final RedkaleClassLoader classLoader;
|
||||||
|
|
||||||
private Application(final AnyValue config) {
|
private Application(final AnyValue config) {
|
||||||
this(false, config);
|
this(false, config);
|
||||||
@@ -267,7 +268,7 @@ public final class Application {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.transportFactory = new TransportFactory(transportExec, transportPool, transportGroup);
|
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);
|
Thread.currentThread().setContextClassLoader(this.classLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -279,7 +280,7 @@ public final class Application {
|
|||||||
return transportFactory;
|
return transportFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NodeClassLoader getNodeClassLoader() {
|
public RedkaleClassLoader getClassLoader() {
|
||||||
return classLoader;
|
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());
|
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);
|
String lib = config.getValue("lib", "").trim().replace("${APP_HOME}", homepath);
|
||||||
lib = lib.isEmpty() ? (homepath + "/conf") : (lib + ";" + homepath + "/conf");
|
lib = lib.isEmpty() ? (homepath + "/conf") : (lib + ";" + homepath + "/conf");
|
||||||
Server.loadLib(logger, lib);
|
Server.loadLib(classLoader, logger, lib);
|
||||||
|
|
||||||
//------------------------------------------------------------------------
|
//------------------------------------------------------------------------
|
||||||
final AnyValue resources = config.getAnyValue("resources");
|
final AnyValue resources = config.getAnyValue("resources");
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.boot;
|
package org.redkale.boot;
|
||||||
|
|
||||||
|
import org.redkale.util.RedkaleClassLoader;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
@@ -54,7 +55,7 @@ public abstract class NodeServer {
|
|||||||
protected final Server server;
|
protected final Server server;
|
||||||
|
|
||||||
//ClassLoader
|
//ClassLoader
|
||||||
protected final NodeClassLoader classLoader;
|
protected final RedkaleClassLoader classLoader;
|
||||||
|
|
||||||
//当前Server的SNCP协议的组
|
//当前Server的SNCP协议的组
|
||||||
protected String sncpGroup = null;
|
protected String sncpGroup = null;
|
||||||
@@ -89,7 +90,7 @@ public abstract class NodeServer {
|
|||||||
this.resourceFactory = application.getResourceFactory().createChild();
|
this.resourceFactory = application.getResourceFactory().createChild();
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.logger = Logger.getLogger(this.getClass().getSimpleName());
|
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);
|
Thread.currentThread().setContextClassLoader(this.classLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,7 +159,7 @@ public abstract class NodeServer {
|
|||||||
|
|
||||||
final String homepath = myroot.getCanonicalPath();
|
final String homepath = myroot.getCanonicalPath();
|
||||||
//加入指定的classpath
|
//加入指定的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
|
//必须要进行初始化, 构建Service时需要使用Context中的ExecutorService
|
||||||
server.init(this.serverConf);
|
server.init(this.serverConf);
|
||||||
@@ -597,7 +598,7 @@ public abstract class NodeServer {
|
|||||||
return resourceFactory;
|
return resourceFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NodeClassLoader getNodeClassLoader() {
|
public RedkaleClassLoader getClassLoader() {
|
||||||
return classLoader;
|
return classLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
package org.redkale.net;
|
package org.redkale.net;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.text.*;
|
import java.text.*;
|
||||||
@@ -14,7 +13,7 @@ import java.util.*;
|
|||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.logging.Logger;
|
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);
|
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];
|
if (lib == null || lib.isEmpty()) return new URL[0];
|
||||||
final Set<URL> set = new HashSet<>();
|
final Set<URL> set = new HashSet<>();
|
||||||
for (String s : lib.split(";")) {
|
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];
|
if (set.isEmpty()) return new URL[0];
|
||||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
for (URL url : set) {
|
||||||
if (cl instanceof URLClassLoader) {
|
classLoader.addURL(url);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
List<URL> list = new ArrayList<>(set);
|
List<URL> list = new ArrayList<>(set);
|
||||||
Collections.sort(list, (URL o1, URL o2) -> o1.getFile().compareTo(o2.getFile()));
|
Collections.sort(list, (URL o1, URL o2) -> o1.getFile().compareTo(o2.getFile()));
|
||||||
|
|||||||
34
src/org/redkale/util/RedkaleClassLoader.java
Normal file
34
src/org/redkale/util/RedkaleClassLoader.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user