Advertisement
Guest User

ConfigReaderFactory

a guest
Sep 13th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. package it.sensorsystem.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.sensorsystem.core.camel.CamelService;
  18. import it.sensorsystem.core.camel_processor.CSVProcessor;
  19. import it.sensorsystem.core.camel_processor.Constants;
  20. import it.sensorsystem.core.camel_processor.Processor;
  21. import it.sensorsystem.core.converter.TypeConverter;
  22. import it.sensorsystem.core.utils.Config;
  23. import it.sensorsystem.core.utils.Utils;
  24. import it.sensorsystem.events.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 = "noop=true&delete=true";
  29. //private static final String DELETE_CONDITIONS = "readLock=changed&idempotent=false&noop=true";
  30. private static final String NON_DELETE_CONDITIONS = "noop=true";
  31.  
  32. private volatile DependencyManager dependencyManager;
  33. private final Map<String, Component> components = new HashMap<>();
  34. private String url;
  35. private String name;
  36. private int confLine;
  37. private String endpointType;
  38. private String ip;
  39. private String username;
  40. private String password;
  41. private String folder;
  42. private boolean delete;
  43. private CamelService camel;
  44. private TypeConverter converter;
  45. private EventPublisher publisher;
  46.  
  47. @Reference(service=TypeConverter.class)
  48. public void setTypeConverter(TypeConverter converter) {
  49. this.converter = converter;
  50. }
  51. public void unsetTypeConverter(TypeConverter converter) {
  52. this.converter = null;
  53. }
  54.  
  55. @Reference(service=EventPublisher.class)
  56. public void setEventPublisher(EventPublisher publisher) {
  57. this.publisher = publisher;
  58. }
  59. public void unsetEventPublisher(EventPublisher publisher) {
  60. this.publisher = null;
  61. }
  62. @Reference(service=CamelService.class)
  63. public void setCamelService(CamelService camel) {
  64. this.camel=camel;
  65. }
  66. public void unsetCamelService(CamelService camel) {
  67. this.camel=null;
  68. }
  69.  
  70. @Override
  71. public String getName() {
  72. return this.getClass().getName();
  73. }
  74.  
  75. @Override
  76. public void updated(String pid, @SuppressWarnings("rawtypes") Dictionary props) throws ConfigurationException {
  77. if(components.containsKey(pid)) {
  78. return;
  79. }
  80. if(props!=null){
  81. List<String> attributes = new ArrayList<>();
  82. List<String> csvTypes = new ArrayList<>();
  83.  
  84. int count=1;
  85. String configurationLine;
  86. while((configurationLine = (String) props.get(Integer.toString(count++))) !=null){
  87. List<String> values = Utils.getValuesFromLine(configurationLine);
  88. attributes.add(values.size()>=1 ? values.get(0) : TypeConverter.NAMELESS);
  89. csvTypes.add(values.size()>=2 ? values.get(1) : TypeConverter.NAMELESS);
  90. }
  91. confLine = Integer.parseInt((String) props.get(Config.CONFIG_LINE));
  92. name = (String) props.get(Config.NAME);
  93.  
  94. initConfigParameters(props);
  95. buildURL();
  96. System.out.println("[URL] "+url);
  97.  
  98. try {
  99. Map<String, Object> params = new HashMap<>();
  100. params.put(Constants.ATTRIBUTES, attributes);
  101. params.put(Constants.TYPES, csvTypes);
  102. params.put(Constants.CONF_LINE, confLine);
  103. params.put(Constants.NAME, name);
  104. params.put(Constants.PUBLISHER, publisher);
  105. params.put(Constants.CONVERTER, converter);
  106. Processor processor = new CSVProcessor(params);
  107. camel.start(FrameworkUtil.getBundle(this.getClass()).getBundleContext(), processor, url);
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. }
  111. }
  112. }
  113.  
  114. @Override
  115. public void deleted(String pid) {
  116. Component component = components.remove(pid);
  117. dependencyManager.remove(component);
  118. component.stop();
  119. }
  120. private void buildURL() {
  121. url = "";
  122. if(endpointType.equals("ftp")) {
  123. url += "ftp";
  124. } else if(endpointType.equals("file")) {
  125. url += "file";
  126. }
  127. url += "://"+username+"@"+ip+"/"+folder+"?";
  128. if(!password.equals("")) {
  129. url+="password="+password+"&";
  130. }
  131. if(delete) {
  132. url += DELETE_CONDITIONS;
  133. } else {
  134. url += NON_DELETE_CONDITIONS;
  135. }
  136. }
  137. private void initConfigParameters(@SuppressWarnings("rawtypes") Dictionary props) {
  138. confLine = Integer.parseInt((String) props.get(Config.CONFIG_LINE));
  139. name = (String) props.get(Config.NAME);
  140. endpointType = (String) props.get(Config.ENDPOINT_TYPE);
  141. ip = (String) props.get(Config.IP_ADDRESS);
  142. username = (String) props.get(Config.USERNAME);
  143. password = (String) props.get(Config.PASSWORD);
  144. folder = (String) props.get(Config.FOLDER);
  145. delete = ((String) props.get(Config.DELETE)).equals("true");
  146. printParameters();
  147. }
  148. private void printParameters() {
  149. System.out.println("\nStarting camel with parameters:");
  150. System.out.println("[sensor_name]::"+name);
  151. System.out.println("[endpoint_type]::"+endpointType);
  152. System.out.println("[ip_address]::"+ip);
  153. System.out.println("[user]::"+username);
  154. System.out.println("[password]::"+password);
  155. System.out.println("[folder]::"+folder);
  156. System.out.println("[delete]::"+delete);
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement