Exemple #1
0
  public TranscodeFileImpl lookupFile(TranscodeProfile profile, DiskManagerFileInfo file) {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        String key =
            ByteFormatter.encodeString(file.getDownloadHash())
                + ":"
                + file.getIndex()
                + ":"
                + profile.getUID();

        if (device_files.containsKey(key)) {

          try {
            return (new TranscodeFileImpl(this, key, device_files));

          } catch (Throwable e) {

            device_files.remove(key);

            log("Failed to deserialise transcode file", e);
          }
        }
      }
    } catch (Throwable e) {
    }

    return (null);
  }
Exemple #2
0
  protected TranscodeFileImpl getTranscodeFile(String key) {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        if (device_files.containsKey(key)) {

          try {

            return (new TranscodeFileImpl(this, key, device_files));

          } catch (Throwable e) {

            device_files.remove(key);

            log("Failed to deserialise transcode file", e);
          }
        }
      }
    } catch (Throwable e) {
    }

    return (null);
  }
Exemple #3
0
  private TranscodeProfile[] getTranscodeProfiles(String classification) {
    List<TranscodeProfile> profiles = new ArrayList<TranscodeProfile>();

    DeviceManagerImpl dm = getManager();

    TranscodeProvider[] providers = dm.getProviders();

    for (TranscodeProvider provider : providers) {

      TranscodeProfile[] ps = provider.getProfiles();

      for (TranscodeProfile p : ps) {

        String c = p.getDeviceClassification();

        if (c == null) {

          log("Device classification missing for " + p.getName());

        } else {
          if (c.toLowerCase().startsWith(classification.toLowerCase())) {

            profiles.add(p);
          }
        }
      }
    }

    return (profiles.toArray(new TranscodeProfile[profiles.size()]));
  }
Exemple #4
0
  protected void loadDeviceFile() throws IOException {

    device_files_last_mod = SystemTime.getMonotonousTime();

    if (device_files_ref != null) {

      device_files = device_files_ref.get();
    }

    if (device_files == null) {

      Map map = FileUtil.readResilientFile(getDeviceFile());

      device_files = (Map<String, Map<String, ?>>) map.get("files");

      if (device_files == null) {

        device_files = new HashMap<String, Map<String, ?>>();
      }

      device_files_ref = new WeakReference<Map<String, Map<String, ?>>>(device_files);

      log("Loaded device file for " + getName() + ": files=" + device_files.size());
    }

    final int GC_TIME = 15000;

    new DelayedEvent(
        "Device:gc",
        GC_TIME,
        new AERunnable() {
          public void runSupport() {
            synchronized (DeviceImpl.this) {
              if (SystemTime.getMonotonousTime() - device_files_last_mod >= GC_TIME) {

                if (device_files_dirty) {

                  saveDeviceFile();
                }

                device_files = null;

              } else {

                new DelayedEvent("Device:gc2", GC_TIME, this);
              }
            }
          }
        });
  }
Exemple #5
0
  public TranscodeFileImpl[] getFiles() {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        List<TranscodeFile> result = new ArrayList<TranscodeFile>();

        Iterator<Map.Entry<String, Map<String, ?>>> it = device_files.entrySet().iterator();

        while (it.hasNext()) {

          Map.Entry<String, Map<String, ?>> entry = it.next();

          try {
            TranscodeFileImpl tf = new TranscodeFileImpl(this, entry.getKey(), device_files);

            result.add(tf);

          } catch (Throwable e) {

            it.remove();

            log("Failed to deserialise transcode file", e);
          }
        }

        return (result.toArray(new TranscodeFileImpl[result.size()]));
      }
    } catch (Throwable e) {

      Debug.out(e);

      return (new TranscodeFileImpl[0]);
    }
  }
Exemple #6
0
  public TranscodeFileImpl allocateFile(
      TranscodeProfile profile, boolean no_xcode, DiskManagerFileInfo file, boolean for_job)
      throws TranscodeException {
    TranscodeFileImpl result = null;

    setError(KEY_FILE_ALLOC_ERROR, null);

    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        String key =
            ByteFormatter.encodeString(file.getDownloadHash())
                + ":"
                + file.getIndex()
                + ":"
                + profile.getUID();

        if (device_files.containsKey(key)) {

          try {
            result = new TranscodeFileImpl(this, key, device_files);

          } catch (Throwable e) {

            device_files.remove(key);

            log("Failed to deserialise transcode file", e);
          }
        }

        if (result == null) {

          String ext = profile.getFileExtension();

          String target_file = file.getFile(true).getName();

          if (ext != null && !no_xcode) {

            int pos = target_file.lastIndexOf('.');

            if (pos != -1) {

              target_file = target_file.substring(0, pos);
            }

            target_file += ext;
          }

          target_file = allocateUniqueFileName(target_file);

          File output_file = getWorkingDirectory(true);

          if (!output_file.canWrite()) {

            throw (new TranscodeException(
                "Can't write to transcode folder '" + output_file.getAbsolutePath() + "'"));
          }

          output_file = new File(output_file.getAbsoluteFile(), target_file);

          result =
              new TranscodeFileImpl(
                  this, key, profile.getName(), device_files, output_file, for_job);

          result.setSourceFile(file);

          device_files_last_mod = SystemTime.getMonotonousTime();

          device_files_dirty = true;

        } else {

          result.setSourceFile(file);

          result.setProfileName(profile.getName());
        }
      }
    } catch (Throwable e) {

      setError(KEY_FILE_ALLOC_ERROR, Debug.getNestedExceptionMessage(e));

      throw (new TranscodeException("File allocation failed", e));
    }

    for (TranscodeTargetListener l : listeners) {

      try {
        l.fileAdded(result);

      } catch (Throwable e) {

        Debug.out(e);
      }
    }

    return (result);
  }
Exemple #7
0
  protected void deleteFile(TranscodeFileImpl file, boolean delete_contents, boolean remove)
      throws TranscodeException {
    if (file.isDeleted()) {

      return;
    }

    if (delete_contents) {

      File f = file.getCacheFile();

      int time = 0;

      while (f.exists() && !f.delete()) {

        if (time > 3000) {

          log("Failed to remove file '" + f.getAbsolutePath() + "'");

          break;

        } else {

          try {
            Thread.sleep(500);

          } catch (Throwable e) {

          }

          time += 500;
        }
      }
    }

    if (remove) {

      try {
        // fire the listeners FIRST as this gives listeners a chance to extract data
        // from the file before it is deleted (otherwise operations fail with 'file has been
        // deleted'

        for (TranscodeTargetListener l : listeners) {

          try {
            l.fileRemoved(file);

          } catch (Throwable e) {

            Debug.out(e);
          }
        }

        synchronized (this) {
          if (device_files == null) {

            loadDeviceFile();

          } else {

            device_files_last_mod = SystemTime.getMonotonousTime();
          }

          device_files.remove(file.getKey());

          device_files_dirty = true;
        }

      } catch (Throwable e) {

        throw (new TranscodeException("Delete failed", e));
      }
    }
  }