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

net.sf.gluebooster.java.booster.basic.container.ConfigurationBoostUtils Maven / Gradle / Ivy

package net.sf.gluebooster.java.booster.basic.container;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Map;

import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;
import net.sf.gluebooster.java.booster.essentials.utils.Constants;
import net.sf.gluebooster.java.booster.essentials.utils.ContainerBoostUtils;
import net.sf.gluebooster.java.booster.essentials.utils.IoBoostUtils;
import net.sf.gluebooster.java.booster.essentials.utils.ThrowableBoostUtils;
import net.sf.gluebooster.java.booster.essentials.utils.XmlBoostUtils;

/**
 * Utilitites for configurations.
 * 
 * @author cbauer
 *
 */
public class ConfigurationBoostUtils extends CallableAbstraction {

	/**
	 * Saves a configuration.
	 * 
	 * @param entries
	 *            a subset of the entries will be saved
	 * @param keys
	 *            the keys of the entries that are to be saved
	 * @param output
	 *            the configuration will be written into the output
	 * @param closeAfterSaving
	 *            is the output to be closed after saving.
	 */
	public static void saveConfiguration(Map entries, Collection keys, OutputStream output, boolean closeAfterSaving) throws Exception {

		Map configuration = ContainerBoostUtils.createSubmap(entries, keys, false);

		output.write("\n".getBytes(IoBoostUtils.UTF_8));
		XmlBoostUtils.writeAsUtf8Xml(configuration, output, false, 0);
		// XMLEncoder encoder = new XMLEncoder(output);
		// encoder.writeObject(configuration);
		// if (closeAfterSaving) {
		// encoder.close();
		// }

		//
		// XMLConfiguration result = new XMLConfiguration();
		// for (Object key : keys) {
		// String name;
		// if (key instanceof HasName) {
		// name = ((HasName) key).getName().toString();
		// } else {
		// name = key.toString();
		// }
		// Object value = entries.get(key);
		// value = "noch nicht gesetzt";
		// result.setProperty(name, value);
		// }
		//
		// return result;

	}

	/**
	 * Saves a configuration.
	 * 
	 * @param entries
	 *            all the entries will be saved
	 * @param output
	 *            the configuration will be written into the output
	 * @param closeAfterSaving
	 *            is the output to be closed after saving.
	 */
	public static void saveConfiguration(Map entries, OutputStream output, boolean closeAfterSaving) throws Exception {
		saveConfiguration(entries, entries.keySet(), output, closeAfterSaving);
	}

	/**
	 * Version of the load and save must be considered if the file format does change.
	 * 
	 * @param input
	 *            contains the previously saved data
	 * @param closeAfterReading
	 *            is the input to be closed after the reading
	 * @return the created configuration (map).
	 */
	public static  Result loadConfiguration(InputStream input, boolean closeAfterReading) throws Exception {

		return (Result) XmlBoostUtils.readFromUtf8Xml(input);
		// close after reading done automatically
		// XMLDecoder decoder = new XMLDecoder(input);
		// Object result = decoder.readObject();
		// if (closeAfterReading) {
		// decoder.close();
		// }
		// return result;
	}

	@Override
	protected Object callImpl(Object... parameters) throws Exception {
		Object command = parameters[0];
		if (Constants.SAVE.equals(command)){
			Map entries = (Map) parameters[1];
			Collection keys = (Collection) parameters[2];
			File output = (File) parameters[3];
			if (output == null) {
				return null;
			} else {
				FileOutputStream outputStream = new FileOutputStream(output);
				saveConfiguration(entries, keys, outputStream, true);
				return output;
			}
		} else {
			throw ThrowableBoostUtils.createNotImplementedException("command not supported: ", command);
		}
			
	}
}