All is well
A tool is implemented in the project (a Java project that runs independently, which is packaged into a jar package and executed through java-jar **. jar .), This tool can implement some business functions through configuration,
The tool provides interfaces and abstract classes for others to expand its functions.
This involves a problem: when others expand it, they need to introduce some jar or configuration files. The jar and configuration files on which the tool depends are recorded in the manifest file,
It is impossible for someone else to modify the manifest file when adding the jar package and configuration file.
Therefore, I provide another portal for the tool to configure the path through the configuration file. The configuration file is defined as follows because there may be multiple or multiple groups of extension users:
Paths starting with ext_classpath, such as ext_classpath_biz1, are added to classpath.
Paths starting with ext_resourcepath, such as ext_resourcepath_biz1, are added to classpath.
The code is implemented as follows:
Package com. bz. utils;
Import java. io. File;
Import java. lang. reflect. Method;
Import java.net. URL;
Import java.net. URLClassLoader;
Import java. util. List;
/***//**
* Load the jar and configuration files to the classpath according to the path configured in properties.
* @ Author jnbzwm
*
*/
Public final class ExtClasspathLoader {
/*** // ** AddURL method of URLClassLoader */
Private static Method addURL = initAddMethod ();
Private static URLClassLoader classloader = (URLClassLoader) ClassLoader. getSystemClassLoader ();
/***//**
* Initialize the addUrl method.
* @ Return Method object that can access the addUrl Method
*/
Private static Method initAddMethod (){
Try {
Method add = URLClassLoader. class. getDeclaredMethod ("addURL", new Class [] {URL. class });
Add. setAccessible (true );
Return add;
}
Catch (Exception e ){
Throw new RuntimeException (e );
}
}
/***//**
* Load jar classpath.
*/
Public static void loadClasspath (){
List <String> files = getJarFiles ();
For (String f: files ){
LoadClasspath (f );
}
List <String> resFiles = getResFiles ();
For (String r: resFiles ){
LoadResourceDir (r );
}
}
Private static void loadClasspath (String filepath ){
File file = new File (filepath );
LoopFiles (file );
}
Private static void loadResourceDir (String filepath ){
File file = new File (filepath );
LoopDirs (file );
}
/***//**
* Traverse the directory cyclically to find all resource paths.
* @ Param file: current file Traversal
*/
Private static void loopDirs (File file ){
// Resource file loading path only
If (file. isDirectory ()){
AddURL (file );
File [] tmps = file. listFiles ();
For (File tmp: tmps ){
LoopDirs (tmp );
}
}
}
/***//**
* Traverse the directory cyclically to find all jar packages.
* @ Param file: current file Traversal
*/
Private static void loopFiles (File file ){
If (file. isDirectory ()){
File [] tmps = file. listFiles ();
For (File tmp: tmps ){
LoopFiles (tmp );
}
}
Else {
If (file. getAbsolutePath (). endsWith (". jar") | file. getAbsolutePath (). endsWith (". zip ")){
AddURL (file );
}
}
}
/***//**
* Use filepath to load files to classpath.
* @ Param filePath: file path
* @ Return URL
* @ Throws Exception
*/
Private static void addURL (File file ){
Try {
AddURL. invoke (classloader, new Object [] {file. toURI (). toURL ()});
}
Catch (Exception e ){
}
}
/***//**
* Obtain the path set from the configuration file that needs to be loaded into classpath.
* @ Return
*/
Private static List <String> getJarFiles (){
// TODO reads configuration information from the properties File
Return null;
}
/***//**
* Obtain the resource path set in the classpath to be loaded from the configuration file.
* @ Return
*/
Private static List <String> getResFiles (){
// TODO reads configuration information from the properties File
Return null;
}
Public static void main (String [] args ){
ExtClasspathLoader. loadClasspath ();
}
}