Advertisement
Guest User

factory

a guest
Sep 15th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. package it.hash.osgi.iot.producer.core.config;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Dictionary;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.apache.felix.dm.Component;
  10. import org.apache.felix.dm.DependencyManager;
  11. import org.osgi.framework.FrameworkUtil;
  12. import org.osgi.service.cm.ConfigurationException;
  13. import org.osgi.service.cm.ManagedServiceFactory;
  14. import org.osgi.service.component.annotations.ConfigurationPolicy;
  15. import org.osgi.service.component.annotations.Reference;
  16.  
  17. import it.hash.osgi.iot.producer.core.camel.CamelService;
  18. import it.hash.osgi.iot.producer.core.camel_processor.CSVProcessor;
  19. import it.hash.osgi.iot.producer.core.camel_processor.Constants;
  20. import it.hash.osgi.iot.producer.core.camel_processor.Processor;
  21. import it.hash.osgi.iot.producer.converter.TypeConverter;
  22. import it.hash.osgi.iot.producer.core.utils.Config;
  23. import it.hash.osgi.iot.producer.core.utils.Utils;
  24. import it.hash.osgi.iot.producer.events.publisher.EventPublisher;
  25.  
  26. @org.osgi.service.component.annotations.Component(name = "factory", property = "service.pid=it.sensorsystem.core.config.factory", configurationPolicy = ConfigurationPolicy.IGNORE)
  27. public class ConfigReaderFactory implements ManagedServiceFactory {
  28. private static final String DELETE_CONDITIONS = "readLock=changed&idempotent=false&noop=true&delete=true";
  29. private static final String NON_DELETE_CONDITIONS = "noop=true";
  30.  
  31. private volatile DependencyManager dependencyManager;
  32. private final Map<String, Component> components = new HashMap<>();
  33. private String url;
  34. private String name;
  35. private int confLine;
  36. private String endpointType;
  37. private String ip;
  38. private String username;
  39. private String password;
  40. private String folder;
  41. private boolean delete;
  42. private CamelService camel;
  43. private TypeConverter converter;
  44. private EventPublisher publisher;
  45. private Double latitude;
  46. private Double longitude;
  47.  
  48. @Reference(service = TypeConverter.class)
  49. public void setTypeConverter(TypeConverter converter) {
  50. this.converter = converter;
  51. }
  52.  
  53. public void unsetTypeConverter(TypeConverter converter) {
  54. this.converter = null;
  55. }
  56.  
  57. @Reference(service = EventPublisher.class)
  58. public void setEventPublisher(EventPublisher publisher) {
  59. this.publisher = publisher;
  60. }
  61.  
  62. public void unsetEventPublisher(EventPublisher publisher) {
  63. this.publisher = null;
  64. }
  65.  
  66. @Reference(service = CamelService.class)
  67. public void setCamelService(CamelService camel) {
  68. this.camel = camel;
  69. }
  70.  
  71. public void unsetCamelService(CamelService camel) {
  72. this.camel = null;
  73. }
  74.  
  75. @Override
  76. public String getName() {
  77. return this.getClass().getName();
  78. }
  79.  
  80. @Override
  81. public void updated(String pid, @SuppressWarnings("rawtypes") Dictionary props) throws ConfigurationException {
  82. if (components.containsKey(pid)) {
  83. return;
  84. }
  85. if (props != null) {
  86. List<String> attributes = new ArrayList<>();
  87. List<String> csvTypes = new ArrayList<>();
  88.  
  89. int count = 1;
  90. String configurationLine;
  91. while ((configurationLine = (String) props.get(Integer.toString(count++))) != null) {
  92. List<String> values = Utils.getValuesFromLine(configurationLine);
  93. attributes.add(values.size() >= 1 ? values.get(0) : TypeConverter.NAMELESS);
  94. csvTypes.add(values.size() >= 2 ? values.get(1) : TypeConverter.NAMELESS);
  95. }
  96. confLine = Integer.parseInt((String) props.get(Config.CONFIG_LINE));
  97. name = (String) props.get(Config.NAME);
  98.  
  99. initConfigParameters(props);
  100. buildURL();
  101. System.out.println("[URL] " + url);
  102.  
  103. try {
  104. Map<String, Object> params = new HashMap<>();
  105. params.put(Constants.ATTRIBUTES, attributes);
  106. params.put(Constants.TYPES, csvTypes);
  107. params.put(Constants.CONF_LINE, confLine);
  108. params.put(Constants.SOURCE, name);
  109. params.put(Constants.PUBLISHER, publisher);
  110. params.put(Constants.CONVERTER, converter);
  111. params.put(Constants.LATITUDE, latitude);
  112. params.put(Constants.LONGITUDE, longitude);
  113. params.put(Constants.ENDPOINT_TYPE, endpointType);
  114. Processor processor = new CSVProcessor(params);
  115. camel.start(FrameworkUtil.getBundle(this.getClass()).getBundleContext(), processor, url);
  116. } catch (Exception e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. }
  121.  
  122. @Override
  123. public void deleted(String pid) {
  124. Component component = components.remove(pid);
  125. dependencyManager.remove(component);
  126. component.stop();
  127. }
  128.  
  129. private void buildURL() {
  130. url = "";
  131. switch(endpointType) {
  132. case Constants.FTP:
  133. url += "ftp://" + username + "@" + ip + "/" + folder + "?";
  134. if(!password.equals("")) {
  135. url += "password=" + password + "&";
  136. }
  137. break;
  138. case Constants.FILE:
  139. url += "file://" + folder + "?";
  140. break;
  141. case Constants.MAIL:
  142. url += "imaps://imap.gmail.com?username="+username+"&password="+password
  143. +"&delete=false&unseen=true&consumer.delay=100";
  144. }
  145. if(endpointType.equals(Constants.FTP) || endpointType.equals(Constants.FILE)) {
  146. if (delete) {
  147. url += DELETE_CONDITIONS;
  148. } else {
  149. url += NON_DELETE_CONDITIONS;
  150. }
  151. }
  152.  
  153. }
  154.  
  155. private void initConfigParameters(@SuppressWarnings("rawtypes") Dictionary props) {
  156. confLine = Integer.parseInt((String) props.get(Config.CONFIG_LINE));
  157. name = (String) props.get(Config.NAME);
  158. endpointType = (String) props.get(Config.ENDPOINT_TYPE);
  159. ip = (String) props.get(Config.IP_ADDRESS);
  160. username = (String) props.get(Config.USERNAME);
  161. password = (String) props.get(Config.PASSWORD);
  162. folder = (String) props.get(Config.FOLDER);
  163. delete = ((String) props.get(Config.DELETE)).equals("true");
  164. if((String) props.get(Config.LATITUDE)!=null) {
  165. latitude = Double.parseDouble((String) props.get(Config.LATITUDE));
  166. }
  167. if((String) props.get(Config.LONGITUDE)!=null) {
  168. longitude = Double.parseDouble((String) props.get(Config.LONGITUDE));
  169. }
  170.  
  171. printParameters();
  172. }
  173.  
  174. private void printParameters() {
  175. System.out.println("\nStarting camel with parameters:");
  176. System.out.println("[sensor_name]::" + name);
  177. System.out.println("[latitude]::" + latitude);
  178. System.out.println("[longitude]::" + longitude);
  179. System.out.println("[endpoint_type]::" + endpointType);
  180. System.out.println("[ip_address]::" + ip);
  181. System.out.println("[user]::" + username);
  182. System.out.println("[password]::" + password);
  183. System.out.println("[folder]::" + folder);
  184. System.out.println("[delete]::" + delete);
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement