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

com.appslandia.plum.defaults.DefaultResourcesProvider Maven / Gradle / Ivy

// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.plum.defaults;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.servlet.ServletContext;

import com.appslandia.common.base.InitializeException;
import com.appslandia.common.base.Language;
import com.appslandia.common.utils.StringFormatUtils;
import com.appslandia.plum.base.AppConfig;
import com.appslandia.plum.base.LanguageProvider;
import com.appslandia.plum.base.Resources;
import com.appslandia.plum.base.ResourcesProvider;

/**
 *
 * @author Loc Ha
 *
 */
@ApplicationScoped
public class DefaultResourcesProvider extends ResourcesProvider {

	@Inject
	protected AppConfig appConfig;

	@Inject
	protected ServletContext servletContext;

	@Inject
	protected LanguageProvider languageProvider;

	@Override
	protected void init() throws Exception {
		super.init();

		for (Language language : this.languageProvider.getLanguages()) {
			addResources(language.getId(), loadResources(language.getId()));
		}
	}

	protected Resources loadResources(String language) throws InitializeException {
		Properties resources = new Properties();
		String[] resourceNames = this.appConfig.getStringArray(AppConfig.CONFIG_RESOURCE_NAMES);

		for (String resourceName : resourceNames) {
			String resName = StringFormatUtils.format("/WEB-INF/resources/{0}.{1}.properties", resourceName, language);

			InputStream is = this.servletContext.getResourceAsStream(resName);
			if (is != null) {
				loadResources(resources, is);
			}
		}
		return new Resources(language, resources);
	}

	private static void loadResources(Properties resources, InputStream is) throws InitializeException {
		try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
			resources.load(br);

		} catch (IOException ex) {
			throw new InitializeException(ex);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy