private void generateResourcesFile(final File resfile, final String format)
      throws ResourceModelSourceException {
    final NodeEntryImpl node = framework.createFrameworkNode();
    node.setFrameworkProject(configuration.project);
    final ResourceFormatGenerator generator;
    if (null != format) {
      try {
        generator = framework.getResourceFormatGeneratorService().getGeneratorForFormat(format);
      } catch (UnsupportedFormatException e) {
        throw new ResourceModelSourceException(e);
      }
    } else {
      try {
        generator =
            framework.getResourceFormatGeneratorService().getGeneratorForFileExtension(resfile);
      } catch (UnsupportedFormatException e) {
        throw new ResourceModelSourceException(e);
      }
    }
    if (configuration.includeServerNode) {
      NodeSetImpl nodes = new NodeSetImpl();
      nodes.putNode(node);

      if (!resfile.getParentFile().exists()) {
        if (!resfile.getParentFile().mkdirs()) {
          throw new ResourceModelSourceException(
              "Parent dir for resource file does not exists, and could not be created: " + resfile);
        }
      }

      try {
        final FileOutputStream stream = new FileOutputStream(resfile);
        try {
          generator.generateDocument(nodes, stream);
        } finally {
          stream.close();
        }
      } catch (IOException e) {
        throw new ResourceModelSourceException(e);
      } catch (ResourceFormatGeneratorException e) {
        throw new ResourceModelSourceException(e);
      }
    }
  }
示例#2
0
  /**
   * Update the resources file given an input Nodes set
   *
   * @param nodeset nodes
   * @throws UpdateUtils.UpdateException if an error occurs while trying to update the resources
   *     file or generate nodes
   */
  @Override
  public void updateNodesResourceFile(final INodeSet nodeset, final String nodesResourceFilePath)
      throws UpdateUtils.UpdateException {
    final ResourceFormatGenerator generator;
    File destfile = new File(nodesResourceFilePath);
    try {
      generator = resourceFormatGeneratorService.getGeneratorForFileExtension(destfile);
    } catch (UnsupportedFormatException e) {
      throw new UpdateUtils.UpdateException(
          "Unable to determine file format for file: " + nodesResourceFilePath, e);
    }
    File resfile = null;
    try {
      resfile = File.createTempFile("resource-temp", destfile.getName());
      resfile.deleteOnExit();
    } catch (IOException e) {
      throw new UpdateUtils.UpdateException("Unable to create temp file: " + e.getMessage(), e);
    }
    // serialize nodes and replace the nodes resource file

    try {
      final FileOutputStream stream = new FileOutputStream(resfile);
      try {
        generator.generateDocument(nodeset, stream);
      } finally {
        stream.close();
      }
    } catch (IOException e) {
      throw new UpdateUtils.UpdateException(
          "Unable to generate resources file: " + e.getMessage(), e);
    } catch (ResourceFormatGeneratorException e) {
      throw new UpdateUtils.UpdateException(
          "Unable to generate resources file: " + e.getMessage(), e);
    }

    updateNodesResourceFile(resfile, nodesResourceFilePath);
    if (!resfile.delete()) {
      logger.warn("failed to remove temp file: " + resfile);
    }
    logger.debug("generated resources file: " + resfile.getAbsolutePath());
  }