Advertisement
Guest User

Untitled

a guest
Dec 17th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.DBMS.view;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.nio.charset.Charset;
  8. import java.nio.file.Files;
  9. import java.nio.file.attribute.PosixFilePermission;
  10. import java.util.Arrays;
  11. import java.util.HashSet;
  12. import java.util.List;
  13. import java.util.Properties;
  14. import java.util.Set;
  15.  
  16. import eg.edu.alexu.csd.oop.DBMS.app.AppLogger;
  17. import eg.edu.alexu.csd.oop.DBMS.controller.CLIController;
  18. import eg.edu.alexu.csd.oop.DBMS.util.App;
  19. import eg.edu.alexu.csd.oop.DBMS.util.ErrorCode;
  20.  
  21. public class CLI {
  22.  
  23.     private BufferedReader bufferedReader;
  24.     private CLIController cliController;
  25.     private String feedback;
  26.     private long start;
  27.     private String table;
  28.     private Properties info;
  29.  
  30.     private File configFile;
  31.  
  32.     public CLI(CLIController cliController) {
  33.         this.cliController = cliController;
  34.         this.bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  35.         this.feedback = "";
  36.         this.table = null;
  37.         this.info = new Properties();
  38.         this.info.setProperty("path", App.DEFAULT_DIR_PATH);
  39.         this.createAppPath();
  40.     }
  41.  
  42.     public void close() {
  43.         try {
  44.             this.bufferedReader.close();
  45.         } catch (IOException e) {
  46.             e.printStackTrace();
  47.             AppLogger.getInstance().error(ErrorCode.CLI_CLOSE + " " + e.getMessage());
  48.         }
  49.     }
  50.  
  51.     private void createAppPath() {
  52.         File workspace = new File(App.DEFAULT_DIR_PATH);
  53.         if (!workspace.exists()) {
  54.             if (!workspace.mkdir()) {
  55.                 AppLogger.getInstance().fatal("Failed to create DBMS Directory");
  56.                 throw new RuntimeException("Failed to create DBMS Directory");
  57.             }
  58.         }
  59.     }
  60.  
  61.     public boolean credentialsExists() {
  62.         String configDir = info.getProperty("path");
  63.         this.configFile = new File(configDir + File.separator + ".cred.conf");
  64.         return configFile.exists();
  65.     }
  66.  
  67.     public Properties getInfo() {
  68.         return info;
  69.     }
  70.  
  71.     public String getPassword() {
  72.         System.out.print("Enter password: ");
  73. //         char[] pass = System.console().readPassword();
  74. //         return new String(pass);
  75.          try {
  76.          return this.bufferedReader.readLine();
  77.          } catch (IOException e) {
  78.          return null;
  79.          }
  80.     }
  81.  
  82.     public String getURL() {
  83.         System.out.print("Enter Driver's URL: ");
  84.         try {
  85.             return this.bufferedReader.readLine();
  86.         } catch (IOException e) {
  87.             e.printStackTrace();
  88.             return null;
  89.         }
  90.  
  91.     }
  92.  
  93.     public void newCredentials(String username, String password) throws IOException {
  94.         String configDir = info.getProperty("path");
  95.         List<String> lines = Arrays.asList("username:" + username, "password:" + password);
  96.         File file = new File(configDir + File.separator + ".cred.conf");
  97.         file.createNewFile();
  98.         Files.write(file.toPath(), lines, Charset.forName("UTF-8"));
  99.         Set<PosixFilePermission> perms = new HashSet<>();
  100.         perms.add(PosixFilePermission.OWNER_READ);
  101.         perms.add(PosixFilePermission.GROUP_READ);
  102.         Files.setPosixFilePermissions(file.toPath(), perms);
  103.     }
  104.  
  105.     public String newPassword() {
  106.         System.out.print("Enter a New password: ");
  107. //         char[] pass = System.console().readPassword();
  108. //         return new String(pass);
  109.          try {
  110.          return this.bufferedReader.readLine();
  111.          } catch (IOException e) {
  112.          return null;
  113.          }
  114.     }
  115.  
  116.     public void newPrompt() {
  117.         try {
  118.             while (true) {
  119.                 this.print();
  120.             }
  121.         } catch (IOException e) {
  122.             e.printStackTrace();
  123.             AppLogger.getInstance().error(ErrorCode.CLI_READLINE + " " + e.getMessage());
  124.         }
  125.     }
  126.  
  127.     public void out(String s) {
  128.         System.out.println(s);
  129.     }
  130.  
  131.     private void print() throws IOException {
  132.         if (App.checkForExistence(this.feedback)) {
  133.             String log = this.feedback + " (" + (System.currentTimeMillis() - this.start) + "ms)";
  134.             System.out.println(log);
  135.            
  136.         }
  137.         if (App.checkForExistence(this.table))
  138.             System.out.println(this.table);
  139.         this.table = null;
  140.         System.out.print(App.PS1);
  141.         this.scan();
  142.     }
  143.  
  144.     public void run() {
  145.         this.newPrompt();
  146.     }
  147.  
  148.     private void scan() throws IOException {
  149.         String temp = this.bufferedReader.readLine();
  150.         AppLogger.getInstance().info("NEW REQUEST: <" + temp + ">");
  151.         this.start = System.currentTimeMillis();
  152.         if (App.checkForExistence(temp) && temp.trim().equals("exit")) {
  153.             this.cliController.end();
  154.         } else
  155.             this.feedback = this.cliController.newInput(temp);
  156.     }
  157.  
  158.     public void setTable(String table) {
  159.         this.table = table;
  160.     }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement