Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package com.accenture.biesse.csvreader;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.Map;
  8.  
  9. import org.eclipse.kura.configuration.ConfigurableComponent;
  10. import org.osgi.service.component.ComponentContext;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13.  
  14. import org.json.*;
  15.  
  16. public class CsvreaderService implements ConfigurableComponent {
  17.    
  18.     private static final Logger logger = LoggerFactory.getLogger(CsvreaderService.class);
  19.    
  20.     private static final String CSV_SEPARATOR = ",";
  21.  
  22.  
  23.     /**
  24.      * This method activates the services of the bundle.
  25.      *
  26.      * @author m.di.luca
  27.      * @param context
  28.      *            the OSGi context
  29.      */
  30.     protected void activate(ComponentContext context, Map<String, Object> properties) {
  31.                
  32.         logger.info("Bundle activated");
  33.        
  34.     }
  35.  
  36.     /**
  37.      * This method stops the services of the bundle.
  38.      *
  39.      * @author m.di.luca
  40.      * @param context
  41.      *            the OSGi context
  42.      */
  43.     protected void deactivate(ComponentContext context) {
  44.        
  45.         logger.info("Bundle deactivated");
  46.        
  47.     }
  48.  
  49.     /**
  50.      * This method updates the bundle properties.
  51.      *
  52.      * @author m.di.luca
  53.      * @param properties
  54.      *            the properties of the bundle
  55.      */
  56.     public void update(Map<String, Object> properties) {
  57.        
  58.         logger.info("Bundle updated");
  59.        
  60.         // Declaration
  61.         BufferedReader br = null;
  62.         FileReader fr = null;
  63.         String filePath = "";
  64.         String exitFile = "";
  65.        
  66.         filePath = properties.get(Constants.PATH_FILE).toString();
  67.        
  68.         try {
  69.            
  70.             fr = new FileReader(filePath);
  71.             br = new BufferedReader(fr);
  72.            
  73.             String[] fileLine, headerLine;
  74.             String currentLine;
  75.             JSONObject exitJson = new JSONObject();
  76.            
  77.             headerLine = br.readLine().split(CSV_SEPARATOR);
  78.            
  79.             while ((currentLine = br.readLine()) != null) {
  80.                
  81.                 fileLine = currentLine.split(CSV_SEPARATOR);
  82.                
  83.                 for (int i = 0; i < headerLine.length; i++ ) {
  84.                    
  85.                     exitJson.put(headerLine[i], fileLine[i]);
  86.                    
  87.                 }
  88.                
  89.             }
  90.            
  91.             exitFile = exitJson.toString();
  92.            
  93.         } catch (FileNotFoundException e) {
  94.            
  95.             // Execute Log
  96.             e.printStackTrace();
  97.             logger.error(e.toString());
  98.            
  99.         } catch (IOException e) {
  100.            
  101.             // Execute Log
  102.             e.printStackTrace();
  103.             logger.error(e.toString());
  104.            
  105.         } catch (JSONException e) {
  106.            
  107.             // Execute Log
  108.             e.printStackTrace();
  109.             logger.error(e.toString());
  110.            
  111.         }
  112.        
  113.         logger.info(exitFile);
  114.        
  115.     }
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement