Advertisement
Guest User

Untitled

a guest
Feb 8th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. /*
  2.  * Copyright (c) Orchestral Developments Ltd and the Orion Health group of companies (2001 - 2018).
  3.  *
  4.  * This document is copyright. Except for the purpose of fair reviewing, no part
  5.  * of this publication may be reproduced or transmitted in any form or by any
  6.  * means, electronic or mechanical, including photocopying, recording, or any
  7.  * information storage and retrieval system, without permission in writing from
  8.  * the publisher. Infringers of copyright render themselves liable for
  9.  * prosecution.
  10.  */
  11. package com.orchestral.releasemanagement.compatibilitymanager;
  12.  
  13. import java.io.IOException;
  14. import java.util.Properties;
  15.  
  16. import org.apache.commons.codec.binary.Base64;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19.  
  20. public class Credentials {
  21.     private static final Logger LOGGER = LoggerFactory.getLogger(Credentials.class);
  22.     private static final String LDAP_CONFIG_FILE = "ldap-credentials.properties";
  23.  
  24.     private final String USERNAME;
  25.     private final String PASSWORD;
  26.     private final String BASIC_AUTHENTICATION;
  27.  
  28.     private Credentials() {
  29.         LOGGER.debug("Creating new instance of Credentials");
  30.         Properties properties = null;
  31.         try {
  32.             properties = PropertiesHelper.load(LDAP_CONFIG_FILE);
  33.         } catch (final IOException e) {
  34.             LOGGER.error("Couldn't load credentials properties file: {}", LDAP_CONFIG_FILE);
  35.             e.printStackTrace();
  36.             System.exit(1);
  37.         }
  38.         this.USERNAME = properties.getProperty("username", "");
  39.         this.PASSWORD = properties.getProperty("password", "");
  40.         this.BASIC_AUTHENTICATION = new String(Base64.encodeBase64((this.USERNAME + ":" + this.PASSWORD).getBytes()));
  41.         LOGGER.debug("Credentials object has been created");
  42.     }
  43.  
  44.     private static class CredentialsHolder {
  45.         private static final Credentials INSTANCE = new Credentials();
  46.     }
  47.  
  48.     public static Credentials getInstance() {
  49.         return CredentialsHolder.INSTANCE;
  50.     }
  51.  
  52.     public String getUsername() {
  53.         return this.USERNAME;
  54.     }
  55.  
  56.     public String getPassword() {
  57.         return this.PASSWORD;
  58.     }
  59.  
  60.     public String getHttpBasicAuthentication() {
  61.         return this.BASIC_AUTHENTICATION;
  62.     }
  63.  
  64.     private Credentials(final String configFileName) {
  65.         Properties properties = null;
  66.         try {
  67.             properties = PropertiesHelper.load(configFileName);
  68.         } catch (final IOException e) {
  69.             LOGGER.error("Couldn't load credentials properties file: {}", configFileName);
  70.             e.printStackTrace();
  71.             System.exit(1);
  72.         }
  73.  
  74.         this.USERNAME = properties.getProperty("username", "");
  75.         this.PASSWORD = properties.getProperty("password", "");
  76.         this.BASIC_AUTHENTICATION = new String(Base64.encodeBase64((this.USERNAME + ":" + this.PASSWORD).getBytes()));
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement