You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ni...@apache.org on 2013/11/27 19:55:11 UTC
[14/26] CC refactoring, API cleaning r1
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/1654262f/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/AxiomXpathParserUtil.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/AxiomXpathParserUtil.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/AxiomXpathParserUtil.java
new file mode 100644
index 0000000..cc78501
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/AxiomXpathParserUtil.java
@@ -0,0 +1,263 @@
+/*
+ * 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.cloud.controller.axiom;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;
+import org.apache.axiom.om.impl.dom.ElementImpl;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.exception.CloudControllerException;
+import org.apache.stratos.cloud.controller.exception.MalformedConfigurationFileException;
+import org.apache.stratos.cloud.controller.util.*;
+import org.jaxen.JaxenException;
+import org.w3c.dom.Element;
+import org.wso2.securevault.SecretResolver;
+import org.wso2.securevault.SecretResolverFactory;
+import org.xml.sax.SAXException;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.validation.Validator;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.*;
+
+/**
+ * This class is parsing configuration files using Axiom Xpath.
+ */
+public class AxiomXpathParserUtil {
+
+ private static final Log log = LogFactory.getLog(AxiomXpathParserUtil.class);
+
+ public static OMElement parse(File xmlSource) throws MalformedConfigurationFileException,
+ IllegalArgumentException {
+
+ OMElement documentElement;
+
+ if (xmlSource == null) {
+ String msg = "File is null.";
+ log.error(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ try {
+ documentElement = new StAXOMBuilder(xmlSource.getPath()).getDocumentElement();
+ return documentElement;
+
+ } catch (XMLStreamException e) {
+ String msg = "Failed to parse the configuration file : " + xmlSource.getPath();
+ log.error(msg, e);
+ throw new MalformedConfigurationFileException(msg, e);
+ } catch (FileNotFoundException e) {
+ String msg = "Configuration file cannot be found : " + xmlSource.getPath();
+ log.error(msg);
+ throw new MalformedConfigurationFileException(msg);
+ }
+
+ }
+
+ private static Element getDOMElement(final OMElement omElement) {
+
+ // Get the StAX reader from the created element
+ XMLStreamReader llomReader = omElement.getXMLStreamReader();
+
+ // Create the DOOM OMFactory
+ OMFactory doomFactory = DOOMAbstractFactory.getOMFactory();
+
+ // Create the new builder
+ StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory, llomReader);
+
+ // Get the document element
+ OMElement newElem = doomBuilder.getDocumentElement();
+
+ return newElem instanceof Element ? (Element) newElem : null;
+ }
+
+ private static OMElement getElement(final Object obj) {
+ OMNode node;
+ if ((obj instanceof OMNode) && (node = (OMNode) obj).getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement element = (OMElement) node;
+
+ return element;
+
+ }
+
+ return null;
+ }
+
+ public static OMElement getElement(final String fileName, final OMElement rootElt,
+ final String eltStr, final String xpath) {
+ List<?> nodes = getMatchingNodes(xpath, rootElt);
+ neglectingWarn(fileName, eltStr, nodes.size());
+ OMElement element = getElement(nodes.get(0));
+ return element;
+ }
+
+ public static OMElement getFirstChildElement(final OMElement root, final String childName) {
+ Iterator<?> it = root.getChildrenWithName(new QName(childName));
+ if (it.hasNext()) {
+ return (OMElement) it.next();
+ }
+
+ return null;
+ }
+
+ private static void neglectingWarn(final String fileName, final String elt, final int size) {
+ if (size > 1) {
+ log.warn(fileName + " contains more than one " + elt + " elements!" +
+ " Elements other than the first will be neglected.");
+ }
+ }
+
+ public static void plainTextWarn(final String elt) {
+ log.warn("Unable to find a value for " + elt + " element from Secure Vault." +
+ "Hence we will try to assign the plain text value (if specified).");
+ }
+
+ /**
+ * @param xpath
+ * XPATH expression to be read.
+ * @param elt
+ * OMElement to be used for the search.
+ * @return List matching OMNode list
+ */
+ @SuppressWarnings("unchecked")
+ public static OMNode getFirstMatchingNode(final String xpath, final OMElement elt) throws MalformedConfigurationFileException{
+
+ AXIOMXPath axiomXpath;
+ List<OMNode> nodeList = null;
+ try {
+ axiomXpath = new AXIOMXPath(xpath);
+ nodeList = axiomXpath.selectNodes(elt);
+ return nodeList.isEmpty() ? null : nodeList.get(0);
+ } catch (JaxenException e) {
+ String msg = "Error occurred while reading the Xpath (" + xpath + ")";
+ log.error(msg, e);
+ throw new MalformedConfigurationFileException(msg, e);
+ }
+
+ }
+
+ /**
+ * @param xpath
+ * XPATH expression to be read.
+ * @return List matching list
+ */
+ @SuppressWarnings("unchecked")
+ public static List<OMNode> getMatchingNodes(OMElement elt, final String xpath) throws MalformedConfigurationFileException{
+
+ AXIOMXPath axiomXpath;
+ List<OMNode> nodeList = null;
+ try {
+ axiomXpath = new AXIOMXPath(xpath);
+ nodeList = axiomXpath.selectNodes(elt);
+ return nodeList;
+ } catch (JaxenException e) {
+ String msg = "Error occurred while reading the Xpath (" + xpath + ")";
+ log.error(msg, e);
+ throw new MalformedConfigurationFileException(msg, e);
+ }
+
+ }
+
+ /**
+ * @param xpath
+ * XPATH expression to be read.
+ * @param elt
+ * OMElement to be used for the search.
+ * @return List matching OMNode list
+ */
+ @SuppressWarnings("unchecked")
+ public static List<OMNode> getMatchingNodes(final String xpath, final OMElement elt) throws MalformedConfigurationFileException{
+
+ AXIOMXPath axiomXpath;
+ List<OMNode> nodeList = null;
+ try {
+ axiomXpath = new AXIOMXPath(xpath);
+ nodeList = axiomXpath.selectNodes(elt);
+ return nodeList;
+ } catch (JaxenException e) {
+ String msg = "Error occurred while reading the Xpath (" + xpath + ")";
+ log.error(msg, e);
+ throw new MalformedConfigurationFileException(msg, e);
+ }
+
+ }
+
+ public static void validate(final OMElement omElement, final File schemaFile) throws SAXException, IOException {
+
+ Element sourceElement;
+
+ // if the OMElement is created using DOM implementation use it
+ if (omElement instanceof ElementImpl) {
+ sourceElement = (Element) omElement;
+ } else { // else convert from llom to dom
+ sourceElement = getDOMElement(omElement);
+ }
+
+ // Create a SchemaFactory capable of understanding WXS schemas.
+
+ // Load a WXS schema, represented by a Schema instance.
+ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+ Source source = new StreamSource(schemaFile);
+
+ // Create a Validator object, which can be used to validate
+ // an instance document.
+ Schema schema = factory.newSchema(source);
+ Validator validator = schema.newValidator();
+
+ // Validate the DOM tree.
+ validator.validate(new DOMSource(sourceElement));
+ }
+
+ public static String resolveSecret(final OMElement docElt, final OMElement elt) {
+ // retrieve the value using secure vault
+ SecretResolver secretResolver = SecretResolverFactory.create(docElt, false);
+
+ String alias = elt.getAttributeValue(new QName(CloudControllerConstants.ALIAS_ATTRIBUTE));
+
+ // retrieve the secured password
+ if (secretResolver != null && secretResolver.isInitialized() &&
+ secretResolver.isTokenProtected(alias)) {
+
+ return secretResolver.resolve(alias);
+
+ }
+
+ return null;
+ }
+
+
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/1654262f/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/CartridgeConfigParser.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/CartridgeConfigParser.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/CartridgeConfigParser.java
new file mode 100644
index 0000000..0195fc4
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/CartridgeConfigParser.java
@@ -0,0 +1,383 @@
+/*
+ * 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.cloud.controller.axiom.parser;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNode;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.axiom.AxiomXpathParserUtil;
+import org.apache.stratos.cloud.controller.exception.MalformedConfigurationFileException;
+import org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder;
+import org.apache.stratos.cloud.controller.util.AppType;
+import org.apache.stratos.cloud.controller.util.Cartridge;
+import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
+import org.apache.stratos.cloud.controller.util.IaasProvider;
+import org.apache.stratos.cloud.controller.util.PortMapping;
+
+/**
+ * Parse the cartridge definition files.
+ * @author nirmal
+ *
+ */
+public class CartridgeConfigParser {
+ private static final Log log = LogFactory.getLog(CartridgeConfigParser.class);
+
+ /**
+ * Parse the config file.
+ * @param elt document element.
+ * @throws MalformedConfigurationFileException
+ */
+ public static List<Cartridge> parse(String file, OMElement elt) throws MalformedConfigurationFileException {
+
+ return extractCartridges(file, elt);
+ }
+
+ private static List<Cartridge> extractCartridges(String file, OMElement elt) throws MalformedConfigurationFileException{
+
+ FasterLookUpDataHolder dataHolder = FasterLookUpDataHolder.getInstance();
+
+ List<IaasProvider> iaasProviders = dataHolder.getIaasProviders();
+
+ if (iaasProviders == null) {
+ dataHolder.setIaasProviders((iaasProviders = new ArrayList<IaasProvider>()));
+ }
+
+ List<Cartridge> cartridges = new ArrayList<Cartridge>();
+
+ String xpath = CloudControllerConstants.CARTRIDGES_ELEMENT_XPATH;
+
+ // cartridges can be found from this XPATH
+ List<?> cartridgeNodes = AxiomXpathParserUtil.getMatchingNodes(xpath, elt);
+
+ if (cartridgeNodes == null || cartridgeNodes.isEmpty()) {
+ // or from this XPATH
+ xpath = CloudControllerConstants.CARTRIDGE_ELEMENT_XPATH;
+ cartridgeNodes = AxiomXpathParserUtil.getMatchingNodes(xpath, elt);
+ }
+
+ if (cartridgeNodes == null || cartridgeNodes.isEmpty()) {
+ log.warn("No cartridge found in this configuration file : " + file);
+ return cartridges;
+ }
+
+ for (Object obj : cartridgeNodes) {
+
+ if (obj instanceof OMNode) {
+ OMNode cartridgeNode = (OMNode) obj;
+
+ if (cartridgeNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement cartridgeElement = (OMElement) cartridgeNode;
+
+ // retrieve Attributes of a Cartridge definition
+ String type = cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.TYPE_ATTR));
+ String host = cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.HOST_ATTR));
+ String provider = cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.PROVIDER_ATTR));
+
+ String version =
+ cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.VERSION_ATTR));
+
+ boolean multiTenant = Boolean.valueOf(cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.MULTI_TENANT_ATTR)));
+
+ Cartridge aCartridge;
+
+ if ((aCartridge = dataHolder.getCartridge(type)) == null) {
+
+ aCartridge = new Cartridge(type, host, provider, version, multiTenant);
+ }
+
+ // read displayName
+ Iterator<?> itName =
+ cartridgeElement.getChildrenWithName(new QName(
+ CloudControllerConstants.DISPLAY_NAME_ELEMENT));
+
+ if (itName.hasNext()) {
+ OMElement name = (OMElement) itName.next();
+
+ aCartridge.setDisplayName(name.getText());
+ }
+
+ // read description
+ Iterator<?> it =
+ cartridgeElement.getChildrenWithName(new QName(
+ CloudControllerConstants.DESCRIPTION_ELEMENT));
+
+ if (it.hasNext()) {
+ OMElement desc = (OMElement) it.next();
+
+ aCartridge.setDescription(desc.getText());
+ }
+
+ // load properties of this cartridge
+ IaasProviderConfigParser.loadProperties(file, cartridgeElement, aCartridge.getProperties());
+
+ // retrieve the list of IaaS providers
+ List<?> iaasProviderNodes = AxiomXpathParserUtil.getMatchingNodes(xpath + CloudControllerConstants.IAAS_PROVIDER_ELEMENT_XPATH,
+ cartridgeElement);
+
+ getIaasProviders(file, elt, iaasProviders, cartridgeElement.toString(), aCartridge, iaasProviderNodes);
+
+ // load dirs
+ List<?> deploymentNodes = AxiomXpathParserUtil.getMatchingNodes(xpath + CloudControllerConstants.DEPLOYMENT_ELEMENT_XPATH,
+ cartridgeElement);
+ setDeploymentDirs(file, cartridgeElement.toString(), aCartridge, deploymentNodes);
+
+ // load port mappings
+ List<?> portMappingNodes =
+ AxiomXpathParserUtil.getMatchingNodes(xpath +
+ CloudControllerConstants.PORT_MAPPING_ELEMENT_XPATH,
+ cartridgeElement);
+ getPortMappings(file, cartridgeElement.toString(), aCartridge, portMappingNodes);
+
+ // load appTypes
+ List<?> appTypesNodes =
+ AxiomXpathParserUtil.getMatchingNodes(xpath +
+ CloudControllerConstants.APP_TYPES_ELEMENT_XPATH,
+ cartridgeElement);
+ getAppTypes(file, cartridgeElement.toString(), aCartridge, appTypesNodes);
+
+ cartridges.add(aCartridge);
+
+ if (dataHolder.getCartridge(type) == null) {
+ dataHolder.addCartridge(aCartridge);
+ }
+ }
+ }
+ }
+
+ return cartridges;
+ }
+
+
+ /**
+ * @param iaasProviders
+ * @param cartridgeElementString
+ * @param aCartridge
+ * @param iaasProviderNodes
+ */
+ private static void getIaasProviders(final String fileName, final OMElement elt, List<IaasProvider> iaasProviders,
+ String cartridgeElementString, Cartridge aCartridge,
+ List<?> iaasProviderNodes) {
+ for (Object nodeObj : iaasProviderNodes) {
+ if (nodeObj instanceof OMNode) {
+ OMNode iaasProviderNode = (OMNode) nodeObj;
+
+ if (iaasProviderNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement iaasElt = (OMElement) iaasProviderNode;
+
+ // add the IaasProvider to this cartridge
+ aCartridge.addIaasProvider(IaasProviderConfigParser.getIaasProvider(fileName, elt, iaasElt, iaasProviders));
+
+ } else {
+ String msg =
+ "Essential '" +
+ CloudControllerConstants.IAAS_PROVIDER_ELEMENT +
+ "' element cannot" + " be found in " +
+ cartridgeElementString + " of " +
+ fileName;
+ handleException(msg);
+ }
+
+ }
+ }
+ }
+
+ /**
+ * @param cartridgeElementString Cartridges section as a {@link String}
+ * @param aCartridge {@link Cartridge} instance.
+ * @param deploymentNodes list of deployment directory nodes
+ */
+ private static void setDeploymentDirs(String fileName, String cartridgeElementString, Cartridge aCartridge,
+ List<?> deploymentNodes) {
+ Object nodeObj;
+ if ((nodeObj = deploymentNodes.get(0)) instanceof OMNode) {
+ OMNode deploymentNode = (OMNode) nodeObj;
+
+ if (deploymentNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement deployElt = (OMElement) deploymentNode;
+
+ if (deployElt.getAttributeValue(new QName(
+ CloudControllerConstants.BASE_DIR_ATTR)) != null) {
+
+ aCartridge.setBaseDir(deployElt.getAttributeValue(new QName(
+ CloudControllerConstants.BASE_DIR_ATTR)));
+ }
+
+ for (Iterator<?> iterator =
+ deployElt.getChildrenWithName(new QName(
+ CloudControllerConstants.DIRECTORY_ELEMENT)); iterator.hasNext();) {
+ OMElement dir = (OMElement) iterator.next();
+ aCartridge.addDeploymentDir(dir.getText());
+ }
+
+ } else {
+ String msg =
+ "Essential '" + CloudControllerConstants.DEPLOYMENT_ELEMENT +
+ "' element cannot" + " be found in " +
+ cartridgeElementString + " of " + fileName;
+ handleException(msg);
+ }
+
+ }
+ }
+
+ /**
+ * @param cartridgeElementString Cartridges section as a {@link String}
+ * @param aCartridge {@link Cartridge} instance.
+ * @param portMappingNodes nodes of port mapping elements
+ */
+ private static void getPortMappings(final String fileName, String cartridgeElementString, Cartridge aCartridge,
+ List<?> portMappingNodes) {
+ Object nodeObj;
+ if (!portMappingNodes.isEmpty()) {
+ if ((nodeObj = portMappingNodes.get(0)) instanceof OMNode) {
+ OMNode portMappingNode = (OMNode) nodeObj;
+
+ if (portMappingNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement portMappingElt = (OMElement) portMappingNode;
+
+ for (Iterator<?> iterator =
+ portMappingElt.getChildrenWithName(new QName(
+ CloudControllerConstants.HTTP_ELEMENT)); iterator.hasNext();) {
+ OMElement httpElt = (OMElement) iterator.next();
+
+ String port =
+ httpElt.getAttributeValue(new QName(
+ CloudControllerConstants.PORT_ATTR));
+ String proxyPort =
+ httpElt.getAttributeValue(new QName(
+ CloudControllerConstants.PROXY_PORT_ATTR));
+
+ PortMapping mapping =
+ new PortMapping(
+ CloudControllerConstants.HTTP_ELEMENT,
+ port, proxyPort);
+
+ aCartridge.addPortMapping(mapping);
+ }
+
+ for (Iterator<?> iterator =
+ portMappingElt.getChildrenWithName(new QName(
+ CloudControllerConstants.HTTPS_ELEMENT)); iterator.hasNext();) {
+ OMElement httpsElt = (OMElement) iterator.next();
+
+ String port =
+ httpsElt.getAttributeValue(new QName(
+ CloudControllerConstants.PORT_ATTR));
+ String proxyPort =
+ httpsElt.getAttributeValue(new QName(
+ CloudControllerConstants.PROXY_PORT_ATTR));
+
+ PortMapping mapping =
+ new PortMapping(
+ CloudControllerConstants.HTTPS_ELEMENT,
+ port, proxyPort);
+
+ aCartridge.addPortMapping(mapping);
+ }
+
+ } else {
+ String msg =
+ "Essential '" +
+ CloudControllerConstants.PORT_MAPPING_ELEMENT +
+ "' element cannot" + " be found in " +
+ cartridgeElementString + " of " +
+ fileName;
+ handleException(msg);
+ }
+
+ }
+ }
+ }
+
+ /**
+ * @param cartridgeElementString Cartridges section as a {@link String}
+ * @param aCartridge {@link org.apache.stratos.cloud.controller.util.Cartridge} instance.
+ * @param appTypesNodes nodes of App types.
+ */
+ private static void getAppTypes(final String fileName, String cartridgeElementString, Cartridge aCartridge,
+ List<?> appTypesNodes) {
+ Object nodeObj;
+ if (!appTypesNodes.isEmpty()) {
+ if ((nodeObj = appTypesNodes.get(0)) instanceof OMNode) {
+ OMNode appTypeNode = (OMNode) nodeObj;
+
+ if (appTypeNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement appTypesElt = (OMElement) appTypeNode;
+
+ for (Iterator<?> iterator =
+ appTypesElt.getChildrenWithName(new QName(
+ CloudControllerConstants.APP_TYPE_ELEMENT)); iterator.hasNext();) {
+ OMElement appElt = (OMElement) iterator.next();
+
+ String name =
+ appElt.getAttributeValue(new QName(
+ CloudControllerConstants.NAME_ATTR));
+ String appSpecificMapping =
+ appElt.getAttributeValue(new QName(
+ CloudControllerConstants.APP_SPECIFIC_MAPPING_ATTR));
+
+ AppType appType;
+
+ if (appSpecificMapping == null) {
+ appType = new AppType(name);
+ } else {
+ appType =
+ new AppType(name,
+ Boolean.valueOf(appSpecificMapping));
+ }
+
+ aCartridge.addAppType(appType);
+ }
+
+ } else {
+ String msg =
+ "Essential '" + CloudControllerConstants.APP_TYPE_ELEMENT +
+ "' element cannot" + " be found in " +
+ cartridgeElementString + " of " +
+ fileName;
+ handleException(msg);
+ }
+
+ }
+ }
+ }
+
+
+ private static void handleException(final String msg) throws MalformedConfigurationFileException{
+ log.error(msg);
+ throw new MalformedConfigurationFileException(msg);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/1654262f/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/CloudControllerConfigParser.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/CloudControllerConfigParser.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/CloudControllerConfigParser.java
new file mode 100644
index 0000000..e90ccd3
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/CloudControllerConfigParser.java
@@ -0,0 +1,205 @@
+/*
+ * 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.cloud.controller.axiom.parser;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNode;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.axiom.AxiomXpathParserUtil;
+import org.apache.stratos.cloud.controller.exception.MalformedConfigurationFileException;
+import org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder;
+import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
+import org.apache.stratos.cloud.controller.util.DataPublisherConfig;
+import org.apache.stratos.cloud.controller.util.IaasProvider;
+import org.apache.stratos.cloud.controller.util.TopologyConfig;
+import org.wso2.securevault.SecretResolver;
+import org.wso2.securevault.SecretResolverFactory;
+
+/**
+ * Parse the cloud-controller.xml
+ * @author nirmal
+ *
+ */
+public class CloudControllerConfigParser {
+ private static final Log log = LogFactory.getLog(CloudControllerConfigParser.class);
+ private static final String FILE_NAME = "cloud-controller.xml";
+
+ /**
+ * Parse the cloud-controller.xml file.
+ * @param elt document element.
+ * @throws MalformedConfigurationFileException
+ */
+ public static void parse(OMElement elt) throws MalformedConfigurationFileException {
+
+ extractIaasProviders(elt, AxiomXpathParserUtil.getMatchingNodes(elt, CloudControllerConstants.IAAS_PROVIDER_XPATH));
+ extractDataPublisherConfig(elt, AxiomXpathParserUtil.getElement(FILE_NAME, elt, CloudControllerConstants.DATA_PUBLISHER_ELEMENT,
+ CloudControllerConstants.DATA_PUBLISHER_XPATH));
+ extractTopologySyncConfig(elt, AxiomXpathParserUtil.getElement(FILE_NAME, elt, CloudControllerConstants.TOPOLOGY_SYNC_ELEMENT,
+ CloudControllerConstants.TOPOLOGY_SYNC_XPATH));
+ }
+
+
+
+
+ private static void extractIaasProviders(OMElement elt, List<OMNode> nodeList) {
+ List<IaasProvider> iaasProviders = FasterLookUpDataHolder.getInstance().getIaasProviders();
+
+ if (iaasProviders == null) {
+ FasterLookUpDataHolder.getInstance()
+ .setIaasProviders((iaasProviders = new ArrayList<IaasProvider>()));
+ }
+
+ // this is a valid scenario. User can have 0..1 iaas provider elements
+ // in cloud-controller xml.
+ if (nodeList == null || nodeList.isEmpty()) {
+ log.debug("No IaasProvider element found in "+FILE_NAME);
+ return;
+ }
+
+ for (OMNode node : nodeList) {
+ iaasProviders.add(IaasProviderConfigParser.getIaasProvider(FILE_NAME, elt, node, iaasProviders));
+ }
+ }
+
+ private static void extractDataPublisherConfig(OMElement rootElt, OMElement element) {
+ if (element == null) {
+ log.debug("No data publisher config found in "+FILE_NAME);
+ return;
+ }
+
+ FasterLookUpDataHolder dataHolder = FasterLookUpDataHolder.getInstance();
+ // get enable attribute
+ boolean isEnable =
+ Boolean.parseBoolean(element.getAttributeValue(new QName(
+ CloudControllerConstants.ENABLE_ATTR)));
+ dataHolder.setEnableBAMDataPublisher(isEnable);
+
+ if (isEnable) {
+ // get bam server info
+ OMElement childElement =
+ AxiomXpathParserUtil.getFirstChildElement(element,
+ CloudControllerConstants.BAM_SERVER_ELEMENT);
+ OMElement elt;
+
+ DataPublisherConfig config = new DataPublisherConfig();
+ dataHolder.setDataPubConfig(config);
+
+ if (childElement != null) {
+ // set bam user name
+ elt =
+ AxiomXpathParserUtil.getFirstChildElement(childElement,
+ CloudControllerConstants.BAM_SERVER_ADMIN_USERNAME_ELEMENT);
+ if (elt != null) {
+ config.setBamUsername(elt.getText());
+ }
+ // set bam password
+ elt =
+ AxiomXpathParserUtil.getFirstChildElement(childElement,
+ CloudControllerConstants.BAM_SERVER_ADMIN_PASSWORD_ELEMENT);
+ if (elt != null) {
+ String password = AxiomXpathParserUtil.resolveSecret(rootElt, elt);
+ if (password == null) {
+ AxiomXpathParserUtil.plainTextWarn(CloudControllerConstants.BAM_SERVER_ADMIN_PASSWORD_ELEMENT);
+ password = elt.getText();
+ }
+
+ if (password != null) {
+ config.setBamPassword(password);
+ }
+ }
+ }
+
+ // set cron
+ childElement = AxiomXpathParserUtil.getFirstChildElement(element, CloudControllerConstants.CRON_ELEMENT);
+ if (childElement != null) {
+ config.setDataPublisherCron(childElement.getText());
+ }
+
+ // set cassandra info
+ childElement = AxiomXpathParserUtil.getFirstChildElement(element, CloudControllerConstants.CASSANDRA_INFO_ELEMENT);
+
+ if (childElement != null) {
+ // set connection url
+ elt = AxiomXpathParserUtil.getFirstChildElement(childElement, CloudControllerConstants.CONNECTION_URL_ELEMENT);
+ if (elt != null) {
+ config.setCassandraConnUrl(elt.getText());
+ }
+
+ // set user name
+ elt = AxiomXpathParserUtil.getFirstChildElement(childElement, CloudControllerConstants.USER_NAME_ELEMENT);
+ if (elt != null) {
+ config.setCassandraUser(elt.getText());
+ }
+ // set password
+ elt = AxiomXpathParserUtil.getFirstChildElement(childElement, CloudControllerConstants.PASSWORD_ELEMENT);
+ if (elt != null) {
+ String password = AxiomXpathParserUtil.resolveSecret(rootElt, elt);
+ if (password == null) {
+ AxiomXpathParserUtil.plainTextWarn(CloudControllerConstants.PASSWORD_ELEMENT);
+ password = elt.getText();
+ }
+
+ if (password != null) {
+ config.setCassandraPassword(password);
+ }
+ }
+ }
+
+ }
+ }
+
+ private static void extractTopologySyncConfig(OMElement elt, OMElement element) {
+
+ if (element == null) {
+ log.debug("No Topology sync config is found "+FILE_NAME);
+ return;
+ }
+
+ // get enable attribute
+ boolean isEnable =
+ Boolean.parseBoolean(element.getAttributeValue(new QName(
+ CloudControllerConstants.ENABLE_ATTR)));
+
+ FasterLookUpDataHolder dataHolder = FasterLookUpDataHolder.getInstance();
+
+ dataHolder.setEnableTopologySync(isEnable);
+
+ if (isEnable) {
+ TopologyConfig topologyConfig = new TopologyConfig();
+ // load properties
+ IaasProviderConfigParser.loadProperties(FILE_NAME, element, topologyConfig.getProperties());
+
+ dataHolder.setTopologyConfig(topologyConfig);
+ }
+ }
+
+ private static void handleException(final String msg) throws MalformedConfigurationFileException{
+ log.error(msg);
+ throw new MalformedConfigurationFileException(msg);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/1654262f/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/IaasProviderConfigParser.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/IaasProviderConfigParser.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/IaasProviderConfigParser.java
new file mode 100644
index 0000000..4038314
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/IaasProviderConfigParser.java
@@ -0,0 +1,308 @@
+/*
+ * 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.cloud.controller.axiom.parser;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNode;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.exception.MalformedConfigurationFileException;
+import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
+import org.apache.stratos.cloud.controller.util.IaasProvider;
+import org.wso2.securevault.SecretResolver;
+import org.wso2.securevault.SecretResolverFactory;
+
+/**
+ * Parse the iaas providers.
+ * @author nirmal
+ *
+ */
+public class IaasProviderConfigParser {
+ private static final Log log = LogFactory.getLog(IaasProviderConfigParser.class);
+
+ public static IaasProvider getIaasProvider(final String fileName, final OMElement elt, final OMNode item, List<IaasProvider> iaases) {
+
+ IaasProvider iaas = null;
+
+ if (item.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement iaasElt = (OMElement) item;
+
+ if (iaases != null) {
+ // check whether this is a reference to a predefined IaaS.
+ for (IaasProvider iaasProvider : iaases) {
+ if (iaasProvider.getType()
+ .equals(iaasElt.getAttribute(new QName(
+ CloudControllerConstants.TYPE_ATTR))
+ .getAttributeValue())) {
+ iaas = new IaasProvider(iaasProvider);
+ break;
+ }
+ }
+ }
+
+ if (iaas == null) {
+ iaas = new IaasProvider();
+ }
+
+ if (iaas.getType() == null) {
+ iaas.setType(iaasElt.getAttribute(new QName(CloudControllerConstants.TYPE_ATTR))
+ .getAttributeValue());
+ }
+
+ if ("".equals(iaas.getType())) {
+ String msg =
+ "'" + CloudControllerConstants.IAAS_PROVIDER_ELEMENT + "' element's '" +
+ CloudControllerConstants.TYPE_ATTR +
+ "' attribute should be specified!";
+
+ handleException(msg);
+
+ }
+
+ // this is not mandatory
+ String name =
+ (iaasElt.getAttribute(new QName(CloudControllerConstants.NAME_ATTR)) == null)
+ ? iaas.getName()
+ : iaasElt.getAttributeValue(new QName(
+ CloudControllerConstants.NAME_ATTR));
+
+ iaas.setName(name);
+
+ String xpath = CloudControllerConstants.IAAS_PROVIDER_ELEMENT_XPATH;
+
+ // load other elements
+ loadClassName(fileName, iaas, iaasElt);
+ loadProvider(fileName, iaas, iaasElt);
+ loadProperties(fileName, iaasElt, iaas.getProperties());
+ loadTemplate(fileName, iaas, iaasElt);
+ loadIdentity(fileName, elt, iaas, iaasElt);
+ loadCredentials(fileName, elt, iaas, iaasElt, xpath);
+ }
+
+ return iaas;
+ }
+
+ private static void loadClassName(final String fileName, final IaasProvider iaas, final OMElement iaasElt) {
+
+ Iterator<?> it =
+ iaasElt.getChildrenWithName(new QName(
+ CloudControllerConstants.CLASS_NAME_ELEMENT));
+
+ if (it.hasNext()) {
+ OMElement classNameElt = (OMElement) it.next();
+ iaas.setClassName(classNameElt.getText());
+ }
+
+ if (it.hasNext()) {
+ log.warn(" file contains more than one " +
+ CloudControllerConstants.CLASS_NAME_ELEMENT + " elements!" +
+ " Elements other than the first will be neglected.");
+ }
+
+ if (iaas.getClassName() == null) {
+ String msg =
+ "Essential '" + CloudControllerConstants.CLASS_NAME_ELEMENT + "' element " +
+ "has not specified in " + fileName;
+ handleException(msg);
+ }
+
+ }
+
+ private static void loadCredentials(final String fileName, final OMElement elt, final IaasProvider iaas, final OMElement iaasElt,
+ final String xpath) {
+
+ Iterator<?> it =
+ iaasElt.getChildrenWithName(new QName(
+ CloudControllerConstants.CREDENTIAL_ELEMENT));
+
+ if (it.hasNext()) {
+ OMElement credentialElt = (OMElement) it.next();
+
+ // retrieve the value using secure vault
+ SecretResolver secretResolver = SecretResolverFactory.create(elt, false);
+ String alias =
+ credentialElt.getAttributeValue(new QName(
+ CloudControllerConstants.ALIAS_ATTRIBUTE));
+
+ // retrieve the secured password
+ if (secretResolver != null && secretResolver.isInitialized() &&
+ secretResolver.isTokenProtected(alias)) {
+
+ iaas.setCredential(secretResolver.resolve(alias));
+
+ }
+
+ // if we still cannot find a value, we try to assign the value which
+ // is specified
+ // in the element, if any
+ if (iaas.getCredential() == null) {
+ log.warn("Unable to find a value for " + CloudControllerConstants.CREDENTIAL_ELEMENT +
+ " element from Secure Vault." +
+ "Hence we will try to assign the plain text value (if specified).");
+ iaas.setCredential(credentialElt.getText());
+ }
+ }
+
+ if (it.hasNext()) {
+ log.warn(fileName + " contains more than one " +
+ CloudControllerConstants.CREDENTIAL_ELEMENT + " elements!" +
+ " Elements other than the first will be neglected.");
+ }
+
+ if (iaas.getCredential() == null) {
+ String msg =
+ "Essential '" + CloudControllerConstants.CREDENTIAL_ELEMENT + "' element" +
+ " has not specified in " + fileName;
+ handleException(msg);
+ }
+
+ }
+
+
+ private static void loadIdentity(final String fileName, final OMElement elt, final IaasProvider iaas, final OMElement iaasElt) {
+
+ Iterator<?> it =
+ iaasElt.getChildrenWithName(new QName(CloudControllerConstants.IDENTITY_ELEMENT));
+
+ if (it.hasNext()) {
+ OMElement identityElt = (OMElement) it.next();
+
+ // retrieve the value using secure vault
+ SecretResolver secretResolver = SecretResolverFactory.create(elt, false);
+ String alias =
+ identityElt.getAttributeValue(new QName(
+ CloudControllerConstants.ALIAS_ATTRIBUTE));
+
+ // retrieve the secured password
+ if (secretResolver != null && secretResolver.isInitialized() &&
+ secretResolver.isTokenProtected(alias)) {
+
+ iaas.setIdentity(secretResolver.resolve(alias));
+
+ }
+
+ // if we still cannot find a value, we try to assign the value which
+ // is specified
+ // in the element, if any
+ if (iaas.getIdentity() == null) {
+ log.warn("Unable to find a value for " + CloudControllerConstants.IDENTITY_ELEMENT +
+ " element from Secure Vault." +
+ "Hence we will try to assign the plain text value (if specified).");
+ iaas.setIdentity(identityElt.getText());
+ }
+ }
+
+ if (it.hasNext()) {
+ log.warn(fileName + " contains more than one " + CloudControllerConstants.IDENTITY_ELEMENT +
+ " elements!" + " Elements other than the first will be neglected.");
+ }
+
+ if (iaas.getIdentity() == null) {
+ String msg =
+ "Essential '" + CloudControllerConstants.IDENTITY_ELEMENT + "' element" +
+ " has not specified in " + fileName;
+ handleException(msg);
+ }
+
+ }
+
+
+
+ public static void loadProperties(final String fileName, final OMElement elt, final Map<String, String> propertyMap) {
+
+ Iterator<?> it =
+ elt.getChildrenWithName(new QName(CloudControllerConstants.PROPERTY_ELEMENT));
+
+ while (it.hasNext()) {
+ OMElement prop = (OMElement) it.next();
+
+ if (prop.getAttribute(new QName(CloudControllerConstants.PROPERTY_NAME_ATTR)) == null ||
+ prop.getAttribute(new QName(CloudControllerConstants.PROPERTY_VALUE_ATTR)) == null) {
+
+ String msg =
+ "Property element's, name and value attributes should be specified " +
+ "in " + fileName;
+
+ handleException(msg);
+ }
+
+ propertyMap.put(prop.getAttribute(new QName(CloudControllerConstants.PROPERTY_NAME_ATTR))
+ .getAttributeValue(),
+ prop.getAttribute(new QName(CloudControllerConstants.PROPERTY_VALUE_ATTR))
+ .getAttributeValue());
+ }
+
+ }
+
+ private static void loadProvider(final String fileName, final IaasProvider iaas, final OMElement iaasElt) {
+
+ Iterator<?> it =
+ iaasElt.getChildrenWithName(new QName(CloudControllerConstants.PROVIDER_ELEMENT));
+
+ if (it.hasNext()) {
+ OMElement providerElt = (OMElement) it.next();
+ iaas.setProvider(providerElt.getText());
+ }
+
+ if (it.hasNext()) {
+ log.warn(fileName + " contains more than one " + CloudControllerConstants.PROVIDER_ELEMENT +
+ " elements!" + " Elements other than the first will be neglected.");
+ }
+
+ if (iaas.getProvider() == null) {
+ String msg =
+ "Essential '" + CloudControllerConstants.PROVIDER_ELEMENT + "' element " +
+ "has not specified in " + fileName;
+ handleException(msg);
+ }
+
+ }
+
+
+ private static void loadTemplate(final String fileName, final IaasProvider iaas, final OMElement iaasElt) {
+
+ Iterator<?> it =
+ iaasElt.getChildrenWithName(new QName(CloudControllerConstants.IMAGE_ID_ELEMENT));
+
+ if (it.hasNext()) {
+ OMElement imageElt = (OMElement) it.next();
+ iaas.setImage(imageElt.getText());
+ }
+
+ if (it.hasNext()) {
+ log.warn(fileName + " contains more than one " + CloudControllerConstants.IMAGE_ID_ELEMENT +
+ " elements!" + " Elements other than the first will be neglected.");
+ }
+
+ }
+
+
+ private static void handleException(final String msg) throws MalformedConfigurationFileException{
+ log.error(msg);
+ throw new MalformedConfigurationFileException(msg);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/1654262f/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/ServiceConfigParser.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/ServiceConfigParser.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/ServiceConfigParser.java
new file mode 100644
index 0000000..2c335ae
--- /dev/null
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/axiom/parser/ServiceConfigParser.java
@@ -0,0 +1,563 @@
+/*
+ * 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.cloud.controller.axiom.parser;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNode;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.axiom.AxiomXpathParserUtil;
+import org.apache.stratos.cloud.controller.exception.MalformedConfigurationFileException;
+import org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder;
+import org.apache.stratos.cloud.controller.util.AppType;
+import org.apache.stratos.cloud.controller.util.Cartridge;
+import org.apache.stratos.cloud.controller.util.CloudControllerConstants;
+import org.apache.stratos.cloud.controller.util.IaasProvider;
+import org.apache.stratos.cloud.controller.util.PortMapping;
+import org.apache.stratos.cloud.controller.util.ServiceContext;
+
+/**
+ * Parse the cartridge definition files.
+ *
+ * @author nirmal
+ *
+ */
+public class ServiceConfigParser {
+ private static final Log log = LogFactory.getLog(ServiceConfigParser.class);
+
+ /**
+ * Parse the config file.
+ *
+ * @param elt
+ * document element.
+ * @throws MalformedConfigurationFileException
+ */
+ public static List<Cartridge>
+ parse(String file, OMElement elt) throws MalformedConfigurationFileException {
+
+ return extractCartridges(file, elt);
+ }
+
+ public static List<ServiceContext> extractServiceContexts(File file, OMElement elt) {
+
+ String fileName = file.getAbsolutePath();
+ List<ServiceContext> serviceContextList = new ArrayList<ServiceContext>();
+
+ // services can be found from this XPATH
+ String xpath = CloudControllerConstants.SERVICES_ELEMENT_XPATH;
+ List<?> serviceNodes = AxiomXpathParserUtil.getMatchingNodes(xpath, elt);
+
+ if (serviceNodes == null || serviceNodes.isEmpty()) {
+ // or from this XPATH
+ xpath = CloudControllerConstants.SERVICE_ELEMENT_XPATH;
+ serviceNodes = AxiomXpathParserUtil.getMatchingNodes(xpath, elt);
+ }
+
+ if (serviceNodes == null || serviceNodes.isEmpty()) {
+ log.warn("No service found in this configuration file : " + fileName);
+ return serviceContextList;
+ }
+
+ for (Object obj : serviceNodes) {
+ ServiceContext serviceCtxt = new ServiceContext();
+
+ // set the definition file
+ serviceCtxt.setFile(file);
+
+ if (obj instanceof OMNode) {
+ OMNode serviceNode = (OMNode) obj;
+
+ if (serviceNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement node = (OMElement) serviceNode;
+
+ if (node.getAttribute(new QName(CloudControllerConstants.SERVICE_DOMAIN_ATTR)) == null) {
+ String msg =
+ "Essential '" + CloudControllerConstants.SERVICE_DOMAIN_ATTR +
+ "' " + "attribute of '" +
+ CloudControllerConstants.SERVICE_ELEMENT +
+ "' element cannot be found in " + fileName;
+
+ handleException(msg);
+ }
+
+ // set domain name
+ serviceCtxt.setClusterId(node.getAttribute(new QName(
+ CloudControllerConstants.SERVICE_DOMAIN_ATTR))
+ .getAttributeValue());
+ // set tenant range
+ serviceCtxt.setTenantRange(node.getAttribute(new QName(
+ CloudControllerConstants.SERVICE_TENANT_RANGE_ATTR))
+ .getAttributeValue());
+
+ serviceCtxt.setAutoScalerPolicyName(node.getAttribute(new QName(
+ CloudControllerConstants.POLICY_NAME))
+ .getAttributeValue());
+
+ OMNode cartridgeNode =
+ AxiomXpathParserUtil.getFirstMatchingNode(xpath +
+ CloudControllerConstants.CARTRIDGE_ELEMENT_XPATH,
+ node);
+
+ if (cartridgeNode != null && cartridgeNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement cartridgeElt = (OMElement) cartridgeNode;
+
+ String type =
+ cartridgeElt.getAttribute(new QName(
+ CloudControllerConstants.TYPE_ATTR))
+ .getAttributeValue();
+
+ if ("".equals(type)) {
+ String msg =
+ "Essential '" + CloudControllerConstants.TYPE_ATTR + "' " +
+ " attribute of '" +
+ CloudControllerConstants.CARTRIDGE_ELEMENT +
+ "' of '" +
+ CloudControllerConstants.SERVICE_ELEMENT +
+ "' element cannot be found in " + fileName;
+
+ handleException(msg);
+ }
+
+ // set Cartridge type
+ serviceCtxt.setCartridgeType(type);
+
+ }
+ if (serviceCtxt.getCartridgeType() == null) {
+ String msg =
+ "Essential '" + CloudControllerConstants.CARTRIDGE_ELEMENT +
+ "' element" + " has not specified in " + fileName;
+ handleException(msg);
+ }
+
+ // load payload
+ loadPayload(AxiomXpathParserUtil.getMatchingNodes(xpath +
+ CloudControllerConstants.PAYLOAD_ELEMENT_XPATH,
+ node), serviceCtxt);
+
+ // load host name
+ loadHostName(AxiomXpathParserUtil.getMatchingNodes(xpath +
+ CloudControllerConstants.HOST_ELEMENT_XPATH,
+ node), serviceCtxt);
+
+ // load properties
+ IaasProviderConfigParser.loadProperties(fileName, node,
+ serviceCtxt.getProperties());
+
+ }
+ }
+
+ FasterLookUpDataHolder.getInstance().addServiceContext(serviceCtxt);
+ // add each domain specific template to list
+ serviceContextList.add(serviceCtxt);
+ }
+
+ return serviceContextList;
+
+ }
+
+ private static void loadHostName(final List<OMNode> nodes, final ServiceContext serviceCtxt) {
+
+ if (nodes == null || nodes.isEmpty()) {
+ return;
+ }
+
+ // read host element
+ if (nodes.get(0).getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement node = (OMElement) nodes.get(0);
+
+ if (node.getText() != null) {
+ serviceCtxt.setHostName(node.getText());
+ }
+
+ }
+ }
+
+ private static void loadPayload(final List<OMNode> nodes, final ServiceContext serviceCtxt) {
+
+ if (nodes == null || nodes.isEmpty()) {
+ return;
+ }
+
+ // read payload element
+ if (nodes.get(0).getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement node = (OMElement) nodes.get(0);
+
+ if (node.getText() != null) {
+ StringBuilder payload = new StringBuilder(node.getText());
+ serviceCtxt.setPayload(payload);
+
+ }
+
+ }
+
+ }
+
+ private static List<Cartridge>
+ extractCartridges(String file, OMElement elt) throws MalformedConfigurationFileException {
+
+ FasterLookUpDataHolder dataHolder = FasterLookUpDataHolder.getInstance();
+
+ List<IaasProvider> iaasProviders = dataHolder.getIaasProviders();
+
+ if (iaasProviders == null) {
+ dataHolder.setIaasProviders((iaasProviders = new ArrayList<IaasProvider>()));
+ }
+
+ List<Cartridge> cartridges = new ArrayList<Cartridge>();
+
+ String xpath = CloudControllerConstants.CARTRIDGES_ELEMENT_XPATH;
+
+ // cartridges can be found from this XPATH
+ List<?> cartridgeNodes = AxiomXpathParserUtil.getMatchingNodes(xpath, elt);
+
+ if (cartridgeNodes == null || cartridgeNodes.isEmpty()) {
+ // or from this XPATH
+ xpath = CloudControllerConstants.CARTRIDGE_ELEMENT_XPATH;
+ cartridgeNodes = AxiomXpathParserUtil.getMatchingNodes(xpath, elt);
+ }
+
+ if (cartridgeNodes == null || cartridgeNodes.isEmpty()) {
+ log.warn("No cartridge found in this configuration file : " + file);
+ return cartridges;
+ }
+
+ for (Object obj : cartridgeNodes) {
+
+ if (obj instanceof OMNode) {
+ OMNode cartridgeNode = (OMNode) obj;
+
+ if (cartridgeNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement cartridgeElement = (OMElement) cartridgeNode;
+
+ // retrieve Attributes of a Cartridge definition
+ String type =
+ cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.TYPE_ATTR));
+ String host =
+ cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.HOST_ATTR));
+ String provider =
+ cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.PROVIDER_ATTR));
+
+ String version =
+ cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.VERSION_ATTR));
+
+ boolean multiTenant =
+ Boolean.valueOf(cartridgeElement.getAttributeValue(new QName(
+ CloudControllerConstants.MULTI_TENANT_ATTR)));
+
+ Cartridge aCartridge;
+
+ if ((aCartridge = dataHolder.getCartridge(type)) == null) {
+
+ aCartridge = new Cartridge(type, host, provider, version, multiTenant);
+ }
+
+ // read displayName
+ Iterator<?> itName =
+ cartridgeElement.getChildrenWithName(new QName(
+ CloudControllerConstants.DISPLAY_NAME_ELEMENT));
+
+ if (itName.hasNext()) {
+ OMElement name = (OMElement) itName.next();
+
+ aCartridge.setDisplayName(name.getText());
+ }
+
+ // read description
+ Iterator<?> it =
+ cartridgeElement.getChildrenWithName(new QName(
+ CloudControllerConstants.DESCRIPTION_ELEMENT));
+
+ if (it.hasNext()) {
+ OMElement desc = (OMElement) it.next();
+
+ aCartridge.setDescription(desc.getText());
+ }
+
+ // load properties of this cartridge
+ IaasProviderConfigParser.loadProperties(file, cartridgeElement,
+ aCartridge.getProperties());
+
+ // retrieve the list of IaaS providers
+ List<?> iaasProviderNodes =
+ AxiomXpathParserUtil.getMatchingNodes(xpath +
+ CloudControllerConstants.IAAS_PROVIDER_ELEMENT_XPATH,
+ cartridgeElement);
+
+ getIaasProviders(file, elt, iaasProviders, cartridgeElement.toString(),
+ aCartridge, iaasProviderNodes);
+
+ // load dirs
+ List<?> deploymentNodes =
+ AxiomXpathParserUtil.getMatchingNodes(xpath +
+ CloudControllerConstants.DEPLOYMENT_ELEMENT_XPATH,
+ cartridgeElement);
+ setDeploymentDirs(file, cartridgeElement.toString(), aCartridge,
+ deploymentNodes);
+
+ // load port mappings
+ List<?> portMappingNodes =
+ AxiomXpathParserUtil.getMatchingNodes(xpath +
+ CloudControllerConstants.PORT_MAPPING_ELEMENT_XPATH,
+ cartridgeElement);
+ getPortMappings(file, cartridgeElement.toString(), aCartridge, portMappingNodes);
+
+ // load appTypes
+ List<?> appTypesNodes =
+ AxiomXpathParserUtil.getMatchingNodes(xpath +
+ CloudControllerConstants.APP_TYPES_ELEMENT_XPATH,
+ cartridgeElement);
+ getAppTypes(file, cartridgeElement.toString(), aCartridge, appTypesNodes);
+
+ cartridges.add(aCartridge);
+
+ if (dataHolder.getCartridge(type) == null) {
+ dataHolder.addCartridge(aCartridge);
+ }
+ }
+ }
+ }
+
+ return cartridges;
+ }
+
+ /**
+ * @param iaasProviders
+ * @param cartridgeElementString
+ * @param aCartridge
+ * @param iaasProviderNodes
+ */
+ private static void getIaasProviders(final String fileName, final OMElement elt,
+ List<IaasProvider> iaasProviders, String cartridgeElementString, Cartridge aCartridge,
+ List<?> iaasProviderNodes) {
+ for (Object nodeObj : iaasProviderNodes) {
+ if (nodeObj instanceof OMNode) {
+ OMNode iaasProviderNode = (OMNode) nodeObj;
+
+ if (iaasProviderNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement iaasElt = (OMElement) iaasProviderNode;
+
+ // add the IaasProvider to this cartridge
+ aCartridge.addIaasProvider(IaasProviderConfigParser.getIaasProvider(fileName,
+ elt,
+ iaasElt,
+ iaasProviders));
+
+ } else {
+ String msg =
+ "Essential '" + CloudControllerConstants.IAAS_PROVIDER_ELEMENT +
+ "' element cannot" + " be found in " +
+ cartridgeElementString + " of " + fileName;
+ handleException(msg);
+ }
+
+ }
+ }
+ }
+
+ /**
+ * @param cartridgeElementString
+ * Cartridges section as a {@link String}
+ * @param aCartridge
+ * {@link Cartridge} instance.
+ * @param deploymentNodes
+ * list of deployment directory nodes
+ */
+ private static void setDeploymentDirs(String fileName, String cartridgeElementString,
+ Cartridge aCartridge, List<?> deploymentNodes) {
+ Object nodeObj;
+ if ((nodeObj = deploymentNodes.get(0)) instanceof OMNode) {
+ OMNode deploymentNode = (OMNode) nodeObj;
+
+ if (deploymentNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement deployElt = (OMElement) deploymentNode;
+
+ if (deployElt.getAttributeValue(new QName(CloudControllerConstants.BASE_DIR_ATTR)) != null) {
+
+ aCartridge.setBaseDir(deployElt.getAttributeValue(new QName(
+ CloudControllerConstants.BASE_DIR_ATTR)));
+ }
+
+ for (Iterator<?> iterator =
+ deployElt.getChildrenWithName(new QName(
+ CloudControllerConstants.DIRECTORY_ELEMENT)); iterator.hasNext();) {
+ OMElement dir = (OMElement) iterator.next();
+ aCartridge.addDeploymentDir(dir.getText());
+ }
+
+ } else {
+ String msg =
+ "Essential '" + CloudControllerConstants.DEPLOYMENT_ELEMENT +
+ "' element cannot" + " be found in " + cartridgeElementString +
+ " of " + fileName;
+ handleException(msg);
+ }
+
+ }
+ }
+
+ /**
+ * @param cartridgeElementString
+ * Cartridges section as a {@link String}
+ * @param aCartridge
+ * {@link Cartridge} instance.
+ * @param portMappingNodes
+ * nodes of port mapping elements
+ */
+ private static void getPortMappings(final String fileName, String cartridgeElementString,
+ Cartridge aCartridge, List<?> portMappingNodes) {
+ Object nodeObj;
+ if (!portMappingNodes.isEmpty()) {
+ if ((nodeObj = portMappingNodes.get(0)) instanceof OMNode) {
+ OMNode portMappingNode = (OMNode) nodeObj;
+
+ if (portMappingNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement portMappingElt = (OMElement) portMappingNode;
+
+ for (Iterator<?> iterator =
+ portMappingElt.getChildrenWithName(new QName(
+ CloudControllerConstants.HTTP_ELEMENT)); iterator.hasNext();) {
+ OMElement httpElt = (OMElement) iterator.next();
+
+ String port =
+ httpElt.getAttributeValue(new QName(
+ CloudControllerConstants.PORT_ATTR));
+ String proxyPort =
+ httpElt.getAttributeValue(new QName(
+ CloudControllerConstants.PROXY_PORT_ATTR));
+
+ PortMapping mapping =
+ new PortMapping(
+ CloudControllerConstants.HTTP_ELEMENT,
+ port, proxyPort);
+
+ aCartridge.addPortMapping(mapping);
+ }
+
+ for (Iterator<?> iterator =
+ portMappingElt.getChildrenWithName(new QName(
+ CloudControllerConstants.HTTPS_ELEMENT)); iterator.hasNext();) {
+ OMElement httpsElt = (OMElement) iterator.next();
+
+ String port =
+ httpsElt.getAttributeValue(new QName(
+ CloudControllerConstants.PORT_ATTR));
+ String proxyPort =
+ httpsElt.getAttributeValue(new QName(
+ CloudControllerConstants.PROXY_PORT_ATTR));
+
+ PortMapping mapping =
+ new PortMapping(
+ CloudControllerConstants.HTTPS_ELEMENT,
+ port, proxyPort);
+
+ aCartridge.addPortMapping(mapping);
+ }
+
+ } else {
+ String msg =
+ "Essential '" + CloudControllerConstants.PORT_MAPPING_ELEMENT +
+ "' element cannot" + " be found in " +
+ cartridgeElementString + " of " + fileName;
+ handleException(msg);
+ }
+
+ }
+ }
+ }
+
+ /**
+ * @param cartridgeElementString
+ * Cartridges section as a {@link String}
+ * @param aCartridge
+ * {@link org.apache.stratos.cloud.controller.util.Cartridge} instance.
+ * @param appTypesNodes
+ * nodes of App types.
+ */
+ private static void getAppTypes(final String fileName, String cartridgeElementString,
+ Cartridge aCartridge, List<?> appTypesNodes) {
+ Object nodeObj;
+ if (!appTypesNodes.isEmpty()) {
+ if ((nodeObj = appTypesNodes.get(0)) instanceof OMNode) {
+ OMNode appTypeNode = (OMNode) nodeObj;
+
+ if (appTypeNode.getType() == OMNode.ELEMENT_NODE) {
+
+ OMElement appTypesElt = (OMElement) appTypeNode;
+
+ for (Iterator<?> iterator =
+ appTypesElt.getChildrenWithName(new QName(
+ CloudControllerConstants.APP_TYPE_ELEMENT)); iterator.hasNext();) {
+ OMElement appElt = (OMElement) iterator.next();
+
+ String name =
+ appElt.getAttributeValue(new QName(
+ CloudControllerConstants.NAME_ATTR));
+ String appSpecificMapping =
+ appElt.getAttributeValue(new QName(
+ CloudControllerConstants.APP_SPECIFIC_MAPPING_ATTR));
+
+ AppType appType;
+
+ if (appSpecificMapping == null) {
+ appType = new AppType(name);
+ } else {
+ appType = new AppType(name, Boolean.valueOf(appSpecificMapping));
+ }
+
+ aCartridge.addAppType(appType);
+ }
+
+ } else {
+ String msg =
+ "Essential '" + CloudControllerConstants.APP_TYPE_ELEMENT +
+ "' element cannot" + " be found in " +
+ cartridgeElementString + " of " + fileName;
+ handleException(msg);
+ }
+
+ }
+ }
+ }
+
+ private static void
+ handleException(final String msg) throws MalformedConfigurationFileException {
+ log.error(msg);
+ throw new MalformedConfigurationFileException(msg);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/1654262f/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/ThreadExecutor.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/ThreadExecutor.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/ThreadExecutor.java
index 984dd11..82c6d12 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/ThreadExecutor.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/ThreadExecutor.java
@@ -27,8 +27,8 @@ import java.util.concurrent.Executors;
public class ThreadExecutor {
private ExecutorService executor;
- public ThreadExecutor(int poolSize) {
- executor = Executors.newFixedThreadPool(poolSize);
+ public ThreadExecutor() {
+ executor = Executors.newCachedThreadPool();
}
public void execute(Runnable job){
http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/1654262f/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/deployers/CartridgeDeployer.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/deployers/CartridgeDeployer.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/deployers/CartridgeDeployer.java
index 376e44b..863d508 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/deployers/CartridgeDeployer.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/deployers/CartridgeDeployer.java
@@ -18,6 +18,7 @@
*/
package org.apache.stratos.cloud.controller.deployers;
+import org.apache.axiom.om.OMElement;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.deployment.AbstractDeployer;
import org.apache.axis2.deployment.DeploymentException;
@@ -25,8 +26,11 @@ import org.apache.axis2.deployment.repository.util.DeploymentFileData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stratos.cloud.controller.axiom.AxiomXpathParser;
+import org.apache.stratos.cloud.controller.axiom.AxiomXpathParserUtil;
+import org.apache.stratos.cloud.controller.axiom.parser.CartridgeConfigParser;
import org.apache.stratos.cloud.controller.concurrent.ThreadExecutor;
import org.apache.stratos.cloud.controller.exception.CloudControllerException;
+import org.apache.stratos.cloud.controller.exception.MalformedConfigurationFileException;
import org.apache.stratos.cloud.controller.interfaces.Iaas;
import org.apache.stratos.cloud.controller.runtime.FasterLookUpDataHolder;
import org.apache.stratos.cloud.controller.topology.TopologyBuilder;
@@ -68,33 +72,36 @@ public class CartridgeDeployer extends AbstractDeployer{
public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException {
log.debug("Started to deploy the deployment artifact: "+deploymentFileData.getFile());
-
- AxiomXpathParser parser = new AxiomXpathParser(deploymentFileData.getFile());
- parser.parse();
try {
+ OMElement docElt = AxiomXpathParserUtil.parse(deploymentFileData.getFile());
+ String fileName = deploymentFileData.getFile().getAbsolutePath();
+
// validate
- validateCartridge(parser);
+ validateCartridge(docElt, fileName);
// deploy - grab cartridges
- List<Cartridge> cartridges = parser.getCartridgesList();
+ List<Cartridge> cartridges = CartridgeConfigParser.parse(fileName, docElt);
- ThreadExecutor exec = new ThreadExecutor(3);
+ // update map
+ fileToCartridgeListMap.put(deploymentFileData.getAbsolutePath(),
+ new ArrayList<Cartridge>(cartridges));
+
+ ThreadExecutor exec = new ThreadExecutor();
// create Jclouds objects, for each IaaS
for (Cartridge cartridge : cartridges) {
// jclouds object building is time consuming, hence I use Java executor framework
exec.execute(new JcloudsObjectBuilder(cartridge, deploymentFileData));
}
+ // wait till the jobs finish.
exec.shutdown();
- // update map
- fileToCartridgeListMap.put(deploymentFileData.getAbsolutePath(),
- new ArrayList<Cartridge>(cartridges));
+ TopologyBuilder.handleServiceCreated(cartridges);
log.info("Successfully deployed the Cartridge definition specified at " + deploymentFileData.getAbsolutePath());
- TopologyBuilder.handleServiceCreated(cartridges);
+
} catch (Exception e) {
String msg = "Invalid deployment artefact at "+deploymentFileData.getAbsolutePath();
- // back up the file
+ // back up the file - this will in-turn triggers undeploy()
File f = deploymentFileData.getFile();
f.renameTo(new File(deploymentFileData.getAbsolutePath()+".back"));
log.error(msg, e);
@@ -102,13 +109,13 @@ public class CartridgeDeployer extends AbstractDeployer{
}
}
- private void validateCartridge(AxiomXpathParser parser) throws Exception {
+ private void validateCartridge(final OMElement elt, final String fileName) throws MalformedConfigurationFileException {
boolean validated = false;
Exception firstException = null;
try{
// first try to validate using cartridges schema
- parser.validate(cartridgesSchema);
+ AxiomXpathParserUtil.validate(elt, cartridgesSchema);
validated = true;
}catch (Exception e) {
@@ -118,14 +125,14 @@ public class CartridgeDeployer extends AbstractDeployer{
if(!validated){
try{
// Now try to validate using cartridge schema
- parser.validate(cartridgeSchema);
+ AxiomXpathParserUtil.validate(elt, cartridgeSchema);
validated = true;
log.debug("Cartridge validation was successful.");
}catch (Exception e) {
- String msg = "Cartridge XML validation failed. Invalid Cartridge XML: "+parser.getXmlSource().getAbsolutePath();
+ String msg = "Cartridge XML validation failed. Invalid Cartridge XML: "+fileName;
log.error(msg, firstException);
- throw firstException;
+ throw new MalformedConfigurationFileException(msg, firstException);
}
}