/**
  * Make absolute the directory passed in parameter. If it was relative, then store absolute path
  * in user config instead of relative and return value
  *
  * @param key Directory system key
  * @param directory absolute or relative directory path
  * @return absolute directory path
  * @since 5.4.2
  */
 private String setAbsolutePath(String key, String directory) {
   if (!new File(directory).isAbsolute()) {
     directory = new File(generator.getNuxeoHome(), directory).getPath();
     generator.getUserConfig().setProperty(key, directory);
   }
   return directory;
 }
 /**
  * Delete files previously deployed by templates. If a file had been overwritten by a template, it
  * will be restored. Helps the server returning to the state before any template was applied.
  *
  * @throws IOException
  * @throws ConfigurationException
  */
 private void deleteTemplateFiles() throws IOException, ConfigurationException {
   File newFiles = new File(generator.getNuxeoHome(), NEW_FILES);
   if (!newFiles.exists()) {
     return;
   }
   BufferedReader reader = null;
   try {
     reader = new BufferedReader(new FileReader(newFiles));
     String line;
     while ((line = reader.readLine()) != null) {
       if (line.endsWith(".bak")) {
         log.debug("Restore " + line);
         try {
           File backup = new File(generator.getNuxeoHome(), line);
           File original =
               new File(generator.getNuxeoHome(), line.substring(0, line.length() - 4));
           FileUtils.copyFile(backup, original);
           backup.delete();
         } catch (IOException e) {
           throw new ConfigurationException(
               String.format(
                   "Failed to restore %s from %s\nEdit or " + "delete %s to bypass that error.",
                   line.substring(0, line.length() - 4), line, newFiles),
               e);
         }
       } else {
         log.debug("Remove " + line);
         new File(generator.getNuxeoHome(), line).delete();
       }
     }
   } finally {
     IOUtils.closeQuietly(reader);
   }
   newFiles.delete();
 }
 /**
  * Check server paths; warn if existing deprecated paths. Override this method to perform server
  * specific checks.
  *
  * @throws ConfigurationException If deprecated paths have been detected
  * @since 5.4.2
  */
 public void checkPaths() throws ConfigurationException {
   File badInstanceClid =
       new File(generator.getNuxeoHome(), getDefaultDataDir() + File.separator + "instance.clid");
   if (badInstanceClid.exists()
       && !getDataDir().equals(new File(generator.getNuxeoHome(), getDefaultDataDir()))) {
     log.warn("Moving " + badInstanceClid + " to " + getDataDir() + ".");
     try {
       FileUtils.moveFileToDirectory(badInstanceClid, getDataDir(), true);
     } catch (IOException e) {
       throw new ConfigurationException("Move failed.", e);
     }
   }
 }
 /**
  * Store into {@link #NEW_FILES} the list of new files deployed by the templates. For later use by
  * {@link #deleteTemplateFiles()}
  *
  * @param newFilesList
  * @throws IOException
  */
 private void storeNewFilesList(List<String> newFilesList) throws IOException {
   BufferedWriter writer = null;
   try {
     // Store new files listing
     File newFiles = new File(generator.getNuxeoHome(), NEW_FILES);
     writer = new BufferedWriter(new FileWriter(newFiles, false));
     int index = generator.getNuxeoHome().getCanonicalPath().length() + 1;
     for (String filepath : newFilesList) {
       writer.write(new File(filepath).getCanonicalPath().substring(index));
       writer.newLine();
     }
   } finally {
     IOUtils.closeQuietly(writer);
   }
 }
  /**
   * Generate configuration files from templates and given configuration parameters
   *
   * @param config Properties with configuration parameters for template replacement
   * @throws ConfigurationException
   */
  protected void parseAndCopy(Properties config)
      throws IOException, TemplateException, ConfigurationException {
    // FilenameFilter for excluding "nuxeo.defaults" files from copy
    final FilenameFilter filter =
        new FilenameFilter() {
          @Override
          public boolean accept(File dir, String name) {
            return !"nuxeo.defaults".equals(name);
          }
        };
    final TextTemplate templateParser = new TextTemplate(config);
    templateParser.setTrim(true);
    templateParser.setTextParsingExtensions(
        config.getProperty(
            ConfigurationGenerator.PARAM_TEMPLATES_PARSING_EXTENSIONS, "xml,properties,nx"));
    templateParser.setFreemarkerParsingExtensions(
        config.getProperty(ConfigurationGenerator.PARAM_TEMPLATES_FREEMARKER_EXTENSIONS, "nxftl"));

    deleteTemplateFiles();
    // add included templates directories
    List<String> newFilesList = new ArrayList<String>();
    for (File includedTemplate : generator.getIncludedTemplates()) {
      if (includedTemplate.listFiles(filter) != null) {
        String templateName = includedTemplate.getName();
        // Check for deprecation
        Boolean isDeprecated = Boolean.valueOf(config.getProperty(templateName + ".deprecated"));
        if (isDeprecated) {
          log.warn("WARNING: Template " + templateName + " is deprecated.");
          String deprecationMessage = config.getProperty(templateName + ".deprecation");
          if (deprecationMessage != null) {
            log.warn(deprecationMessage);
          }
        }
        // Retrieve optional target directory if defined
        String outputDirectoryStr = config.getProperty(templateName + ".target");
        File outputDirectory =
            (outputDirectoryStr != null)
                ? new File(generator.getNuxeoHome(), outputDirectoryStr)
                : getOutputDirectory();
        for (File in : includedTemplate.listFiles(filter)) {
          // copy template(s) directories parsing properties
          newFilesList.addAll(
              templateParser.processDirectory(in, new File(outputDirectory, in.getName())));
        }
      }
    }
    storeNewFilesList(newFilesList);
  }
  @Test
  public void testSimpleObject() {
    Collection<EPackage> inputPackages = new ArrayList<EPackage>();
    inputPackages.add(metamodelPackage);
    Collection<EClass> mainClasses = new ArrayList<EClass>();
    mainClasses.add(getNetClass());
    ConfigurationGenerator generator = new ConfigurationGenerator(inputPackages, mainClasses);
    Collection<EPackage> configurationPackages = generator.generateConfigurationPackages();
    assertEquals(1, configurationPackages.size());
    testConfigurationPackage(configurationPackages.iterator().next());

    Collection<EObject> originalObjects = new ArrayList<EObject>();
    originalObjects.add(netObject);
    ConfigurationObjectMap map = new ConfigurationObjectMap(originalObjects, configurationPackages);
    EObject confNetObject = map.getConfigurationObject(netObject);
    assertNotNull(confNetObject);
    assertEquals(confNetObject, map.getConfigurationObject(map.getOriginalObject(confNetObject)));

    testReferenceValues(map, confNetObject);
    testAttributeValues(map, confNetObject);
  }
 /**
  * @param userConfig Properties to dump into config directory
  * @since 5.4.2
  */
 public void dumpProperties(Properties userConfig) {
   Properties dumpedProperties = filterSystemProperties(userConfig);
   File dumpedFile = generator.getDumpedConfig();
   OutputStream os = null;
   try {
     os = new FileOutputStream(dumpedFile, false);
     dumpedProperties.store(os, "Generated by " + getClass());
   } catch (FileNotFoundException e) {
     log.error(e);
   } catch (IOException e) {
     log.error("Could not dump properties to " + dumpedFile, e);
   } finally {
     IOUtils.closeQuietly(os);
   }
 }
 /**
  * @return Temporary directory
  * @since 5.4.2
  */
 public File getTmpDir() {
   if (tmpDir == null) {
     tmpDir = new File(generator.getNuxeoHome(), getDefaultTmpDir());
   }
   return tmpDir;
 }
 /**
  * @return Log directory
  * @since 5.4.2
  */
 public File getLogDir() {
   if (logDir == null) {
     logDir = new File(generator.getNuxeoHome(), DEFAULT_LOG_DIR);
   }
   return logDir;
 }
 /**
  * @return Data directory
  * @since 5.4.2
  */
 public File getDataDir() {
   if (dataDir == null) {
     dataDir = new File(generator.getNuxeoHome(), getDefaultDataDir());
   }
   return dataDir;
 }