@@ -364,19 +364,6 @@ protected String defaultProject() {
364364 }
365365
366366 protected static String googleCloudProjectId () {
367- try {
368- URL url = new URL ("http://metadata/computeMetadata/v1/project/project-id" );
369- HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
370- connection .setRequestProperty ("X-Google-Metadata-Request" , "True" );
371- InputStream input = connection .getInputStream ();
372- if (connection .getResponseCode () == 200 ) {
373- try (BufferedReader reader = new BufferedReader (new InputStreamReader (input , UTF_8 ))) {
374- return reader .readLine ();
375- }
376- }
377- } catch (IOException ignore ) {
378- // ignore
379- }
380367 File configDir ;
381368 if (System .getenv ().containsKey ("CLOUDSDK_CONFIG" )) {
382369 configDir = new File (System .getenv ("CLOUDSDK_CONFIG" ));
@@ -385,38 +372,52 @@ protected static String googleCloudProjectId() {
385372 } else {
386373 configDir = new File (System .getProperty ("user.home" ), ".config/gcloud" );
387374 }
388- FileReader fileReader ;
375+ FileReader fileReader = null ;
389376 try {
390377 fileReader = new FileReader (new File (configDir , "configurations/config_default" ));
391378 } catch (FileNotFoundException newConfigFileNotFoundEx ) {
392379 try {
393380 fileReader = new FileReader (new File (configDir , "properties" ));
394381 } catch (FileNotFoundException oldConfigFileNotFoundEx ) {
395- // return null if we can't find config file
396- return null ;
382+ // ignore
397383 }
398384 }
399- try (BufferedReader reader = new BufferedReader (fileReader )) {
400- String line ;
401- String section = null ;
402- Pattern projectPattern = Pattern .compile ("^project\\ s*=\\ s*(.*)$" );
403- Pattern sectionPattern = Pattern .compile ("^\\ [(.*)\\ ]$" );
404- while ((line = reader .readLine ()) != null ) {
405- if (line .isEmpty () || line .startsWith (";" )) {
406- continue ;
407- }
408- line = line .trim ();
409- Matcher matcher = sectionPattern .matcher (line );
410- if (matcher .matches ()) {
411- section = matcher .group (1 );
412- } else if (section == null || section .equals ("core" )) {
413- matcher = projectPattern .matcher (line );
385+ if (fileReader != null ) {
386+ try (BufferedReader reader = new BufferedReader (fileReader )) {
387+ String line ;
388+ String section = null ;
389+ Pattern projectPattern = Pattern .compile ("^project\\ s*=\\ s*(.*)$" );
390+ Pattern sectionPattern = Pattern .compile ("^\\ [(.*)\\ ]$" );
391+ while ((line = reader .readLine ()) != null ) {
392+ if (line .isEmpty () || line .startsWith (";" )) {
393+ continue ;
394+ }
395+ line = line .trim ();
396+ Matcher matcher = sectionPattern .matcher (line );
414397 if (matcher .matches ()) {
415- return matcher .group (1 );
398+ section = matcher .group (1 );
399+ } else if (section == null || section .equals ("core" )) {
400+ matcher = projectPattern .matcher (line );
401+ if (matcher .matches ()) {
402+ return matcher .group (1 );
403+ }
416404 }
417405 }
406+ } catch (IOException ex ) {
407+ // ignore
418408 }
419- } catch (IOException ex ) {
409+ }
410+ try {
411+ URL url = new URL ("http://metadata/computeMetadata/v1/project/project-id" );
412+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
413+ connection .setRequestProperty ("X-Google-Metadata-Request" , "True" );
414+ InputStream input = connection .getInputStream ();
415+ if (connection .getResponseCode () == 200 ) {
416+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (input , UTF_8 ))) {
417+ return reader .readLine ();
418+ }
419+ }
420+ } catch (IOException ignore ) {
420421 // ignore
421422 }
422423 // return null if can't determine
0 commit comments