Updated Branches: refs/heads/master 58b06ce91 -> 908504dfb
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/c8985d58/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentConstants.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentConstants.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentConstants.java new file mode 100644 index 0000000..0992b51 --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentConstants.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.stratos.cartridge.agent.util; + +import java.io.Serializable; + +public class CartridgeAgentConstants implements Serializable{ + + public static final String JNDI_PROPERTIES_DIR = "jndi.properties.dir"; + public static final String PARAM_FILE_PATH = "param.file.path"; + + public static final String CARTRIDGE_KEY = "CARTRIDGE_KEY"; + public static final String APP_PATH = "APP_PATH"; + public static final String SERVICE_NAME = "SERVICE_NAME"; + public static final String CLUSTER_ID = "CLUSTER_ID"; + public static final String NETWORK_PARTITION_ID = "NETWORK_PARTITION_ID"; + public static final String PARTITION_ID = "PARTITION_ID"; + public static final String MEMBER_ID = "MEMBER_ID"; + public static final String REPO_URL = "REPO_URL"; + public static final String PORTS = "PORTS"; + public static final String MEMORY_CONSUMPTION = "memory_consumption"; + public static final String LOAD_AVERAGE = "load_average"; + public static final String PORTS_NOT_OPEN = "ports_not_open"; +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/c8985d58/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentUtils.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentUtils.java b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentUtils.java new file mode 100644 index 0000000..0137966 --- /dev/null +++ b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/util/CartridgeAgentUtils.java @@ -0,0 +1,114 @@ +package org.apache.stratos.cartridge.agent.util; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.stratos.cartridge.agent.config.CartridgeAgentConfiguration; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +/** + * Cartridge agent utility methods. + */ +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()); + } + return list; + } + + public static String decryptPassword(String 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) { + e.printStackTrace(); + } + return decryptPassword; + } + + public static void waitUntilPortsActive() { + 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"); + } + active = checkPortsActive(); + long endTime = System.currentTimeMillis(); + long duration = endTime - startTime; + if (duration > portCheckTimeOut) { + return; + } + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + } + } + } + + public static boolean checkPortsActive() { + 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("localhost", port); + socket = new Socket(); + socket.connect(httpSockaddr, 5000); + if (log.isInfoEnabled()) { + log.info(String.format("Port %s is active", port)); + } + } catch (Exception e) { + if (log.isInfoEnabled()) { + log.info(String.format("Port %s is not active", port)); + } + return false; + } finally { + if (socket != null) { + try { + socket.close(); + } catch (IOException e) { + } + } + } + } + return true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/c8985d58/components/pom.xml ---------------------------------------------------------------------- diff --git a/components/pom.xml b/components/pom.xml index afde350..e2bfb60 100644 --- a/components/pom.xml +++ b/components/pom.xml @@ -50,6 +50,8 @@ <module>org.apache.stratos.deployment</module> <!-- CLI --> <module>org.apache.stratos.cli</module> + <!-- Cartridge Agent --> + <module>org.apache.stratos.cartridge.agent</module> <!-- Load Balancer --> <module>org.apache.stratos.load.balancer.common</module> <module>org.apache.stratos.load.balancer</module>
