Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.accenture.biesse.csvreader;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Map;
- import org.eclipse.kura.configuration.ConfigurableComponent;
- import org.osgi.service.component.ComponentContext;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.json.*;
- public class CsvreaderService implements ConfigurableComponent {
- private static final Logger logger = LoggerFactory.getLogger(CsvreaderService.class);
- private static final String CSV_SEPARATOR = ",";
- /**
- * This method activates the services of the bundle.
- *
- * @author m.di.luca
- * @param context
- * the OSGi context
- */
- protected void activate(ComponentContext context, Map<String, Object> properties) {
- logger.info("Bundle activated");
- }
- /**
- * This method stops the services of the bundle.
- *
- * @author m.di.luca
- * @param context
- * the OSGi context
- */
- protected void deactivate(ComponentContext context) {
- logger.info("Bundle deactivated");
- }
- /**
- * This method updates the bundle properties.
- *
- * @author m.di.luca
- * @param properties
- * the properties of the bundle
- */
- public void update(Map<String, Object> properties) {
- logger.info("Bundle updated");
- // Declaration
- BufferedReader br = null;
- FileReader fr = null;
- String filePath = "";
- String exitFile = "";
- filePath = properties.get(Constants.PATH_FILE).toString();
- try {
- fr = new FileReader(filePath);
- br = new BufferedReader(fr);
- String[] fileLine, headerLine;
- String currentLine;
- JSONObject exitJson = new JSONObject();
- headerLine = br.readLine().split(CSV_SEPARATOR);
- while ((currentLine = br.readLine()) != null) {
- fileLine = currentLine.split(CSV_SEPARATOR);
- for (int i = 0; i < headerLine.length; i++ ) {
- exitJson.put(headerLine[i], fileLine[i]);
- }
- }
- exitFile = exitJson.toString();
- } catch (FileNotFoundException e) {
- // Execute Log
- e.printStackTrace();
- logger.error(e.toString());
- } catch (IOException e) {
- // Execute Log
- e.printStackTrace();
- logger.error(e.toString());
- } catch (JSONException e) {
- // Execute Log
- e.printStackTrace();
- logger.error(e.toString());
- }
- logger.info(exitFile);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement