protected void cacheIdeLocation()

in plugin/src/com/microsoft/alm/plugin/idea/common/setup/ApplicationStartup.java [134:198]


    protected void cacheIdeLocation(final File vstsDirectory, final String currentLocation) {
        final Map<String, String> locationEntries = new HashMap<String, String>();
        final File locationsFile = new File(vstsDirectory, LOCATION_FILE);
        final String ideName = ApplicationNamesInfo.getInstance().getProductName().toLowerCase();
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        String currentEntry = StringUtils.EMPTY;

        try {
            // if file doesn't exist create it else read the entries in it
            if (!locationsFile.exists()) {
                locationsFile.createNewFile();
            } else {
                String line;
                bufferedReader = new BufferedReader(new FileReader(locationsFile));
                while ((line = bufferedReader.readLine()) != null) {
                    final String[] entry = line.split(CSV_COMMA);
                    if (entry.length == 2) {
                        // find existing IDE entry if there is one
                        if (ideName.equals(entry[0])) {
                            currentEntry = entry[1];
                        }
                        locationEntries.put(entry[0], entry[1]);
                    }
                }
                bufferedReader.close();
            }

            if (!currentEntry.equals(currentLocation) && !currentLocation.isEmpty()) {
                // delete current entry if it exists
                if (!currentEntry.isEmpty()) {
                    locationEntries.remove(ideName);
                }

                // add current entry
                locationEntries.put(ideName, currentLocation);

                // rewrite file with new entry
                bufferedWriter = new BufferedWriter(new FileWriter(locationsFile.getPath()));
                for (String key : locationEntries.keySet()) {
                    bufferedWriter.write(key + CSV_COMMA + locationEntries.get(key) + "\n");
                }
                bufferedWriter.close();
            }
        } catch (FileNotFoundException e) {
            logger.warn("A FileNotFoundException was caught while trying to cache the IDE location", e);
        } catch (IOException e) {
            logger.warn("An IOException was caught while trying to cache the IDE location", e);
        } catch (Exception e) {
            logger.warn("An Exception was caught while trying to cache the IDE location", e);
        } finally {
            // try closing the buffered reader/writer in case it was missed
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }

                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
            } catch (IOException e) {
                logger.warn("An IOException was caught while trying to close the buffered reader/writer", e);
            }
        }
    }