Beispiel #1
0
 private static void tryResUrls(Picture picture) {
   String hi_res = "";
   String url = picture.media_url.toString();
   for (String ending : Main.endings) {
     try {
       hi_res = url.replace(url.substring(url.lastIndexOf("_"), url.lastIndexOf(".")), ending);
       URL hi_url = new URL(hi_res);
       File hi_name = Helper.extractMediaFileNameFromURL(hi_url);
       if (hi_name.equals(picture.media_name)) {
         picture.hi_url = hi_url;
         picture.hi_name = hi_name;
         picture.downloaded_hi = true;
         break;
       } else {
         boolean success = Helper.downloadFileFromURLToFileInTemp(hi_url, hi_name);
         if (success) {
           picture.hi_url = hi_url;
           picture.hi_name = hi_name;
           picture.downloaded_hi = true;
           Helper.moveTempImageToStore(hi_name, new File(Main.blogdir, picture.md5_id));
           break;
         }
       }
     } catch (MalformedURLException ex) {
       Main.error(String.format("Attempted hi res url %s is a malformed URL.", hi_res));
     }
   }
 }
 /*
  * Iterate over the sites in the given configuration and remove the one which
  * has a url matching the given location.
  */
 public boolean removeSite(Configuration configuration, String location)
     throws IOException, URISyntaxException {
   File left = new File(new URI(location)).getCanonicalFile();
   List sites = configuration.getSites();
   for (Iterator iter = sites.iterator(); iter.hasNext(); ) {
     Site tempSite = (Site) iter.next();
     String siteURL = tempSite.getUrl();
     File right = new File(new URI(siteURL)).getCanonicalFile();
     if (left.equals(right)) {
       return configuration.removeSite(tempSite);
     }
   }
   return false;
 }
    /** @param workTokDir Token directory (common for multiple nodes). */
    private void processTokenDirectory(File workTokDir) {
      for (File f : workTokDir.listFiles()) {
        if (!f.isDirectory()) {
          if (!f.getName().equals(LOCK_FILE_NAME)) {
            if (log.isDebugEnabled()) log.debug("Unexpected file: " + f.getName());
          }

          continue;
        }

        if (f.equals(tokDir)) {
          if (log.isDebugEnabled()) log.debug("Skipping own token directory: " + tokDir.getName());

          continue;
        }

        String name = f.getName();

        int pid;

        try {
          pid = Integer.parseInt(name.substring(name.lastIndexOf('-') + 1));
        } catch (NumberFormatException ignored) {
          if (log.isDebugEnabled()) log.debug("Failed to parse file name: " + name);

          continue;
        }

        // Is process alive?
        if (IpcSharedMemoryUtils.alive(pid)) {
          if (log.isDebugEnabled()) log.debug("Skipping alive node: " + pid);

          continue;
        }

        if (log.isDebugEnabled()) log.debug("Possibly stale token folder: " + f);

        // Process each token under stale token folder.
        File[] shmemToks = f.listFiles();

        if (shmemToks == null)
          // Although this is strange, but is reproducible sometimes on linux.
          return;

        int rmvCnt = 0;

        try {
          for (File f0 : shmemToks) {
            if (log.isDebugEnabled()) log.debug("Processing token file: " + f0.getName());

            if (f0.isDirectory()) {
              if (log.isDebugEnabled()) log.debug("Unexpected directory: " + f0.getName());
            }

            // Token file format: gg-shmem-space-[auto_idx]-[other_party_pid]-[size]
            String[] toks = f0.getName().split("-");

            if (toks.length != 6) {
              if (log.isDebugEnabled()) log.debug("Unrecognized token file: " + f0.getName());

              continue;
            }

            int pid0;
            int size;

            try {
              pid0 = Integer.parseInt(toks[4]);
              size = Integer.parseInt(toks[5]);
            } catch (NumberFormatException ignored) {
              if (log.isDebugEnabled()) log.debug("Failed to parse file name: " + name);

              continue;
            }

            if (IpcSharedMemoryUtils.alive(pid0)) {
              if (log.isDebugEnabled()) log.debug("Skipping alive process: " + pid0);

              continue;
            }

            if (log.isDebugEnabled()) log.debug("Possibly stale token file: " + f0);

            IpcSharedMemoryUtils.freeSystemResources(f0.getAbsolutePath(), size);

            if (f0.delete()) {
              if (log.isDebugEnabled()) log.debug("Deleted file: " + f0.getName());

              rmvCnt++;
            } else if (!f0.exists()) {
              if (log.isDebugEnabled())
                log.debug("File has been concurrently deleted: " + f0.getName());

              rmvCnt++;
            } else if (log.isDebugEnabled()) log.debug("Failed to delete file: " + f0.getName());
          }
        } finally {
          // Assuming that no new files can appear, since
          if (rmvCnt == shmemToks.length) {
            U.delete(f);

            if (log.isDebugEnabled()) log.debug("Deleted empty token directory: " + f.getName());
          }
        }
      }
    }