Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.sensorsystem.core.config;
- import java.util.ArrayList;
- import java.util.Dictionary;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.felix.dm.Component;
- import org.apache.felix.dm.DependencyManager;
- import org.osgi.framework.FrameworkUtil;
- import org.osgi.service.cm.ConfigurationException;
- import org.osgi.service.cm.ManagedServiceFactory;
- import org.osgi.service.component.annotations.ConfigurationPolicy;
- import org.osgi.service.component.annotations.Reference;
- import it.sensorsystem.core.camel.CamelService;
- import it.sensorsystem.core.camel_processor.CSVProcessor;
- import it.sensorsystem.core.camel_processor.Constants;
- import it.sensorsystem.core.camel_processor.Processor;
- import it.sensorsystem.core.converter.TypeConverter;
- import it.sensorsystem.core.utils.Config;
- import it.sensorsystem.core.utils.Utils;
- import it.sensorsystem.events.EventPublisher;
- @org.osgi.service.component.annotations.Component(name="factory", property="service.pid=it.sensorsystem.core.config.factory", configurationPolicy=ConfigurationPolicy.IGNORE)
- public class ConfigReaderFactory implements ManagedServiceFactory{
- private static final String DELETE_CONDITIONS = "noop=true&delete=true";
- //private static final String DELETE_CONDITIONS = "readLock=changed&idempotent=false&noop=true";
- private static final String NON_DELETE_CONDITIONS = "noop=true";
- private volatile DependencyManager dependencyManager;
- private final Map<String, Component> components = new HashMap<>();
- private String url;
- private String name;
- private int confLine;
- private String endpointType;
- private String ip;
- private String username;
- private String password;
- private String folder;
- private boolean delete;
- private CamelService camel;
- private TypeConverter converter;
- private EventPublisher publisher;
- @Reference(service=TypeConverter.class)
- public void setTypeConverter(TypeConverter converter) {
- this.converter = converter;
- }
- public void unsetTypeConverter(TypeConverter converter) {
- this.converter = null;
- }
- @Reference(service=EventPublisher.class)
- public void setEventPublisher(EventPublisher publisher) {
- this.publisher = publisher;
- }
- public void unsetEventPublisher(EventPublisher publisher) {
- this.publisher = null;
- }
- @Reference(service=CamelService.class)
- public void setCamelService(CamelService camel) {
- this.camel=camel;
- }
- public void unsetCamelService(CamelService camel) {
- this.camel=null;
- }
- @Override
- public String getName() {
- return this.getClass().getName();
- }
- @Override
- public void updated(String pid, @SuppressWarnings("rawtypes") Dictionary props) throws ConfigurationException {
- if(components.containsKey(pid)) {
- return;
- }
- if(props!=null){
- List<String> attributes = new ArrayList<>();
- List<String> csvTypes = new ArrayList<>();
- int count=1;
- String configurationLine;
- while((configurationLine = (String) props.get(Integer.toString(count++))) !=null){
- List<String> values = Utils.getValuesFromLine(configurationLine);
- attributes.add(values.size()>=1 ? values.get(0) : TypeConverter.NAMELESS);
- csvTypes.add(values.size()>=2 ? values.get(1) : TypeConverter.NAMELESS);
- }
- confLine = Integer.parseInt((String) props.get(Config.CONFIG_LINE));
- name = (String) props.get(Config.NAME);
- initConfigParameters(props);
- buildURL();
- System.out.println("[URL] "+url);
- try {
- Map<String, Object> params = new HashMap<>();
- params.put(Constants.ATTRIBUTES, attributes);
- params.put(Constants.TYPES, csvTypes);
- params.put(Constants.CONF_LINE, confLine);
- params.put(Constants.NAME, name);
- params.put(Constants.PUBLISHER, publisher);
- params.put(Constants.CONVERTER, converter);
- Processor processor = new CSVProcessor(params);
- camel.start(FrameworkUtil.getBundle(this.getClass()).getBundleContext(), processor, url);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void deleted(String pid) {
- Component component = components.remove(pid);
- dependencyManager.remove(component);
- component.stop();
- }
- private void buildURL() {
- url = "";
- if(endpointType.equals("ftp")) {
- url += "ftp";
- } else if(endpointType.equals("file")) {
- url += "file";
- }
- url += "://"+username+"@"+ip+"/"+folder+"?";
- if(!password.equals("")) {
- url+="password="+password+"&";
- }
- if(delete) {
- url += DELETE_CONDITIONS;
- } else {
- url += NON_DELETE_CONDITIONS;
- }
- }
- private void initConfigParameters(@SuppressWarnings("rawtypes") Dictionary props) {
- confLine = Integer.parseInt((String) props.get(Config.CONFIG_LINE));
- name = (String) props.get(Config.NAME);
- endpointType = (String) props.get(Config.ENDPOINT_TYPE);
- ip = (String) props.get(Config.IP_ADDRESS);
- username = (String) props.get(Config.USERNAME);
- password = (String) props.get(Config.PASSWORD);
- folder = (String) props.get(Config.FOLDER);
- delete = ((String) props.get(Config.DELETE)).equals("true");
- printParameters();
- }
- private void printParameters() {
- System.out.println("\nStarting camel with parameters:");
- System.out.println("[sensor_name]::"+name);
- System.out.println("[endpoint_type]::"+endpointType);
- System.out.println("[ip_address]::"+ip);
- System.out.println("[user]::"+username);
- System.out.println("[password]::"+password);
- System.out.println("[folder]::"+folder);
- System.out.println("[delete]::"+delete);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement