risdenk commented on a change in pull request #231: KNOX-2128 - Custom 
DataSource and SQL Commands for KnoxShell and KnoxShellTable
URL: https://github.com/apache/knox/pull/231#discussion_r365970436
 
 

 ##########
 File path: 
gateway-shell/src/main/java/org/apache/knox/gateway/shell/KnoxSession.java
 ##########
 @@ -570,6 +584,121 @@ public String toString() {
     return String.format(Locale.ROOT, "KnoxSession{base='%s'}", base);
   }
 
+  /**
+   * Persist provided Map to a file within the {user.home}/.knoxshell directory
+   * @param <T>
+   * @param fileName of persisted file
+   * @param map to persist
+   */
+  public static <T> void persistToKnoxShell(String fileName, Map<String, 
List<T>> map) {
+    String s = JsonUtils.renderAsJsonString(map);
+    String home = System.getProperty("user.home");
+    synchronized(KnoxSession.class) {
+      try {
+        write(new File(
+            home + File.separator +
+            ".knoxshell" + File.separator + fileName),
+            s, StandardCharsets.UTF_8);
+      } catch (IOException e) {
+        e.printStackTrace();
+      }
+    }
+  }
+
+  private static void write(File file, String s, Charset utf8) throws 
IOException {
+    try (FileChannel channel = FileChannel.open(file.toPath(), 
StandardOpenOption.WRITE,
+        StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
+      channel.tryLock();
+      FileUtils.write(file, s, utf8);
+    }
+    catch (OverlappingFileLockException e) {
+      System.out.println("Unable to acquire write lock for: " + 
file.getAbsolutePath());
+    }
+  }
+
+  public static void persistSQLHistory(Map<String, List<String>> sqlHistories) 
{
+    persistToKnoxShell(KNOXSQLHISTORIES_JSON, sqlHistories);
+  }
+
+  public static void persistDataSources(Map<String, List<KnoxDataSource>> 
datasources) {
+    persistToKnoxShell(KNOXDATASOURCES_JSON, datasources);
+  }
+
+  /**
+   * Load and return a map of datasource names to sql commands
+   * from the {user.home}/.knoxshell/knoxsqlhistories.json file.
+   * @return sqlHistory map
+   */
+  public static Map<String, List<String>> loadSQLHistories() throws 
IOException {
+    Map<String, List<String>> sqlHistories = null;
+    String home = System.getProperty("user.home");
+
+    File historyFile = new File(
+        home + File.separator +
+        ".knoxshell" + File.separator + KNOXSQLHISTORIES_JSON);
+    synchronized(KnoxSession.class) {
+      if (historyFile.exists()) {
+        String json = readFileToString(historyFile, "UTF8");
+        sqlHistories = (Map<String, List<String>>) 
getMapOfStringArrayListsFromJsonString(json);
+      }
+    }
+    return sqlHistories;
+  }
+
+  private static String readFileToString(File file, String s)
+      throws FileNotFoundException, IOException {
+    String content = null;
+
+    try (FileChannel channel = FileChannel.open(file.toPath(), 
StandardOpenOption.READ)) {
+      channel.tryLock(0L, Long.MAX_VALUE, true);
+      content = FileUtils.readFileToString(file, s);
+    }
+    catch (OverlappingFileLockException e) {
+      System.out.println("Unable to acquire write lock for: " + 
file.getAbsolutePath());
+    }
+
+    return content;
+  }
+
+  /**
+   * Load and return a map of datasource names to KnoxDataSource
+   * objects from the {user.home}/.knoxshell/knoxdatasources.json file.
+   * @return
+   */
+  public static Map<String, KnoxDataSource> loadDataSources() throws 
IOException {
+    Map<String, KnoxDataSource> datasources = null;
+    String home = System.getProperty("user.home");
+    String json = null;
+
+    File dsFile = new File(
+        home + File.separator +
+        ".knoxshell" + File.separator + KNOXDATASOURCES_JSON);
+    synchronized(KnoxSession.class) {
+      json = readFileToString(dsFile, "UTF8");
 
 Review comment:
   nit: might make sense to make readFileToString synchronized. to avoid 
calling that method without synchronization. Or at least move the synchronized 
block into the readFileToString method.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to