@Before
  public void before() throws Exception {
    File test = new File("dir");
    test.mkdir();
    File testFile = new File(test, "test.txt");
    FileUtils.writeLines(testFile, Arrays.asList("Hello", "My", "Name"));

    File multiDir = new File("multidir");
    for (int i = 0; i < 2; i++) {
      File newTestFile = new File(multiDir, "testfile-" + i);
      FileUtils.writeLines(newTestFile, Arrays.asList("Sentence 1.", "Sentence 2.", "Sentence 3."));
    }
  }
Ejemplo n.º 2
0
  @Before
  public void createTestFiles() throws IOException {
    SOME_FILE = test_folder.newFile();
    FileUtils.writeLines(SOME_FILE, Arrays.asList(SOME_FILE_LINE1, SOME_FILE_LINE2));

    OTHER_FILE = test_folder.newFile();
    FileUtils.writeLines(
        OTHER_FILE, Arrays.asList(OTHER_FILE_LINE1, OTHER_FILE_LINE2, OTHER_FILE_LINE3));

    DIRECTORY_DOES_NOT_EXIST = new File("");
    VALID_DIRECTORY_INPUT = new DirectoryInput(test_folder.getRoot());

    NON_READABLE_DIRECTORY = test_folder.newFolder();
    NON_READABLE_DIRECTORY.setReadable(false);
  }
  private List<String> loadAllApkIds() throws Exception {
    File allFile = getAllApkIdsFile();
    if (allFile.exists()) return FileUtils.readLines(allFile);

    List<String> apkIds =
        (List<String>)
            executeSql(
                new ConnectionHandler() {
                  @Override
                  public Object handle(Connection conn) throws Exception {
                    ArrayList<String> l = new ArrayList<String>();
                    Statement stmt = conn.createStatement();
                    ResultSet rs =
                        stmt.executeQuery("select package, version_code, architecture from qapk");
                    while (rs.next()) {
                      l.add(
                          ApkId.of(
                                  rs.getString("package"),
                                  rs.getInt("version_code"),
                                  rs.getInt("architecture"))
                              .toString());
                    }
                    stmt.close();
                    return l;
                  }
                });
    FileUtils.writeLines(allFile, apkIds);
    return apkIds;
  }
Ejemplo n.º 4
0
  private File checkUrlrewriteConf(String basePath) throws IOException {

    // create and register config file
    String urlrewritePath = basePath + "/urlwrewrite.xml";
    File urlrewriteFile = new File(urlrewritePath);

    // Create structr.conf if not existing
    if (!urlrewriteFile.exists()) {

      // synthesize a urlrewrite.xml file
      List<String> config = new LinkedList<String>();

      config.add("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
      config.add(
          "<!DOCTYPE urlrewrite\n"
              + "        PUBLIC \"-//tuckey.org//DTD UrlRewrite 3.2//EN\"\n"
              + "        \"http://www.tuckey.org/res/dtds/urlrewrite3.2.dtd\">");

      config.add("<urlrewrite>");
      config.add("    <rule match-type=\"regex\">");
      config.add("        <name>RedirectToHtmlServlet</name>");
      config.add(
          "        <condition type=\"request-uri\" operator=\"notequal\">^/structr/</condition>");
      config.add("        <from>^/(.*)$</from>");
      config.add("        <to type=\"forward\" last=\"true\">/structr/html/$1</to>");
      config.add("    </rule>");
      config.add("</urlrewrite>");

      urlrewriteFile.createNewFile();
      FileUtils.writeLines(urlrewriteFile, "UTF-8", config);
    }

    return urlrewriteFile;
  }
Ejemplo n.º 5
0
 private synchronized void addFailedRecord(
     PollTableEntry pollTableEntry, FileObject failedObject, String timeString) {
   try {
     String record =
         failedObject.getName().getBaseName() + VFSConstants.FAILED_RECORD_DELIMITER + timeString;
     String recordFile =
         pollTableEntry.getFailedRecordFileDestination()
             + pollTableEntry.getFailedRecordFileName();
     File failedRecordFile = new File(recordFile);
     if (!failedRecordFile.exists()) {
       FileUtils.writeStringToFile(failedRecordFile, record);
       if (log.isDebugEnabled()) {
         log.debug("Added fail record '" + record + "' into the record file '" + recordFile + "'");
       }
     } else {
       List<String> content = FileUtils.readLines(failedRecordFile);
       if (!content.contains(record)) {
         content.add(record);
       }
       FileUtils.writeLines(failedRecordFile, content);
     }
   } catch (IOException e) {
     log.fatal("Failure while writing the failed records!", e);
   }
 }
Ejemplo n.º 6
0
 public static void writeLines(File file, String encoding, Collection lines) {
   try {
     FileUtils.writeLines(file, encoding, lines);
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
Ejemplo n.º 7
0
 public static void writeLines(File file, Collection lines, String lineEnding) {
   try {
     FileUtils.writeLines(file, lines, lineEnding);
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
 @Override
 public String analyze(Iterable<Mutation> mutations, HtmlReport report) {
   List<String> lines = new ArrayList<String>();
   for (Mutation mutation : mutations) {
     if (mutation.isKilled()) {
       MutationTestResult result = mutation.getMutationResult();
       Set<TestMessage> detecting = new HashSet<TestMessage>();
       Collection<TestMessage> failures = result.getFailures();
       detecting.addAll(failures);
       Collection<TestMessage> errors = result.getErrors();
       detecting.addAll(errors);
       String tests = getIds(detecting);
       String line = mutation.getId() + "," + tests;
       lines.add(line);
     }
   }
   Set<Entry<String, Integer>> entrySet = testsIds.entrySet();
   lines.add("Ids");
   for (Entry<String, Integer> entry : entrySet) {
     lines.add(entry.getKey() + "," + entry.getValue());
   }
   try {
     FileUtils.writeLines(new File("detectedByTest.csv"), lines);
   } catch (IOException e) {
     e.printStackTrace();
   }
   return Joiner.on("\n").join(lines);
 }
Ejemplo n.º 9
0
 public static void generatePageLinks() throws Exception {
   List<String> pageLinks = new ArrayList<String>();
   for (int i = 1; i <= 100; i++) {
     String url = "http://esf.sh.fang.com/house-a026/i3" + i + "/";
     pageLinks.add(url);
   }
   FileUtils.writeLines(new File("./output/soufang/pagelink.txt"), pageLinks, true);
 }
Ejemplo n.º 10
0
 public void createAnnotationFile(List<String> sentences) {
   try {
     final String file = FrameNetOptions.ABS_PATH_FNDATA + FrameNetOptions.FN_FILE_NAME;
     FileUtils.writeLines(new File(file), sentences);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 11
0
 private void writeItems() {
   File filesDir = getFilesDir();
   File todoFile = new File(filesDir, "todo.txt");
   try {
     FileUtils.writeLines(todoFile, items);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 12
0
 private static void output_result(String outPath, Map<String, List<String>> result_map) {
   for (String s : result_map.keySet()) {
     try {
       FileUtils.writeLines(new File(outPath + s), result_map.get(s));
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
  @Test
  public void verify_readFileSource() throws IOException {
    File file = writer.getSourceFile(COMPONENT_REF);
    FileUtils.writeLines(file, of("1", "2", "3"));

    CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF).get();
    assertThat(res).containsExactly("1", "2", "3");
    res.close();
  }
Ejemplo n.º 14
0
 public static void writeLines(final File file, final List<String> lines) {
   try {
     if (!file.exists()) {
       file.createNewFile();
     }
     FileUtils.writeLines(file, lines);
   } catch (IOException e) {
     logger.warn("I/O error while writing lines to file " + file.getPath(), e);
   }
 }
Ejemplo n.º 15
0
 private void createReadMeFile(final File binaryRepoFolder) throws IOException {
   // add a "README.md" file and commit
   final File readmeFile = new File(binaryRepoFolder, "README.md");
   final List<String> readmeContent = new ArrayList<String>();
   readmeContent.add("Binary Repository For " + getSourceRemoteUrl());
   readmeContent.add("=======================================================");
   readmeContent.add("");
   readmeContent.add("Stores the class files for the above source repository");
   org.apache.commons.io.FileUtils.writeLines(readmeFile, readmeContent, "\n");
 }
  /**
   * Modifies public getters to return {@code Object} so inheriting classes could override them with
   * different signature.
   *
   * @param distPath the top level directory of the source code
   * @param accessors a map where keys are class names and values are the list of properties (and
   *     thus getters) to be modified
   */
  public static void modifyGetters(String distPath, Map<String, List<String>> accessors)
      throws IOException {
    String templateOriginal = "    public $accessor$ get$accessor$() {";
    String templateReplace = "    public Object get$accessor$() {";
    String placeHolder = "$accessor$";
    boolean isInAccessor = false;

    // change shadowed getters
    File entitiesDir = new File(distPath, ENTITIES_PACKAGE.replace('.', File.separatorChar));
    for (File file : entitiesDir.listFiles()) {
      List<String> finalLines = new ArrayList<>(COPYRIGHT_LINES);
      List<String> tempLines = new ArrayList<>();

      List<String> accessorsToCheck = accessors.get(file.getName().replace(".java", ""));
      if (accessorsToCheck == null) {
        injectCopyrightHeader(file);
        continue;
      }

      for (String line : FileUtils.readLines(file)) {
        if (line.isEmpty()) {
          isInAccessor = false;
          finalLines.addAll(tempLines);
          finalLines.add(line);
          tempLines.clear();
        } else if (line.equals("}")) {
          isInAccessor = false;
          finalLines.add("}");
        } else {
          if (!isInAccessor) {
            for (String accessor : accessorsToCheck) {
              if (line.toLowerCase()
                  .equals(templateOriginal.replace(placeHolder, accessor).toLowerCase())) {
                isInAccessor = true;
                tempLines.add(templateReplace.replace(placeHolder, accessor));
                break;
              }
            }
            if (!isInAccessor) {
              tempLines.add(line);
            } else {
              isInAccessor = false;
            }
          }
        }
      }

      // Save the new content:
      finalLines = StringUtils.removeTrailingWhitespace(finalLines);
      FileUtils.writeLines(file, finalLines);
    }
  }
  // "hermitfang";
  // "https://abs.twimg.com/sticky/default_profile_images/default_profile_5_bigger.png";
  private void writeItems() {
    myProfile.add("hermitfang");
    myProfile.add(
        "https://abs.twimg.com/sticky/default_profile_images/default_profile_5_bigger.png");

    File filesDir = getFilesDir();
    File todoFile = new File(filesDir, "myProfile.txt");
    try {
      FileUtils.writeLines(todoFile, myProfile);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 18
0
 /**
  * Write manifest in the install jar.
  *
  * @throws IOException for any I/O error
  */
 protected void writeManifest() throws IOException {
   // Add splash screen configuration
   List<String> lines = IOUtils.readLines(getClass().getResourceAsStream("MANIFEST.MF"));
   if (splashScreenImage != null) {
     String destination = String.format("META-INF/%s", splashScreenImage.getName());
     mergeManager.addResourceToMerge(splashScreenImage.getAbsolutePath(), destination);
     lines.add(String.format("SplashScreen-Image: %s", destination));
   }
   lines.add("");
   File tempManifest = com.izforge.izpack.util.file.FileUtils.createTempFile("MANIFEST", ".MF");
   FileUtils.writeLines(tempManifest, lines);
   mergeManager.addResourceToMerge(tempManifest.getAbsolutePath(), "META-INF/MANIFEST.MF");
 }
Ejemplo n.º 19
0
  public void output(int start, int end)
      throws MalformedURLException, IOException, XPatherException {

    ArrayList<InstitutionDataItem> data = new ArrayList<InstitutionDataItem>();

    for (int i = start; i <= end; i++) {
      data.addAll(this.getData(i));
    }

    File outputFile = new File("complete-institutions-" + start + "-" + end + ".csv");
    String headings = data.get(0).getHeadings() + "\n";
    FileUtils.write(outputFile, headings);
    FileUtils.writeLines(outputFile, data, true);
  }
Ejemplo n.º 20
0
  /**
   * 将日志写入文件
   *
   * @param logs
   * @param destFile
   */
  private static void writeLogToFile(Collection<SVNLog> logs, File destFile) throws Exception {
    if (logs == null || logs.isEmpty()) {
      return;
    }
    if (!destFile.exists()) {
      destFile.createNewFile();
    }
    List<String> contents = new LinkedList<String>();
    for (SVNLog e : logs) {
      contents.add(e.toSimpleString());
    }

    FileUtils.writeLines(destFile, contents, false);
  }
Ejemplo n.º 21
0
  /**
   * Generate the new config spec
   *
   * @return The generated {@link ConfigSpec}
   * @throws net.praqma.clearcase.exceptions.CleartoolException Thrown when ClearCase reports errors
   * @throws java.io.IOException Thrown if we're not able to create the temporary config spec file
   */
  public ConfigSpec generate() throws CleartoolException, IOException {
    List<String> csLines = catcs();

    /* Remove custom load rules */
    removeLoadRules(csLines);

    /* Add new custom load rules */
    logger.fine("Add new custom load rules");
    logger.fine("Load rules: " + loadRules);
    csLines.addAll(addLoad(loadRules));

    temporaryCSFile = File.createTempFile("ClearCase", "configspec");
    FileUtils.writeLines(temporaryCSFile, csLines);

    return this;
  }
  private static void cleanFile(final InputStream is, final File file) throws IOException {

    try {
      final List<String> updatedLines = new LinkedList<>();
      final List<String> lines = FileUtils.readLines(file);
      for (String line : lines) {
        if (line.contains("DOCTYPE")) {
          break;
        }
        updatedLines.add(line);
      }
      FileUtils.writeLines(file, updatedLines, false);
    } catch (IOException e) {
      throw e;
    }
  }
Ejemplo n.º 23
0
  @RequestMapping(value = "export-list", method = RequestMethod.GET)
  public String exportTaskList() {

    Collection<Task> tasks = this.taskService.getAllTasks();

    try {

      String filePath = this.localSettingsProperties.getProperty(Constants.TASKS_FILE);

      FileUtils.writeLines(new File(filePath), tasks);
    } catch (IOException ex) {
      LOGGER.error(ExceptionUtils.getFullStackTrace(ex));
    }

    return "redirect:/";
  }
  public static void main(String[] args) throws Throwable {

    final InputStream is = ServerLogsService.class.getResourceAsStream("/access.log");
    final File log = new File("teste.txt");
    FileUtils.copyInputStreamToFile(is, log);

    final List<String> updatedLines = new LinkedList<>();
    final List<String> lines = FileUtils.readLines(log);
    for (String line : lines) {
      if (line.contains("DOCTYPE")) {
        break;
      }
      updatedLines.add(line);
    }
    final File file = new File("teste_resul.log");
    FileUtils.writeLines(file, updatedLines, false);
  }
Ejemplo n.º 25
0
  /**
   * 得到图片地址路径
   *
   * @param path
   * @return
   */
  public static boolean listDir(String path) {
    File picPath = new File("/home/lifeix/temp/uploadpicPath.txt");
    //		File picPath = new File("/home/lifeix/temp/picPath.txt");
    if (picPath.exists()) {
      try {
        imgsArr = FileUtils.readLines(picPath);
        return true;
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    File dir = new File(path);
    File[] files = dir.listFiles();
    for (File file : files) {

      if (file.isDirectory()) {
        listDir(file.getAbsolutePath());
      } else {
        try {
          ImageInputStream is = ImageIO.createImageInputStream(file);
          if (is != null && file.getAbsolutePath().endsWith(".jpg")) {
            imgsArr.add(file.getAbsolutePath());
            System.out.println(file.getAbsolutePath());
          }
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
        //				System.out.println(file.getAbsolutePath());
        // System.out.println(file.getParent() + "-->" + file.getName());
      }
    }

    if (picPath.exists()) {
      try {
        FileUtils.writeLines(picPath, imgsArr);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return true;
  }
Ejemplo n.º 26
0
  private static void execPrintTypesDoeCorpus(String[] args) {
    boolean ascending = true;

    // Ascending or descending?
    if (args.length > 1)
      if ("asc".equalsIgnoreCase(args[1])) ascending = true;
      else if ("desc".equalsIgnoreCase(args[1])) ascending = false;
      else throw new IllegalArgumentException("Second parameter must be either 'asc' or 'desc'");

    // Save to file?
    String path = "";
    List<String> lines = null;
    boolean writeToFile = false;
    if (args.length > 2) {
      path = args[2];
      lines = new ArrayList<>();
      writeToFile = true;
    }

    // Load corpus first
    execParseDoeCorpus(false /* doSave */);

    HashMap<String, Integer> types = corpus.getVocabulary();
    Map<String, Integer> sortedTypes = ListAndMapUtil.sortByComparator(types, ascending);

    for (String key : sortedTypes.keySet()) {
      String line = key + ";" + sortedTypes.get(key);
      System.out.println(line);

      if (writeToFile) lines.add(line);
    }

    // Save to file
    if (writeToFile)
      try {
        FileUtils.writeLines(new File(path), lines);
      } catch (IOException e) {
        System.out.println("Error while trying to write to file " + path);
        e.printStackTrace();
      }
  }
Ejemplo n.º 27
0
 /** Write manifest in the install jar. */
 @Override
 public void writeManifest() throws IOException {
   IXMLElement data = resourceFinder.getXMLTree();
   IXMLElement guiPrefsElement = data.getFirstChildNamed("guiprefs");
   // Add splash screen configuration
   List<String> lines = IOUtils.readLines(getClass().getResourceAsStream("MANIFEST.MF"));
   IXMLElement splashNode = guiPrefsElement.getFirstChildNamed("splash");
   if (splashNode != null) {
     // Add splash image to installer jar
     File splashImage =
         FileUtils.toFile(
             resourceFinder.findProjectResource(splashNode.getContent(), "Resource", splashNode));
     String destination = String.format("META-INF/%s", splashImage.getName());
     mergeManager.addResourceToMerge(splashImage.getAbsolutePath(), destination);
     lines.add(String.format("SplashScreen-Image: %s", destination));
   }
   lines.add("");
   File tempManifest = com.izforge.izpack.util.file.FileUtils.createTempFile("MANIFEST", ".MF");
   FileUtils.writeLines(tempManifest, lines);
   mergeManager.addResourceToMerge(tempManifest.getAbsolutePath(), "META-INF/MANIFEST.MF");
 }
Ejemplo n.º 28
0
  public static void saveAgentLinks(String pageLink) throws Exception {

    String html = HttpUtil.HttpGet(pageLink);
    // System.out.println(html);
    Document doc = Jsoup.parse(html);
    // Document doc = Jsoup.parse(new File("./input/html.txt"), "utf-8");
    List<String> agentLinks = new ArrayList<String>();

    Elements elements = doc.select("A[target=_blank]");

    for (Element element : elements) {
      String href = element.attr("href");
      if (href.startsWith("/a/")) {
        String url = "http://esf.sh.fang.com" + href;
        System.out.println(url);
        agentLinks.add(url);
      }
    }

    FileUtils.writeLines(new File("./output/soufang/agentlink.txt"), agentLinks, true);
  }
Ejemplo n.º 29
0
 public ModuleRunner(String sdkHome) throws Exception {
   if (sdkHome == null) {
     sdkHome = System.getenv(ModuleBuilder.GLOBAL_SDK_HOME_ENV_VAR);
     if (sdkHome == null)
       throw new IllegalStateException(
           "Path to kb-sdk home folder should be set either"
               + " in command line (-h) or in "
               + ModuleBuilder.GLOBAL_SDK_HOME_ENV_VAR
               + " system environment variable");
   }
   File sdkHomeDir = new File(sdkHome);
   if (!sdkHomeDir.exists()) sdkHomeDir.mkdirs();
   File sdkCfgFile = new File(sdkHomeDir, "sdk.cfg");
   String sdkCfgPath = sdkCfgFile.getCanonicalPath();
   if (!sdkCfgFile.exists()) {
     System.out.println(
         "Warning: file "
             + sdkCfgFile.getAbsolutePath()
             + " will be "
             + "initialized (with 'kbase_endpoint'/'catalog_url' pointing to AppDev "
             + "environment, user and password will be prompted every time if not set)");
     FileUtils.writeLines(
         sdkCfgFile,
         Arrays.asList(
             "kbase_endpoint=https://appdev.kbase.us/services",
             "catalog_url=https://appdev.kbase.us/services/catalog",
             "user="******"password="******"### Please use next parameter to specify custom list of network",
             "### interfaces used for callback IP address lookup:",
             "#callback_networks=docker0,vboxnet0,vboxnet1,en0,en1,en2,en3"));
   }
   Properties sdkConfig = new Properties();
   try (InputStream is = new FileInputStream(sdkCfgFile)) {
     sdkConfig.load(is);
   }
   kbaseEndpoint = sdkConfig.getProperty("kbase_endpoint");
   if (kbaseEndpoint == null) {
     throw new IllegalStateException("Couldn't find 'kbase_endpoint' parameter in " + sdkCfgFile);
   }
   String catalogUrlText = sdkConfig.getProperty("catalog_url");
   if (catalogUrlText == null) {
     throw new IllegalStateException("Couldn't find 'catalog_url' parameter in " + sdkCfgFile);
   }
   catalogUrl = new URL(catalogUrlText);
   runDir = new File(sdkHomeDir, "run_local");
   user = sdkConfig.getProperty("user");
   password = sdkConfig.getProperty("password");
   if (user == null || user.trim().isEmpty()) {
     System.out.println(
         "You haven't preset your user/password in " + sdkCfgPath + ". " + "Please enter it now.");
     user = new String(System.console().readLine("User: "******"Password: "******"You haven't preset your password in " + sdkCfgPath + ". " + "Please enter it now.");
       password = new String(System.console().readPassword("Password: "******"callback_networks");
   if (callbackNetworksText != null) {
     callbackNetworks = callbackNetworksText.trim().split("\\s*,\\s*");
   }
 }
Ejemplo n.º 30
0
 public int run(
     String methodName,
     File inputFile,
     boolean stdin,
     String inputJson,
     File output,
     String tagVer,
     boolean verbose,
     boolean keepTempFiles,
     String provRefs,
     String mountPoints)
     throws Exception {
   AuthToken auth = AuthService.login(user, password).getToken();
   ////////////////////////////////// Loading image name /////////////////////////////////////
   CatalogClient client = new CatalogClient(catalogUrl);
   String moduleName = methodName.split(Pattern.quote("."))[0];
   ModuleVersion mv =
       client.getModuleVersion(
           new SelectModuleVersion()
               .withModuleName(moduleName)
               .withVersion(tagVer)
               .withIncludeCompilationReport(1L));
   if (mv.getDataVersion() != null)
     throw new IllegalStateException(
         "Reference data is required for module "
             + moduleName
             + ". This feature is not supported for local calls.");
   String dockerImage = mv.getDockerImgName();
   System.out.println("Docker image name recieved from Catalog: " + dockerImage);
   ////////////////////////////////// Standard files in run_local ////////////////////////////
   if (!runDir.exists()) runDir.mkdir();
   File runLocalSh = new File(runDir, "run_local.sh");
   File runDockerSh = new File(runDir, "run_docker.sh");
   if (!runLocalSh.exists()) {
     FileUtils.writeLines(
         runLocalSh,
         Arrays.asList(
             "#!/bin/bash",
             "sdir=\"$(cd \"$(dirname \"$(readlink -f \"$0\")\")\" && pwd)\"",
             "callback_url=$1",
             "cnt_id=$2",
             "docker_image=$3",
             "mount_points=$4",
             "$sdir/run_docker.sh run -v $sdir/workdir:/kb/module/work $mount_points "
                 + "-e \"SDK_CALLBACK_URL=$callback_url\" --name $cnt_id $docker_image async"));
     ProcessHelper.cmd("chmod", "+x", runLocalSh.getCanonicalPath()).exec(runDir);
   }
   if (!runDockerSh.exists()) {
     FileUtils.writeLines(runDockerSh, Arrays.asList("#!/bin/bash", "docker \"$@\""));
     ProcessHelper.cmd("chmod", "+x", runDockerSh.getCanonicalPath()).exec(runDir);
   }
   ////////////////////////////////// Temporary files ////////////////////////////////////////
   StringBuilder mountPointsDocker = new StringBuilder();
   if (mountPoints != null) {
     for (String part : mountPoints.split(Pattern.quote(","))) {
       String[] fromTo = part.split(Pattern.quote(":"));
       String to;
       if (fromTo.length != 2) {
         if (fromTo.length == 1) {
           to = "tmp";
         } else {
           throw new IllegalStateException("Unexpected mount point format: " + part);
         }
       } else {
         to = fromTo[1];
       }
       File fromDir = new File(fromTo[0]);
       if ((!fromDir.exists()) || (!fromDir.isDirectory()))
         throw new IllegalStateException("Mount point directory doesn't exist: " + fromDir);
       String from = fromDir.getCanonicalPath();
       if (!to.startsWith("/")) to = "/kb/module/work/" + to;
       if (mountPointsDocker.length() > 0) mountPointsDocker.append(" ");
       mountPointsDocker.append("-v ").append(from).append(":").append(to);
     }
   }
   File workDir = new File(runDir, "workdir");
   if (workDir.exists()) FileUtils.deleteDirectory(workDir);
   workDir.mkdir();
   File subjobsDir = new File(runDir, "subjobs");
   if (subjobsDir.exists()) FileUtils.deleteDirectory(subjobsDir);
   File tokenFile = new File(workDir, "token");
   try (FileWriter fw = new FileWriter(tokenFile)) {
     fw.write(auth.getToken());
   }
   String jobSrvUrl = kbaseEndpoint + "/userandjobstate";
   String wsUrl = kbaseEndpoint + "/ws";
   String shockUrl = kbaseEndpoint + "/shock-api";
   File configPropsFile = new File(workDir, "config.properties");
   PrintWriter pw = new PrintWriter(configPropsFile);
   try {
     pw.println("[global]");
     pw.println("job_service_url = " + jobSrvUrl);
     pw.println("workspace_url = " + wsUrl);
     pw.println("shock_url = " + shockUrl);
     pw.println("kbase_endpoint = " + kbaseEndpoint);
   } finally {
     pw.close();
   }
   ////////////////////////////////// Preparing input.json ///////////////////////////////////
   String jsonString;
   if (inputFile != null) {
     jsonString = FileUtils.readFileToString(inputFile);
   } else if (inputJson != null) {
     jsonString = inputJson;
   } else if (stdin) {
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     IOUtils.copy(System.in, baos);
     jsonString = new String(baos.toByteArray(), Charset.forName("utf-8"));
   } else {
     throw new IllegalStateException("No one input method is used");
   }
   jsonString = jsonString.trim();
   if (!jsonString.startsWith("["))
     jsonString = "[" + jsonString + "]"; // Wrapping one argument by array
   if (verbose) System.out.println("Input parameters: " + jsonString);
   Map<String, Object> rpc = new LinkedHashMap<String, Object>();
   rpc.put("version", "1.1");
   rpc.put("method", methodName);
   List<UObject> params =
       UObject.getMapper().readValue(jsonString, new TypeReference<List<UObject>>() {});
   rpc.put("params", params);
   rpc.put("context", new LinkedHashMap<String, Object>());
   UObject.getMapper().writeValue(new File(workDir, "input.json"), rpc);
   ////////////////////////////////// Starting callback service //////////////////////////////
   int callbackPort = NetUtils.findFreePort();
   URL callbackUrl = CallbackServer.getCallbackUrl(callbackPort, callbackNetworks);
   Server jettyServer = null;
   if (callbackUrl != null) {
     if (System.getProperty("os.name").startsWith("Windows")) {
       JsonServerSyslog.setStaticUseSyslog(false);
       JsonServerSyslog.setStaticMlogFile(new File(workDir, "callback.log").getCanonicalPath());
     }
     CallbackServerConfig cfg =
         new CallbackServerConfigBuilder(
                 new URL(kbaseEndpoint),
                 callbackUrl,
                 runDir.toPath(),
                 new LineLogger() {
                   @Override
                   public void logNextLine(String line, boolean isError) {
                     if (isError) {
                       System.err.println(line);
                     } else {
                       System.out.println(line);
                     }
                   }
                 })
             .build();
     Set<String> releaseTags = new TreeSet<String>();
     if (mv.getReleaseTags() != null) releaseTags.addAll(mv.getReleaseTags());
     String requestedRelease =
         releaseTags.contains("release")
             ? "release"
             : (releaseTags.contains("beta") ? "beta" : "dev");
     final ModuleRunVersion runver =
         new ModuleRunVersion(
             new URL(mv.getGitUrl()),
             new ModuleMethod(methodName),
             mv.getGitCommitHash(),
             mv.getVersion(),
             requestedRelease);
     List<String> inputWsObjects = new ArrayList<String>();
     if (provRefs != null) {
       inputWsObjects.addAll(Arrays.asList(provRefs.split(Pattern.quote(","))));
     }
     JsonServerServlet catalogSrv =
         new SDKCallbackServer(auth, cfg, runver, params, inputWsObjects);
     jettyServer = new Server(callbackPort);
     ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
     context.setContextPath("/");
     jettyServer.setHandler(context);
     context.addServlet(new ServletHolder(catalogSrv), "/*");
     jettyServer.start();
   } else {
     if (callbackNetworks != null && callbackNetworks.length > 0) {
       throw new IllegalStateException(
           "No proper callback IP was found, "
               + "please check callback_networks parameter in configuration");
     }
     System.out.println(
         "WARNING: No callback URL was recieved "
             + "by the job runner. Local callbacks are disabled.");
   }
   ////////////////////////////////// Running Docker /////////////////////////////////////////
   final String containerName =
       "local_" + moduleName.toLowerCase() + "_" + System.currentTimeMillis();
   try {
     System.out.println();
     int exitCode =
         ProcessHelper.cmd(
                 "bash",
                 DirUtils.getFilePath(runLocalSh),
                 callbackUrl.toExternalForm(),
                 containerName,
                 dockerImage,
                 mountPointsDocker.toString())
             .exec(runDir)
             .getExitCode();
     File outputTmpFile = new File(workDir, "output.json");
     if (!outputTmpFile.exists())
       throw new IllegalStateException("Output JSON file was not found");
     FinishJobParams outObj = UObject.getMapper().readValue(outputTmpFile, FinishJobParams.class);
     if (outObj.getError() != null || outObj.getResult() == null) {
       System.out.println();
       if (outObj.getError() == null) {
         System.err.println("Unknown error (no information)");
       } else {
         System.err.println("Error: " + outObj.getError().getMessage());
         if (verbose && outObj.getError().getError() != null) {
           System.err.println("Error details: \n" + outObj.getError().getError());
         }
       }
       System.out.println();
     } else {
       String outputJson = UObject.getMapper().writeValueAsString(outObj.getResult());
       if (output != null) {
         FileUtils.writeStringToFile(output, outputJson);
         System.out.println("Output is saved to file: " + output.getCanonicalPath());
       } else {
         System.out.println();
         System.out.println("Output returned by the method:");
         System.out.println(outputJson);
         System.out.println();
       }
     }
     return exitCode;
   } finally {
     try {
       System.out.println("Deleteing docker container...");
       ProcessHelper.cmd(
               "bash", DirUtils.getFilePath(runDockerSh), "rm", "-v", "-f", containerName)
           .exec(runDir);
     } catch (Exception ex) {
       System.out.println("Error deleting container [" + containerName + "]: " + ex.getMessage());
     }
     if (jettyServer != null) {
       System.out.println("Shutting down callback server...");
       jettyServer.stop();
     }
     if (!keepTempFiles) {
       System.out.println("Deleting temporary files...");
       FileUtils.deleteDirectory(workDir);
       FileUtils.deleteDirectory(subjobsDir);
     }
   }
 }