All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.webpieces.plugins.properties.beans.ApplyDatabaseProperties Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package org.webpieces.plugins.properties.beans;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.webpieces.router.api.extensions.SimpleStorage;

class ApplyDatabaseProperties implements Runnable {
	
	private static final Logger log = LoggerFactory.getLogger(BeanMetaData.class);

	private SimpleStorage storage;
	private Map> meta;
	private PropertyInvoker propertyInvoker;

	public ApplyDatabaseProperties(Map> meta, SimpleStorage storage, PropertyInvoker propertyInvoker) {
		this.meta = meta;
		this.storage = storage;
		this.propertyInvoker = propertyInvoker;
	}

	@Override
	public void run() {
		try {
			log.info("Starting to apply properties from database to beans.  read Database first");

			CompletableFuture> dbRead = storage.read(KeyUtil.PLUGIN_PROPERTIES_KEY);

			Map dbProps = dbRead.get();

			for(Map.Entry> entry : meta.entrySet()) {
				String category = entry.getKey();
				processBean(dbProps, category, entry.getValue());
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private void processBean(Map dbProps, String category, Map beanMap) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		for(Map.Entry entry : beanMap.entrySet()) {
			String beanName = entry.getKey();
			BeanMeta beanMeta = entry.getValue();
			applyChangesToBean(dbProps, category, beanName, beanMeta);
		}
	}

	private void applyChangesToBean(Map dbProps, String category, String beanName, BeanMeta beanMeta) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		List props = beanMeta.getProperties();
		for(PropertyInfo prop : props) {
			String key = KeyUtil.formKey(category, beanName, prop.getName());
			String datbasePropValue = dbProps.get(key);
			if(datbasePropValue == null) {
				//as of yet, no one has saved to database, so skip it
				continue;
			}
			
			String existingPropValue = propertyInvoker.readPropertyAsString(prop);
			if(!datbasePropValue.equals(existingPropValue)) {
				try {
					propertyInvoker.writeProperty(prop, datbasePropValue);
					log.info("Property="+key+" changed from="+existingPropValue+" to new="+datbasePropValue+" and we updated the bean");
				} catch(Exception e) {
					log.info("Property="+key+" failed to update on bean="+prop.getInjectee().getClass(), e);
				}
			}
			
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy