コード例 #1
0
  /*
   * Working directory property must point to an existing location and it must
   * be a directory
   */
  @Test
  public void testWorkingDirectory() throws Exception {
    File notExistingFile = new File("not-existing-path");
    Assert.state(!notExistingFile.exists());

    try {
      tasklet.setWorkingDirectory(notExistingFile.getCanonicalPath());
      fail();
    } catch (IllegalArgumentException e) {
      // expected
    }

    File notDirectory = File.createTempFile(this.getClass().getName(), null);
    Assert.state(notDirectory.exists());
    Assert.state(!notDirectory.isDirectory());

    try {
      tasklet.setWorkingDirectory(notDirectory.getCanonicalPath());
      fail();
    } catch (IllegalArgumentException e) {
      // expected
    }

    File directory = notDirectory.getParentFile();
    Assert.state(directory.exists());
    Assert.state(directory.isDirectory());

    // no error expected now
    tasklet.setWorkingDirectory(directory.getCanonicalPath());
  }
コード例 #2
0
ファイル: Files.java プロジェクト: siwiwit/furnace
  /**
   * Copies a filtered directory to a new location.
   *
   * <p>This method copies the contents of the specified source directory to within the specified
   * destination directory.
   *
   * <p>The destination directory is created if it does not exist. If the destination directory did
   * exist, then this method merges the source with the destination, with the source taking
   * precedence.
   *
   * <p><strong>Note:</strong> Setting <code>preserveFileDate</code> to {@code true} tries to
   * preserve the files' last modified date/times using {@link File#setLastModified(long)}, however
   * it is not guaranteed that those operations will succeed. If the modification operation fails,
   * no indication is provided.
   *
   * <h4>Example: Copy directories only</h4>
   *
   * <pre>
   * // only copy the directory structure
   * FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);
   * </pre>
   *
   * <h4>Example: Copy directories and txt files</h4>
   *
   * <pre>
   * // Create a filter for &quot;.txt&quot; files
   * IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(&quot;.txt&quot;);
   * IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter);
   *
   * // Create a filter for either directories or &quot;.txt&quot; files
   * FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles);
   *
   * // Copy using the filter
   * FileUtils.copyDirectory(srcDir, destDir, filter, false);
   * </pre>
   *
   * @param srcDir an existing directory to copy, must not be {@code null}
   * @param destDir the new directory, must not be {@code null}
   * @param filter the filter to apply, null means copy all directories and files
   * @param preserveFileDate true if the file date of the copy should be the same as the original
   * @throws NullPointerException if source or destination is {@code null}
   * @throws IOException if source or destination is invalid
   * @throws IOException if an IO error occurs during copying
   * @since 1.4
   */
  public static void copyDirectory(File srcDir, File destDir, FileFilter filter)
      throws IOException {
    if (srcDir == null) {
      throw new NullPointerException("Source must not be null");
    }
    if (destDir == null) {
      throw new NullPointerException("Destination must not be null");
    }
    if (srcDir.exists() == false) {
      throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
    }
    if (srcDir.isDirectory() == false) {
      throw new IOException("Source '" + srcDir + "' exists but is not a directory");
    }
    if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) {
      throw new IOException(
          "Source '" + srcDir + "' and destination '" + destDir + "' are the same");
    }

    // Cater for destination being directory within the source directory (see IO-141)
    List<String> exclusionList = null;
    if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
      File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
      if (srcFiles != null && srcFiles.length > 0) {
        exclusionList = new ArrayList<String>(srcFiles.length);
        for (File srcFile : srcFiles) {
          File copiedFile = new File(destDir, srcFile.getName());
          exclusionList.add(copiedFile.getCanonicalPath());
        }
      }
    }
    doCopyDirectory(srcDir, destDir, filter, exclusionList);
  }
コード例 #3
0
  protected void createModuleDirectory(File moduleDirectory, boolean overwrite) throws IOException {
    if (moduleDirectory.exists()) {
      if (moduleDirectory.isDirectory()) {
        if (overwrite) {
          FileUtils.cleanDirectory(moduleDirectory);
        }
      } else
      // file if ( moduleDirectory.isFile() )
      {
        getLog()
            .info(
                String.format(
                    "Deleting \"%s\" file", moduleDirectory)); // TODO-more descriptive message
        if (!moduleDirectory.delete()) {
          throw new IOException(
              String.format("Cannot delete \"%s\" file", moduleDirectory.getCanonicalPath()));
        }
      }
    }

    if (!moduleDirectory.exists()) {
      if (!moduleDirectory.mkdirs()) {
        throw new IOException(
            String.format("Cannot create \"%s\" directory", moduleDirectory.getCanonicalPath()));
      }
    }
  }
コード例 #4
0
  private void exportDistributorVersions(File baseDir) throws IOException {
    List<DistributorVersion> versions = distVerCurator.findAll();
    if (versions == null || versions.isEmpty()) {
      return;
    }

    File distVerDir = new File(baseDir.getCanonicalPath(), "distributor_version");
    distVerDir.mkdir();

    FileWriter writer = null;
    for (DistributorVersion dv : versions) {
      if (log.isDebugEnabled()) {
        log.debug("Exporting Distributor Version" + dv.getName());
      }
      try {
        File file = new File(distVerDir.getCanonicalPath(), dv.getName() + ".json");
        writer = new FileWriter(file);
        distVerExporter.export(mapper, writer, dv);
      } finally {
        if (writer != null) {
          writer.close();
        }
      }
    }
  }
コード例 #5
0
  /**
   * Gets a list of all the known plugin base directories (directories where plugins are installed
   * to).
   *
   * @return Returns the base location where plugins are kept
   */
  @SuppressWarnings("unchecked")
  public Collection<String> getPluginBaseDirectories() {
    List<String> dirs = (List<String>) cache.get(KEY_PLUGIN_BASE_DIRECTORIES);
    if (dirs == null) {
      dirs = new ArrayList<String>();
      if (projectPluginsDir != null)
        try {
          dirs.add(projectPluginsDir.getCanonicalPath());
        } catch (IOException e) {
          System.err.println(
              "Cannot read project plugins directory ["
                  + projectPluginsDir
                  + "] due to I/O error: "
                  + e.getMessage());
        }

      if (globalPluginsDir != null)
        try {
          dirs.add(globalPluginsDir.getCanonicalPath());
        } catch (IOException e) {
          System.err.println(
              "Cannot read global plugins directory ["
                  + globalPluginsDir
                  + "] due to I/O error: "
                  + e.getMessage());
        }
      cache.put(KEY_PLUGIN_BASE_DIRECTORIES, dirs);
    }
    return dirs;
  }
コード例 #6
0
 /**
  * write to an existing file in a webdav server using the current domain
  *
  * @param path The path relative to the domain for the file
  * @param buffer The contents of the file
  * @param last true if this is the last block of data
  * @return The total number of bytes stored or null if method failed
  */
 public long writeToFile(String path, byte[] buffer, boolean last) {
   long result = -1L;
   if (_tempFile == null) _tempFile = Util.createCache();
   try {
     if (buffer != null && buffer.length > 0) {
       FileOutputStream fOut = new FileOutputStream(_tempFile.getCanonicalPath(), true);
       fOut.write(buffer);
       fOut.close();
     }
     if (last) {
       if (_isLocal) {
         File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + path);
         if (_tempFile.renameTo(tmpFile)) {
           _tempFile = null;
           return tmpFile.length();
         }
       } else {
         WebdavResource webdavRes = Util.initWebdavResource(_domain);
         if (webdavRes == null) {
           _tempFile.delete();
           _tempFile = null;
           return result;
         }
         result = Util.putFile(webdavRes, _domain, path, _tempFile);
       }
     } else {
       return _tempFile.length();
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return result;
 }
コード例 #7
0
  private void exportEntitlementsCerts(
      File baseDir, Consumer consumer, Set<Long> serials, boolean manifest) throws IOException {

    File entCertDir = new File(baseDir.getCanonicalPath(), "entitlement_certificates");
    entCertDir.mkdir();

    for (EntitlementCertificate cert : entCertAdapter.listForConsumer(consumer)) {
      if (manifest && !this.exportRules.canExport(cert.getEntitlement())) {
        if (log.isDebugEnabled()) {
          log.debug(
              "Skipping export of entitlement cert with product: {}",
              cert.getEntitlement().getPool().getProductId());
        }
        continue;
      }

      if ((serials == null) || (serials.contains(cert.getSerial().getId()))) {
        log.debug("Exporting entitlement certificate: " + cert.getSerial());
        File file = new File(entCertDir.getCanonicalPath(), cert.getSerial().getId() + ".pem");
        FileWriter writer = null;
        try {
          writer = new FileWriter(file);
          entCert.export(writer, cert);
        } finally {
          if (writer != null) {
            writer.close();
          }
        }
      }
    }
  }
コード例 #8
0
  public static Commandline createCommandLine(
      PerforceScmProviderRepository repo, File workingDirectory, ScmFileSet files) {
    Commandline command = PerforceScmProvider.createP4Command(repo, workingDirectory);

    command.createArg().setValue("edit");

    try {
      String candir = workingDirectory.getCanonicalPath();
      List fs = files.getFileList();
      for (int i = 0; i < fs.size(); i++) {
        File file = (File) fs.get(i);
        // I want to use relative paths to add files to make testing
        // simpler.
        // Otherwise the absolute path will be different on everyone's
        // machine
        // and testing will be a little more painful.
        String canfile = file.getCanonicalPath();
        if (canfile.startsWith(candir)) {
          canfile = canfile.substring(candir.length() + 1);
        }
        command.createArg().setValue(canfile);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return command;
  }
コード例 #9
0
  /** @param valid: used to change a valid/invalid array of paths */
  private void setPath(boolean valid) throws IOException {
    File file = new File(".");

    // Array paths contains a valid path
    if (valid) {
      paths =
          "'"
              + file.getCanonicalPath()
              + "',"
              + "'"
              + file.getCanonicalPath()
              + File.separator
              + "test"
              + File.separator
              + "unit"
              + File.separator
              + "TestURLClassLoader.jar'";
      paths +=
          ","
              + "'"
              + file.getCanonicalPath()
              + File.separator
              + "test"
              + File.separator
              + "unit"
              + File.separator
              + "TestInterfaces.jar'";
    }
    // Array paths does not contain a valid path
    else {
      paths = "'" + file.getCanonicalPath() + "'";
    }
  }
コード例 #10
0
  public static void main(String[] args) {
    new ExampleProjects();

    try {

      File exsDir = new File("osb/showcase/neuroConstructShowcase");

      File mainFile = new File("docs/XML/xmlForHtml/samples/index.xml");

      logger.logComment("Going to create docs at: " + mainFile.getCanonicalPath(), true);

      generateMainPage(mainFile, exsDir);

      logger.logComment("Created doc at: " + mainFile.getCanonicalPath(), true);
      /*
      File modelsDir = new File("nCmodels");
      mainFile = new File("docs/XML/xmlForHtml/models/index.xml");


      generateMainPage(mainFile, modelsDir);

      logger.logComment("Created doc at: "+ mainFile.getCanonicalPath(), true);

       */
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #11
0
ファイル: FileHandler.java プロジェクト: JSansalone/Marathon
 public File getFile(String fileName) {
   try {
     File file;
     if (fileName.contains(testDirectory.getCanonicalPath())) {
       String relativeFileName =
           fileName.substring(testDirectory.getCanonicalPath().length() + 1, fileName.length());
       file = new File(testDirectory, relativeFileName);
       if (file.exists()) {
         if (file.isFile()) return file;
       }
     } else
       for (int i = 0; i < moduleDirectories.length; i++) {
         if (fileName.contains(moduleDirectories[i].getCanonicalPath())) {
           String relativeFileName =
               fileName.substring(
                   moduleDirectories[i].getCanonicalPath().length() + 1, fileName.length());
           file = new File(moduleDirectories[i], relativeFileName);
         } else file = new File(moduleDirectories[i], fileName);
         if (file.exists()) {
           if (file.isFile()) return file;
         }
       }
     file = new File(fixtureDirectory, fileName);
     if (file.exists()) return file;
   } catch (IOException e) {
     e.printStackTrace();
     return null;
   }
   return null;
 }
コード例 #12
0
  /**
   * Copy method to copy a file to another file
   *
   * @param sourceFile
   * @param targetFile
   * @param move true: move file; false: copy file
   * @return true: success; false: failure
   */
  public static boolean copyFileToFile(File sourceFile, File targetFile, boolean move) {
    try {
      if (sourceFile.isDirectory() || targetFile.isDirectory()) {
        return false;
      }

      // create target directories
      targetFile.getParentFile().mkdirs(); // don't check for success... would return false on

      // catch move/copy of "same" file -> buggy under Windows.
      if (sourceFile.getCanonicalPath().equals(targetFile.getCanonicalPath())) return true;
      if (move) {
        // try to rename it first - operation might only be successful on a local filesystem!
        if (sourceFile.renameTo(targetFile)) return true;
        // it failed, so continue with copy code!
      }

      bcopy(sourceFile, targetFile, "copyFileToFile");

      if (move) {
        // to finish the move accross different filesystems we need to delete the source file
        sourceFile.delete();
      }
    } catch (IOException e) {
      log.error(
          "Could not copy file::"
              + sourceFile.getAbsolutePath()
              + " to file::"
              + targetFile.getAbsolutePath(),
          e);
      return false;
    }
    return true;
  } // end copy
コード例 #13
0
ファイル: Uploads.java プロジェクト: ceylon/ceylon-herd
  private static void deleteFileImpl(String path, File file, File uploadsDir, Upload upload)
      throws IOException {
    checkUploadPath(file, uploadsDir);

    if (!file.exists()) {
      notFound(path);
    }

    Logger.info("delete: %s exists: %s", path, file.exists());

    if (uploadsDir.getCanonicalPath().equals(file.getCanonicalPath())) {
      // clear the entire repo
      for (File f : uploadsDir.listFiles()) {
        if (f.isDirectory()) FileUtils.deleteDirectory(f);
        else f.delete();
      }
      // let's be helpful and remove maven dependencies too
      for (MavenDependency md : upload.mavenDependencies) {
        md.delete();
      }
      flash("message", "Upload cleared");
    } else if (file.isDirectory()) {
      FileUtils.deleteDirectory(file);
      flash("message", "Directory deleted");
    } else {
      file.delete();
      flash("message", "File deleted");
    }
  }
コード例 #14
0
 IRequestTarget serveImage(String uri, int size)
     throws IOException, FileNotFoundException, ImageConversionException {
   File baseDirectory = new File(settings.getGeneratedImageDirectoryPath() + "/" + size);
   File file = new File(baseDirectory, uri);
   if (!file.getCanonicalPath().startsWith(baseDirectory.getCanonicalPath())) {
     // For security matters, we don't serve a file above the base
     // directory, can happen if user forges the URL
     throw new FileNotFoundException(file.toString());
   }
   log.debug("Serving " + file);
   File srcfile = new File(settings.getImageDirectoryRoot(), uri);
   if (!srcfile.exists()) {
     // Don't throw an exception, it means the URL is not correct
     // FIXME return a PageNotFoundRequestTarget
     return EmptyRequestTarget.getInstance();
   }
   if (!file.exists() || srcfile.lastModified() > file.lastModified()) {
     imageConverter.scaleImage(srcfile, file, size, null);
   }
   IResourceStream fileResource =
       new FileResourceStream(new org.apache.wicket.util.file.File(file)) {
         @Override
         public String getContentType() {
           return "image/jpeg";
         }
       };
   return new ResourceStreamRequestTarget(fileResource);
 }
コード例 #15
0
  public void ReadLogFile() {

    try {

      if (returnValue == JFileChooser.APPROVE_OPTION) {
        fileWithXmlAndGarbageData = chooserForReadingLogFile.getSelectedFile();
        filepath =
            fileWithXmlAndGarbageData
                .getCanonicalPath()
                .substring(
                    0, fileWithXmlAndGarbageData.getCanonicalPath().lastIndexOf(File.separator));
        System.out.println(filepath);
        PathOfFileUsedInDateBasedGrouping = filepath;

        PathOfFile = fileWithXmlAndGarbageData.getAbsolutePath();
      }
      if (returnValue == JFileChooser.CANCEL_OPTION) {
        try {
          fileWithXmlAndGarbageData = null;
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } catch (Exception e) {

      e.printStackTrace();
    }
  }
コード例 #16
0
ファイル: FileUtils.java プロジェクト: Cognami/graphene
 public static void deleteRecursively(final File directory) throws IOException {
   final Stack<File> stack = new Stack<>();
   final List<File> temp = new LinkedList<>();
   stack.push(directory.getAbsoluteFile());
   while (!stack.isEmpty()) {
     final File top = stack.pop();
     File[] files = top.listFiles();
     if (files != null) {
       for (final File child : files) {
         if (child.isFile()) {
           if (!deleteFile(child)) {
             throw new IOException("Failed to delete " + child.getCanonicalPath());
           }
         } else {
           temp.add(child);
         }
       }
     }
     files = top.listFiles();
     if ((files == null) || (files.length == 0)) {
       if (!deleteFile(top)) {
         throw new IOException("Failed to delete " + top.getCanonicalPath());
       }
     } else {
       stack.push(top);
       for (final File f : temp) {
         stack.push(f);
       }
     }
     temp.clear();
   }
 }
コード例 #17
0
 /**
  * copies a folder from one location in a webdav server to another using the current domain
  *
  * @param domain The namespace used to identify the application domain (1st level directory) to
  *     use
  * @param src The path relative to the domain for the source folder
  * @param des The path relative to the domain for the destination folder
  * @param overwrite If there is an existing folder, it will be overwritten if this parameter is
  *     set to true
  * @return The complete path of the folder created or null if not created
  */
 public String copyFolder(String src, String des, boolean overwrite) {
   String result = null;
   try {
     if (_isLocal) {
       File srcFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + src);
       if (srcFile.isDirectory()) {
         File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + des);
         result = tmpFile.getCanonicalPath();
         if (tmpFile.mkdirs()) {
           File[] files = srcFile.listFiles();
           for (int i = 0; i < files.length; i++) {
             String tmpPath = File.separatorChar + files[i].getName();
             if (files[i].isDirectory()) {
               if (!copyFolder(src + tmpPath, des + tmpPath, overwrite)
                   .equals(_rootFile.getCanonicalPath() + File.separatorChar + des + tmpPath))
                 result = null;
             } else if (files[i].isFile())
               if (!copyFile(src + tmpPath, des + tmpPath, overwrite)
                   .equals(_rootFile.getCanonicalPath() + File.separatorChar + des + tmpPath))
                 result = null;
           }
         }
       }
     } else {
       result = _remote.copyFolder(_domain, src, des, overwrite);
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return result;
 }
  public static ParentalControlConcern parse(File file) {

    try {

      log.info("Parsing '" + file.getCanonicalPath() + "'...");
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setValidating(true);
      SAXParser parser = factory.newSAXParser();
      parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
      ParentalControlConcernHandler handler = new ParentalControlConcernHandler();
      parser.parse(file, handler);
      log.info("Parsed '" + file.getCanonicalPath() + "'");
      return handler.result;

    } catch (SAXParseException ex) {

      log.error(
          "Could not validate the parental control concern XML file!  Validation error follows.");
      log.error(ex.getMessage());
      ex.printStackTrace();
      return null;

    } catch (Exception ex) {

      log.error(ex);
      return null;
    }
  }
コード例 #19
0
 private boolean isCanonicalPathEqual(File f1, File f2) {
   try {
     return f1.getCanonicalPath().equals(f2.getCanonicalPath());
   } catch (IOException e) {
     return false;
   }
 }
コード例 #20
0
  public void printDirectory() throws IOException {
    File f = new File(this.dir);
    String[] directories =
        f.list(
            new FilenameFilter() {
              @Override
              public boolean accept(File current, String name) {
                return new File(current, name).isDirectory();
              }
            });
    System.out.println(Arrays.toString(directories));

    File dir = new File(this.dir);
    if (dir.isDirectory()) {
      FileFilter fileFilter =
          new FileFilter() {
            @Override
            public boolean accept(File pathname) {
              return pathname.isFile();
            }
          };
      for (File file : dir.listFiles(fileFilter)) {
        System.out.println(file.getCanonicalPath());
        all[indAll] = new String();
        all[indAll++] = file.getCanonicalPath();
      }
    }
  }
コード例 #21
0
  private void exportEntitlements(File baseDir, Consumer consumer)
      throws IOException, ExportCreationException {
    File entCertDir = new File(baseDir.getCanonicalPath(), "entitlements");
    entCertDir.mkdir();

    for (Entitlement ent : entitlementCurator.listByConsumer(consumer)) {
      if (ent.isDirty()) {
        log.error("Entitlement " + ent.getId() + " is marked as dirty.");
        throw new ExportCreationException("Attempted to export dirty entitlements");
      }

      if (!this.exportRules.canExport(ent)) {
        if (log.isDebugEnabled()) {
          log.debug(
              "Skipping export of entitlement with product: {}", ent.getPool().getProductId());
        }

        continue;
      }

      if (log.isDebugEnabled()) {
        log.debug("Exporting entitlement for product" + ent.getPool().getProductId());
      }
      FileWriter writer = null;
      try {
        File file = new File(entCertDir.getCanonicalPath(), ent.getId() + ".json");
        writer = new FileWriter(file);
        entExporter.export(mapper, writer, ent);
      } finally {
        if (writer != null) {
          writer.close();
        }
      }
    }
  }
コード例 #22
0
 private String downloadResource(String value, boolean convertToUnix) {
   if (value.matches("(" + getMatchingSchemaAsRegex() + ")://.*")) {
     getConsole().printInfo("converting to local " + value);
     File resourceDir = new File(getConf().getVar(HiveConf.ConfVars.DOWNLOADED_RESOURCES_DIR));
     String destinationName = new Path(value).getName();
     File destinationFile = new File(resourceDir, destinationName);
     if (resourceDir.exists() && !resourceDir.isDirectory()) {
       throw new RuntimeException(
           "The resource directory is not a directory, resourceDir is set to" + resourceDir);
     }
     if (!resourceDir.exists() && !resourceDir.mkdirs()) {
       throw new RuntimeException("Couldn't create directory " + resourceDir);
     }
     try {
       FileSystem fs = FileSystem.get(new URI(value), conf);
       fs.copyToLocalFile(new Path(value), new Path(destinationFile.getCanonicalPath()));
       value = destinationFile.getCanonicalPath();
       if (convertToUnix && DosToUnix.isWindowsScript(destinationFile)) {
         try {
           DosToUnix.convertWindowsScriptToUnix(destinationFile);
         } catch (Exception e) {
           throw new RuntimeException("Caught exception while converting to unix line endings", e);
         }
       }
     } catch (Exception e) {
       throw new RuntimeException("Failed to read external resource " + value, e);
     }
   }
   return value;
 }
コード例 #23
0
  private void exportContentDeliveryNetworks(File baseDir) throws IOException {
    List<Cdn> cdns = cdnCurator.list();
    if (cdns == null || cdns.isEmpty()) {
      return;
    }

    File cdnDir = new File(baseDir.getCanonicalPath(), "content_delivery_network");
    cdnDir.mkdir();

    FileWriter writer = null;
    for (Cdn cdn : cdns) {
      if (log.isDebugEnabled()) {
        log.debug("Exporting Content Delivery Network" + cdn.getName());
      }
      try {
        File file = new File(cdnDir.getCanonicalPath(), cdn.getLabel() + ".json");
        writer = new FileWriter(file);
        cdnExporter.export(mapper, writer, cdn);
      } finally {
        if (writer != null) {
          writer.close();
        }
      }
    }
  }
コード例 #24
0
  @Test
  public void testStatisticStatistics() throws Exception {
    File tempFile = File.createTempFile("ais-ab-stat-builder", "");
    String outputFilename = tempFile.getCanonicalPath();
    String inputDirectory = "src/test/resources";
    String inputFilenamePattern = "ais-sample-micro.txt.gz";
    String[] args =
        new String[] {
          "-inputDirectory",
          inputDirectory,
          "-input",
          inputFilenamePattern,
          "-output",
          outputFilename
        };

    Injector injector =
        Guice.createInjector(
            new AbnormalStatBuilderAppTestModule(
                tempFile.getCanonicalPath(), inputDirectory, inputFilenamePattern, false, 200.0));
    AbnormalStatBuilderApp.setInjector(injector);
    AbnormalStatBuilderApp app = injector.getInstance(AbnormalStatBuilderApp.class);

    AbnormalStatBuilderApp.userArguments = parseUserArguments(args);
    app.execute(new String[] {});

    AppStatisticsService appStatistics = injector.getInstance(AppStatisticsService.class);
    assertEquals(
        (Long) 8L,
        appStatistics.getStatisticStatistics("ShipTypeAndSizeStatistic", "Events processed"));
  }
コード例 #25
0
ファイル: Files.java プロジェクト: siwiwit/furnace
 /**
  * Copies a file to a new location.
  *
  * <p>This method copies the contents of the specified source file to the specified destination
  * file. The directory holding the destination file is created if it does not exist. If the
  * destination file exists, then this method will overwrite it.
  *
  * @param srcFile an existing file to copy, must not be <code>null</code>
  * @param destFile the new file, must not be <code>null</code>
  * @param preserveFileDate true if the file date of the copy should be the same as the original
  * @throws NullPointerException if source or destination is <code>null</code>
  * @throws IOException if source or destination is invalid
  * @throws IOException if an IO error occurs during copying
  * @see #copyFileToDirectory(File, File, boolean)
  */
 public static void copyFile(File srcFile, File destFile) throws IOException {
   if (srcFile == null) {
     throw new NullPointerException("Source must not be null");
   }
   if (destFile == null) {
     throw new NullPointerException("Destination must not be null");
   }
   if (srcFile.exists() == false) {
     throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
   }
   if (srcFile.isDirectory()) {
     throw new IOException("Source '" + srcFile + "' exists but is a directory");
   }
   if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
     throw new IOException(
         "Source '" + srcFile + "' and destination '" + destFile + "' are the same");
   }
   if (destFile.getParentFile() != null && destFile.getParentFile().exists() == false) {
     if (destFile.getParentFile().mkdirs() == false) {
       throw new IOException("Destination '" + destFile + "' directory cannot be created");
     }
   }
   if (destFile.exists() && destFile.canWrite() == false) {
     throw new IOException("Destination '" + destFile + "' exists but is read-only");
   }
   doCopyFile(srcFile, destFile);
 }
コード例 #26
0
 /**
  * Based on <a href="http://www.screaming-penguin.com/node/7749">Backing up your Android SQLite
  * database to the SD card</a>
  *
  * @param src
  * @param dst
  * @return true if success
  * @throws IOException
  */
 boolean copyFile(File src, File dst) throws IOException {
   long sizeIn = -1;
   long sizeCopied = 0;
   boolean ok = false;
   if (src != null && src.exists()) {
     sizeIn = src.length();
     if (!dst.createNewFile()) {
       MyLog.e(this, "New file was not created: '" + dst.getCanonicalPath() + "'");
     } else if (src.getCanonicalPath().compareTo(dst.getCanonicalPath()) == 0) {
       MyLog.d(this, "Cannot copy to itself: '" + src.getCanonicalPath() + "'");
     } else {
       FileInputStream fileInputStream = null;
       java.nio.channels.FileChannel inChannel = null;
       FileOutputStream fileOutputStream = null;
       java.nio.channels.FileChannel outChannel = null;
       try {
         fileInputStream = new FileInputStream(src);
         inChannel = fileInputStream.getChannel();
         fileOutputStream = new FileOutputStream(dst);
         outChannel = fileOutputStream.getChannel();
         sizeCopied = inChannel.transferTo(0, inChannel.size(), outChannel);
         ok = (sizeIn == sizeCopied);
       } finally {
         DbUtils.closeSilently(outChannel);
         DbUtils.closeSilently(fileOutputStream);
         DbUtils.closeSilently(inChannel);
         DbUtils.closeSilently(fileInputStream);
       }
     }
   }
   MyLog.d(this, "Copied " + sizeCopied + " bytes of " + sizeIn);
   return ok;
 }
コード例 #27
0
ファイル: FileUtils.java プロジェクト: DicomFlow/repos
  /**
   * Create a new file based on hash code, including the directory structure if necessary. In case
   * of collision, we will reset hashcode and try again.
   *
   * @param dir the directory where the new file will be created
   * @param hash the initial hash value
   * @return the file created successfully
   * @throws Exception
   */
  public static File createNewFile(File dir, int hash) throws Exception {
    File f = null;
    boolean success = false;
    Exception lastException = null;
    for (int i = 0; i < MAX_TIMES_CREATE_FILE && !success; i++) {
      // In UNIX, mkdirs and createNewFile can throw IOException
      // instead of nicely returning false
      try {
        if (!dir.exists()) {
          success = dir.mkdirs();
          if (!success)
            throw new IOException("Directory creation failed: " + dir.getCanonicalPath());
        }

        // Try to construct a new file name everytime
        f = new File(dir, toHex(hash++));

        success = f.createNewFile();

        if (i > 0)
          log.info("file: " + dir.getCanonicalPath() + " created successfully after retries: " + i);

        if (success) return f;
        else i--; // if not success, we should not increment i which is
        // for exceptional retry
      } catch (Exception e) {
        if (lastException == null)
          log.warn(
              "failed to create file: "
                  + dir.getCanonicalPath()
                  + " - retry: "
                  + (i + 1)
                  + " of "
                  + MAX_TIMES_CREATE_FILE
                  + ". Will retry again.",
              e);
        else
          log.warn(
              "failed to create file: "
                  + dir.getCanonicalPath()
                  + " - got the same exception as above - retry: "
                  + (i + 1)
                  + " of "
                  + MAX_TIMES_CREATE_FILE
                  + ". Will retry again");
        lastException = e;

        success = false;

        // Maybe other thread is trying to do the same thing
        // Let's wait for INTERVAL_CREATE_FILE ms - this rarely happens
        try {
          Thread.sleep(INTERVAL_CREATE_FILE);
        } catch (InterruptedException e1) {
        }
      }
    }
    // Run out of retries, throw original exception
    throw lastException;
  }
コード例 #28
0
 /** Walk project references recursively, adding thrift files to the provided list. */
 List<File> getRecursiveThriftFiles(MavenProject project, String outputDirectory, List<File> files)
     throws IOException {
   HashFunction hashFun = Hashing.md5();
   if (dependencyIncludes.contains(project.getArtifactId())) {
     File dir = new File(new File(project.getFile().getParent(), "target"), outputDirectory);
     if (dir.exists()) {
       URI baseDir = getFileURI(dir);
       for (File f : findThriftFilesInDirectory(dir)) {
         URI fileURI = getFileURI(f);
         String relPath = baseDir.relativize(fileURI).getPath();
         File destFolder = getResourcesOutputDirectory();
         destFolder.mkdirs();
         File destFile = new File(destFolder, relPath);
         if (!destFile.exists()
             || (destFile.isFile()
                 && !Files.hash(f, hashFun).equals(Files.hash(destFile, hashFun)))) {
           getLog()
               .info(
                   format("copying %s to %s", f.getCanonicalPath(), destFile.getCanonicalPath()));
           copyFile(f, destFile);
         }
         files.add(destFile);
       }
     }
   }
   Map<String, MavenProject> refs = project.getProjectReferences();
   for (String name : refs.keySet()) {
     getRecursiveThriftFiles(refs.get(name), outputDirectory, files);
   }
   return files;
 }
コード例 #29
0
 protected File getPlayHome() throws IOException, MojoExecutionException {
   File targetDir = new File(project.getBuild().getDirectory());
   File playTmpDir = new File(targetDir, "play");
   File playTmpHomeDir = new File(playTmpDir, "home");
   if (!playTmpHomeDir.exists()) {
     throw new MojoExecutionException(
         String.format(
             "Play! home directory \"%s\" does not exist. Run \"mvn play:initialize\" first.",
             playTmpHomeDir.getCanonicalPath()));
   }
   if (!playTmpHomeDir.isDirectory()) {
     throw new MojoExecutionException(
         String.format(
             "Play! home directory \"%s\" is not a directory", playTmpHomeDir.getCanonicalPath()));
   }
   // Additional check whether the temporary Play! home directory is created by this plugin
   File warningFile = new File(playTmpHomeDir, "WARNING.txt");
   if (warningFile.exists()) {
     if (!warningFile.isFile()) {
       throw new MojoExecutionException(
           String.format(
               "Play! home directory warning file \"%s\" is not a file",
               warningFile.getCanonicalPath()));
     }
   } else {
     throw new MojoExecutionException(
         String.format(
             "Play! home directory warning file \"%s\" does not exist",
             warningFile.getCanonicalPath()));
   }
   return playTmpHomeDir;
 }
コード例 #30
0
  public static List<URL> getFilenames(String filenameStr) throws IOException, URISyntaxException {
    List<URL> filenames = new ArrayList<URL>();

    URI uri = new URI(filenameStr.replaceAll("\\\\", "/"));
    if (null == uri.getScheme()
        || uri.getScheme().length() < 3
        || "file:".equals(uri.getScheme())) {
      File file = new File(filenameStr);
      if (file.exists()) {
        if (file.isDirectory()) {
          File[] fs = file.listFiles(getFileSelector());
          for (File f : fs) {
            filenames.add(new URL("file", "", f.getCanonicalPath()));
          }
        } else {
          filenames.add(new URL("file", "", file.getCanonicalPath()));
        }
      } else {
        throw new IOException(
            Messages.getErrorMessage(ErrorMessage.IO_FILE_NOT_EXIST, new Object[] {filenameStr}));
      }
    } else {
      filenames.add(uri.toURL());
    }
    return filenames;
  }