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

com.carrotgarden.maven.aws.util.Util Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
/**
 * Copyright (C) 2010-2012 Andrei Pozolotin 
 *
 * All rights reserved. Licensed under the OSI BSD License.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */
package com.carrotgarden.maven.aws.util;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;

import org.apache.maven.plugin.logging.Log;
import org.codehaus.jackson.map.ObjectMapper;

public class Util {

	public static void assertNotNull(final Object instance, final String message) {
		if (instance == null) {
			throw new IllegalStateException(message);
	
		}
	}

	public static String concatenate(final List list,
			final String separator) {

		final StringBuilder text = new StringBuilder();

		for (final String item : list) {
			text.append(item);
			text.append(separator);
		}

		return text.toString();

	}

	public static Field findField(final Class klaz, final String fieldName) {

		Class type = klaz;

		do {

			for (final Field field : type.getDeclaredFields()) {

				final String name = field.getName();

				if (!name.equals(fieldName)) {
					continue;
				}

				field.setAccessible(true);

				return field;

			}

			type = type.getSuperclass();

		} while (klaz != null);

		return null;

	}

	public static boolean isValidFolder(final File file) {

		if (file == null) {
			return false;
		}

		if (!file.exists()) {
			return false;
		}

		if (!file.isDirectory()) {
			return false;
		}

		if (!file.canRead()) {
			return false;
		}

		if (!file.canWrite()) {
			return false;
		}

		return true;

	}

	public static  T jsonLoad(final File file, final Class klaz)
			throws Exception {

		final ObjectMapper mapper = new ObjectMapper();

		return mapper.readValue(file, klaz);

	}

	public static void overrideInstanceProps(final Log log,
			final Object instance, final String prefix,
			final Map props) {

		final Set> entrySet = //
		new HashSet>(props.entrySet());

		final Class klaz = instance.getClass();

		for (final Map.Entry entry : entrySet) {

			final String key = entry.getKey();
			final String value = entry.getValue();

			if (!key.startsWith(prefix)) {
				continue;
			}

			props.remove(key);

			try {

				final Field field = findField(klaz, key);

				field.set(instance, value);

				log.info("override : " + key + "=" + value);

			} catch (final Exception e) {

				log.warn("override : invalid stack param=" + key, e);

			}

		}

	}

	public static Properties propsLoad(final Log log, final File file)
			throws Exception {

		final Properties props = new Properties();

		if (file == null || !file.exists()) {
			log.debug("file == null || !file.exists()");
			return props;
		}

		final Reader reader = new FileReader(file);

		props.load(reader);

		reader.close();

		return props;

	}

	public static void propsReport(final Log log, final String title,
			final Properties props) {

		log.info("properties : " + title);

		final Object[] keyArray = props.keySet().toArray();

		Arrays.sort(keyArray);

		for (final Object key : keyArray) {
			log.info("\t" + key + "=" + props.get(key));
		}

	}

	public static void propsSave(final Log log, final Properties props,
			final File file) throws Exception {

		if (props == null || file == null) {
			log.debug("props == null || file == null");
			return;
		}

		final File folder = file.getParentFile();

		if (!folder.exists()) {
			folder.mkdirs();
		}

		final Writer writer = new FileWriter(file);

		props.store(writer, null);

	}

	public static Map safeMap(final Map props) {

		final Map map = new TreeMap();

		if (props == null) {
			return map;
		}

		return props;

	}

	public static Map safeMap(final Properties props) {

		final Map map = new TreeMap();

		if (props == null) {
			return map;
		}

		for (final Map.Entry entry : props.entrySet()) {

			final String key = entry.getKey().toString();
			final String value = entry.getValue().toString();

			map.put(key, value);

		}

		return map;

	}

	public static long safeNumber(final Log log, final String numberText,
			final long numberDefault) {
		try {
			return Long.parseLong(numberText);
		} catch (final Throwable e) {
			log.warn("using numberDefault=" + numberDefault);
			return numberDefault;
		}
	}

	public static String stringNullOrValue(final Object object) {
		return object == null ? null : object.toString();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy