From 7b19d4a8666f201d42ff6b3cbb582bab4cfdbb89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 7 Jul 2011 19:21:03 +0200 Subject: [PATCH] Java OCA: Some formatting, and a couple of new helper methods for OneResponse and Pool --- .../org/opennebula/client/OneResponse.java | 40 ++++++++++++++----- .../java/src/org/opennebula/client/Pool.java | 32 ++++++++++++--- .../org/opennebula/client/group/Group.java | 2 +- .../opennebula/client/group/GroupPool.java | 2 +- 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/oca/java/src/org/opennebula/client/OneResponse.java b/src/oca/java/src/org/opennebula/client/OneResponse.java index d88a37da46..7b00c7aada 100644 --- a/src/oca/java/src/org/opennebula/client/OneResponse.java +++ b/src/oca/java/src/org/opennebula/client/OneResponse.java @@ -1,12 +1,12 @@ /******************************************************************************* * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) - * + * * Licensed 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. @@ -16,14 +16,14 @@ package org.opennebula.client; /** - * This class encapsulates OpenNebula's XML-RPC responses. Each response - * carries a boolean indicating if it is an error. It can also contain a - * success message, or an error message. + * This class encapsulates OpenNebula's XML-RPC responses. Each response + * carries a boolean indicating if it is an error. It can also contain a + * success message, or an error message. */ public class OneResponse{ /** * Creates a new response. - * + * * @param success Indicates if the call was successful, and if * the message is an error or an information string. * @param message String containing the response message, or @@ -37,7 +37,7 @@ public class OneResponse{ /** * Returns true if the call resulted in error. - * + * * @return True if the call resulted in error. */ public boolean isError() @@ -48,7 +48,7 @@ public class OneResponse{ /** * Returns a string containing the error message, or null * if the response isn't an error. - * + * * @return A string containing the error message, or null * if the response isn't an error. */ @@ -61,7 +61,7 @@ public class OneResponse{ * Returns a string containing the response information, or * null if the response was an error. Note that the success * message could be also null. - * + * * @return A string containing the response information, or * null if the response was an error. Note that the success * message could be also null. @@ -71,6 +71,26 @@ public class OneResponse{ return success ? msg : null; } + /** + * Parses the string returned by getMessage + * + * @return The parsed int, or Integer.MIN_VALUE in case of error + * + * @see #getMessage + */ + public int getIntMessage() + { + int ret = Integer.MIN_VALUE; + + try + { + ret = Integer.parseInt( getMessage() ); + } + catch (NumberFormatException e) {} + + return ret; + } + // ------------------------------------------------------------------------ // PRIVATE ATTRIBUTES // ------------------------------------------------------------------------ diff --git a/src/oca/java/src/org/opennebula/client/Pool.java b/src/oca/java/src/org/opennebula/client/Pool.java index 9815f8740f..9bd7a85d8a 100644 --- a/src/oca/java/src/org/opennebula/client/Pool.java +++ b/src/oca/java/src/org/opennebula/client/Pool.java @@ -1,12 +1,12 @@ /******************************************************************************* * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) - * + * * Licensed 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. @@ -82,7 +82,7 @@ public abstract class Pool{ new ByteArrayInputStream(info.getMessage().getBytes())); xml = doc.getDocumentElement(); - poolElements = xml.getElementsByTagName(elementName); + poolElements = xml.getElementsByTagName(elementName); } catch (ParserConfigurationException e) {} catch (SAXException e) {} @@ -92,7 +92,7 @@ public abstract class Pool{ /** * Returns the indexth element in the pool. If index is greater than or * equal to the number of elements in the pool, this returns null. - * + * * @param index Index of the element. * @return The element at the indexth position in the pool, or * null if that is not a valid index. @@ -103,7 +103,7 @@ public abstract class Pool{ if (poolElements != null) { - Node node =poolElements.item(index); + Node node = poolElements.item(index); if (node != null) { @@ -114,6 +114,26 @@ public abstract class Pool{ return theElement; } + protected PoolElement getById(int id) + { + // TODO: Use xpath to find the element //ID + + PoolElement theElement = null; + PoolElement tmpElement = null; + + for( int i = 0; i < getLength(); i++ ) + { + tmpElement = item(i); + if( tmpElement.id() == id ) + { + theElement = tmpElement; + break; + } + } + + return theElement; + } + /** * The number of elements in the pool. * @return The number of elements in the pool. diff --git a/src/oca/java/src/org/opennebula/client/group/Group.java b/src/oca/java/src/org/opennebula/client/group/Group.java index f3f511233d..a32c5d5fb0 100644 --- a/src/oca/java/src/org/opennebula/client/group/Group.java +++ b/src/oca/java/src/org/opennebula/client/group/Group.java @@ -36,7 +36,7 @@ public class Group extends PoolElement{ /** * Creates a new Group representation. * - * @param id The group id (hid) of the machine. + * @param id The group id. * @param client XML-RPC Client. */ public Group(int id, Client client) diff --git a/src/oca/java/src/org/opennebula/client/group/GroupPool.java b/src/oca/java/src/org/opennebula/client/group/GroupPool.java index bfdc346305..74038a2d4a 100644 --- a/src/oca/java/src/org/opennebula/client/group/GroupPool.java +++ b/src/oca/java/src/org/opennebula/client/group/GroupPool.java @@ -50,7 +50,7 @@ public class GroupPool extends Pool implements Iterable{ } /** - * Retrieves all the hosts in the pool. + * Retrieves all the groups in the pool. * * @param client XML-RPC Client. * @return If successful the message contains the string