/** Tests the result of a rest api GET for flow classifier id. */
  @Test
  public void testGetFlowClassifierId() {

    final Set<FlowClassifier> flowClassifiers = new HashSet<>();
    flowClassifiers.add(flowClassifier1);

    expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
    expect(flowClassifierService.getFlowClassifier(anyObject()))
        .andReturn(flowClassifier1)
        .anyTimes();
    replay(flowClassifierService);

    final WebResource rs = resource();
    final String response =
        rs.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051").get(String.class);
    final JsonObject result = JsonObject.readFrom(response);
    assertThat(result, notNullValue());
  }
  private String getEtag(URI uri) throws IOException {
    Response resp = adminExecutor.execute(Request.Get(uri));

    HttpResponse httpResp = resp.returnResponse();
    assertNotNull(httpResp);
    HttpEntity entity = httpResp.getEntity();
    assertNotNull(entity);
    StatusLine statusLine = httpResp.getStatusLine();
    assertNotNull(statusLine);

    assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode());
    assertNotNull("content type not null", entity.getContentType());
    assertEquals(
        "check content type",
        Representation.HAL_JSON_MEDIA_TYPE,
        entity.getContentType().getValue());

    String content = EntityUtils.toString(entity);

    assertNotNull("", content);

    JsonObject json = null;

    try {
      json = JsonObject.readFrom(content);
    } catch (Throwable t) {
      fail("parsing received json");
    }

    assertNotNull("check not null json", json);

    assertNotNull("check not null _etag", json.get("_etag"));
    assertTrue("check _etag is object", json.get("_etag").isObject());

    assertNotNull("check not null _etag.$oid", json.get("_etag").asObject().get("$oid"));

    assertNotNull(
        "check _etag.$oid is string", json.get("_etag").asObject().get("$oid").isString());

    return json.get("_etag").asObject().get("$oid").asString();
  }
Example #3
0
  @Override
  public void generateBuildScript(Configuration cfg, Context ctx) {
    try {
      final InputStream input =
          this.getClass().getClassLoader().getResourceAsStream("javascript/lib/package.json");
      final List<String> packLines = IOUtils.readLines(input);
      String pack = "";
      for (String line : packLines) {
        pack += line + "\n";
      }
      input.close();
      pack = pack.replace("<NAME>", cfg.getName());

      final JsonObject json = JsonObject.readFrom(pack);
      final JsonValue deps = json.get("dependencies");
      for (Thing t : cfg.allThings()) {
        for (String dep : t.annotation("js_dep")) {
          deps.asObject().add(dep.split(":")[0].trim(), dep.split(":")[1].trim());
        }
      }

      for (Thing t : cfg.allThings()) {
        if (t.getStreams().size() > 0) {
          deps.asObject().add("rx", "^2.5.3");
          deps.asObject().add("events", "^1.0.2");
          break;
        }
      }

      final File f = new File(ctx.getOutputDirectory() + "/" + cfg.getName() + "/package.json");
      f.setWritable(true);
      final PrintWriter w = new PrintWriter(new FileWriter(f));
      w.println(json.toString());
      w.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private void _testGetAggregation(String uri) throws Exception {
    Response resp;

    URI aggrUri =
        buildURI(
            "/"
                + dbTmpName
                + "/"
                + collectionTmpName
                + "/"
                + RequestContext._AGGREGATIONS
                + "/"
                + uri);

    resp = adminExecutor.execute(Request.Get(aggrUri));

    HttpResponse httpResp = resp.returnResponse();
    assertNotNull(httpResp);
    HttpEntity entity = httpResp.getEntity();
    assertNotNull(entity);
    StatusLine statusLine = httpResp.getStatusLine();
    assertNotNull(statusLine);

    assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode());
    assertNotNull("content type not null", entity.getContentType());
    assertEquals(
        "check content type",
        Representation.HAL_JSON_MEDIA_TYPE,
        entity.getContentType().getValue());

    String content = EntityUtils.toString(entity);

    assertNotNull("", content);

    JsonObject json = null;

    try {
      json = JsonObject.readFrom(content);
    } catch (Throwable t) {
      fail("parsing received json");
    }

    assertNotNull("check not null json response", json);
    assertNotNull("check not null _embedded", json.get("_embedded"));
    assertTrue("check _embedded", json.get("_embedded").isObject());

    assertNotNull("", json.get("_embedded").asObject().get("rh:result"));

    assertTrue(
        "check _embedded[\"rh:result\"]",
        json.get("_embedded").asObject().get("rh:result").isArray());

    JsonArray results = json.get("_embedded").asObject().get("rh:result").asArray();

    assertTrue("check we have 2 results", results.size() == 2);

    results
        .values()
        .stream()
        .map(
            (v) -> {
              assertNotNull("check not null _id property", v.asObject().get("_id"));
              return v;
            })
        .map(
            (v) -> {
              assertTrue(
                  "check results _id property is string", v.asObject().get("_id").isString());
              return v;
            })
        .map(
            (v) -> {
              assertNotNull("check not null value property", v.asObject().get("value"));
              return v;
            })
        .forEach(
            (v) -> {
              assertTrue(
                  "check results value property is number", v.asObject().get("value").isNumber());
            });
  }
  @Test
  public void testGetMapReduceWithVariable() throws Exception {
    String uri = "avg_ages";

    String aggregationsMetadata =
        "{\"aggrs\": ["
            + "{"
            + "\"type\":\"mapReduce\""
            + ","
            + "\"uri\": \""
            + uri
            + "\","
            + "\"map\": \"function() { var minage = JSON.parse($vars).minage; if (this.age > minage ) { emit(this.name, this.age); }; }\","
            + "\"reduce\":\"function(key, values) { return Array.avg(values); }\""
            + ","
            + "\"query\":{\"name\":{\"_$var\":\"name\"}}"
            + "}]}";

    createTmpCollection();
    createMetadataAndTestData(aggregationsMetadata);

    Response resp;

    URI aggrUri =
        buildURI(
            "/"
                + dbTmpName
                + "/"
                + collectionTmpName
                + "/"
                + RequestContext._AGGREGATIONS
                + "/"
                + uri,
            new NameValuePair[] {
              new BasicNameValuePair("avars", "{\"name\": \"a\", \"minage\": 20}")
            });

    resp = adminExecutor.execute(Request.Get(aggrUri));

    HttpResponse httpResp = resp.returnResponse();
    assertNotNull(httpResp);
    HttpEntity entity = httpResp.getEntity();
    assertNotNull(entity);
    StatusLine statusLine = httpResp.getStatusLine();
    assertNotNull(statusLine);

    assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode());
    assertNotNull("content type not null", entity.getContentType());
    assertEquals(
        "check content type",
        Representation.HAL_JSON_MEDIA_TYPE,
        entity.getContentType().getValue());

    String content = EntityUtils.toString(entity);

    assertNotNull("", content);

    JsonObject json = null;

    try {
      json = JsonObject.readFrom(content);
    } catch (Throwable t) {
      fail("parsing received json");
    }

    assertNotNull("check not null json response", json);
    assertNotNull("check not null _embedded", json.get("_embedded"));
    assertTrue("check _embedded", json.get("_embedded").isObject());

    assertNotNull("", json.get("_embedded").asObject().get("rh:result"));
    assertTrue(
        "check _embedded[\"rh:results\"]",
        json.get("_embedded").asObject().get("rh:result").isArray());

    JsonArray results = json.get("_embedded").asObject().get("rh:result").asArray();

    assertTrue("check we have 2 results", results.size() == 1);

    results
        .values()
        .stream()
        .map(
            (v) -> {
              assertNotNull("check not null _id property", v.asObject().get("_id"));
              return v;
            })
        .map(
            (v) -> {
              assertTrue(
                  "check results _id property is string", v.asObject().get("_id").isString());
              return v;
            })
        .map(
            (v) -> {
              assertTrue(
                  "check results _id property is a",
                  v.asObject().get("_id").asString().equals("a"));
              return v;
            })
        .map(
            (v) -> {
              assertNotNull("check not null value property", v.asObject().get("value"));
              return v;
            })
        .forEach(
            (v) -> {
              assertTrue(
                  "check results value property is number", v.asObject().get("value").isNumber());
            });
  }
Example #6
0
  public static Set<Mod> load(
      final Path path,
      final int order,
      final SettingsModelFactory settingsFactory,
      final DatabaseModelFactory databaseFactory,
      final LocalizerModelFactory localizerFactory) {

    log.debug("Loading mod: {}", path);

    Set<Mod> mods = new HashSet<>();

    // TODO Count all mods and send back progress info

    Set<Archive> archives = null;

    try {
      archives = processModFile(path, settingsFactory);
    } catch (IOException | StarDBException | ParseException e) {
      // TODO Error Dialogue
      log.error("", e);
      return new HashSet<Mod>();
    }

    for (Archive archive : archives) {

      Mod mod = new Mod(localizerFactory, settingsFactory);

      mod.setOrder(order);
      mod.files = new HashSet<>();
      mod.setArchiveName(archive.getFileName());

      // Get the modinfo file and parse it
      JsonObject obj = JsonObject.readFrom(new String(archive.getFile(".modinfo").getData()));

      mod.setInternalName(obj.get("name").asString());

      if (obj.get("version") != null) {
        mod.setGameVersion(obj.get("version").asString());
      } else {
        mod.setGameVersion("Field Empty");
      }

      Set<String> dependencies = new HashSet<>();
      Set<String> ignoredFileNames = new HashSet<>();

      if (obj.get("dependencies") != null) {
        JsonArray arr = obj.get("dependencies").asArray();
        for (int i = 0; i < arr.size(); i++) {
          dependencies.add(arr.get(i).asString());
        }
      }

      mod.setDependencies(dependencies);

      if (obj.get("metadata") != null) {

        JsonObject metadata = obj.get("metadata").asObject();

        mod.setDisplayName(JSONHelper.getString(metadata, "displayname", mod.getInternalName()));
        mod.setAuthor(JSONHelper.getString(metadata, "author", NO_AUTHOR));
        mod.setDescription(JSONHelper.getString(metadata, "description", NO_DESCRIPTION));
        mod.setURL(JSONHelper.getString(metadata, "support_url", ""));
        mod.setModVersion(JSONHelper.getString(metadata, "version", NO_VERSION));

        if (obj.get("ignoredfiles") != null) {
          JsonArray arr = obj.get("ignoredfiles").asArray();
          for (int i = 0; i < arr.size(); i++) {
            ignoredFileNames.add(arr.get(i).asString());
          }
        }

      } else {

        mod.setDisplayName(mod.getInternalName());
        mod.setAuthor(NO_AUTHOR);
        mod.setDescription(NO_DESCRIPTION);
        mod.setURL("");
        mod.setModVersion(NO_VERSION);
      }

      try {
        mod.setChecksum(
            FileHelper.getChecksum(
                new File(
                        settingsFactory.getInstance().getPropertyString("modsdir")
                            + File.separator
                            + mod.archiveName)
                    .toPath())); // TODO Better Path manipulation
      } catch (IOException e) {
        log.error("Setting Checksum", e);
      }

      for (ArchiveFile archiveFile : archive.getFiles()) {

        ModFile modFile = new ModFile();
        modFile.setPath(archiveFile.getPath());

        // Find and list all ignored files
        for (String ignored : ignoredFileNames) {
          if (archiveFile.getPath().endsWith(ignored) || archiveFile.getPath().endsWith(".txt")) {
            modFile.setIgnored(true);
          }
        }

        // Scan all json files and find those with mergeability
        if (!archiveFile.isFolder() && FileHelper.isJSON(archiveFile.getPath())) {

          modFile.setJson(true);

          String fileContents = new String(archiveFile.getData());

          if (fileContents.contains("__merge")) {
            modFile.setAutoMerged(true);
          }
        }

        if (!archiveFile.isFolder()) {
          mod.files.add(modFile);
        }
      }

      try {
        databaseFactory.getInstance().updateMod(mod);
      } catch (SQLException e) {
        log.error("", e);
        MessageDialogue dialogue =
            new MessageDialogue(
                localizer.getMessage("mod.dbconnectionerror"),
                localizer.getMessage("mod.dbconnectionerror.title"),
                MessageType.ERROR,
                new LocalizerFactory());
        dialogue.getResult();
        return new HashSet<Mod>();
      }

      mods.add(mod);
    }

    return mods;
  }
Example #7
0
  private static void processArchive(
      final Path path, final Set<Archive> output, final SettingsModelFactory settingsFactory)
      throws IOException, StarDBException {

    Archive originalArchive = new Archive(path);

    if (!originalArchive.extract()) {
      throw new IOException("Could not extract archive.");
    }

    Set<ArchiveFile> usedPaks = new HashSet<>();

    for (ArchiveFile file : originalArchive.getFiles()) {

      if (FileHelper.getExtension(file.getPath()).equals("modinfo")) {

        JsonObject o = JsonObject.readFrom(new String(file.getData()));

        String preformattedPath = o.get("path").asString().replaceAll("\"", "");

        if (preformattedPath.startsWith("./")) {
          preformattedPath = preformattedPath.replace("./", "");
        }

        if (preformattedPath.startsWith(".")) {
          preformattedPath = preformattedPath.replace(".", "");
        }

        if (preformattedPath.startsWith("/")) {
          preformattedPath = preformattedPath.replace("/", "");
        }

        Path modinfoPath = file.getPath();

        if (modinfoPath.getNameCount() > 2) {
          modinfoPath = modinfoPath.subpath(0, modinfoPath.getNameCount() - 1);
        } else if (modinfoPath.getNameCount() > 1) {
          modinfoPath = modinfoPath.getName(0);
        } else {
          modinfoPath = Paths.get("");
        }

        Archive outputArchive =
            new Archive(
                settingsFactory
                    .getInstance()
                    .getPropertyPath("modsdir")
                    .resolve(Paths.get(o.get("name").asString() + ".zip")));

        if (file.getPath().getNameCount() == 1) {
          outputArchive.addFile(new ArchiveFile(file.getData(), file.getPath(), false));
          log.debug(
              "'{}' -> '{}' relativized to '{}'", modinfoPath, file.getPath(), file.getPath());
        } else {
          outputArchive.addFile(
              new ArchiveFile(
                  file.getData(), modinfoPath.relativize(file.getPath()).normalize(), false));
          log.debug(
              "'{}' -> '{}' relativized to '{}'",
              modinfoPath,
              file.getPath(),
              modinfoPath.relativize(file.getPath()).normalize());
        }

        Path assetsPath = modinfoPath.resolve(Paths.get(preformattedPath));

        log.debug("Assets path for modinfo '{}': {}", file.getPath(), assetsPath);

        if (originalArchive.getFile(assetsPath) != null
            && !assetsPath.toString().isEmpty()
            && !originalArchive.getFile(assetsPath).isFolder()) {

          if (FileHelper.identifyType(originalArchive.getFile(assetsPath).getData())
              .equals("pak")) {

            log.debug("Assets for mod '{}' identified as .pak file: {}", modinfoPath, assetsPath);

            usedPaks.add(originalArchive.getFile(assetsPath));

            ArchiveFile modinfo = outputArchive.getFile(".modinfo");
            JsonObject o2 = JsonObject.readFrom(new String(modinfo.getData()));
            o2.set("path", "assets");

            modinfo.setData(o2.toString().getBytes());

            // TODO Update StarDB to let me pass in a byte array instead of needing to open a file

            Path tempPath = Paths.get("tempPak" + System.nanoTime());

            SeekableByteChannel tempPakFile =
                Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
            tempPakFile.write(ByteBuffer.wrap(originalArchive.getFile(assetsPath).getData()));
            tempPakFile.close();

            AssetDatabase database = AssetDatabase.open(tempPath);

            for (String assetFile : database.getFileList()) {
              outputArchive.addFile(
                  new ArchiveFile(
                      database.getAsset(assetFile), Paths.get("assets/" + assetFile), false));
              log.trace("Asset extracted: {} | {}", assetFile, Paths.get("assets/" + assetFile));
            }

            Files.deleteIfExists(tempPath);
          }

        } else {

          ArchiveFile modinfo = outputArchive.getFile(".modinfo");
          JsonObject o2 = JsonObject.readFrom(new String(modinfo.getData()));
          o2.set("path", "assets");

          modinfo.setData(o2.toString().getBytes());

          log.debug("Assets for mod '{}' is a standard assets folder: {}", modinfoPath, assetsPath);

          for (ArchiveFile f2 : originalArchive.getFiles()) {

            if ((assetsPath.toString().isEmpty() || f2.getPath().startsWith(assetsPath))
                && !f2.isFolder()
                && !f2.getPath().toString().endsWith(".modinfo")) {

              if (modinfoPath.toString().isEmpty()) {

                if (f2.getPath().getNameCount() == 1) {

                  if (!f2.getPath().startsWith(assetsPath)) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(),
                            Paths.get("assets/").resolve(f2.getPath()).normalize(),
                            false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/").resolve(f2.getPath()).normalize());
                  } else {
                    outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        f2.getPath());
                  }

                } else {

                  if (!f2.getPath().startsWith(assetsPath)) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(),
                            Paths.get("assets/").resolve(f2.getPath()).normalize(),
                            false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/").resolve(f2.getPath()).normalize());
                  } else {
                    outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        f2.getPath());
                  }
                }

              } else {

                if (f2.getPath().getNameCount() == 1) {

                  if (!modinfoPath.relativize(f2.getPath()).startsWith("assets")) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(), Paths.get("assets/").resolve(f2.getPath()), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/").resolve(f2.getPath()));
                  } else {
                    outputArchive.addFile(new ArchiveFile(f2.getData(), f2.getPath(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        f2.getPath());
                  }

                } else {

                  if (!modinfoPath.relativize(f2.getPath()).startsWith("assets")) {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(),
                            Paths.get("assets/")
                                .resolve(modinfoPath.relativize(f2.getPath()).normalize()),
                            false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        Paths.get("assets/")
                            .resolve(modinfoPath.relativize(f2.getPath()).normalize()));
                  } else {
                    outputArchive.addFile(
                        new ArchiveFile(
                            f2.getData(), modinfoPath.relativize(f2.getPath()).normalize(), false));
                    log.trace(
                        "'{}' -> '{}' relativized to '{}'",
                        modinfoPath,
                        f2.getPath(),
                        modinfoPath.relativize(f2.getPath()).normalize());
                  }
                }
              }
            }
          }
        }

        outputArchive.writeToFile(
            settingsFactory
                .getInstance()
                .getPropertyPath("modsdir")
                .resolve(Paths.get(o.get("name").asString() + ".zip"))
                .toFile()); // TODO

        output.add(outputArchive);
      }
    }

    for (ArchiveFile file : originalArchive.getFiles()) {

      if (!usedPaks.contains(file)
          && !file.isFolder()
          && FileHelper.identifyType(file.getData()).equals("pak")) {

        log.debug("Additional .pak identified: {}", file.getPath());

        Path tempPath = Paths.get("tempPak" + System.nanoTime());

        SeekableByteChannel tempPakFile =
            Files.newByteChannel(tempPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        tempPakFile.write(ByteBuffer.wrap(file.getData()));
        tempPakFile.close();

        AssetDatabase database = AssetDatabase.open(tempPath);
        log.debug(database.getFileList());

        if (database.getAsset("/pak.modinfo") != null) {
          log.debug("{} has a .modinfo file, parsing into mod.", file.getPath());
          processPakFile(tempPath, output, settingsFactory);
        } else {
          log.debug("{} does not contain a .modinfo file, skipping.", file.getPath());
        }

        Files.deleteIfExists(tempPath);
      }
    }
  }
Example #8
0
  private static void processPakFile(
      final Path path, final Set<Archive> output, final SettingsModelFactory settingsFactory)
      throws IOException, StarDBException {

    SettingsModelInterface settings = settingsFactory.getInstance();

    AssetDatabase database = AssetDatabase.open(path);
    log.debug(database.getFileList());

    byte[] modinfoFile = database.getAsset("/pak.modinfo");
    String[] modinfoContents = new String(modinfoFile).split("\n");

    String modName = "";
    String assetsPath = "";

    // TODO Use actual JSON parser
    for (String line : modinfoContents) {
      if (line.contains("\"name\"")) {
        modName = line.trim().split(":")[1];
        modName = modName.substring(modName.indexOf("\"") + 1, modName.lastIndexOf("\""));
      }
      if (line.contains("\"path\"")) {
        assetsPath = line.trim().split(":")[1];
        assetsPath =
            assetsPath.substring(assetsPath.indexOf("\"") + 1, assetsPath.lastIndexOf("\""));
      }
    }

    if (assetsPath.startsWith(".")) {
      assetsPath = assetsPath.replace(".", "");
    }

    if (assetsPath.startsWith("/")) {
      assetsPath = assetsPath.replace("/", "");
    }

    if (assetsPath.startsWith("./")) {
      assetsPath = assetsPath.replace("./", "");
    }

    Archive modArchive = new Archive(settings.getPropertyPath("modsdir").resolve(modName + ".zip"));

    modArchive.addFile(new ArchiveFile(modinfoFile, Paths.get(modName + ".modinfo"), false));

    ArchiveFile modinfo = modArchive.getFile(".modinfo");
    JsonObject o2 = JsonObject.readFrom(new String(modinfo.getData()));
    o2.set("path", "assets");

    modinfo.setData(o2.toString().getBytes());

    assetsPath = "/" + assetsPath + "/";
    log.debug("Assets path is '{}'", assetsPath);

    for (String file : database.getFileList()) {

      log.trace("{} is in {}", file, path);

      if (file.endsWith(".modinfo") || file.endsWith("desktop.ini") || file.endsWith("thumbs.db")) {
        continue;
      }

      if (assetsPath.isEmpty() || !file.startsWith(assetsPath)) {
        modArchive.addFile(
            new ArchiveFile(
                database.getAsset(file), Paths.get("assets/" + file.substring(1)), false));
      } else {
        log.debug("{} --- {}", assetsPath, file.substring(assetsPath.length()));
        modArchive.addFile(
            new ArchiveFile(
                database.getAsset(file),
                Paths.get("assets/" + file.substring(assetsPath.length())),
                false));
      }
    }

    Files.deleteIfExists(
        settings
            .getPropertyPath("modsdir")
            .resolve(path.subpath(path.getNameCount() - 1, path.getNameCount())));

    modArchive.writeToFile();

    output.add(modArchive);
  }
Example #9
0
  // main program
  public static void main(String[] args) throws Exception {
    String parfile = "";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    ResultSetMetaData rsmd = null;
    int n = 0;
    long oldTime, curTime, sumTime, avgTime = 0;
    int rows = 0;
    try {
      parfile = args[0];
    } catch (Exception exc) {
      System.out.println("Please use SpoolIt [configuration file]");
      System.exit(1);
    }

    try {
      System.out.println("------------------------------------------------------------");
      System.out.println("spoolIt Version " + _APP_VERSION);
      System.out.println("------------------------------------------------------------");
      System.out.println("Report bugs to : [email protected]");
      // record start time
      oldTime = System.currentTimeMillis();

      // read parameter file
      try {
        /*
         * Example of configuration
         *
        	{
        	"DATABASE": "oracle",
        	"INSTANCE": "your-database-instance-or-sid",
        	"HOST": "your-database-ip-address",
        	"PORT": "1521",
        	"USER": "******",
        	"PASSWORD": "******",
        	"QUERY" : "SELECT * FROM DUAL WHERE ROWNUM <= 3",
        	"OUTFILE": "/your/path/filename.dat",
        	"DELIMITER": "|"
        	}
        *
        */
        BufferedReader in = new BufferedReader(new FileReader(parfile));
        jsonObject = JsonObject.readFrom(in);
        jsonObject.add("CONNECTION_STRING", "");
      } catch (IOException e) {
      }

      System.out.print("\n-------------------- Configuration -------------------------\n");
      System.out.println("Database product     : " + jsonObject.get("DATABASE").asString());
      System.out.println("Database Instance    : " + jsonObject.get("INSTANCE").asString());
      System.out.println("Database Port	     : " + jsonObject.get("PORT").asString());
      System.out.println("Database User ID     : " + jsonObject.get("USER").asString());
      System.out.println("SQL Query for export : " + jsonObject.get("QUERY").asString());

      System.out.println("\n-------------------- Dump File -----------------------------");
      System.out.println("Path : " + jsonObject.get("OUTFILE").asString());
      System.out.println(
          "Type : Variable length separated by delimeter \""
              + jsonObject.get("DELIMITER").asString()
              + "\"");
      System.out.println("Rows Fetch Size : " + FETCHSIZE);

      // load driver
      LoadDriver ld = new LoadDriver();
      ld.loadNow(jsonObject.get("DATABASE").asString());

      // create connection to database
      if (jsonObject.get("DATABASE").asString().equals("mysql")) {
        jsonObject.set(
            "CONNECTION_STRING",
            "jdbc:"
                + jsonObject.get("DATABASE").asString()
                + "://"
                + jsonObject.get("HOST").asString()
                + "/"
                + jsonObject.get("INSTANCE").asString()
                + "?user="******"USER").asString()
                + "&password="******"PASSWORD").asString());
        // System.out.println("Connection string : " +
        // jsonObject.get("CONNECTION_STRING").asString() );

        conn = DriverManager.getConnection(jsonObject.get("CONNECTION_STRING").asString());
      } else if (jsonObject.get("DATABASE").asString().equals("oracle")) {
        /*
         * Connection string using service name
        		jdbc:oracle:thin:@//oracle.hostserver2.mydomain.ca:1522/ABCD
         *
         * Connection string using tnsname
        		jdbc:oracle:thin:@oracle.hostserver2.mydomain.ca:1522:ABCD
         *
        */
        // jsonObject.set("CONNECTION_STRING", "jdbc:oracle:thin:@" +
        // jsonObject.get("HOST").asString() +
        //		":" + jsonObject.get("PORT").asString() + ":" + jsonObject.get("INSTANCE").asString() );
        jsonObject.set(
            "CONNECTION_STRING",
            "jdbc:oracle:thin:@//"
                + jsonObject.get("HOST").asString()
                + ":"
                + jsonObject.get("PORT").asString()
                + "/"
                + jsonObject.get("INSTANCE").asString());

        // System.out.println("Connection string : " +
        // jsonObject.get("CONNECTION_STRING").asString() );
        conn =
            DriverManager.getConnection(
                jsonObject.get("CONNECTION_STRING").asString(),
                jsonObject.get("USER").asString(),
                jsonObject.get("PASSWORD").asString());
      } else if (jsonObject.get("DATABASE").asString().equals("teradata")) {
        conn =
            DriverManager.getConnection(
                "jdbc:teradata://"
                    + jsonObject.get("HOST").asString()
                    + "/DATABASE="
                    + jsonObject.get("INSTANCE").asString()
                    + ",TMODE=ANSI,CHARSET=UTF8,TYPE=FASTEXPORT",
                jsonObject.get("USER").asString(),
                jsonObject.get("PASSWORD").asString());
      } else if (jsonObject.get("DATABASE").asString().equals("impala")) {
        // jdbc:hive2://myhost.example.com:21050/test_db;auth=noSasl
        conn =
            DriverManager.getConnection(
                "jdbc:hive2://"
                    + jsonObject.get("HOST").asString()
                    + ":"
                    + jsonObject.get("PORT").asString()
                    + "/"
                    + jsonObject.get("INSTANCE").asString()
                    + ";auth=noSasl");
      } else if (jsonObject.get("DATABASE").asString().equals("hive")) {
        // jdbc:hive2://localhost:10000/default
        conn =
            DriverManager.getConnection(
                "jdbc:hive2://"
                    + jsonObject.get("HOST").asString()
                    + ":"
                    + jsonObject.get("PORT").asString()
                    + "/"
                    + jsonObject.get("INSTANCE").asString());
      } else if (jsonObject.get("DATABASE").asString().equals("db2")) {
        // String url = "jdbc:db2://myhost:5021/mydb:" + "user=dbadm;password=dbadm;"
        conn =
            DriverManager.getConnection(
                "jdbc:db2://"
                    + jsonObject.get("HOST").asString()
                    + ":"
                    + jsonObject.get("PORT").asString()
                    + "/"
                    + jsonObject.get("INSTANCE").asString()
                    + ":user="******"USER").asString()
                    + ";password="******"PASSWORD").asString()
                    + ";");
      }

      // Implementation
      try {
        PreparedStatement ps = conn.prepareStatement(jsonObject.get("QUERY").asString());
        // stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        // stmt.setFetchSize(FETCHSIZE);
        System.out.print("\nExecuting query...");
        // change fetch size on result set
        // rs.setFetchSize(FETCHSIZE);
        // rs.setFetchDirection(ResultSet.FETCH_FORWARD);
        // rs = stmt.executeQuery(vQuery);
        rs = ps.executeQuery();
        if (rs != null) {
          System.out.print("Done\n");
          // change fetch size on result set
          // rs = stmt.getResultSet();
          // rs.setFetchSize(FETCHSIZE);
          rsmd = rs.getMetaData();
          n = rsmd.getColumnCount();
          // fetch data and save to file
          BufferedWriter out =
              new BufferedWriter(new FileWriter(jsonObject.get("OUTFILE").asString()));
          String tmpStr = "";
          System.out.print("Exporting data to " + jsonObject.get("OUTFILE").asString() + "\n");
          while (rs.next()) {
            try {
              tmpStr = "";

              for (int index = 1; index <= n; index++) {
                if (index != n)
                  tmpStr = tmpStr + rs.getString(index) + jsonObject.get("DELIMITER").asString();
                else tmpStr = tmpStr + rs.getString(index) + "\n";
              }
              // tmpStr = rs.getString(1) + "\n";
              out.write(tmpStr);
              rows++;
            } catch (IOException e) {
            }
            // sleep milisecond
          }
          // close dump file
          out.close();
          System.out.println("Export succesfully");
        }
      } finally {
        try {
          if (rs != null) {
            rs.close();
          }
          rs = null;
        } catch (SQLException sqlEx) {
        }
      }

      try {
        // release connection
        conn.close();
      } catch (SQLException sqlEx) {
      }
      // summary of time execution
      curTime = System.currentTimeMillis();
      sumTime = (curTime - oldTime);

      System.out.println("\n-------------------- Performance ---------------------------");
      System.out.println("Total rows exported : " + rows + " rows");
      System.out.println("Overall dump speed  : " + sumTime + " miliseconds");
    } catch (SQLException ex) {
      System.out.println("SQLException : " + ex.getMessage());
    }
  }