private String getLicenseHeader(Platform platform, Daemon daemon)
     throws DaemonGeneratorException {
   List lines = null;
   if (isDefaultLicenseHeaderRequested(daemon)) {
     getLogger().debug("Using default licence file (" + DEFAULT_LICENSE_HEADER + ".");
     lines = readLicenseHeader();
   } else {
     getLogger().debug("Using license file: " + daemon.getLicenseHeaderFile());
     lines = readLicenseHeaderFromFile(new File(daemon.getLicenseHeaderFile()));
   }
   StringBuffer resultLines = new StringBuffer();
   for (int i = 0; i < lines.size(); i++) {
     String licenseLine = platform.getCommentPrefix() + lines.get(i);
     resultLines.append(licenseLine.trim() + platform.getNewLine());
   }
   return resultLines.toString();
 }
  private void testNormalShellScriptGenerationWithRepo(Platform platform) throws Exception {
    ScriptGenerator generator = (ScriptGenerator) lookup(ScriptGenerator.ROLE);

    Daemon daemon = new Daemon();

    daemon.setId("repo-test");
    daemon.setMainClass("foo.Bar");
    daemon.setJvmSettings(new JvmSettings());
    daemon
        .getJvmSettings()
        .setExtraArguments(Arrays.asList(new String[] {"Yo", "dude", "xyz=@REPO@"}));
    daemon.setEnvironmentSetupFileName("setup");
    daemon.setRepositoryName("repo");
    File outputDirectory = getTestFile("target/test-output/normal-shell/" + platform.getName());

    generator.createBinScript(platform.getName(), daemon, outputDirectory, "bin");

    File expectedFile =
        getTestFile(PREFIX + "expected-" + daemon.getId() + platform.getBinFileExtension());
    File actualFile =
        new File(outputDirectory, "bin/" + daemon.getId() + platform.getBinFileExtension());

    assertEquals(FileUtils.fileRead(expectedFile), FileUtils.fileRead(actualFile));
  }
 public void testNormalShellScriptGenerationWithRepo() throws Exception {
   for (Platform platform : Platform.getAllPlatforms()) {
     testNormalShellScriptGenerationWithRepo(platform);
   }
 }
  /* (non-Javadoc)
   * @see org.codehaus.mojo.appassembler.daemon.script.ScriptGenerator#createBinScript(java.lang.String, org.codehaus.mojo.appassembler.model.Daemon, java.io.File, java.lang.String)
   */
  public void createBinScript(
      String platformName, Daemon daemon, File outputDirectory, String binFolder)
      throws DaemonGeneratorException {
    Platform platform = Platform.getInstance(platformName);

    InputStream in = null;

    FileWriter out = null;

    try {
      in = getClass().getResourceAsStream(platformName + "BinTemplate");

      if (in == null) {
        throw new DaemonGeneratorException(
            "Internal error: could not find template for platform '" + platformName + "'.");
      }
      InputStreamReader reader = new InputStreamReader(in);

      Map context = new HashMap();
      context.put("MAINCLASS", daemon.getMainClass());
      context.put("CLASSPATH", platform.getClassPath(daemon));
      context.put("EXTRA_JVM_ARGUMENTS", platform.getExtraJvmArguments(daemon.getJvmSettings()));
      context.put("APP_NAME", daemon.getId());
      context.put("ENV_SETUP", platform.getEnvSetup(daemon));
      context.put("REPO", daemon.getRepositoryName());
      context.put("LICENSE_HEADER", getLicenseHeader(platform, daemon));
      if (platform.isShowConsoleWindow(daemon)) {
        context.put("JAVA_BINARY", "java");
      } else {
        context.put("JAVA_BINARY", "start /min javaw");
      }

      String appArguments = platform.getAppArguments(daemon);
      if (appArguments != null) {
        context.put("APP_ARGUMENTS", appArguments + " ");
      } else {
        context.put("APP_ARGUMENTS", "");
      }

      String interpolationToken = platform.getInterpolationToken();
      InterpolationFilterReader interpolationFilterReader =
          new InterpolationFilterReader(reader, context, interpolationToken, interpolationToken);

      // Set the name of the bin file
      String programName = "";

      if (daemon.getId() == null || daemon.getId().trim().equals("")) {
        // Get class name and use it as the filename
        StringTokenizer tokenizer = new StringTokenizer(daemon.getMainClass(), ".");
        while (tokenizer.hasMoreElements()) {
          programName = tokenizer.nextToken();
        }

        programName = programName.toLowerCase();
      } else {
        programName = daemon.getId();
      }

      File binDir = new File(outputDirectory, binFolder);
      FileUtils.forceMkdir(binDir);
      File binFile = new File(binDir, programName + platform.getBinFileExtension());

      out = new FileWriter(binFile);
      getLogger()
          .debug(
              "Writing shell file for platform '"
                  + platform.getName()
                  + "' to '"
                  + binFile.getAbsolutePath()
                  + "'.");

      IOUtil.copy(interpolationFilterReader, out);
    } catch (FileNotFoundException e) {
      throw new DaemonGeneratorException("Failed to get template for bin file.", e);
    } catch (IOException e) {
      throw new DaemonGeneratorException("Failed to write bin file.", e);
    } finally {
      IOUtil.close(out);
      IOUtil.close(in);
    }
  }