src/solaris/classes/java/util/prefs/FileSystemPreferences.java

Print this page
rev 3509 : 7021209: convert lang, math, util to use try-with-resources
Reviewed-by: XXX


 554         }
 555     }
 556 
 557     /**
 558      * Attempt to load prefsCache from the backing store.  If the attempt
 559      * succeeds, lastSyncTime will be updated (the new value will typically
 560      * correspond to the data loaded into the map, but it may be less,
 561      * if another VM is updating this node concurrently).  If the attempt
 562      * fails, a BackingStoreException is thrown and both prefsCache and
 563      * lastSyncTime are unaffected by the call.
 564      */
 565     private void loadCache() throws BackingStoreException {
 566         try {
 567             AccessController.doPrivileged(
 568                 new PrivilegedExceptionAction<Void>() {
 569                 public Void run() throws BackingStoreException {
 570                     Map<String, String> m = new TreeMap<>();
 571                     long newLastSyncTime = 0;
 572                     try {
 573                         newLastSyncTime = prefsFile.lastModified();
 574                         FileInputStream fis = new FileInputStream(prefsFile);
 575                         XmlSupport.importMap(fis, m);
 576                         fis.close();
 577                     } catch(Exception e) {
 578                         if (e instanceof InvalidPreferencesFormatException) {
 579                             getLogger().warning("Invalid preferences format in "
 580                                                         +  prefsFile.getPath());
 581                             prefsFile.renameTo( new File(
 582                                                     prefsFile.getParentFile(),
 583                                                   "IncorrectFormatPrefs.xml"));
 584                             m = new TreeMap<>();
 585                         } else if (e instanceof FileNotFoundException) {
 586                         getLogger().warning("Prefs file removed in background "
 587                                            + prefsFile.getPath());
 588                         } else {
 589                             throw new BackingStoreException(e);
 590                         }
 591                     }
 592                     // Attempt succeeded; update state
 593                     prefsCache = m;
 594                     lastSyncTime = newLastSyncTime;
 595                     return null;
 596                 }


 601     }
 602 
 603     /**
 604      * Attempt to write back prefsCache to the backing store.  If the attempt
 605      * succeeds, lastSyncTime will be updated (the new value will correspond
 606      * exactly to the data thust written back, as we hold the file lock, which
 607      * prevents a concurrent write.  If the attempt fails, a
 608      * BackingStoreException is thrown and both the backing store (prefsFile)
 609      * and lastSyncTime will be unaffected by this call.  This call will
 610      * NEVER leave prefsFile in a corrupt state.
 611      */
 612     private void writeBackCache() throws BackingStoreException {
 613         try {
 614             AccessController.doPrivileged(
 615                 new PrivilegedExceptionAction<Void>() {
 616                 public Void run() throws BackingStoreException {
 617                     try {
 618                         if (!dir.exists() && !dir.mkdirs())
 619                             throw new BackingStoreException(dir +
 620                                                              " create failed.");
 621                         FileOutputStream fos = new FileOutputStream(tmpFile);
 622                         XmlSupport.exportMap(fos, prefsCache);
 623                         fos.close();
 624                         if (!tmpFile.renameTo(prefsFile))
 625                             throw new BackingStoreException("Can't rename " +
 626                             tmpFile + " to " + prefsFile);
 627                     } catch(Exception e) {
 628                         if (e instanceof BackingStoreException)
 629                             throw (BackingStoreException)e;
 630                         throw new BackingStoreException(e);
 631                     }
 632                     return null;
 633                 }
 634             });
 635         } catch (PrivilegedActionException e) {
 636             throw (BackingStoreException) e.getException();
 637         }
 638     }
 639 
 640     protected String[] keysSpi() {
 641         initCacheIfNecessary();
 642         return prefsCache.keySet().toArray(new String[prefsCache.size()]);
 643     }




 554         }
 555     }
 556 
 557     /**
 558      * Attempt to load prefsCache from the backing store.  If the attempt
 559      * succeeds, lastSyncTime will be updated (the new value will typically
 560      * correspond to the data loaded into the map, but it may be less,
 561      * if another VM is updating this node concurrently).  If the attempt
 562      * fails, a BackingStoreException is thrown and both prefsCache and
 563      * lastSyncTime are unaffected by the call.
 564      */
 565     private void loadCache() throws BackingStoreException {
 566         try {
 567             AccessController.doPrivileged(
 568                 new PrivilegedExceptionAction<Void>() {
 569                 public Void run() throws BackingStoreException {
 570                     Map<String, String> m = new TreeMap<>();
 571                     long newLastSyncTime = 0;
 572                     try {
 573                         newLastSyncTime = prefsFile.lastModified();
 574                         try (FileInputStream fis = new FileInputStream(prefsFile)) {
 575                             XmlSupport.importMap(fis, m);
 576                         }
 577                     } catch(Exception e) {
 578                         if (e instanceof InvalidPreferencesFormatException) {
 579                             getLogger().warning("Invalid preferences format in "
 580                                                         +  prefsFile.getPath());
 581                             prefsFile.renameTo( new File(
 582                                                     prefsFile.getParentFile(),
 583                                                   "IncorrectFormatPrefs.xml"));
 584                             m = new TreeMap<>();
 585                         } else if (e instanceof FileNotFoundException) {
 586                         getLogger().warning("Prefs file removed in background "
 587                                            + prefsFile.getPath());
 588                         } else {
 589                             throw new BackingStoreException(e);
 590                         }
 591                     }
 592                     // Attempt succeeded; update state
 593                     prefsCache = m;
 594                     lastSyncTime = newLastSyncTime;
 595                     return null;
 596                 }


 601     }
 602 
 603     /**
 604      * Attempt to write back prefsCache to the backing store.  If the attempt
 605      * succeeds, lastSyncTime will be updated (the new value will correspond
 606      * exactly to the data thust written back, as we hold the file lock, which
 607      * prevents a concurrent write.  If the attempt fails, a
 608      * BackingStoreException is thrown and both the backing store (prefsFile)
 609      * and lastSyncTime will be unaffected by this call.  This call will
 610      * NEVER leave prefsFile in a corrupt state.
 611      */
 612     private void writeBackCache() throws BackingStoreException {
 613         try {
 614             AccessController.doPrivileged(
 615                 new PrivilegedExceptionAction<Void>() {
 616                 public Void run() throws BackingStoreException {
 617                     try {
 618                         if (!dir.exists() && !dir.mkdirs())
 619                             throw new BackingStoreException(dir +
 620                                                              " create failed.");
 621                         try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
 622                             XmlSupport.exportMap(fos, prefsCache);
 623                         }
 624                         if (!tmpFile.renameTo(prefsFile))
 625                             throw new BackingStoreException("Can't rename " +
 626                             tmpFile + " to " + prefsFile);
 627                     } catch(Exception e) {
 628                         if (e instanceof BackingStoreException)
 629                             throw (BackingStoreException)e;
 630                         throw new BackingStoreException(e);
 631                     }
 632                     return null;
 633                 }
 634             });
 635         } catch (PrivilegedActionException e) {
 636             throw (BackingStoreException) e.getException();
 637         }
 638     }
 639 
 640     protected String[] keysSpi() {
 641         initCacheIfNecessary();
 642         return prefsCache.keySet().toArray(new String[prefsCache.size()]);
 643     }