Author: adietish
Date: 2011-01-10 11:13:00 -0500 (Mon, 10 Jan 2011)
New Revision: 28044
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfileUnmarshaller.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfilesUnmarshaller.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/client/DeltaCloudClientImpl.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/Property.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/AbstractDOMUnmarshaller.java
Log:
[JBIDE-7935] moved HardwareProfile unmarshalling to HardwareProfileUnmarshaller
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2011-01-10 15:06:32
UTC (rev 28043)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2011-01-10 16:13:00
UTC (rev 28044)
@@ -1,3 +1,9 @@
+2011-01-10 André Dietisheim <André Dietisheim@adietisheim-thinkpad>
+
+ * src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfileUnmarshaller.java:
+ * src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientImpl.java (listProfile):
+ [JBIDE-7935] moved HardwareProfile unmarshalling to HardwareProfileUnmarshaller
+
2011-01-04 André Dietisheim <André Dietisheim@adietisheim-thinkpad>
* src/org/jboss/tools/deltacloud/core/DeltaCloudInstancesRepository.java:
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientImpl.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientImpl.java 2011-01-10
15:06:32 UTC (rev 28043)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientImpl.java 2011-01-10
16:13:00 UTC (rev 28044)
@@ -42,7 +42,6 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.eclipse.core.runtime.Assert;
-import org.jboss.tools.deltacloud.core.DeltaCloudHardwareProperty.Kind;
import org.jboss.tools.deltacloud.core.client.request.AbstractListObjectsRequest;
import org.jboss.tools.deltacloud.core.client.request.CreateInstanceRequest;
import org.jboss.tools.deltacloud.core.client.request.CreateKeyRequest;
@@ -60,6 +59,7 @@
import org.jboss.tools.deltacloud.core.client.request.ListRealmsRequest;
import org.jboss.tools.deltacloud.core.client.request.PerformInstanceActionRequest;
import org.jboss.tools.deltacloud.core.client.request.TypeRequest;
+import org.jboss.tools.deltacloud.core.client.unmarshal.HardwareProfilesUnmarshaller;
import org.jboss.tools.deltacloud.core.client.unmarshal.InstanceUnmarshaller;
import org.jboss.tools.deltacloud.core.client.unmarshal.InstancesUnmarshaller;
import org.jboss.tools.deltacloud.core.client.unmarshal.KeyUnmarshaller;
@@ -326,87 +326,16 @@
@Override
public List<HardwareProfile> listProfiles() throws DeltaCloudClientException {
try {
- return buildProfiles(request(new ListHardwareProfilesRequest(baseUrl)));
+ InputStream response = request(new ListHardwareProfilesRequest(baseUrl));
+ List<HardwareProfile> profiles = new ArrayList<HardwareProfile>();
+ new HardwareProfilesUnmarshaller().unmarshall(response, profiles);
+ return profiles;
} catch (Exception e) {
throw new DeltaCloudClientException(MessageFormat.format("could not get realms on
cloud at \"{0}\"",
baseUrl), e);
}
}
- private List<HardwareProfile> buildProfiles(InputStream inputStream)
- throws ParserConfigurationException, SAXException, IOException,
DeltaCloudClientException {
- Document document = getDocument(getResponse(inputStream));
- List<HardwareProfile> profiles = new ArrayList<HardwareProfile>();
- NodeList elements = document.getElementsByTagName("hardware_profile");
- for (int i = 0; i < elements.getLength(); i++) {
- HardwareProfile profile = createProfile(elements.item(i));
- profiles.add(profile);
- }
- return profiles;
- }
-
- private HardwareProfile createProfile(Node node) {
- Assert.isLegal(node instanceof Element);
- Element element = (Element) node;
- HardwareProfile profile = new HardwareProfile();
- profile.setId(element.getAttribute("id"));
- profile.setProperties(createProperties(element.getElementsByTagName("property")));
- return profile;
- }
-
- private List<Property> createProperties(NodeList propertiesList) {
- List<Property> properties = new ArrayList<Property>();
- for (int i = 0; i < propertiesList.getLength(); i++) {
- Property property = createProperty(propertiesList.item(i));
- properties.add(property);
- }
- return properties;
- }
-
- private Property createProperty(Node node) {
- Assert.isTrue(node instanceof Element);
- Element element = (Element) node;
- Property property = new Property();
- property.setName(element.getAttribute("name"));
- property.setId(element.getAttribute("id"));
- property.setUnit(element.getAttribute("unit"));
- property.setValue(element.getAttribute("value"));
- String kind = element.getAttribute("kind");
- Assert.isTrue(kind != null);
- kind = kind.toUpperCase();
- property.setKind(kind);
- if (Kind.RANGE.toString().equals(property.getKind())) {
- setRange(element, property);
- } else if (Kind.ENUM.toString().equals(property.getKind())) {
- setEnum(element, property);
- } else if (Kind.FIXED.toString().equals(property.getKind())) {
- // no special treatement
- }
- return property;
- }
-
- private void setRange(Element propertyElement, Property property) {
- Node node = propertyElement.getElementsByTagName("range").item(0);
- Assert.isLegal(node instanceof Element);
- Element rangeElement = (Element) node;
- property.setRange(rangeElement.getAttribute("first"),
rangeElement.getAttribute("last"));
- }
-
- private void setEnum(Element propertyElement, Property property) {
- Node node = propertyElement.getElementsByTagName("enum").item(0);
- Assert.isLegal(node instanceof Element);
- Element enumElement = (Element) node;
- NodeList nodeList = enumElement.getElementsByTagName("entry");
- ArrayList<String> enumValues = new ArrayList<String>();
- for (int i = 0; i < nodeList.getLength(); i++) {
- Node entryNode = nodeList.item(i);
- Assert.isTrue(entryNode instanceof Element);
- Element entryElement = (Element) entryNode;
- enumValues.add(entryElement.getAttribute("value"));
- }
- property.setEnums(enumValues);
- }
-
@Override
public List<Image> listImages() throws DeltaCloudClientException {
return listDeltaCloudObjects(Image.class,
Modified:
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 2011-01-10
15:06:32 UTC (rev 28043)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HardwareProfile.java 2011-01-10
16:13:00 UTC (rev 28044)
@@ -13,30 +13,38 @@
import java.util.ArrayList;
import java.util.List;
+import org.jboss.tools.deltacloud.core.client.Property.Names;
+
/**
* @author Martyn Taylor
+ * @author André Dietisheim
*/
-public class HardwareProfile extends AbstractDeltaCloudObject
-{
+public class HardwareProfile extends AbstractDeltaCloudObject {
private static final long serialVersionUID = 1L;
private List<Property> properties;
-
- protected HardwareProfile()
- {
+
+ public HardwareProfile() {
}
- protected void setProperties(List<Property> properties) {
+ public void setProperties(List<Property> properties) {
this.properties = properties;
}
-
+
public List<Property> getProperties() {
if (properties == null)
properties = new ArrayList<Property>();
return properties;
}
-
- private Property getNamedProperty(String name) {
+
+ public Property getNamedProperty(Names nameEnum) {
+ if (nameEnum == null) {
+ return null;
+ }
+ return getNamedProperty(nameEnum.name().toLowerCase());
+ }
+
+ public Property getNamedProperty(String name) {
if (properties != null) {
for (Property p : properties) {
if (p.getName().equals(name))
@@ -45,43 +53,37 @@
}
return null;
}
-
- public String getArchitecture()
- {
- Property p = getNamedProperty("architecture");
+
+ public String getArchitecture() {
+ Property p = getNamedProperty(Property.Names.ARCHITECTURE);
if (p != null)
return p.getValue();
return null;
}
- public String getMemory()
- {
+ public String getMemory() {
Property p = getNamedProperty("memory");
if (p != null)
return p.toString();
return null;
}
-
- public String getStorage()
- {
+
+ public String getStorage() {
Property p = getNamedProperty("storage");
if (p != null)
return p.toString();
return null;
}
- public String getCPU()
- {
+ public String getCPU() {
Property p = getNamedProperty("cpu");
if (p != null)
return p.getValue();
return null;
}
-
-
+
@Override
- public String toString()
- {
+ public String toString() {
String s = "";
s += "Hardware-profile:\t\t" + getId() + "\n";
for (Property p : properties) {
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Property.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Property.java 2011-01-10
15:06:32 UTC (rev 28043)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/Property.java 2011-01-10
16:13:00 UTC (rev 28044)
@@ -12,121 +12,151 @@
import java.util.List;
-import javax.xml.bind.annotation.XmlElement;
-
/**
* @author Martyn Taylor
*/
public class Property extends AbstractDeltaCloudObject {
-
+
private static final long serialVersionUID = 1L;
- public static enum Kind {FIXED, RANGE, ENUM};
+ public static enum Kind {
+ FIXED, RANGE, ENUM
+ };
+ public static enum Names {
+ MEMORY, STORAGE, CPU, ARCHITECTURE
+ }
+
+ public static enum UNIT {
+ MB {
+ public boolean matches(String value) {
+ return name().equals(value);
+ }},
+ GB{
+ public boolean matches(String value) {
+ return name().equals(value);
+ }},
+ LABEL{
+ public boolean matches(String value) {
+ return name().toLowerCase().equals(value);
+ }},
+ COUNT{
+ public boolean matches(String value) {
+ return name().toLowerCase().equals(value);
+ }};
+
+ public abstract boolean matches(String value);
+ }
+
public class Range {
private String first;
private String last;
-
+
public Range(String first, String last) {
this.first = first;
this.last = last;
}
-
+
public String getFirst() {
return first;
}
-
+
public String getLast() {
return last;
}
}
-
- public Property()
- {
+
+ public Property() {
}
-
- @XmlElement
+
private String kind;
-
- @XmlElement
+
private String unit;
-
- @XmlElement
+
private String name;
-
- @XmlElement
+
private String value;
-
+
// For range
private String first;
private String last;
-
+
// For enum
private List<String> enums;
-
+
public String getKind() {
return kind;
}
-
+
public String getUnit() {
return unit;
}
-
+
public String getName() {
return name;
}
-
+
public String getValue() {
return value;
}
-
+
public Range getRange() {
return new Range(first, last);
}
-
+
public List<String> getEnums() {
return enums;
}
-
+
public void setName(String name) {
this.name = name;
}
-
+
public void setValue(String value) {
this.value = value;
}
-
+
public void setUnit(String unit) {
this.unit = unit;
}
-
+
public void setKind(String kind) {
this.kind = kind;
}
-
+
public void setRange(String first, String last) {
this.first = first;
this.last = last;
}
-
+
public void setEnums(List<String> enums) {
this.enums = enums;
}
-
+
public String toString() {
if (kind.equals("range")) {
- return first += "-" + last + "(default:" + value + ")";
- }
- else if (kind.equals("enum")) {
+ // return first += "-" + last + "(default:" + value +
")";
+ return new StringBuilder()
+ .append(first)
+ .append('-').append(last)
+ .append("(default: ").append(value).append(")")
+ .toString();
+ } else if (kind.equals("enum")) {
String s = enums.get(0);
for (int i = 1; i < enums.size(); ++i) {
s += ", " + enums.get(i);
}
- s += " (default:" + value + ")";
+ s += " (default:" + value + ")";
return s;
+ } else {
+ // return value += " " + (unit.equals("label") ? "" :
unit);
+ StringBuilder builder = new StringBuilder();
+ builder.append(value);
+ if (!UNIT.LABEL.matches(unit)) {
+ builder.append(' ').append(unit);
+ }
+ return builder.toString();
}
- // must be "fixed"
- return value += " " + (unit.equals("label") ? "" :
unit);
}
-
+
}
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/AbstractDOMUnmarshaller.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/AbstractDOMUnmarshaller.java 2011-01-10
15:06:32 UTC (rev 28043)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/AbstractDOMUnmarshaller.java 2011-01-10
16:13:00 UTC (rev 28044)
@@ -41,10 +41,13 @@
this.tagName = tagName;
}
- public DELTACLOUDOBJECT unmarshall(InputStream inputStream, DELTACLOUDOBJECT resource)
throws DeltaCloudClientException {
+ public DELTACLOUDOBJECT unmarshall(InputStream inputStream, DELTACLOUDOBJECT
deltacloudObject) throws DeltaCloudClientException {
try {
Element element = getFirstElement(tagName, getDocument(inputStream));
- return unmarshall(element, resource);
+ if (element == null) {
+ return null;
+ }
+ return unmarshall(element, deltacloudObject);
} catch (Exception e) {
// TODO: internationalize strings
throw new DeltaCloudClientException(
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfileUnmarshaller.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfileUnmarshaller.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfileUnmarshaller.java 2011-01-10
16:13:00 UTC (rev 28044)
@@ -0,0 +1,93 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.deltacloud.core.client.unmarshal;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.Assert;
+import org.jboss.tools.deltacloud.core.DeltaCloudHardwareProperty.Kind;
+import org.jboss.tools.deltacloud.core.client.HardwareProfile;
+import org.jboss.tools.deltacloud.core.client.Property;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * @author André Dietisheim
+ */
+public class HardwareProfileUnmarshaller extends
AbstractDOMUnmarshaller<HardwareProfile> {
+
+ public HardwareProfileUnmarshaller() {
+ super("hardware_profile", HardwareProfile.class);
+ }
+
+ @Override
+ protected HardwareProfile doUnmarshall(Element element, HardwareProfile profile) throws
Exception {
+ profile.setId(getAttributeText("id", element));
+ profile.setProperties(createProperties(element.getElementsByTagName("property")));
+ return profile;
+ }
+
+ private List<Property> createProperties(NodeList propertiesList) {
+ List<Property> properties = new ArrayList<Property>();
+ for (int i = 0; i < propertiesList.getLength(); i++) {
+ Property property = createProperty(propertiesList.item(i));
+ properties.add(property);
+ }
+ return properties;
+ }
+
+ private Property createProperty(Node node) {
+ Assert.isTrue(node instanceof Element);
+ Element element = (Element) node;
+ Property property = new Property();
+ property.setName(element.getAttribute("name"));
+ property.setId(element.getAttribute("id"));
+ property.setUnit(element.getAttribute("unit"));
+ property.setValue(element.getAttribute("value"));
+ String kind = element.getAttribute("kind");
+ Assert.isTrue(kind != null);
+ kind = kind.toUpperCase();
+ property.setKind(kind);
+ if (Kind.RANGE.toString().equals(property.getKind())) {
+ setRange(element, property);
+ } else if (Kind.ENUM.toString().equals(property.getKind())) {
+ setEnum(element, property);
+ } else if (Kind.FIXED.toString().equals(property.getKind())) {
+ // no special treatement
+ }
+ return property;
+ }
+
+ private void setRange(Element propertyElement, Property property) {
+ Node node = propertyElement.getElementsByTagName("range").item(0);
+ Assert.isLegal(node instanceof Element);
+ Element rangeElement = (Element) node;
+ property.setRange(rangeElement.getAttribute("first"),
rangeElement.getAttribute("last"));
+ }
+
+ private void setEnum(Element propertyElement, Property property) {
+ Node node = propertyElement.getElementsByTagName("enum").item(0);
+ Assert.isLegal(node instanceof Element);
+ Element enumElement = (Element) node;
+ NodeList nodeList = enumElement.getElementsByTagName("entry");
+ ArrayList<String> enumValues = new ArrayList<String>();
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Node entryNode = nodeList.item(i);
+ Assert.isTrue(entryNode instanceof Element);
+ Element entryElement = (Element) entryNode;
+ enumValues.add(entryElement.getAttribute("value"));
+ }
+ property.setEnums(enumValues);
+ }
+
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfileUnmarshaller.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfilesUnmarshaller.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfilesUnmarshaller.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfilesUnmarshaller.java 2011-01-10
16:13:00 UTC (rev 28044)
@@ -0,0 +1,31 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.deltacloud.core.client.unmarshal;
+
+import org.jboss.tools.deltacloud.core.client.DeltaCloudClientException;
+import org.jboss.tools.deltacloud.core.client.HardwareProfile;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+/**
+ * @author André Dietisheim
+ */
+public class HardwareProfilesUnmarshaller extends
AbstractDeltaCloudObjectsUnmarshaller<HardwareProfile> {
+
+ public HardwareProfilesUnmarshaller() {
+ super("hardware_profiles", "hardware_profile");
+ }
+
+ @Override
+ protected HardwareProfile unmarshallChild(Node node) throws DeltaCloudClientException {
+ return new HardwareProfileUnmarshaller().unmarshall((Element) node, new
HardwareProfile());
+ }
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/unmarshal/HardwareProfilesUnmarshaller.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain