PageRenderTime 63ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/TelnetServer/src/main/java/com/telnet/core/multithreadmodel/ParseCLICommand.java

https://bitbucket.org/akshayk1987/javaexamples
Java | 100 lines | 71 code | 18 blank | 11 comment | 6 complexity | 6ab6c51e2eca7d3059b909d52e76033f MD5 | raw file
  1. package com.telnet.core.multithreadmodel;
  2. import com.telnet.core.interfaces.OSType;
  3. import com.telnet.core.util.FindOS;
  4. import com.telnet.core.util.CLICommands;
  5. import com.telnet.core.util.TelnetUtil;
  6. import java.io.PrintWriter;
  7. import java.io.InputStream;
  8. import java.util.StringTokenizer;
  9. /**
  10. * Created by IntelliJ IDEA.
  11. * User: nareshk
  12. * Date: Dec 29, 2012
  13. * Time: 8:59:08 PM
  14. * To change this template use File | Settings | File Templates.
  15. */
  16. public class ParseCLICommand {
  17. private String cmd;
  18. private PrintWriter out;
  19. private InputStream is;
  20. public ParseCLICommand(String cmd, PrintWriter out, InputStream is) {
  21. this.cmd = cmd;
  22. this.out = out;
  23. this.is = is;
  24. }
  25. public void parseInputCommand(String CURRENT_DIRECTORY) {
  26. /*if(CURRENT_DIRECTORY == null || CURRENT_DIRECTORY.equals("")) {
  27. CURRENT_DIRECTORY = TelnetUtil.getUserHomeDirectory();
  28. }*/
  29. OSType ostype = FindOS.findOSUnderneath(); //using factory pattern concrete object will be returned based on OS Type
  30. StringTokenizer stk = new StringTokenizer(cmd, " ");
  31. cmd = stk.nextToken();
  32. CLICommands.WindowsCommands wcmd = CLICommands.WindowsCommands
  33. .value(cmd);
  34. if (wcmd != null) {
  35. switch (wcmd) {
  36. case LS:
  37. String lsParams = null;
  38. try{
  39. lsParams = stk.nextToken();
  40. }catch(Exception ex){lsParams = null;}
  41. String list = ostype.listCurrentDirecotryDetails(lsParams, CURRENT_DIRECTORY);
  42. System.out.println("ls -l "+ list);
  43. out.println(list);
  44. break;
  45. case PWD:
  46. //String outputData = ostype.getPresentWorkingDirectory();
  47. out.println(CURRENT_DIRECTORY);
  48. break;
  49. case DIR:
  50. String filesList = ostype.listCurrentDirecotryDetails(cmd, CURRENT_DIRECTORY);
  51. out.println(filesList);
  52. break;
  53. case CD:
  54. String stringAfterCD = null;
  55. try{
  56. stringAfterCD = stk.nextToken();
  57. }catch(Exception ex){stringAfterCD = null;}
  58. String finalPath = ostype.executeCDCommand(stringAfterCD, CURRENT_DIRECTORY);
  59. if(TelnetUtil.validateIfPathExistForDirectory(finalPath)) {
  60. CURRENT_DIRECTORY = finalPath;
  61. }
  62. System.out.println("cd command "+ finalPath);
  63. out.println(finalPath);
  64. break;
  65. case MKDIR:
  66. String stringAfterMKDIR = null;
  67. try{
  68. stringAfterMKDIR = stk.nextToken();
  69. }catch(Exception ex){stringAfterMKDIR = null;}
  70. String outfile = ostype.executeMKDIRCommand(stringAfterMKDIR, CURRENT_DIRECTORY);
  71. if(outfile != null) {
  72. out.println("");
  73. }else{
  74. }
  75. default:
  76. break;
  77. }
  78. }else {
  79. out.print("Unable to find the command");
  80. }
  81. }
  82. }