JAR que faz o load do properties de outras aplicações!

1 resposta
stolakos

Bom dia Pessoal!

Tenho um JAR que carrega um arquivo de properties, ele vai ficar no servidor Websphere em uma shared lib, esse arquivo que ele vai carregar vai ficar dentro da aplicação que vai chamar esse JAR, deu para entender???

Estou tendo um problema na referência desse arquivo, eu sei o caminho dele dentro das aplicações porque já foi definido exemplo (config/application.properties), mas não sei o caminho completo para achar no servidor, quando subo a aplicação e tendo fazer o load do arquivo dá um erro porque não consegue achar o arquivo.

Se alguém puder me ajudar agradeço!

Obrigado

1 Resposta

stolakos

Galera,

Boa tarde!

Consegui fazer o que eu estava querendo…acho importante compartilhar a solução, então, segue abaixo o código da classe:

public abstract class Property{

private static final boolean THROW_ON_LOAD_FAILURE = true;
private static final boolean LOAD_AS_RESOURCE_BUNDLE = false;
private static final String SUFFIX = ".properties";

private static Properties loadProperties(String name, ClassLoader loader) {
	if (name == null)
		throw new IllegalArgumentException("null input: name");

	if (name.startsWith("/"))
		name = name.substring(1);

	if (name.endsWith(SUFFIX))
		name = name.substring(0, name.length() - SUFFIX.length());

	Properties result = null;

	InputStream in = null;
	try {
		if (loader == null)
			loader = ClassLoader.getSystemClassLoader();

		if (LOAD_AS_RESOURCE_BUNDLE) {
			name = name.replace('/', '.');
			// Throws MissingResourceException on lookup failures:
			final ResourceBundle rb = ResourceBundle.getBundle(name, Locale
					.getDefault(), loader);

			result = new Properties();
			for (Enumeration<String> keys = rb.getKeys(); keys.hasMoreElements();) {
				final String key = (String) keys.nextElement();
				final String value = rb.getString(key);

				result.put(key, value);
			}
		} else {
			name = name.replace('.', '/');

			if (!name.endsWith(SUFFIX))
				name = name.concat(SUFFIX);

			in = loader.getResourceAsStream(name);
			if (in != null) {
				result = new Properties();
				result.load(in); // Can throw IOException
			}
		}
	} catch (Exception e) {
		result = null;
	} finally {
		if (in != null)
			try {
				in.close();
			} catch (Throwable ignore) {
			}
	}

	if (THROW_ON_LOAD_FAILURE && (result == null)) {
		throw new IllegalArgumentException("could not load ["
				+ name
				+ "]"
				+ " as "
				+ (LOAD_AS_RESOURCE_BUNDLE ? "a resource bundle"
						: "a classloader resource"));
	}

	return result;
}

public static Properties loadProperties(final String name) {
	return loadProperties(name, Thread.currentThread().getContextClassLoader());
}

Qualquer dúvida é só perguntar!

abs

Criado 30 de agosto de 2012
Ultima resposta 4 de set. de 2012
Respostas 1
Participantes 1