Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit d7a87bc

Browse files
Merge pull request #13 from Simplix-Softworks/shading
Hotfix
2 parents d244f9c + daf1720 commit d7a87bc

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

simplixcore-common/simplixcore-common-api/src/main/java/dev/simplix/core/common/inject/SimplixInstaller.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
import java.util.function.Consumer;
3030
import java.util.function.Supplier;
3131
import java.util.logging.Logger;
32-
import lombok.*;
32+
import lombok.AllArgsConstructor;
33+
import lombok.Cleanup;
34+
import lombok.NonNull;
35+
import lombok.SneakyThrows;
3336
import org.jetbrains.annotations.Nullable;
3437
import org.reflections.Reflections;
3538

@@ -51,12 +54,12 @@ public class SimplixInstaller {
5154
.registerTypeAdapter(UpdatePolicy.class, new UpdatePolicyDeserializer())
5255
.create();
5356
private final Timer updateTimer = new Timer("UpdateTimer");
57+
private final org.slf4j.Logger log;
5458
private Platform platform;
5559
private Injector bossInjector;
5660
private DependencyLoader dependencyLoader;
5761
private LibraryLoader libraryLoader;
5862
private Updater updater;
59-
private final org.slf4j.Logger log;
6063

6164
public SimplixInstaller(org.slf4j.Logger logger) {
6265
this.log = logger;
@@ -103,13 +106,31 @@ public Optional<DependencyLoadingException> register(
103106
@NonNull Class<?> owner,
104107
@NonNull Consumer<Exception> onException,
105108
@NonNull Module... modules) {
109+
return register(owner, onException, false, modules);
110+
}
111+
112+
/**
113+
* This will register a class annotated with {@link SimplixApplication}.
114+
*
115+
* @param owner The main class of the application
116+
* @param onException Will accept the exception if some exception occurs while installing
117+
* @param isLibrary Defines whether the application is a library or not
118+
* @param modules Pre-constructed modules which shall be available in the injection context
119+
* @throws IllegalArgumentException when the owner class is not annotated with {@link
120+
* SimplixApplication}
121+
*/
122+
public Optional<DependencyLoadingException> register(
123+
@NonNull Class<?> owner,
124+
@NonNull Consumer<Exception> onException,
125+
boolean isLibrary,
126+
@NonNull Module... modules) {
106127
if (!owner.isAnnotationPresent(SimplixApplication.class)) {
107128
throw new IllegalArgumentException("Owner class must be annotated with @SimplixApplication");
108129
}
109130
SimplixApplication application = owner.getAnnotation(SimplixApplication.class);
110131
if (this.toInstall.containsKey(application.name())) {
111132
log.debug(SIMPLIX_BOOTSTRAP + application.name() + " is already registered. Please check " +
112-
"for unnecessary double registration of your application.");
133+
"for unnecessary double registration of your application.");
113134
return Optional.empty();
114135
}
115136
Set<String> basePackages = determineBasePackages(owner);
@@ -126,6 +147,7 @@ public Optional<DependencyLoadingException> register(
126147
new InstallationContext(owner, new Reflections(basePackages, owner.getClassLoader()),
127148
info, detectReferencedModules(owner, modules, application.name()), onException));
128149
final Optional<DependencyLoadingException> optionalDependencyLoadingException = processRemoteDependencies(
150+
isLibrary,
129151
owner,
130152
info);
131153
if (this.bossInjector != null) {
@@ -384,10 +406,14 @@ private boolean installApplication(
384406
* @throws JsonParseException If the dependencies.json file is formatted invalidly
385407
*/
386408
@SneakyThrows
387-
private Optional<DependencyManifest> loadDependencies(@NonNull Class<?> appOwner)
409+
private Optional<DependencyManifest> loadDependencies(
410+
boolean isLibrary,
411+
@NonNull Class<?> appOwner)
388412
throws JsonParseException {
389413
@Cleanup
390-
InputStream inputStream = appOwner.getResourceAsStream("/dependencies.json");
414+
InputStream inputStream = isLibrary
415+
? appOwner.getResourceAsStream("/library-dependencies.json")
416+
: appOwner.getResourceAsStream("/dependencies.json");
391417

392418
if (inputStream == null) {
393419
return Optional.empty();
@@ -425,10 +451,11 @@ public Optional<DependencyLoadingException> earlyLoadDependencies(
425451
this.platform = platform;
426452
ApplicationInfo tempInfo = new ApplicationInfo(appClass.getSimpleName(),
427453
"1.0", new String[0], new File("."), new String[0]);
428-
return processRemoteDependencies(appClass, tempInfo);
454+
return processRemoteDependencies(false, appClass, tempInfo);
429455
}
430456

431457
private Optional<DependencyLoadingException> processRemoteDependencies(
458+
boolean isLibrary,
432459
@NonNull Class<?> appOwner,
433460
@NonNull ApplicationInfo info) {
434461
if (this.dependencyLoader == null) {
@@ -441,7 +468,7 @@ private Optional<DependencyLoadingException> processRemoteDependencies(
441468
Optional<DependencyManifest> optionalDependencies;
442469

443470
try {
444-
optionalDependencies = loadDependencies(appOwner);
471+
optionalDependencies = loadDependencies(isLibrary, appOwner);
445472
} catch (JsonParseException jsonParseException) {
446473
log.error(
447474
SIMPLIX_BOOTSTRAP + info.name() + ": Cannot parse dependencies.json",
@@ -462,10 +489,10 @@ private Optional<DependencyLoadingException> processRemoteDependencies(
462489
continue;
463490
}
464491
log.debug(SIMPLIX_BOOTSTRAP
465-
+ info.name()
466-
+ ": Load dependency "
467-
+ dependency
468-
+ " from repository...");
492+
+ info.name()
493+
+ ": Load dependency "
494+
+ dependency
495+
+ " from repository...");
469496
dependency.applicationName(info.name());
470497
dependency.applicationClass(appOwner);
471498
final Optional<DependencyLoadingException> load = this.dependencyLoader.load(
@@ -646,9 +673,9 @@ private void detectComponents(
646673
}
647674
simplixModule.components().put(componentClass, component);
648675
log.debug(SIMPLIX_BOOTSTRAP
649-
+ context.applicationInfo.name()
650-
+ ": Detected "
651-
+ componentClass.getName());
676+
+ context.applicationInfo.name()
677+
+ ": Detected "
678+
+ componentClass.getName());
652679
} catch (Throwable throwable) {
653680
if (suppressWarning(componentClass, "exception:*")
654681
|| suppressWarning(
@@ -731,6 +758,7 @@ static final class InstallationContext {
731758
private final Module[] modules;
732759
private final Consumer<Exception> onException;
733760

761+
734762
}
735763

736764
}

simplixcore-common/simplixcore-common-implementation/src/main/java/dev/simplix/core/common/libloader/SimpleLibraryLoader.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Set;
1818
import java.util.function.Function;
1919
import lombok.NonNull;
20+
import org.jetbrains.annotations.NotNull;
2021
import org.slf4j.Logger;
2122

2223
public class SimpleLibraryLoader implements LibraryLoader {
@@ -25,6 +26,7 @@ public class SimpleLibraryLoader implements LibraryLoader {
2526
private final Gson gson = new GsonBuilder().create();
2627
private final Set<File> files = new HashSet<>();
2728
private Method addMethod;
29+
2830
public SimpleLibraryLoader(@NonNull Logger log) {
2931
this.log = log;
3032
try {
@@ -64,7 +66,7 @@ public void loadLibrary(File file) {
6466
}
6567

6668
@Override
67-
public void loadLibraryEncapsulated(@NonNull File file, Class<?> owner) {
69+
public void loadLibraryEncapsulated(@NonNull File file, @NotNull Class<?> owner) {
6870
try {
6971
if (files.contains(file)) {
7072
return;
@@ -77,10 +79,13 @@ public void loadLibraryEncapsulated(@NonNull File file, Class<?> owner) {
7779
loadLibrary(file);
7880
return;
7981
}
82+
8083
addUrlToClassLoader((URLClassLoader) classLoader, file);
84+
checkAndLoadSimplixApplication(file, classLoader);
85+
8186
log.info("[Simplix | LibLoader] Loaded encapsulated library " + file.getName() +
8287
" for application " + owner.getSimpleName());
83-
checkAndLoadSimplixApplication(file, classLoader);
88+
8489
} catch (Exception ex) {
8590
log.info("[Simplix | LibLoader] Unable to load encapsulated library " + file.getName() +
8691
" for application " + owner.getSimpleName(), ex);
@@ -104,10 +109,15 @@ private void checkAndLoadSimplixApplication(@NonNull File file, ClassLoader clas
104109
try {
105110
Class<?> mainClass = classLoader.loadClass(libraryDescription.mainClass());
106111
if (mainClass.isAnnotationPresent(SimplixApplication.class)) {
107-
SimplixInstaller.instance().register(mainClass);
112+
SimplixInstaller.instance().register(mainClass, exception -> {
113+
log.error("Could not install library: "
114+
+ mainClass.getSimpleName()
115+
+ " due to "
116+
+ exception.getMessage());
117+
}, true);
108118
log.debug("[Simplix | LibLoader] "
109-
+ libraryDescription.name()
110-
+ " was registered as a SimplixApplication");
119+
+ libraryDescription.name()
120+
+ " was registered as a SimplixApplication");
111121
log.debug("[Simplix | LibLoader] Application class = "
112122
+ mainClass.getName()
113123
+ " hashCode = "

0 commit comments

Comments
 (0)