/**
  * Create a NodeFileParser given the project and the source file, using the predetermined format
  *
  * @param file the nodes resource file
  * @param format the file format
  * @return a new parser based on the determined format
  * @throws ResourceModelSourceException if the format is not supported
  */
 protected ResourceFormatParser createParser(final File file, final String format)
     throws ResourceModelSourceException {
   try {
     if (null != format) {
       return framework.getResourceFormatParserService().getParserForFormat(format);
     } else {
       return framework.getResourceFormatParserService().getParserForFileExtension(file);
     }
   } catch (UnsupportedFormatException e) {
     throw new ResourceModelSourceException(e);
   }
 }
Example #2
0
 /**
  * @return specific nodes resources file path for the project, based on the
  *     framework.nodes.file.name property
  */
 public static String getNodesResourceFilePath(IRundeckProject project, Framework framework) {
   if (project.hasProperty(ProjectNodeSupport.PROJECT_RESOURCES_FILE_PROPERTY)) {
     return new File(project.getProperty(ProjectNodeSupport.PROJECT_RESOURCES_FILE_PROPERTY))
         .getAbsolutePath();
   }
   if (null != framework) {
     File etcDir = new File(framework.getFrameworkProjectsBaseDir(), project.getName() + "/etc/");
     if (framework.hasProperty(Framework.NODES_RESOURCES_FILE_PROP)) {
       return new File(etcDir, framework.getProperty(Framework.NODES_RESOURCES_FILE_PROP))
           .getAbsolutePath();
     } else {
       return new File(etcDir, ProjectNodeSupport.NODES_XML).getAbsolutePath();
     }
   } else {
     return null;
   }
 }
  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);
      }
    }
  }
 private void loadNodes(final File nodesFile, final String format)
     throws ResourceModelSourceException {
   if (!nodesFile.isFile() && configuration.generateFileAutomatically) {
     generateResourcesFile(nodesFile, format);
   } else if (configuration.includeServerNode) {
     final NodeEntryImpl node = framework.createFrameworkNode();
     nodeSet.putNode(node);
   }
   if (nodesFile.isFile()) {
     final ResourceFormatParser parser = createParser(nodesFile, format);
     try {
       final INodeSet set = parser.parseDocument(nodesFile);
       if (null != set) {
         nodeSet.putNodes(set);
       }
     } catch (ResourceFormatParserException e) {
       throw new ResourceModelSourceException(e);
     }
   } else if (configuration.requireFileExists) {
     throw new ResourceModelSourceException("File does not exist: " + nodesFile);
   }
 }