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

prompto.security.FileKeyStoreFactory Maven / Gradle / Ivy

The newest version!
package prompto.security;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;

import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.document.YamlMapping;

import prompto.config.IConfigurationReader;
import prompto.config.IKeyStoreFactoryConfiguration;
import prompto.utils.Logger;

public class FileKeyStoreFactory implements IKeyStoreFactory {

	static final Logger logger = new Logger();

	@Override
	public IKeyStoreFactoryConfiguration newConfiguration(IConfigurationReader reader) {
		return new Configuration(reader);
	}

	@Override
	public KeyStore newInstance(IKeyStoreFactoryConfiguration config) {
		String filePath = ((Configuration)config).getFile();
		if(filePath==null)
			return null;
		File file = new File(filePath);
		logger.info(()->"Loading certificate store: " + file.getAbsolutePath());
		try(InputStream input = new FileInputStream(filePath)) {
			KeyStore ks = KeyStore.getInstance("JKS");
			ks.load(input, null);
			return ks;
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}

	static class Configuration implements IKeyStoreFactoryConfiguration {

		IConfigurationReader reader;
		
		public Configuration(IConfigurationReader reader) {
			this.reader = reader;
		}

		public String getFile() {
			return reader.getString("file");
		}

		@Override
		public IKeyStoreFactory getKeyStoreFactory() {
			return new FileKeyStoreFactory();
		}
		
		@Override
		public YamlMapping toYaml() throws YamlException {
			YamlMapping yaml = new YamlMapping();
			yaml.setEntry("factory", FileKeyStoreFactory.class.getName());
			yaml.setEntry("file", getFile());
			return yaml;
		}
		
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy