Skip to content

Commit 5f8dbe0

Browse files
Commit attempt 7 - Adding FileRemove interface and RemoveFile class for removing file in Uninstall use case.
1 parent 1f5c1d8 commit 5f8dbe0

File tree

4 files changed

+71
-18
lines changed

4 files changed

+71
-18
lines changed

src/main/java/org/hydev/mcpm/client/injector/UnloadBoundary.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ public interface UnloadBoundary
1111
* Dynamically unload a local plugin through JVM reflections and classloader hacks
1212
*
1313
* @param name Loaded plugin name
14-
* @return Jar file
1514
* @throws PluginNotFoundException If a loaded plugin of the name isn't found
1615
*/
17-
File unloadPlugin(String name) throws PluginNotFoundException;
16+
void unloadPlugin(String name) throws PluginNotFoundException;
1817
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.hydev.mcpm.client.uninstall;
2+
3+
import org.hydev.mcpm.client.injector.LocalJarBoundary;
4+
import org.hydev.mcpm.client.injector.PluginNotFoundException;
5+
6+
import java.io.File;
7+
8+
import static org.hydev.mcpm.client.uninstall.UninstallResult.State.FAILED_TO_DELETE;
9+
import static org.hydev.mcpm.client.uninstall.UninstallResult.State.NOT_FOUND;
10+
11+
/**
12+
* Interface for removing a file
13+
*/
14+
15+
public interface FileRemove {
16+
/**
17+
* Remove file for given plugin (step 2 and 3 of Uninstaller use case)
18+
* @param pluginName
19+
* @return int 0 - true, 1 - NOT_FOUND, 2 - FAILED_TO_DELETE
20+
*/
21+
int removeFile(String pluginName);
22+
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hydev.mcpm.client.uninstall;
2+
3+
import org.hydev.mcpm.client.injector.LocalJarBoundary;
4+
import org.hydev.mcpm.client.injector.PluginNotFoundException;
5+
import org.hydev.mcpm.client.uninstall.FileRemove;
6+
7+
import java.io.File;
8+
9+
/**
10+
* Removes file for FileRemove.java
11+
*/
12+
public class RemoveFile implements FileRemove{
13+
private final LocalJarBoundary jarFinder;
14+
15+
public RemoveFile(LocalJarBoundary jarFinder) {
16+
this.jarFinder = jarFinder;
17+
}
18+
19+
public int removeFile(String pluginName) {
20+
// 2. If it isn't loaded, find the plugin jar file in local file system
21+
// (This will throw PluginNotFoundException when a plugin of the name in the file system
22+
// could not be found).
23+
File jar = null;
24+
try {
25+
jar = jarFinder.findJar(pluginName);
26+
} catch (PluginNotFoundException e) {
27+
return 1;
28+
}
29+
30+
// 3. Delete plugin jar
31+
if (!jar.delete()) {
32+
return 2;
33+
}
34+
return 0;
35+
}
36+
}

src/main/java/org/hydev/mcpm/client/uninstall/Uninstaller.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.hydev.mcpm.client.injector.LocalJarBoundary;
55
import org.hydev.mcpm.client.injector.PluginNotFoundException;
66
import org.hydev.mcpm.client.injector.UnloadBoundary;
7+
import org.hydev.mcpm.client.uninstall.FileRemove;
78

89
import java.io.File;
910

@@ -29,40 +30,33 @@ public Uninstaller(PluginTracker tracker, UnloadBoundary unloader, LocalJarBound
2930
this.tracker = tracker;
3031
this.unloader = unloader;
3132
this.jarFinder = jarFinder;
33+
3234
}
3335

3436
@Override
35-
public UninstallResult uninstall(UninstallInput input) {
37+
public UninstallResult uninstall(UninstallInput input) {
3638
// 1. Unload plugin
3739
// This will throw PluginNotFoundException if the plugin isn't loaded. If it is loaded,
3840
// this will return the jar of the currently loaded plugin, which is the most precise.
3941
// Since people may still uninstall a plugin when it's not loaded, we ignore the not
4042
// found error here.
41-
File jar = null;
4243
if (unloader != null) {
4344
try {
44-
jar = unloader.unloadPlugin(input.name());
45+
unloader.unloadPlugin(input.name());
4546
}
4647
catch (PluginNotFoundException ignored) { }
4748
}
4849

49-
// 2. If it isn't loaded, find the plugin jar file in local file system
50-
if (jar == null) {
51-
// This will throw PluginNotFoundException when a plugin of the name in the file system
52-
// could not be found.
53-
try {
54-
jar = jarFinder.findJar(input.name());
55-
}
56-
catch (PluginNotFoundException e) {
57-
return new UninstallResult(NOT_FOUND);
58-
}
59-
}
50+
// Steps 2. + 3.
51+
RemoveFile fileRemover = new RemoveFile(jarFinder);
6052

61-
// 3. Delete plugin jar
62-
if (!jar.delete()) {
53+
if (fileRemover.removeFile(input.name())==1) {
54+
return new UninstallResult(NOT_FOUND);
55+
} else if (fileRemover.removeFile(input.name())==2) {
6356
return new UninstallResult(FAILED_TO_DELETE);
6457
}
6558

59+
6660
// 4. Remove manually installed flag from the tracker
6761
tracker.removeEntry(input.name());
6862
var result = new UninstallResult(SUCCESS);
@@ -85,4 +79,5 @@ public UninstallResult uninstall(UninstallInput input) {
8579

8680
return result;
8781
}
82+
8883
}

0 commit comments

Comments
 (0)