Skip to content

Instantly share code, notes, and snippets.

@raj-rajaratnam
Last active August 29, 2015 14:10
Show Gist options
  • Save raj-rajaratnam/7f2139f7f804161ac23b to your computer and use it in GitHub Desktop.
Save raj-rajaratnam/7f2139f7f804161ac23b to your computer and use it in GitHub Desktop.
public class CartridgeAgentUtils {
private static final Log log = LogFactory.getLog(CartridgeAgentUtils.class);
public static List<String> splitUsingTokenizer(String string, String delimiter) {
StringTokenizer tokenizer = new StringTokenizer(string, delimiter);
List<String> list = new ArrayList<String>(string.length());
while (tokenizer.hasMoreTokens()) {
list.add(tokenizer.nextToken().trim());
}
return list;
}
public static String decryptPassword(String repoUserPassword) {
if(repoUserPassword == null) {
return repoUserPassword;
}
String decryptPassword = "";
String secret = CartridgeAgentConfiguration.getInstance().getCartridgeKey();
SecretKey key;
Cipher cipher;
Base64 coder;
key = new SecretKeySpec(secret.getBytes(), "AES");
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
coder = new Base64();
byte[] encrypted = coder.decode(repoUserPassword.getBytes());
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(encrypted);
decryptPassword = new String(decrypted);
} catch (Exception e) {
log.error("Exception has occurred. " + e.getMessage());
e.printStackTrace();
}
if (log.isDebugEnabled()) {
log.debug("Decrypted PWD : [" + decryptPassword + "] ");
}
return decryptPassword;
}
public static void waitUntilPortsActive(String ipAddress, List<Integer> ports) {
long portCheckTimeOut = 1000 * 60 * 10;
String portCheckTimeOutStr = System.getProperty("port.check.timeout");
if (StringUtils.isNotBlank(portCheckTimeOutStr)) {
portCheckTimeOut = Integer.parseInt(portCheckTimeOutStr);
}
if (log.isDebugEnabled()) {
log.debug("Port check timeout: " + portCheckTimeOut);
}
long startTime = System.currentTimeMillis();
boolean active = false;
while (!active) {
if(log.isInfoEnabled()) {
log.info("Waiting for ports to be active: [ip] "+ipAddress+" [ports] "+ports);
}
active = checkPortsActive(ipAddress, ports);
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
if (duration > portCheckTimeOut) {
return;
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
}
if(log.isInfoEnabled()) {
log.info("Ports activated: [ip] " + ipAddress + " [ports] "+ports);
}
}
public static boolean checkPortsActive(String ipAddress, List<Integer> ports) {
//List<Integer> ports = CartridgeAgentConfiguration.getInstance().getPorts();
if (ports.size() == 0) {
throw new RuntimeException("No ports found");
}
for (int port : ports) {
Socket socket = null;
try {
SocketAddress httpSockaddr = new InetSocketAddress(ipAddress, port);
socket = new Socket();
socket.connect(httpSockaddr, 5000);
if (log.isDebugEnabled()) {
log.debug(String.format("Port %s is active", port));
}
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug(String.format("Port %s is not active", port));
}
return false;
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment