[jbosstools-commits] JBoss Tools SVN: r23731 - in trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core: src/org/jboss/tools/deltacloud/core and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Mon Jul 26 13:46:08 EDT 2010


Author: jjohnstn
Date: 2010-07-26 13:46:07 -0400 (Mon, 26 Jul 2010)
New Revision: 23731

Added:
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/API.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/AddressList.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientException.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudObject.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HardwareProfile.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Image.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Instance.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Realm.java
Modified:
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudImage.java
   trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java
Log:
2010-07-26  Jeff Johnston  <jjohnstn at redhat.com>

	* src/org/jboss/tools/deltacloud/core/client/AddressList.java: New file.
	* src/org/jboss/tools/deltacloud/core/client/API.java: New file.
	* src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java: New file.
	* src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientException.java: New file.
	* src/org/jboss/tools/deltacloud/core/client/DeltaCloudObject.java: New file.
	* src/org/jboss/tools/deltacloud/core/client/HardwareProfile.java: New file.
	* src/org/jboss/tools/deltacloud/core/client/Image.java: New file.
	* src/org/jboss/tools/deltacloud/core/client/Instance.java: New file.
	* src/org/jboss/tools/deltacloud/core/client/Realm.java: New file.
	* src/org/jboss/tools/deltacloud/core/DeltaCloud.java: Import classes from new
	client package.
	* src/org/jboss/tools/deltacloud/core/DeltaCloudImage.java: Ditto. 
	* src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java (getProfileId): New
	method. 



Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog	2010-07-26 17:43:41 UTC (rev 23730)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog	2010-07-26 17:46:07 UTC (rev 23731)
@@ -1,3 +1,20 @@
+2010-07-26  Jeff Johnston  <jjohnstn at redhat.com>
+
+	* src/org/jboss/tools/deltacloud/core/client/AddressList.java: New file.
+	* src/org/jboss/tools/deltacloud/core/client/API.java: New file.
+	* src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java: New file.
+	* src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientException.java: New file.
+	* src/org/jboss/tools/deltacloud/core/client/DeltaCloudObject.java: New file.
+	* src/org/jboss/tools/deltacloud/core/client/HardwareProfile.java: New file.
+	* src/org/jboss/tools/deltacloud/core/client/Image.java: New file.
+	* src/org/jboss/tools/deltacloud/core/client/Instance.java: New file.
+	* src/org/jboss/tools/deltacloud/core/client/Realm.java: New file.
+	* src/org/jboss/tools/deltacloud/core/DeltaCloud.java: Import classes from new
+	client package.
+	* src/org/jboss/tools/deltacloud/core/DeltaCloudImage.java: Ditto. 
+	* src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java (getProfileId): New
+	method. 
+
 2010-07-23  Jeff Johnston  <jjohnstn at redhat.com>
 
 	* .classpath: New file.

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java	2010-07-26 17:43:41 UTC (rev 23730)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloud.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -7,10 +7,10 @@
 import java.util.List;
 
 import org.eclipse.core.runtime.ListenerList;
