You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by wo...@apache.org on 2010/09/22 16:15:49 UTC

svn commit: r999962 - in /incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm: IBMDriver.java IBMHandler.java

Author: woodser
Date: Wed Sep 22 14:15:49 2010
New Revision: 999962

URL: http://svn.apache.org/viewvc?rev=999962&view=rev
Log:
Updates to the IBM SBC Driver.
- Use XPath to parse XML for consistency with Amazon EC2 & Rackspace
- Overloaded constructor for custom hosts

Modified:
    incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMDriver.java
    incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMHandler.java

Modified: incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMDriver.java
URL: http://svn.apache.org/viewvc/incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMDriver.java?rev=999962&r1=999961&r2=999962&view=diff
==============================================================================
--- incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMDriver.java (original)
+++ incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMDriver.java Wed Sep 22 14:15:49 2010
@@ -5,6 +5,11 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
 import libcloud.NodeDriver;
 import libcloud.data.Node;
 import libcloud.data.NodeAuth;
@@ -22,20 +27,53 @@ import base.connection.ConnectionKey;
 import base.interfaces.IResponse;
 
 /*
- * TODO: comment all classes
- * TODO: overload constructor with userId, key, host, and port
- * TODO: listImages(NodeLocation)
- * TODO: listSizes(NodeLocation)
- * TODO: update to use XPath -- See Amazon EC2 Driver
+ * Driver for the IBM Smarter Business Development & Test Cloud.
  */
 public class IBMDriver extends NodeDriver {
 
 	private static final String REST_BASE = "/computecloud/enterprise/api/rest/20100331";
 
 	private static final Map<Integer, NodeState> NODE_STATE_MAP = new HashMap<Integer, NodeState>();
+	
+	private XPath xpath;
 
+	/*
+	 * Constructs the driver with the given credentials.
+	 * 
+	 * @param userId is the account holder's user ID with the IBM SBC
+	 * @param key is the account holder's password with the IBM SBC
+	 */
 	public IBMDriver(String userId, String key) {
 		connection = new IBMConnection(userId, key, this);
+		xpath = XPathFactory.newInstance().newXPath();
+
+		// Map IBM instance states to libcloud
+		NODE_STATE_MAP.put(0, NodeState.PENDING);
+		NODE_STATE_MAP.put(1, NodeState.PENDING);
+		NODE_STATE_MAP.put(2, NodeState.TERMINATED);
+		NODE_STATE_MAP.put(3, NodeState.TERMINATED);
+		NODE_STATE_MAP.put(4, NodeState.TERMINATED);
+		NODE_STATE_MAP.put(5, NodeState.RUNNING);
+		NODE_STATE_MAP.put(6, NodeState.UNKNOWN);
+		NODE_STATE_MAP.put(7, NodeState.PENDING);
+		NODE_STATE_MAP.put(8, NodeState.REBOOTING);
+		NODE_STATE_MAP.put(9, NodeState.PENDING);
+		NODE_STATE_MAP.put(10, NodeState.PENDING);
+		NODE_STATE_MAP.put(11, NodeState.TERMINATED);
+		NODE_STATE_MAP.put(12, NodeState.PENDING);	// Deprovision pending
+		NODE_STATE_MAP.put(13, NodeState.PENDING);	// Restart pending
+	}
+	
+	/*
+	 * Constructs the driver with the given credentials and host.
+	 * 
+	 * @param userId is the account holder's user ID with the IBM SBC
+	 * @param key is the account holder's password with the IBM SBC
+	 * @param host is the IBM SBC host
+	 */
+	public IBMDriver(String userId, String key, String host) {
+		connection = new IBMConnection(userId, key, true, host, -1, this);
+		xpath = XPathFactory.newInstance().newXPath();
 
 		// Map IBM instance states to libcloud
 		NODE_STATE_MAP.put(0, NodeState.PENDING);
@@ -134,96 +172,64 @@ public class IBMDriver extends NodeDrive
 	// ----------------------------- PRIVATE HELPERS ---------------------------
 
 	private List<NodeImage> parseImages(Document doc) {
-		List<NodeImage> images = new ArrayList<NodeImage>();
-		List<Element> elems = getFirstElementsByTagName(doc
-				.getDocumentElement(), "Image");
-		for (Element elem : elems) {
-			images.add(parseImage(elem));
-		}
-		return images;
+		try {
+			List<NodeImage> images = new ArrayList<NodeImage>();
+			NodeList imageNodes = (NodeList)xpath.evaluate("Image", doc.getDocumentElement(), XPathConstants.NODESET);
+			for (int i = 0; i < imageNodes.getLength(); i++) {
+				images.add(parseImage((Element)imageNodes.item(i)));
+			}
+			return images;
+		} catch (XPathExpressionException e) {
+			e.printStackTrace();
+			return null;
+		}
 	}
 
-	private NodeImage parseImage(Element elem) {
-		return new NodeImage(getTextValue(elem, "ID"),
-				getTextValue(elem, "Name"),
-				null, // TODO: include parametersUrl in extra
-				this);
+	private NodeImage parseImage(Element elem) throws XPathExpressionException {
+		return new NodeImage(xpath.evaluate("ID", elem), xpath.evaluate("Name", elem), null, this);
 	}
 
 	private List<INode> parseNodes(Document doc) {
-		List<INode> nodes = new ArrayList<INode>();
-		List<Element> elems = getFirstElementsByTagName(doc.getDocumentElement(), "Instance");
-		for (Element elem : elems) {
-			nodes.add(parseNode(elem));
-		}
-		return nodes;
+		try {
+			List<INode> nodes = new ArrayList<INode>();
+			NodeList instanceNodes = (NodeList)xpath.evaluate("Instance", doc.getDocumentElement(), XPathConstants.NODESET);
+			for (int i = 0; i < instanceNodes.getLength(); i++) {
+				nodes.add(parseNode((Element)instanceNodes.item(i)));
+			}
+			return nodes;
+		} catch (XPathExpressionException e) {
+			e.printStackTrace();
+			return null;
+		}
 	}
 
-	private INode parseNode(Element elem) {
-		return new Node(getTextValue(elem, "ID"),
-				getTextValue(elem, "Name"),
-				NODE_STATE_MAP.get(new Integer(getTextValue(elem, "Status"))),
-				getTextValue(elem, "IP"),
+	private INode parseNode(Element elem) throws XPathExpressionException {
+		String ip = xpath.evaluate("IP", elem).trim();
+		ip = ip.isEmpty() ? null : ip;
+		return new Node(xpath.evaluate("ID", elem),
+				xpath.evaluate("Name", elem),
+				NODE_STATE_MAP.get(new Integer(xpath.evaluate("Status", elem))),
+				ip,
 				null,
 				null,
 				this);
 	}
 
 	private List<NodeLocation> parseLocations(Document doc) {
-		List<NodeLocation> locations = new ArrayList<NodeLocation>();
-		List<Element> elems = getFirstElementsByTagName(doc
-				.getDocumentElement(), "Location");
-		for (Element elem : elems) {
-			locations.add(parseLocation(elem));
-		}
-		return locations;
-	}
-
-	private NodeLocation parseLocation(Element elem) {
-		return new NodeLocation(getTextValue(elem, "ID"), getTextValue(elem, "Name"), "US", this);
-	}
-	
-	/*
-	 * Utility function to extract the text value of the given element.
-	 * 
-	 * @param elem is the element to find the tag in
-	 * @param tagName is the name of the tag to find the value for
-	 * 
-	 * @return String is the text value of the given tag
-	 */
-	public static String getTextValue(Element elem, String tagName) {
-		NodeList nl = elem.getElementsByTagName(tagName);
-		if (nl.getLength() > 0) {
-			if (nl.item(0).getFirstChild() == null) {
-				return "";
-			} else {
-				return nl.item(0).getFirstChild().getNodeValue();
+		try {
+			List<NodeLocation> locations = new ArrayList<NodeLocation>();
+			NodeList locationNodes = (NodeList)xpath.evaluate("Location", doc.getDocumentElement(), XPathConstants.NODESET);
+			for (int i = 0; i < locationNodes.getLength(); i++) {
+				locations.add(parseLocation((Element)locationNodes.item(i)));
 			}
-		} else {
+			return locations;
+		} catch (XPathExpressionException e) {
+			e.printStackTrace();
 			return null;
 		}
 	}
 
-	/*
-	 * Utility function that returns a list of immediate children to the given
-	 * Element and that matches the given tagName.
-	 * 
-	 * TODO: Use XPath instead (see Amazon EC2 driver)
-	 * 
-	 * @param elem is the parent of the elements to search
-	 * @param tagName identifies the elements to find
-	 * 
-	 * @return List<Element> is a list of immediate children matching tagName
-	 */
-	public static List<Element> getFirstElementsByTagName(Element elem, String tagName) {
-		List<Element> elements = new ArrayList<Element>();
-		NodeList children = elem.getChildNodes();
-		for (int i = 0; i < children.getLength(); i++) {
-			Element child = (Element) children.item(i);
-			if (child.getTagName().equals(tagName)) {
-				elements.add(child);
-			}
-		}
-		return elements;
+	private NodeLocation parseLocation(Element elem) throws XPathExpressionException {
+		return new NodeLocation(xpath.evaluate("ID", elem), xpath.evaluate("Name", elem), "US", this);
 	}
 }

Modified: incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMHandler.java
URL: http://svn.apache.org/viewvc/incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMHandler.java?rev=999962&r1=999961&r2=999962&view=diff
==============================================================================
--- incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMHandler.java (original)
+++ incubator/libcloud/sandbox/java/trunk/src/libcloud/providers/ibm/IBMHandler.java Wed Sep 22 14:15:49 2010
@@ -1,7 +1,6 @@
 package libcloud.providers.ibm;
 
 import java.io.IOException;
-import java.io.StringReader;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -9,7 +8,6 @@ import javax.xml.parsers.ParserConfigura
 
 import libcloud.exceptions.InvalidCredsException;
 
-import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 
 import base.connection.ResponseHandler;
@@ -26,7 +24,7 @@ public class IBMHandler extends Response
 		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 		try {
 			DocumentBuilder db = dbf.newDocumentBuilder();
-			object = db.parse(new InputSource(new StringReader(response.getBody())));
+			object = db.parse(response.getItem().getContent());
 		} catch (ParserConfigurationException e) {
 			e.printStackTrace();
 		} catch (SAXException e) {