예제 #1
1
  public void addExtension(Class<?> loadResourcesUsing, String loadFrom) throws IOException {
    // Is loadFrom a file?
    File file = new File(loadFrom);
    if (file.exists()) {
      addExtension(file);
      return;
    }

    // Try and load it from the classpath
    InputStream resource = loadResourcesUsing.getResourceAsStream(loadFrom);
    if (resource == null && !loadFrom.startsWith("/")) {
      resource = loadResourcesUsing.getResourceAsStream("/" + loadFrom);
    }
    if (resource == null) {
      resource = FirefoxProfile.class.getResourceAsStream(loadFrom);
    }
    if (resource == null && !loadFrom.startsWith("/")) {
      resource = FirefoxProfile.class.getResourceAsStream("/" + loadFrom);
    }
    if (resource == null) {
      throw new FileNotFoundException("Cannot locate resource with name: " + loadFrom);
    }

    File root;
    if (FileHandler.isZipped(loadFrom)) {
      root = FileHandler.unzip(resource);
    } else {
      throw new RuntimeException("Will only install zipped extensions for now");
    }

    addExtension(root);
  }
예제 #2
0
  public void installDevelopmentExtension(String home) throws IOException {
    if (!home.endsWith("extension"))
      throw new RuntimeException(
          "The given source directory does not look like a source "
              + "directory for the extension: "
              + home);

    if (!FileHandler.createDir(extensionsDir))
      throw new IOException(
          "Cannot create extensions directory: " + extensionsDir.getAbsolutePath());

    File writeTo = new File(extensionsDir, EXTENSION_NAME);
    if (writeTo.exists() && !FileHandler.delete(writeTo)) {
      throw new IOException(
          "Cannot delete existing extensions directory: " + extensionsDir.getAbsolutePath());
    }

    FileWriter writer = null;
    try {
      writer = new FileWriter(writeTo);
      writer.write(home);
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      Cleanly.close(writer);
    }
  }
예제 #3
0
  /**
   * Attempt to add an extension to install into this instance.
   *
   * @param extensionToInstall
   * @throws IOException
   */
  public void addExtension(File extensionToInstall) throws IOException {
    if (!extensionToInstall.isDirectory()
        && !FileHandler.isZipped(extensionToInstall.getAbsolutePath())) {
      throw new IOException("Can only install from a zip file, an XPI or a directory");
    }

    File root = obtainRootDirectory(extensionToInstall);

    String id = readIdFromInstallRdf(root);

    File extensionDirectory = new File(extensionsDir, id);

    if (extensionDirectory.exists() && !FileHandler.delete(extensionDirectory)) {
      throw new IOException("Unable to delete existing extension directory: " + extensionDirectory);
    }

    FileHandler.createDir(extensionDirectory);
    FileHandler.makeWritable(extensionDirectory);
    FileHandler.copyDir(root, extensionDirectory);
  }
예제 #4
0
 private File obtainRootDirectory(File extensionToInstall) throws IOException {
   File root = extensionToInstall;
   if (!extensionToInstall.isDirectory()) {
     BufferedInputStream bis = new BufferedInputStream(new FileInputStream(extensionToInstall));
     try {
       root = FileHandler.unzip(bis);
     } finally {
       bis.close();
     }
   }
   return root;
 }
예제 #5
0
  public FirefoxProfile createCopy(int port) {
    File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    File to = new File(tmpDir, "webdriver-" + System.currentTimeMillis());
    to.mkdirs();

    FileHandler.copyDir(profileDir, to);
    FirefoxProfile profile = new FirefoxProfile(to);
    profile.addAdditionalPreferences(additionalPrefs);
    profile.setPort(port);
    profile.updateUserPrefs();

    return profile;
  }
예제 #6
0
  protected void installDevelopmentExtension() throws IOException {
    if (!FileHandler.createDir(extensionsDir))
      throw new IOException(
          "Cannot create extensions directory: " + extensionsDir.getAbsolutePath());

    String home = findFirefoxExtensionRootInSourceCode();

    File writeTo = new File(extensionsDir, EXTENSION_NAME);
    if (writeTo.exists() && !FileHandler.delete(writeTo)) {
      throw new IOException(
          "Cannot delete existing extensions directory: " + extensionsDir.getAbsolutePath());
    }

    FileWriter writer = null;
    try {
      writer = new FileWriter(writeTo);
      writer.write(home);
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      Cleanly.close(writer);
    }
  }