-import org.jboss.deltacloud.client.DeltaCloudClient;
-import org.jboss.deltacloud.client.DeltaCloudClientException;
-import org.jboss.deltacloud.client.Image;
-import org.jboss.deltacloud.client.Instance;
+import org.jboss.tools.deltacloud.core.client.DeltaCloudClient;
+import org.jboss.tools.deltacloud.core.client.DeltaCloudClientException;
+import org.jboss.tools.deltacloud.core.client.Image;
+import org.jboss.tools.deltacloud.core.client.Instance;
 
 public class DeltaCloud {
 	

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudImage.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudImage.java	2010-07-26 17:43:41 UTC (rev 23730)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudImage.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -1,7 +1,8 @@
 package org.jboss.tools.deltacloud.core;
 
-import org.jboss.deltacloud.client.Image;
+import org.jboss.tools.deltacloud.core.client.Image;
 
+
 public class DeltaCloudImage {
 
 	private Image image;

Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java	2010-07-26 17:43:41 UTC (rev 23730)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/DeltaCloudInstance.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -2,8 +2,9 @@
 
 import java.util.List;
 
-import org.jboss.deltacloud.client.Instance;
+import org.jboss.tools.deltacloud.core.client.Instance;
 
+
 public class DeltaCloudInstance {
 
 	private Instance instance;
@@ -24,8 +25,8 @@
 		return instance.getState().toString();
 	}
 	
-	public String getFlavorId() {
-		return instance.getFlavorId();
+	public String getProfileId() {
+		return instance.getProfileId();
 	}
 	
 	public String getRealmId() {

Added: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/API.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/API.java	                        (rev 0)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/API.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -0,0 +1,109 @@
+package org.jboss.tools.deltacloud.core.client;
+
+import java.util.List;
+
+public interface API 
+{
+	/**
+	 * Returns a list of Delta Cloud Realms
+	 * @return List of Delta Cloud Realms
+	 * @throws DeltaCloudClientException
+	 */
+	public List<Realm> listRealms() throws DeltaCloudClientException;
+	
+	/**
+	 * Returns a single Delta Cloud Realm given its ID
+	 * @param realmId
+	 * @return Delta Cloud Realm
+	 * @throws DeltaCloudClientException
+	 */
+	public Realm listRealms(String realmId) throws DeltaCloudClientException;
+	
+	public List<HardwareProfile> listProfiles() throws DeltaCloudClientException;
+	
+	/**
+	 * Returns a Delta Cloud Flavors
+	 * @param flavorId
+	 * @return Delta Cloud Flavor
+	 * @throws DeltaCloudClientException
+	 */
+	public HardwareProfile listProfile(String profileId) throws DeltaCloudClientException;
+	
+	/**
+	 * Returns a List of Delta Cloud Images 
+	 * @return List of Delta Cloud Images
+	 * @throws DeltaCloudClientException
+	 */
+	public List<Image> listImages() throws DeltaCloudClientException;
+	
+	/**
+	 * Returns a Delta Cloud Image given its ID
+	 * @param imageId
+	 * @return
+	 * @throws DeltaCloudClientException
+	 */
+	public Image listImages(String imageId) throws DeltaCloudClientException;
+	
+	/**
+	 * Returns a list of all Instances from the Delta Cloud Provider
+	 * @return
+	 * @throws DeltaCloudClientException
+	 */
+	public List<Instance> listInstances() throws DeltaCloudClientException;
+	
+	/**
+	 * Returns an Instance from the Delta Cloud Provider given on the Instances ID
+	 * @param instanceId
+	 * @return
+	 * @throws DeltaCloudClientException
+	 */
+	public Instance listInstances(String instanceId) throws DeltaCloudClientException;
+	
+	/**
+	 * Creates a new Delta Cloud Instance based on the Image specified by the Image ID.  Default parameters are used for the
+	 * Flavor, Realm and Name.  These are specified by the Delta Cloud Provider
+	 * @param imageId 
+	 * @return The newly created Delta Cloud Instance
+	 * @throws DeltaCloudClientException
+	 */
+	public Instance createInstance(String imageId) throws DeltaCloudClientException;
+	
+	/**
+	 * Creates a new Delta Cloud Instance, the instance will be based on the Image specified by the instance ID.  It will be of 
+	 * type flavor and in the location realm
+	 * @param imageId
+	 * @param flavor
+	 * @param realm
+	 * @param name
+	 * @return
+	 * @throws DeltaCloudClientException
+	 */
+	public Instance createInstance(String imageId, String flavor, String realm, String name) throws DeltaCloudClientException;
+	
+	/**
+	 * Starts a previously stopped Instance given the Instance ID
+	 * @param instanceId
+	 * @throws DeltaCloudClientException
+	 */
+	public void startInstance(String instanceId) throws DeltaCloudClientException;
+	
+	/**
+	 * Reboots a running Instance
+	 * @param instanceId
+	 * @throws DeltaCloudClientException
+	 */
+	public void rebootInstance(String instanceId) throws DeltaCloudClientException;
+	
+	/** Shuts down a running instance
+	 * @param instanceId
+	 * @throws DeltaCloudClientException
+	 */
+	public void shutdownInstance(String instanceId) throws DeltaCloudClientException;
+	
+	/** Destroys a previously Stopped Instance
+	 * 
+	 * @param instanceId
+	 * @throws DeltaCloudClientException
+	 */
+	public void destroyInstance(String instanceId) throws DeltaCloudClientException;
+}

Added: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/AddressList.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/AddressList.java	                        (rev 0)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/AddressList.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -0,0 +1,31 @@
+package org.jboss.tools.deltacloud.core.client;
+
+import java.util.List;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlList;
+import javax.xml.bind.annotation.XmlType;
+
+ at XmlType
+public class AddressList
+{
+	private static final long serialVersionUID = 1L;
+	
+	private AddressList()
+	{
+	}
+	
+	@XmlElement
+	@XmlList
+	private List<String> address;
+	
+	@SuppressWarnings("unused")
+	private void setAddress(List<String> address)
+	{
+		this.address = address;
+	}
+
+	public List<String> getAddress()
+	{
+		return address;
+	}
+}

Added: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java	                        (rev 0)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -0,0 +1,391 @@
+package org.jboss.tools.deltacloud.core.client;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.bind.JAXB;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+
+public class DeltaCloudClient implements API 
+{
+	public static Logger logger = Logger.getLogger(DeltaCloudClient.class);
+	
+	private static enum DCNS
+	{ 
+		INSTANCES, REALMS, IMAGES, HARDWARE_PROFILES, START, STOP, REBOOT, DESTROY;
+		
+		@Override
+		public String toString()
+		{
+			return "/" + name().toLowerCase();
+		}
+	} 
+	
+	private static enum RequestType { POST, GET };
+	
+	private URL baseUrl;
+	
+	private String username;
+	
+	private String password;
+	
+	public DeltaCloudClient(URL url, String username, String password) throws MalformedURLException
+	{
+		
+		logger.debug("Creating new Delta Cloud Client for Server: " + url);
+		
+		this.baseUrl = url;
+		
+		this.username = username;
+		
+		this.password = password;
+	}
+
+	private String sendRequest(String path, RequestType requestType) throws DeltaCloudClientException
+	{
+		DefaultHttpClient httpClient = new DefaultHttpClient();
+        httpClient.getCredentialsProvider().setCredentials(new AuthScope(baseUrl.getHost(), baseUrl.getPort()), new UsernamePasswordCredentials(username, password));
+        
+		String requestUrl = baseUrl.toString() + path;
+		logger.debug("Sending Request to: " + requestUrl);
+		
+		try
+		{
+			HttpUriRequest request = null;
+			if(requestType == RequestType.POST)
+			{
+				request = new HttpPost(requestUrl);
+			}
+			else
+			{
+				request = new HttpGet(requestUrl);
+			}
+			
+			request.setHeader("Accept", "application/xml");
+			HttpResponse httpResponse = httpClient.execute(request);
+			
+			HttpEntity entity = httpResponse.getEntity();
+			
+			
+			if (entity != null)
+			{
+				InputStream is = entity.getContent();
+				String xml = readInputStreamToString(is);
+				httpClient.getConnectionManager().shutdown();
+				
+				logger.debug("Response\n" + xml);
+				return xml;
+			}
+		}
+		catch(IOException e)
+		{
+			logger.error("Error processing request to: " + requestUrl, e);
+			throw new DeltaCloudClientException("Error processing request to: " + requestUrl, e);
+		}
+		throw new DeltaCloudClientException("Could not execute request to:" + requestUrl);
+	}
+	
+	private static String readInputStreamToString(InputStream is) throws DeltaCloudClientException
+	{
+		try
+		{
+			try
+			{
+				if (is != null)
+				{
+					StringBuilder sb = new StringBuilder();
+					String line;
+					
+					BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+					while ((line = reader.readLine()) != null) 
+					{
+						sb.append(line).append("\n");	
+					}
+					return sb.toString();
+				}
+			}
+			finally
+			{
+				is.close();
+			}
+		}
+		catch(Exception e)
+		{
+			throw new DeltaCloudClientException("Error converting Response to String", e);
+		}
+		return "";
+	}
+	
+	@Override
+	public Instance createInstance(String imageId) throws DeltaCloudClientException 
+	{
+		String query = "?image_id=" + imageId;
+		return buildInstance(sendRequest(DCNS.INSTANCES + query, RequestType.POST));
+	}
+	
+	@Override
+	public Instance createInstance(String imageId, String flavorId, String realmId, String name) throws DeltaCloudClientException 
+	{
+		String query = "?image_id=" + imageId + "&flavor_id=" + flavorId + "&realm_id=" + realmId + "&name=" + name + "&commit=create";
+		return buildInstance(sendRequest(DCNS.INSTANCES + query, RequestType.POST));
+	}
+
+	@Override
+	public HardwareProfile listProfile(String profileId) throws DeltaCloudClientException 
+	{
+		String request = DCNS.HARDWARE_PROFILES + "/" + profileId;
+		return JAXB.unmarshal(sendRequest(request, RequestType.GET), HardwareProfile.class);
+	}
+
+	@Override
+	public List<HardwareProfile> listProfiles() throws DeltaCloudClientException 
+	{
+		return listDeltaCloudObjects(HardwareProfile.class, DCNS.HARDWARE_PROFILES.toString(), "hardware_profile");
+	}
+	
+	@Override
+	public List<Image> listImages() throws DeltaCloudClientException 
+	{
+		return listDeltaCloudObjects(Image.class, DCNS.IMAGES.toString(), "image");
+	}
+
+	@Override
+	public Image listImages(String imageId) throws DeltaCloudClientException 
+	{
+		return JAXB.unmarshal(sendRequest(DCNS.IMAGES + "/" + imageId, RequestType.GET), Image.class);
+	}
+
+	@Override
+	public List<Instance> listInstances() throws DeltaCloudClientException 
+	{
+		
+		return listDeltaCloudObjects(Instance.class, DCNS.INSTANCES.toString(), "instance");
+	}
+
+	@Override
+	public Instance listInstances(String instanceId) throws DeltaCloudClientException 
+	{
+		return buildInstance(sendRequest(DCNS.INSTANCES + "/" + instanceId, RequestType.GET));
+	}
+	
+	@Override
+	public List<Realm> listRealms() throws DeltaCloudClientException 
+	{
+		return listDeltaCloudObjects(Realm.class, DCNS.REALMS.toString(), "realm");
+	}
+
+	@Override
+	public Realm listRealms(String realmId) throws DeltaCloudClientException 
+	{
+		return JAXB.unmarshal(sendRequest(DCNS.REALMS + "/" + realmId, RequestType.GET), Realm.class);
+	}
+
+	@Override
+	public void rebootInstance(String instanceId) throws DeltaCloudClientException
+	{
+		sendRequest(DCNS.INSTANCES + "/" + instanceId + DCNS.REBOOT, RequestType.GET);
+	}
+
+	@Override
+	public void shutdownInstance(String instanceId) throws DeltaCloudClientException 
+	{
+		sendRequest(DCNS.INSTANCES + "/" + instanceId + DCNS.STOP, RequestType.GET);
+	}
+	
+	@Override
+	public void startInstance(String instanceId) throws DeltaCloudClientException 
+	{
+		sendRequest(DCNS.INSTANCES + "/" + instanceId + DCNS.START, RequestType.GET);
+	}
+	
+	public void destroyInstance(String instanceId) throws DeltaCloudClientException
+	{
+		sendRequest(DCNS.INSTANCES + "/" + instanceId + DCNS.DESTROY, RequestType.GET);
+	}
+	
+	private Instance buildInstance(String xml)
+	{
+		try
+		{
+			Instance instance = JAXB.unmarshal(new StringReader(xml), Instance.class);
+			
+			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+			DocumentBuilder db = dbf.newDocumentBuilder();
+			Document document = db.parse(new InputSource(new StringReader(xml)));
+					
+			instance.setImageId(getIdFromHref(getAttributeValues(document, "image", "href").get(0)));
+			instance.setProfileId(getIdFromHref(getAttributeValues(document, "hardware-profile", "href").get(0)));
+			setProfileProperties(instance, getPropertyNodes(document, "hardware-profile"));
+			instance.setRealmId(getIdFromHref(getAttributeValues(document, "realm", "href").get(0)));
+			instance.setState(getElementText(document, "state").get(0));
+			
+			ArrayList<Instance.Action> actions = new ArrayList<Instance.Action>();
+			for(String s : getAttributeValues(document, "link", "rel"))
+			{
+				actions.add(Instance.Action.valueOf(s.toUpperCase()));
+			}
+			instance.setActions(actions);
+			
+			return instance;
+		}
+		catch(Exception e)
+		{
+			e.printStackTrace();
+		}
+		return null;
+	}
+	
+	private List<String> getAttributeValues(Document document, String elementName, String attributeName)
+	{
+		NodeList elements = document.getElementsByTagName(elementName);
+		ArrayList<String> values = new ArrayList<String>();
+		for(int i = 0; i < elements.getLength(); i++)
+		{
+			values.add(elements.item(i).getAttributes().getNamedItem(attributeName).getTextContent());
+		}
+		return values;
+	}
+	
+	private List<String> getElementText(Document document, String elementName)
+	{
+		NodeList elements = document.getElementsByTagName(elementName);
+		ArrayList<String> values = new ArrayList<String>();
+		for(int i = 0; i < elements.getLength(); i++)
+		{
+			values.add(elements.item(i).getTextContent());
+		}
+		return values;
+	}
+	
+	private List<Node> getPropertyNodes(Document document, String elementName)
+	{
+		NodeList elements = document.getElementsByTagName(elementName);
+		ArrayList<Node> values = new ArrayList<Node>();
+		for(int i = 0; i < elements.getLength(); i++)
+		{
+			NodeList children = elements.item(i).getChildNodes();
+			for (int j = 0; j < children.getLength(); ++j) 
+			{
+				Node child = children.item(j);
+				if (child.getNodeName().equals("property")) { //$NON-NLS-1$
+					values.add(child);
+				}
+			}
+		}
+		return values;
+	}
+	
+	private void setProfileProperties(Instance instance, List<Node> propertyNodes) {
+		if (propertyNodes != null) {
+			for (Iterator<Node> i = propertyNodes.iterator(); i.hasNext();) {
+				Node n = i.next();
+				NamedNodeMap attrs = n.getAttributes();
+				String name = attrs.getNamedItem("name").getNodeValue(); //$NON-NLS-1$
+				if (name.equals("memory")) { //$NON-NLS-1$
+					String memory = attrs.getNamedItem("value").getNodeValue(); //$NON-NLS-1$
+ 					if (attrs.getNamedItem("unit") != null) { //$NON-NLS-1$
+						memory += " " + attrs.getNamedItem("unit").getNodeValue(); //$NON-NLS-1$
+					}
+					instance.setMemory(memory);
+				} else if (name.equals("storage")) { //$NON-NLS-1$
+					String storage = attrs.getNamedItem("value").getNodeValue(); //$NON-NLS-1$
+ 					if (attrs.getNamedItem("unit") != null) { //$NON-NLS-1$
+						storage += " " + attrs.getNamedItem("unit").getNodeValue(); //$NON-NLS-1$
+					}
+					instance.setStorage(storage);
+				}
+			}
+		}
+	}
+	
+	private String getIdFromHref(String href)
+	{
+		return href.substring(href.lastIndexOf("/") + 1, href.length());
+	}
+	
+	private <T extends DeltaCloudObject> List<T> listDeltaCloudObjects(Class<T> clazz, String path, String elementName) throws DeltaCloudClientException 
+	{
+		try
+		{
+			InputSource is = new InputSource(new StringReader(sendRequest(path, RequestType.GET)));
+			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+			DocumentBuilder db = dbf.newDocumentBuilder();
+			Document document = db.parse(is);
+			
+			document.getElementsByTagName(path).toString(); 
+						
+			ArrayList<T> dco = new ArrayList<T>();
+			
+			NodeList nodeList = document.getElementsByTagName(elementName);
+			for(int i = 0; i < nodeList.getLength(); i ++)
+			{
+				dco.add(buildDeltaCloudObject(clazz, nodeList.item(i)));
+			}
+			return dco;
+		}
+		catch(Exception e)
+		{
+			throw new DeltaCloudClientException("Could not list object of type " + clazz, e);
+		}
+	}
+	
+	@SuppressWarnings("unchecked")
+	private <T extends Object> T buildDeltaCloudObject(Class<T> clazz, Node node) throws DeltaCloudClientException
+	{
+		if(clazz.equals(Instance.class))
+		{
+			return (T) buildInstance(nodeToString(node));
+		}
+		else
+		{
+			return JAXB.unmarshal(new StringReader(nodeToString(node)), clazz);
+		}
+	}
+	
+	private String nodeToString(Node node) throws DeltaCloudClientException
+	{
+		try 
+		{
+			StringWriter writer = new StringWriter();
+			Transformer t = TransformerFactory.newInstance().newTransformer();
+			t.transform(new DOMSource(node), new StreamResult(writer));
+			return writer.toString();
+		} 
+		catch (TransformerException e) 
+		{
+			throw new DeltaCloudClientException("Error transforming node to string", e);
+		}
+		
+	}
+
+}

Added: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientException.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientException.java	                        (rev 0)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientException.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -0,0 +1,21 @@
+package org.jboss.tools.deltacloud.core.client;
+
+public class DeltaCloudClientException extends Exception 
+{
+	private static final long serialVersionUID = 1L;
+	
+	public DeltaCloudClientException(String message, Throwable clause)
+	{
+		super(message, clause);
+	}
+	
+	public DeltaCloudClientException(Throwable clause)
+	{
+		super(clause);
+	}
+	
+	public DeltaCloudClientException(String message)
+	{
+		super(message);
+	}
+}

Added: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudObject.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudObject.java	                        (rev 0)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudObject.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -0,0 +1,24 @@
+package org.jboss.tools.deltacloud.core.client;
+
+import java.io.Serializable;
+
+import javax.xml.bind.annotation.XmlElement;
+
+public abstract class DeltaCloudObject implements Serializable
+{
+	private static final long serialVersionUID = 1L;
+	
+	@XmlElement
+	protected String id;
+	
+	@SuppressWarnings("unused")
+	private void setId(String id) 
+	{
+		this.id = id;
+	}
+
+	public String getId() 
+	{
+		return id;
+	}
+}

Added: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HardwareProfile.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HardwareProfile.java	                        (rev 0)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HardwareProfile.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -0,0 +1,65 @@
+package org.jboss.tools.deltacloud.core.client;
+
+import javax.xml.bind.annotation.XmlElement;
+
+public class HardwareProfile extends DeltaCloudObject
+{
+	private static final long serialVersionUID = 1L;
+
+	@XmlElement
+	private String architecture;
+	
+	@XmlElement
+	private String memory;
+	
+	@XmlElement
+	private String storage;
+	
+	private HardwareProfile()
+	{	
+	}
+
+	@SuppressWarnings("unused")
+	private void setArchitecture(String architecture)
+	{
+		this.architecture = architecture;
+	}
+
+	@SuppressWarnings("unused")
+	private void setMemory(String memory)
+	{
+		this.memory = memory;
+	}
+
+	@SuppressWarnings("unused")
+	private void setStorage(String storage)
+	{
+		this.storage = storage;
+	}
+
+	public String getArchitecture()
+	{
+		return architecture;
+	}
+
+	public String getMemory()
+	{
+		return memory;
+	}
+
+	public String getStorage()
+	{
+		return storage;
+	}
+	
+	@Override
+	public String toString()
+	{
+		String s = "";
+		s += "Hardware-profile:\t\t" + getId() + "\n";
+		s += "Arch:\t\t" + getArchitecture() + "\n";
+		s += "Memory:\t\t" + getMemory() + "\n";
+		s += "Storage:\t" + getStorage() + "\n";
+		return s;
+	}
+}

Added: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Image.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Image.java	                        (rev 0)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Image.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -0,0 +1,80 @@
+package org.jboss.tools.deltacloud.core.client;
+
+import javax.xml.bind.annotation.XmlElement;
+
+public class Image extends DeltaCloudObject
+{	
+	private static final long serialVersionUID = 1L;
+
+	@XmlElement(name="owner_id")
+	private String ownerId;
+	
+	@XmlElement
+	private String name;
+	
+	@XmlElement
+	private String description;
+	
+	@XmlElement
+	private String architecture;
+	
+	private Image()
+	{
+	}
+		
+	@SuppressWarnings("unused")
+	private void setOwnerId(String ownerId)
+	{
+		this.ownerId = ownerId;
+	}
+
+	@SuppressWarnings("unused")
+	private void setName(String name)
+	{
+		this.name = name;
+	}
+
+	@SuppressWarnings("unused")
+	private void setDescription(String description)
+	{
+		this.description = description;
+	}
+
+	@SuppressWarnings("unused")
+	private void setArchitecture(String architecture)
+	{
+		this.architecture = architecture;
+	}
+	
+	public String getOwnerId() 
+	{
+		return ownerId;
+	}
+
+	public String getName()
+	{
+		return name;
+	}
+
+	public String getDescription()
+	{
+		return description;
+	}
+
+	public String getArchitecture()
+	{
+		return architecture;
+	}
+	
+	@Override
+	public String toString()
+	{
+		String s = "";
+		s += "Image:\t\t" + getId() + "\n";
+		s += "Owner:\t\t" + getOwnerId() + "\n";
+		s += "Name:\t\t" + getName() + "\n";
+		s += "Desc:\t\t" + getDescription() + "\n";
+		s += "Arch:\t\t" + getArchitecture() + "\n";
+		return s;
+	}
+}

Added: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Instance.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Instance.java	                        (rev 0)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Instance.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -0,0 +1,213 @@
+package org.jboss.tools.deltacloud.core.client;
+
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlElement;
+
+public class Instance extends DeltaCloudObject
+{	
+	private static final long serialVersionUID = 1L;
+	
+	public static enum State { RUNNING, STOPPED, PENDING };
+	
+	public static enum Action { START, STOP, REBOOT, DESTROY };
+	
+	@XmlElement(name="owner_id")
+	private String ownerId;
+	
+	@XmlElement
+	private String name;
+	
+	private String imageId;
+
+	@XmlElement(name="hardware-profile")
+	private String profileId;
+
+	private String memory;
+	
+	private String storage;
+	
+	private String realmId;
+	
+	@XmlElement
+	private State state;
+	
+	private List<Action> actions;
+	
+	@XmlElement(name="public-addresses")
+	private AddressList publicAddresses;
+	
+	@XmlElement(name="private-addresses")
+	private AddressList privateAddresses;
+	
+	private Instance()
+	{
+	}
+
+	@SuppressWarnings("unused")
+	private void setOwnerId(String ownerId)
+	{
+		this.ownerId = ownerId;
+	}
+	
+	@SuppressWarnings("unused")
+	private void setName(String name)
+	{
+		this.name = name;
+	}
+	
+	protected void setImageId(String imageId)
+	{
+		this.imageId = imageId;
+	}
+	
+	protected void setProfileId(String profileId)
+	{
+		this.profileId = profileId;
+	}
+	
+	protected void setMemory(String memory) {
+		this.memory = memory;
+	}
+	
+	protected void setStorage(String storage) {
+		this.storage = storage;
+	}
+	
+	protected void setRealmId(String realmId)
+	{
+		this.realmId = realmId;
+	}
+	
+	protected void setActions(List<Action> actions)
+	{
+		this.actions = actions;
+	}
+	
+	protected void setState(String state)
+	{
+		this.state = State.valueOf(state);
+	}
+	
+	@SuppressWarnings("unused")
+	private void setPrivateAddresses(AddressList privateAddresses)
+	{
+		this.privateAddresses = privateAddresses;
+	}
+	
+	@SuppressWarnings("unused")
+	private void setPublicAddresses(AddressList publicAddresses)
+	{
+		this.publicAddresses = publicAddresses;
+	}
+	
+	public String getOwnerId()
+	{
+		return ownerId;
+	}
+
+	public String getName()
+	{
+		return name;
+	}
+
+	public String getImageId()
+	{
+		return imageId;
+	}
+
+	public String getProfileId()
+	{
+		return profileId;
+	}
+
+	public String getMemory() {
+		return memory;
+	}
+	
+	public String getStorage() {
+		return storage;
+	}
+	
+	public String getRealmId()
+	{
+		return realmId;
+	}
+
+	public State getState()
+	{
+		return state;
+	}
+	
+	public List<Action> getActions()
+	{
+		return actions;
+	}
+	
+	public List<String> getPublicAddresses()
+	{
+		return publicAddresses.getAddress();
+	}
+
+	public List<String> getPrivateAddresses()
+	{
+		return privateAddresses.getAddress();
+	}	
+	
+	@Override
+	public String toString()
+	{
+		String s = "";
+		s += "Instance:\t" + getId() + "\n";
+		s += "Owner:\t\t" + getOwnerId() + "\n";
+		s += "Image:\t\t" + getImageId() + "\n";
+		s += "Realm:\t\t" + getRealmId() + "\n";
+		s += "Profile:\t\t" + getProfileId() + "\n";
+		if (getMemory() != null)
+		    s += "Memory:\t\t\t" + getMemory() + "\n";
+		if (getStorage() != null) {
+			s += "Storage:\t\t\t" + getStorage() + "\n";
+		}
+		s += "State:\t\t" + getState() + "\n";
+		
+		
+		for(int i = 0; i < actions.size(); i ++)
+		{
+			if(i == 0)
+			{
+				s += "Actions:\t" + actions.get(i)  + "\n";
+			}
+			else
+			{
+				s += "\t\t" + actions.get(i) + "\n";
+			}
+		}
+		
+		
+		for(int i = 0; i < publicAddresses.getAddress().size(); i ++)
+		{
+			if(i == 0)
+			{
+				s += "Public Addr:\t" + publicAddresses.getAddress().get(i) + "\n";
+			}
+			else
+			{
+				s += "\t\t" + publicAddresses.getAddress().get(i) + "\n";
+			}
+		}
+		
+		for(int i = 0; i < publicAddresses.getAddress().size(); i ++)
+		{
+			if(i == 0)
+			{
+				s += "Private Addr:\t" + publicAddresses.getAddress().get(i) + "\n";
+			}
+			else
+			{
+				s += "\t\t" + privateAddresses.getAddress().get(i) + "\n";
+			}
+		}
+		
+		return s;
+	}
+}

Added: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Realm.java
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Realm.java	                        (rev 0)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Realm.java	2010-07-26 17:46:07 UTC (rev 23731)
@@ -0,0 +1,72 @@
+package org.jboss.tools.deltacloud.core.client;
+
+import javax.xml.bind.annotation.XmlElement;
+
+public class Realm extends DeltaCloudObject
+{
+	private static final long serialVersionUID = 1L;
+
+	@XmlElement
+	private String name;
+	
+	@XmlElement
+	private String state;
+	
+	@XmlElement
+	private int limit;
+		
+	private Realm() 
+	{
+	}
+
+	@SuppressWarnings("unused")
+	private void setName(String name)
+	{
+		this.name = name;
+	}
+
+	@SuppressWarnings("unused")
+	private void setState(String state)
+	{
+		this.state = state;
+	}
+
+	@SuppressWarnings("unused")
+	private void setLimit(int limit)
+	{
+		this.limit = limit;
+	}
+
+	public static long getSerialversionuid()
+	{
+		return serialVersionUID;
+	}
+
+	public String getName()
+	{
+		return name;
+	}
+
+	public String getState()
+	{
+		return state;
+	}
+
+	public int getLimit()
+	{
+		return limit;
+	}
+	
+	@Override
+	public String toString()
+	{
+		String s = "";
+		s += "Realm:\t\t" + getId() + "\n";
+		s += "Name\t\t" + getName()+ "\n";
+		s += "State:\t\t" + getState() + "\n";
+		s += "Limit:\t\t" + getLimit() + "\n";
+		return s;
+	}
+	
+	
+}



More information about the jbosstools-commits mailing list