15
15
* credit to Alviann (https://github.com/Alviannn/DependencyHelper)
16
16
*/
17
17
public class DependencyHelper {
18
+
18
19
private final ClassLoader classLoader ;
19
20
20
21
public DependencyHelper (ClassLoader classLoader ) {
21
22
this .classLoader = classLoader ;
22
23
}
23
24
24
- public DependencyHelper (Class <?> clazz ) {
25
- this .classLoader = clazz .getClassLoader ();
26
- }
27
-
28
25
/**
29
26
* downloads a dependency file
30
27
*
@@ -55,89 +52,11 @@ public void download(String fileName, String fileUrl, Path dirPath) throws IOExc
55
52
}
56
53
}
57
54
58
- /**
59
- * downloads a dependency file
60
- * <p>
61
- * without file name parameter, it will automatically take the name from the URL (might not work sometimes)
62
- *
63
- * @param fileUrl the file url
64
- * @param dirPath the directory path
65
- * @throws IOException if the download fails
66
- */
67
- @ Deprecated
68
- @ SuppressWarnings ("ResultOfMethodCallIgnored" )
69
- public void download (String fileUrl , Path dirPath ) throws IOException {
70
- File dir = dirPath .toFile ();
71
- if (!dir .exists ()) {
72
- dir .mkdir ();
73
- }
74
-
75
- String [] strArray = fileUrl .split ("/" );
76
- String fileName = strArray [strArray .length - 1 ];
77
-
78
- File file = new File (dir , fileName );
79
- if (file .exists ()) {
80
- return ;
81
- }
82
-
83
- try {
84
- URL url = new URL (fileUrl );
85
- try (InputStream stream = url .openStream ()) {
86
- Files .copy (stream , file .toPath ());
87
- }
88
- } catch (Exception e ) {
89
- throw new IOException ("Failed to download " + fileName );
90
- }
91
- }
92
-
93
- /**
94
- * loads the dependency file
95
- *
96
- * @param fileName the name
97
- * @param fileUrl the file url
98
- * @param dirPath the directory path
99
- * @throws IOException if the dependency file doesn't exists
100
- * @throws IllegalAccessException if the dependency file isn't a .jar
101
- */
102
- public void load (String fileName , String fileUrl , Path dirPath ) throws IOException , IllegalAccessException {
103
- File dir = dirPath .toFile ();
104
- if (!dir .exists ()) {
105
- return ;
106
- }
107
-
108
- File file = new File (dir , fileName );
109
- if (!file .exists ()) {
110
- throw new FileNotFoundException ("Cannot file " + fileName + "!" );
111
- }
112
- if (!file .getName ().endsWith (".jar" )) {
113
- throw new IllegalAccessException ("Cannot load file that aren't .jar(s)!" );
114
- }
115
-
116
- ClassLoader previousLoader = Thread .currentThread ().getContextClassLoader ();
117
- Thread .currentThread ().setContextClassLoader (classLoader );
118
-
119
- if (classLoader instanceof URLClassLoader ) {
120
- URLClassLoader loader = (URLClassLoader ) classLoader ;
121
- try {
122
- Method addURL = URLClassLoader .class .getDeclaredMethod ("addURL" , URL .class );
123
- addURL .setAccessible (true );
124
- addURL .invoke (loader , file .toURI ().toURL ());
125
- } catch (Exception e ) {
126
- Thread .currentThread ().setContextClassLoader (previousLoader );
127
- throw new RuntimeException ("Failed to load " + fileName + "!" );
128
- }
129
- } else {
130
- Thread .currentThread ().setContextClassLoader (previousLoader );
131
- throw new RuntimeException ("Failed to cast class loader!" );
132
- }
133
- Thread .currentThread ().setContextClassLoader (previousLoader );
134
- }
135
-
136
55
/**
137
56
* loads the dependency file
138
57
*
139
58
* @param file the file
140
- * @throws IOException if the dependency file doesn't exists
59
+ * @throws IOException if the dependency file doesn't exist
141
60
* @throws IllegalAccessException if the dependency file isn't a .jar
142
61
*/
143
62
public void load (File file ) throws IOException , IllegalAccessException {
@@ -172,7 +91,7 @@ public void load(File file) throws IOException, IllegalAccessException {
172
91
* loads all dependencies on a specific directory
173
92
*
174
93
* @param dirPath the directory path
175
- * @throws IOException if the dependency file doesn't exists
94
+ * @throws IOException if the dependency file doesn't exist
176
95
* @throws IllegalAccessException if the dependency file isn't a .jar
177
96
*/
178
97
@ SuppressWarnings ("ConstantConditions" )
@@ -190,20 +109,6 @@ public void loadDir(Path dirPath) throws IOException, IllegalAccessException {
190
109
}
191
110
}
192
111
193
- /**
194
- * loads the dependencies
195
- *
196
- * @param dependencies the dependencies
197
- * @param dirPath the directory path
198
- * @throws IOException if the dependency file doesn't exists
199
- * @throws IllegalAccessException if the dependency file isn't a .jar
200
- */
201
- public void load (Map <String , String > dependencies , Path dirPath ) throws IOException , IllegalAccessException {
202
- for (Map .Entry <String , String > entry : dependencies .entrySet ()) {
203
- this .load (entry .getKey (), entry .getValue (), dirPath );
204
- }
205
- }
206
-
207
112
/**
208
113
* downloads the dependencies
209
114
*
0 commit comments