Java tutorial
package de.fiz.ddb.aas.util; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourcePatternResolver; /** * @author Michael Hoppe * * Reads properties from aas.properties file and hold them. * */ public final class AasConfiguration { private static AasConfiguration instance; private final Properties properties; private static final Logger LOGGER = LoggerFactory.getLogger(AasConfiguration.class); private static final String PROPERTIES_FILENAME = "aas.properties"; private static final String PROPERTIES_DEFAULT_FILENAME = "aas-default.properties"; public static final String LDAP_BASE_DN = "ldap.base.dn"; public static final String LDAP_USERS_OU = "ldap.users.ou"; public static final String LDAP_ORGANIZATIONS_OU = "ldap.organizations.ou"; public static final String LDAP_LICENSEDORGS_OU = "ldap.licensedOrgs.ou"; public static final String LDAP_PERMISSIONS_OU = "ldap.permissions.ou"; public static final String LDAP_PROVIDER_URL = "ldap.provider.url"; public static final String LDAP_ROOT_DN = "ldap.root.dn"; public static final String LDAP_ROOT_DN_PASSWORD = "ldap.root.dn.password"; public static final String HTTP_PROXY_URL = "proxy.url"; public static final String HTTP_PROXY_PORT = "proxy.port"; public static final String HTTP_NONPROXY_HOSTS = "proxy.nonproxy.hosts"; public static final String MAIL_HOST = "mail.host"; public static final String MAIL_SENDER = "mail.sender"; public static final String HASH_ALGORITHM = "hash.algorithm"; static { try { instance = new AasConfiguration(); } catch (final IOException e) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Problem while loading properties."); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Problem while loading properties.", e); } } } /** * Private Constructor, in order to prevent instantiation of this utility class. read the Properties and fill it in * properties attribute. * @throws de.escidoc.core.common.exceptions.system.SystemException */ private AasConfiguration() throws IOException { this.properties = loadProperties(); } /** * Returns Object. * * @return AasConfiguration self * @throws IOException Thrown if properties loading fails. */ public static AasConfiguration getInstance() { return instance; } /** * Returns the property with the given name or null if property was not found. * * @param name The name of the Property. * @return Value of the given Property as String. */ public String get(final String name) { return (String) properties.get(name); } /** * Returns the property with the given name or the second parameter as default value if property was not found. * * @param name The name of the Property. * @param defaultValue The default vaule if property isn't given. * @return Value of the given Property as String. */ public String get(final String name, final String defaultValue) { String prop = (String) properties.get(name); if (prop == null) { prop = defaultValue; } return prop; } /** * Loads the Properties from properties-file. * * @return The properties * @throws IOException If the loading of the properties fails. */ private static Properties loadProperties() throws IOException { final Properties result = getProperties(PROPERTIES_DEFAULT_FILENAME); final Properties overwrite = getProperties(PROPERTIES_FILENAME); result.putAll(overwrite); return result; } /** * Get the properties from a file. * * @param filename The name of the properties file. * @return The properties. * @throws IOException If access to the specified file fails. * @throws java.io.FileNotFoundException */ private static Properties getProperties(final String filename) throws IOException, FileNotFoundException { final Properties result = new Properties(); final InputStream propertiesStream = getInputStream(filename); result.load(propertiesStream); return result; } /** * Get an InputStream for the given file. * * @param filename The name of the file. * @return The InputStream or null if the file could not be located. * @throws FileNotFoundException If access to the specified file fails. */ private static InputStream getInputStream(final String filename) throws IOException, FileNotFoundException { final ResourcePatternResolver applicationContext = new ClassPathXmlApplicationContext(new String[] {}); final Resource[] resource = applicationContext.getResources("classpath*:" + filename); if (resource.length == 0) { throw new FileNotFoundException("Unable to find file '" + filename + "' in classpath."); } return resource[0].getInputStream(); } }