Guest User

Untitled

a guest
Apr 13th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. import java.io.DataInputStream;
  2.  
  3. import java.io.File;
  4.  
  5. import java.io.FileInputStream;
  6.  
  7. import java.io.FileNotFoundException;
  8.  
  9. import java.io.IOException;
  10.  
  11. import java.io.InputStream;
  12.  
  13. import java.rmi.NotBoundException;
  14.  
  15. import java.rmi.RemoteException;
  16.  
  17. import java.rmi.registry.LocateRegistry;
  18.  
  19. import java.rmi.registry.Registry;
  20.  
  21. import java.util.Scanner;
  22.  
  23.  
  24.  
  25. public class Client {
  26.  
  27.  
  28.  
  29.     private String mancom;
  30.  
  31.     private String taskDir;
  32.  
  33.     private static int PortRMI;
  34.  
  35.     private static String HostRMI;
  36.  
  37.  
  38.  
  39.     private static boolean alreadyLoggedIn = false;
  40.  
  41.  
  42.  
  43.     public Client (String mancom,String taskDir){
  44.  
  45.         this.mancom = mancom;
  46.  
  47.         this.setTaskDir(taskDir);
  48.  
  49.     }
  50.  
  51.  
  52.  
  53.     public static void main(String args[]) throws FileNotFoundException{
  54.  
  55.  
  56.  
  57.  
  58.  
  59.         RemoteLoginInterface loginObject = null;
  60.  
  61.         RemoteGeneralUserInterface user = null;
  62.  
  63.         Registry registry;
  64.  
  65.  
  66.  
  67.         if((args.length != 2) ) {
  68.  
  69.             System.out.println("The arguments that you have entered is missing or invalid");
  70.  
  71.             System.out.println("Usage: <managementComponent> <taskDir>");
  72.  
  73.             System.exit(0);
  74.  
  75.         }
  76.  
  77.  
  78.  
  79.         Client client  = new Client(args[0],args[1]);
  80.  
  81.  
  82.  
  83.         client.readRegistryProperties();
  84.  
  85.  
  86.  
  87.         try {
  88.  
  89.  
  90.  
  91.             registry = LocateRegistry.getRegistry(HostRMI,PortRMI);
  92.  
  93.             loginObject = (RemoteLoginInterface) registry.lookup(client.getManagementComponent());
  94.  
  95.  
  96.  
  97.         } catch (RemoteException e) {
  98.  
  99.             // TODO Auto-generated catch block
  100.  
  101.             e.printStackTrace();
  102.  
  103.         } catch (NotBoundException e) {
  104.  
  105.             // TODO Auto-generated catch block
  106.  
  107.             e.printStackTrace();
  108.  
  109.         }
  110.  
  111.  
  112.  
  113.         Scanner sc = new Scanner(System.in);
  114.  
  115.  
  116.  
  117.         try {
  118.  
  119.  
  120.  
  121.             while(sc.hasNext()){
  122.  
  123.  
  124.  
  125.                 String[] befehl = sc.nextLine().split(" ");
  126.  
  127.  
  128.  
  129.                 if(befehl[0].equals("!login")) {
  130.  
  131.  
  132.  
  133.                     if(alreadyLoggedIn){
  134.  
  135.  
  136.  
  137.                         System.out.println("You are already LoggedIn!");
  138.  
  139.  
  140.  
  141.                     }else{
  142.  
  143.  
  144.  
  145.                         String companyName = befehl[1]; // UVN
  146.  
  147.                         String companyPassword = befehl[2]; // 12345
  148.  
  149.  
  150.  
  151.                         user = loginObject.login(companyName,companyPassword);
  152.  
  153.                         alreadyLoggedIn = true;
  154.  
  155.                        
  156.  
  157.                         System.out.print("Succesfully logged in. ");
  158.  
  159.  
  160.  
  161.                         if(user instanceof RemoteAdminInterface ){
  162.  
  163.                             System.out.println("Using admin mode.");
  164.  
  165.                         }else
  166.  
  167.                             System.out.println("Using company mode.");
  168.  
  169.                     }
  170.  
  171.                 }else if(befehl[0].equals("!logout")){
  172.  
  173.  
  174.  
  175.                     if(!alreadyLoggedIn) {
  176.  
  177.  
  178.  
  179.                         System.out.println("You have to log in first.");
  180.  
  181.  
  182.  
  183.                     } else {
  184.  
  185.  
  186.  
  187.                         user.logout();
  188.  
  189.                         alreadyLoggedIn = false;
  190.  
  191.                         user = null;
  192.  
  193.                         System.out.println("Succesfully logged out.");
  194.  
  195.                     }
  196.  
  197.  
  198.  
  199.                 }else if(befehl[0].equals("!exit")){
  200.  
  201.  
  202.  
  203.                     System.out.println("Exiting..");
  204.  
  205.                     System.exit(0);
  206.  
  207.  
  208.  
  209.                 }else if(befehl[0].equals("!list")){
  210.  
  211.  
  212.  
  213.                     if(!alreadyLoggedIn){
  214.  
  215.  
  216.  
  217.                         System.out.println("You have to log in first.");
  218.  
  219.                    
  220.  
  221.                     }else if(user instanceof RemoteAdminInterface){
  222.  
  223.  
  224.  
  225.                         System.out.println("Command not allowed. You are not a company.");
  226.  
  227.  
  228.  
  229.                     }else{
  230.  
  231.  
  232.  
  233.                         File file = new File(client.getTaskDir());
  234.  
  235.                         File[] listOfFiles = file.listFiles();
  236.  
  237.  
  238.  
  239.                         for (int i = 0; i < listOfFiles.length; i++)
  240.  
  241.                         {
  242.  
  243.                             if (listOfFiles[i].isFile())
  244.  
  245.                             {
  246.  
  247.                                 System.out.println(listOfFiles[i].getName());
  248.  
  249.                             }
  250.  
  251.                         }
  252.  
  253.                     }
  254.  
  255.                 }
  256.  
  257.             }
  258.  
  259.         } catch (RemoteException e) {
  260.  
  261.             // TODO Auto-generated catch block
  262.  
  263.             e.printStackTrace();
  264.  
  265.         } catch (Exception e) {
  266.  
  267.             // TODO Auto-generated catch block
  268.  
  269.             e.printStackTrace();
  270.  
  271.         }
  272.  
  273.  
  274.  
  275.     }
  276.  
  277.  
  278.  
  279.  
  280.  
  281.     private  String getManagementComponent() {
  282.  
  283.  
  284.  
  285.         return mancom;
  286.  
  287.     }
  288.  
  289.  
  290.  
  291.     private void setPortRMI(int PortRMI) {
  292.  
  293.         this.PortRMI = PortRMI;
  294.  
  295.     }
  296.  
  297.  
  298.  
  299.     private void setHostRMI(String HostRMI) {
  300.  
  301.         this.HostRMI = HostRMI;
  302.  
  303.     }
  304.  
  305.  
  306.  
  307.     public void readRegistryProperties() throws FileNotFoundException {
  308.  
  309.  
  310.  
  311.         InputStream is = new DataInputStream(new FileInputStream(new File("registry.properties")));
  312.  
  313.         if (is != null) {
  314.  
  315.             java.util.Properties props = new java.util.Properties();
  316.  
  317.             try {
  318.  
  319.                 props.load(is);
  320.  
  321.                 setPortRMI(Integer.valueOf(props.getProperty("registry.port")));
  322.  
  323.  
  324.  
  325.             } catch (IOException e) {
  326.  
  327.                 try {
  328.  
  329.                     is.close();
  330.  
  331.                     System.exit(0);
  332.  
  333.                 } catch (IOException e1) {
  334.  
  335.                     System.err.println("Error mit inputstream !!");
  336.  
  337.  
  338.  
  339.                 }
  340.  
  341.  
  342.  
  343.             }
  344.  
  345.         }
  346.  
  347.     }
  348.  
  349.  
  350.  
  351.     public String getTaskDir() {
  352.  
  353.         return taskDir;
  354.  
  355.     }
  356.  
  357.  
  358.  
  359.     public void setTaskDir(String taskDir) {
  360.  
  361.         this.taskDir = taskDir;
  362.  
  363.     }
Add Comment
Please, Sign In to add comment