2929import java .util .function .Consumer ;
3030import java .util .function .Supplier ;
3131import java .util .logging .Logger ;
32- import lombok .*;
32+ import lombok .AllArgsConstructor ;
33+ import lombok .Cleanup ;
34+ import lombok .NonNull ;
35+ import lombok .SneakyThrows ;
3336import org .jetbrains .annotations .Nullable ;
3437import 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}
0 commit comments