88import com .eternalcode .core .loader .relocation .RelocationHandler ;
99import com .eternalcode .core .loader .repository .Repository ;
1010
11+ import com .spotify .futures .CompletableFutures ;
1112import java .io .File ;
1213import java .io .IOException ;
1314import java .net .URL ;
1819import java .util .HashMap ;
1920import java .util .List ;
2021import java .util .Map ;
22+ import java .util .concurrent .CompletableFuture ;
23+ import java .util .concurrent .Executor ;
24+ import java .util .concurrent .Executors ;
25+ import java .util .concurrent .TimeUnit ;
2126import java .util .logging .Logger ;
2227
2328public class DependencyLoaderImpl implements DependencyLoader {
2429
2530 private static final String LOCAL_REPOSITORY_PATH = "localRepository" ;
2631
32+ private final Executor executor ;
2733 private final Logger logger ;
2834 private final DependencyDownloader downloadDependency ;
2935 private final RelocationHandler relocationHandler ;
3036 private final DependencyScanner pomXmlScanner ;
37+ private final Repository localRepository ;
3138
3239 private final Map <Dependency , Path > loaded = new HashMap <>();
3340
3441 public DependencyLoaderImpl (Logger logger , File dataFolder , List <Repository > repositories ) {
3542 Path localRepositoryPath = this .setupCacheDirectory (dataFolder );
36- Repository localRepository = Repository .localRepository (localRepositoryPath );
43+ this . localRepository = Repository .localRepository (localRepositoryPath );
3744
3845 List <Repository > allRepositories = new ArrayList <>();
3946 allRepositories .add (localRepository );
4047 allRepositories .addAll (repositories );
4148
49+ this .executor = Executors .newCachedThreadPool ();
4250 this .logger = logger ;
4351 this .pomXmlScanner = new PomXmlScanner (allRepositories , localRepository );
4452 this .downloadDependency = new DependencyDownloader (logger , localRepository , allRepositories );
@@ -64,39 +72,43 @@ public DependencyLoadResult load(IsolatedClassLoader loader, List<Dependency> de
6472 collector = this .pomXmlScanner .findAllChildren (collector , dependency );
6573 }
6674
67- collector .scannedDependencies (dependencies );
68- this .logger .info ("Found " + collector .scannedDependencies ().size () + " dependencies" );
75+ collector .addScannedDependencies (dependencies );
76+ this .logger .info ("Found " + collector .getScannedDependencies ().size () + " dependencies" );
6977
70- List <Path > paths = new ArrayList <>();
78+ List <CompletableFuture < DependencyLoadEntry >> futures = new ArrayList <>();
7179
72- for (Dependency dependency : collector .scannedDependencies ()) {
73- Path loaded = this .loaded .get (dependency );
80+ for (Dependency dependency : collector .getScannedDependencies ()) {
81+ CompletableFuture <DependencyLoadEntry > pathToDependency = CompletableFuture .supplyAsync (() -> {
82+ Path loaded = this .loaded .get (dependency );
7483
75- if (loaded != null ) {
76- // TODO: jeśli już wcześniej pobrano zależność to można zweryfikować czy relokacja jest poprawna (może jakiś plik z informacjami o relokacjach?)
77- loader .addPath (loaded );
78- paths .add (loaded );
79- continue ;
80- }
84+ if (loaded != null ) {
85+ return new DependencyLoadEntry (dependency , loaded );
86+ }
8187
82- Path downloadedDependencyPath = this .downloadDependency .downloadDependency (dependency );
88+ Path downloadedDependencyPath = this .downloadDependency .downloadDependency (dependency );
8389
84- if (this .relocationHandler == null ) {
85- this .loaded .put (dependency , downloadedDependencyPath );
86- loader .addPath (downloadedDependencyPath );
87- paths .add (downloadedDependencyPath );
88- continue ;
89- }
90+ if (this .relocationHandler == null ) {
91+ return new DependencyLoadEntry (dependency , downloadedDependencyPath );
92+ }
9093
91- // TODO: to jest trochę blocking można to zrobić w parallel streamie
92- Path relocatedDependency = this .relocationHandler .relocateDependency (downloadedDependencyPath , dependency , relocations );
94+ Path relocatedDependency = this .relocationHandler .relocateDependency (localRepository , downloadedDependencyPath , dependency , relocations );
9395
94- this .loaded .put (dependency , relocatedDependency );
95- loader .addPath (relocatedDependency );
96- paths .add (relocatedDependency );
96+ return new DependencyLoadEntry (dependency , relocatedDependency );
97+ }, this .executor );
98+
99+ futures .add (pathToDependency );
100+ }
101+
102+ List <DependencyLoadEntry > dependencyLoadEntries = CompletableFutures .allAsList (futures )
103+ .orTimeout (60 , TimeUnit .MINUTES )
104+ .join ();
105+
106+ for (DependencyLoadEntry dependencyLoadEntry : dependencyLoadEntries ) {
107+ loader .addPath (dependencyLoadEntry .path ());
108+ this .loaded .put (dependencyLoadEntry .dependency (), dependencyLoadEntry .path ());
97109 }
98110
99- return new DependencyLoadResult (loader , collector . scannedDependencies (), paths );
111+ return new DependencyLoadResult (loader , dependencyLoadEntries );
100112 }
101113
102114 @ Override
0 commit comments