66
77import  java .io .File ;
88import  java .util .ArrayList ;
9- import  java .util .Collections ;
109import  java .util .List ;
10+ import  java .util .Objects ;
1111
12- import  net .minecraftforge .java_provisioner .Disco .Arch ;
1312import  net .minecraftforge .java_provisioner .Disco .Distro ;
1413import  net .minecraftforge .java_provisioner .api .IJavaInstall ;
15- import  net .minecraftforge .util .os .OS ;
14+ import  net .minecraftforge .java_provisioner .api .JavaLocatingFailedException ;
15+ import  net .minecraftforge .java_provisioner .api .JavaProvisioningFailedException ;
1616
1717/** 
1818 * Locates java installs that have been downloaded from the <a href="https://github.com/foojayio/discoapi">disco API</a> 
@@ -44,45 +44,50 @@ public DiscoLocator(File cache, boolean offline) {
4444    }
4545
4646    @ Override 
47-     public  File  find (int  version ) {
48-         List <IJavaInstall > results  = findInternal (version );
49-         return  results .isEmpty () ? null  : results .get (0 ).home ();
47+     public  File  find (int  version ) throws  JavaLocatingFailedException  {
48+         return  findInternal (version ).installs ().get (0 ).home ();
5049    }
5150
5251    @ Override 
53-     public  List < IJavaInstall >  findAll () {
52+     public  JavaLocatorResult  findAll ()  throws   JavaLocatingFailedException  {
5453        return  findInternal (-1 );
5554    }
5655
57-     private  List < IJavaInstall >  findInternal (int  version ) {
56+     private  JavaLocatorResult  findInternal (int  version )  throws   JavaLocatingFailedException  {
5857        if  (!cache .exists () || !cache .isDirectory ())
59-             return   Collections . emptyList ( );
58+             throw   new   JavaLocatingFailedException ( "Java Provisioner has not provisioned any Java installations" );
6059
6160        List <IJavaInstall > results  = new  ArrayList <>();
62-         for  (File  dir  : cache .listFiles ()) {
61+         List <Throwable > errors  = new  ArrayList <>();
62+ 
63+         File [] listFiles ;
64+         try  {
65+             listFiles  = Objects .requireNonNull (cache .listFiles ());
66+         } catch  (Exception  e ) {
67+             throw  new  JavaLocatingFailedException ("An unexpected error occured trying to query the Disco cache: "  + cache , e );
68+         }
69+ 
70+         for  (File  dir  : listFiles ) {
6371            if  (!dir .isDirectory ())
6472                continue ;
6573
6674            log ("Disco Cache: \" "  + dir .getAbsolutePath () + "\" " );
6775
68-             IJavaInstall  ret  = fromPath (dir );
69-             if  (ret  != null ) {
70-                 if  (version  == -1 ) {
71-                     results .add (ret );
72-                 } else  if  (ret .majorVersion () != version ) {
73-                     log ("  Wrong version: Was "  + ret .majorVersion () + " wanted "  + version );
74-                 } else  {
75-                     results .add (ret );
76-                     return  results ;
77-                 }
76+             try  {
77+                 results .add (fromPath (dir , version ));
78+             } catch  (Exception  e ) {
79+                 errors .add (e );
7880            }
7981        }
8082
81-         return  results ;
83+         if  (!results .isEmpty ())
84+             return  new  JavaLocatorResult (results , errors );
85+ 
86+         throw  Throwing .provisioningFailed ("Failed to find any Java installations from Disco cache" , errors );
8287    }
8388
8489    @ Override 
85-     public  IJavaInstall  provision (int  version )  {
90+     public  IJavaInstall  provision (int  version ,  Distro   distro )  throws   JavaProvisioningFailedException  {
8691        log ("Locators failed to find any suitable installs, attempting Disco download" );
8792        Disco  disco  = new  Disco (cache , offline ) { // TODO: [DISCO][Logging] Add a proper logging handler sometime 
8893            @ Override 
@@ -96,34 +101,31 @@ protected void error(String message) {
96101            }
97102        };
98103
99-         List <Disco .Package > jdks  = disco .getPackages (version , Disco .CURRENT_OS , Distro .TEMURIN , Disco .CURRENT_ARCH );
100-         if  (jdks  == null  || jdks .isEmpty ()) {
101-             log ("Failed to find any distros from Disco for "  + version  + " "  + Disco .CURRENT_OS  + " "  + Disco .CURRENT_ARCH  + " "  + Distro .TEMURIN );
102- 
103-             // Try any vendor 
104-             jdks  = disco .getPackages (version , Disco .CURRENT_OS , null , Disco .CURRENT_ARCH );
105-             if  (jdks  == null  || jdks .isEmpty ()) {
106-                 log ("Failed to find any distros from Disco for "  + version  + " "  + Disco .CURRENT_OS  + " "  + Disco .CURRENT_ARCH );
107- 
108-                 // Try any Architecture and just hope for the best 
109-                 jdks  = disco .getPackages (version , Disco .CURRENT_OS , null , null );
110-                 if  (jdks  == null  || jdks .isEmpty ()) {
111-                     log ("Failed to find any distros from Disco for "  + version  + " "  + Disco .CURRENT_OS );
112-                     return  null ;
113-                 }
104+         List <Disco .Package > jdks ;
105+         try  {
106+             jdks  = disco .getPackages (version , Disco .CURRENT_OS , distro , Disco .CURRENT_ARCH );
107+         } catch  (Exception  e ) {
108+             log (String .format ("Failed to find any JDKs from Disco for: %s%d - %s %s" , distro  != null  ? distro  + " "  : "" , version , Disco .CURRENT_OS , Disco .CURRENT_ARCH ));
109+ 
110+             // Try any Architecture and just hope for the best 
111+             try  {
112+                 jdks  = disco .getPackages (version , Disco .CURRENT_OS , distro , null );
113+             } catch  (Exception  suppressed ) {
114+                 log (String .format ("Failed to find any JDKs from Disco for: %s%d - %s ANY" , distro  != null  ? distro  + " "  : "" , version , Disco .CURRENT_OS ));
115+                 e .addSuppressed (suppressed );
116+                 throw  new  JavaProvisioningFailedException ("Failed to provision Disco download" , e );
114117            }
115118        }
116119
117-         log ("Found "  + jdks .size () + " download canidates " );
120+         log ("Found "  + jdks .size () + " download candidates " );
118121        Disco .Package  pkg  = jdks .get (0 );
119122        log ("Selected "  + pkg .distribution  + ": "  + pkg .filename );
120123
121-         File  java_home  = disco .extract (pkg );
122- 
123-         if  (java_home  == null )
124-             return  null ;
125- 
126-         IJavaInstall  result  = fromPath (java_home );
127-         return  result ;
124+         try  {
125+             File  java_home  = disco .extract (pkg );
126+             return  fromPath (java_home );
127+         } catch  (Exception  e ) {
128+             throw  new  JavaProvisioningFailedException ("Failed to provision Disco download: "  + pkg .filename , e );
129+         }
128130    }
129131}
0 commit comments