static void setPermissionsToOwnerOnly()

in google-http-client/src/main/java/com/google/api/client/util/store/FileDataStoreFactory.java [124:156]


  static void setPermissionsToOwnerOnly(File file) throws IOException {
    // Disable access by other users if O/S allows it and set file permissions to readable and
    // writable by user. Use reflection since JDK 1.5 will not have these methods
    try {
      Method setReadable = File.class.getMethod("setReadable", boolean.class, boolean.class);
      Method setWritable = File.class.getMethod("setWritable", boolean.class, boolean.class);
      Method setExecutable = File.class.getMethod("setExecutable", boolean.class, boolean.class);
      if (!(Boolean) setReadable.invoke(file, false, false)
          || !(Boolean) setWritable.invoke(file, false, false)
          || !(Boolean) setExecutable.invoke(file, false, false)) {
        LOGGER.warning("unable to change permissions for everybody: " + file);
      }
      if (!(Boolean) setReadable.invoke(file, true, true)
          || !(Boolean) setWritable.invoke(file, true, true)
          || !(Boolean) setExecutable.invoke(file, true, true)) {
        LOGGER.warning("unable to change permissions for owner: " + file);
      }
    } catch (InvocationTargetException exception) {
      Throwable cause = exception.getCause();
      Throwables.propagateIfPossible(cause, IOException.class);
      // shouldn't reach this point, but just in case...
      throw new RuntimeException(cause);
    } catch (NoSuchMethodException exception) {
      LOGGER.warning("Unable to set permissions for " + file
          + ", likely because you are running a version of Java prior to 1.6");
    } catch (SecurityException exception) {
      // ignored
    } catch (IllegalAccessException exception) {
      // ignored
    } catch (IllegalArgumentException exception) {
      // ignored
    }
  }