Advertisement
Jnk1296

IP-Check New DB Log Method

May 25th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.87 KB | None | 0 0
  1. @Override
  2.     public void log(String player, String ip) {
  3.         // Generate the File Path for the player log
  4.         File playerFile = new File("plugins/IP-Check/DATABASE/PLAYERS/" + player + ".log");
  5.        
  6.         // Convert IP from XXX.XXX.XXX.XXX to XXX_XXX_XXX_XXX
  7.         String convertedIP = convertIPFormat(ip);
  8.        
  9.         // Generate the File Path for the ip log
  10.         File ipFile = new File("plugins/IP-Check/DATABASE/IPS/" + convertedIP + ".log");
  11.        
  12.         //### IP-LOG SECTOR ###//
  13.         if (!ipFile.exists()) {
  14.            
  15.             // FILE WRITER
  16.             FileWriter f = null;
  17.        
  18.             try {
  19.                 f = new FileWriter(ipFile, true);
  20.                 f.write(player + "\r\n"); //Store the player name passed to the log file.
  21.                
  22.             } catch (Exception e) {
  23.                 ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  24.                 EL.execute(e);
  25.                 logger.severe(FLAT_FILE_GEN_ERR);
  26.                
  27.             } finally {
  28.                 try {
  29.                     if (f != null) {
  30.                         f.close(); // Close the file writer
  31.                     }
  32.                 } catch (Exception e){
  33.                     ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  34.                     EL.execute(e);
  35.                     logger.severe(e.getMessage());
  36.                 }
  37.             }
  38.             // END FILE WRITER
  39.            
  40.         // If the file does exist, read it into memory
  41.         } else {
  42.             ArrayList<String> list = new ArrayList<String>();
  43.             BufferedReader br = null;
  44.             boolean shouldWrite = true;
  45.  
  46.             // FILE READER
  47.             try {
  48.                 // Open a file stream to read the file
  49.                 FileInputStream fstream = new FileInputStream(ipFile);
  50.                 DataInputStream in = new DataInputStream(fstream);
  51.                 br = new BufferedReader(new InputStreamReader(in));
  52.                 String strLine;
  53.  
  54.                 while ((strLine = br.readLine()) != null) {
  55.                     list.add(strLine.toLowerCase()); // Add each line of the file to memory
  56.                 }
  57.  
  58.             } catch (Exception e) {
  59.                 ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  60.                 EL.execute(e);
  61.                 logger.severe(FLAT_FILE_READ_ERR);
  62.                 list = null; // If we encountered an error reading the file, set the memory location to NULL so that we can handle the error.
  63.  
  64.             } finally {
  65.                 try {
  66.                     if (br != null) {
  67.                         br.close();
  68.                     }
  69.                 } catch (Exception e) {
  70.                     ErrorLogger EL = new ErrorLogger(); // Catch the error, pass to handler
  71.                     EL.execute(e);
  72.                     logger.severe(e.getMessage());
  73.                 }
  74.             }
  75.             // END FILE READER
  76.            
  77.             for (String s:list) {
  78.                 if (s.equals(player)) {
  79.                     shouldWrite = false; // If the file already exists and the IP is already logged, do not write the log file
  80.                 }
  81.             }
  82.            
  83.             // If we did not find the IP
  84.             if (shouldWrite) {
  85.                 list.add(ip); // Add the IP to the file's memory block
  86.  
  87.                 // Write the log file to disk
  88.  
  89.                 // FILE WRITER
  90.                 FileWriter f = null;
  91.  
  92.                 try {
  93.                     if (playerFile.exists()) playerFile.delete(); // Delete the old log file
  94.  
  95.                     f = new FileWriter(playerFile, true);
  96.  
  97.                     for (String s:list) {
  98.                         f.write(s + "\r\n"); //Write each line in memory to the file, plus a line break and carriage return.
  99.                     }
  100.  
  101.                 } catch (Exception e) {
  102.                     ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  103.                     EL.execute(e);
  104.                     logger.severe(FLAT_FILE_WRITE_ERR);
  105.  
  106.                 } finally {
  107.                     try {
  108.                         if (f != null) {
  109.                             f.close(); // Close the file writer
  110.                         }
  111.                     } catch (Exception e){
  112.                         ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  113.                         EL.execute(e);
  114.                         logger.severe(e.getMessage());
  115.                     }
  116.                 }
  117.                 // END FILE WRITER
  118.             }
  119.         }
  120.        
  121.         //### PLAYER-LOG SECTOR ###/
  122.         // If the player log path does not exist, generate it
  123.         if (!playerFile.exists()) {
  124.            
  125.             // FILE WRITER
  126.             FileWriter f = null;
  127.        
  128.             try {
  129.                 f = new FileWriter(playerFile, true);
  130.                 f.write(ip + "\r\n"); //Store the IP-Address passed to the log file.
  131.                
  132.             } catch (Exception e) {
  133.                 ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  134.                 EL.execute(e);
  135.                 logger.severe(FLAT_FILE_GEN_ERR);
  136.                
  137.             } finally {
  138.                 try {
  139.                     if (f != null) {
  140.                         f.close(); // Close the file writer
  141.                     }
  142.                 } catch (Exception e){
  143.                     ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  144.                     EL.execute(e);
  145.                     logger.severe(e.getMessage());
  146.                 }
  147.             }
  148.             // END FILE WRITER
  149.            
  150.         // If the file does exist, read it into memory
  151.         } else {
  152.             ArrayList<String> list = new ArrayList<String>();
  153.             BufferedReader br = null;
  154.  
  155.             // FILE READER
  156.             try {
  157.                 // Open a file stream to read the file
  158.                 FileInputStream fstream = new FileInputStream(playerFile);
  159.                 DataInputStream in = new DataInputStream(fstream);
  160.                 br = new BufferedReader(new InputStreamReader(in));
  161.                 String strLine;
  162.  
  163.                 while ((strLine = br.readLine()) != null) {
  164.                     list.add(strLine.toLowerCase()); // Add each line of the file to memory
  165.                 }
  166.  
  167.             } catch (Exception e) {
  168.                 ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  169.                 EL.execute(e);
  170.                 logger.severe(FLAT_FILE_READ_ERR);
  171.                 list = null; // If we encountered an error reading the file, set the memory location to NULL so that we can handle the error.
  172.  
  173.             } finally {
  174.                 try {
  175.                     if (br != null) {
  176.                         br.close();
  177.                     }
  178.                 } catch (Exception e) {
  179.                     ErrorLogger EL = new ErrorLogger(); // Catch the error, pass to handler
  180.                     EL.execute(e);
  181.                     logger.severe(e.getMessage());
  182.                 }
  183.             }
  184.             // END FILE READER
  185.            
  186.             for (String s:list) {
  187.                 if (s.equals(ip)) {
  188.                     return; // If the file already exists and the IP is already logged, we're done.
  189.                 }
  190.             }
  191.            
  192.             // If we did not find the IP
  193.             list.add(ip); // Add the IP to the file's memory block
  194.            
  195.             // Write the log file to disk
  196.            
  197.             // FILE WRITER
  198.             FileWriter f = null;
  199.        
  200.             try {
  201.                 if (playerFile.exists()) playerFile.delete(); // Delete the old log file
  202.                
  203.                 f = new FileWriter(playerFile, true);
  204.                
  205.                 for (String s:list) {
  206.                     f.write(s + "\r\n"); //Write each line in memory to the file, plus a line break and carriage return.
  207.                 }
  208.                
  209.             } catch (Exception e) {
  210.                 ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  211.                 EL.execute(e);
  212.                 logger.severe(FLAT_FILE_WRITE_ERR);
  213.                
  214.             } finally {
  215.                 try {
  216.                     if (f != null) {
  217.                         f.close(); // Close the file writer
  218.                     }
  219.                 } catch (Exception e){
  220.                     ErrorLogger EL = new ErrorLogger(); // Catch the exception and pass it to the error logger
  221.                     EL.execute(e);
  222.                     logger.severe(e.getMessage());
  223.                 }
  224.             }
  225.             // END FILE WRITER
  226.         }
  227.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement