diff --git a/src/oca/java/src/org/opennebula/client/Client.java b/src/oca/java/src/org/opennebula/client/Client.java index 49c9918a47..bf4b76ad55 100644 --- a/src/oca/java/src/org/opennebula/client/Client.java +++ b/src/oca/java/src/org/opennebula/client/Client.java @@ -106,8 +106,16 @@ public class Client{ } catch (ClassCastException e) { - // The result may be an Integer - msg = ((Integer) result[1]).toString(); + try + { + // The result may be an Integer + msg = ((Integer) result[1]).toString(); + } + catch (ClassCastException ei) + { + // The result may be a Boolean + msg = ((Boolean) result[1]).toString(); + } } } } diff --git a/src/oca/java/src/org/opennebula/client/OneResponse.java b/src/oca/java/src/org/opennebula/client/OneResponse.java index b4c9beef9a..fa212427a8 100644 --- a/src/oca/java/src/org/opennebula/client/OneResponse.java +++ b/src/oca/java/src/org/opennebula/client/OneResponse.java @@ -91,6 +91,20 @@ public class OneResponse{ return ret; } + /** + * Parses the string returned by getMessage + * + * @return The parsed boolean. False in case of error + * + * @see #getMessage + */ + public boolean getBooleanMessage() + { + boolean ret = Boolean.parseBoolean( getMessage() ); + + return ret; + } + // ------------------------------------------------------------------------ // PRIVATE ATTRIBUTES // ------------------------------------------------------------------------ diff --git a/src/oca/java/src/org/opennebula/client/document/Document.java b/src/oca/java/src/org/opennebula/client/document/Document.java index 72ff55e87e..6bd6dba732 100644 --- a/src/oca/java/src/org/opennebula/client/document/Document.java +++ b/src/oca/java/src/org/opennebula/client/document/Document.java @@ -66,6 +66,8 @@ public abstract class Document extends PoolElement private static final String CHMOD = METHOD_PREFIX + "chmod"; private static final String CLONE = METHOD_PREFIX + "clone"; private static final String RENAME = METHOD_PREFIX + "rename"; + private static final String LOCK = METHOD_PREFIX + "lock"; + private static final String UNLOCK = METHOD_PREFIX + "unlock"; /** * Creates a new Document representation. @@ -239,6 +241,29 @@ public abstract class Document extends PoolElement return client.call(RENAME, id, name); } + /** + * Locks this object + * + * @param owner String to identify the application requestiong the lock + * @return In case of success, a boolean with true if the lock was granted, + * and false if the object is already locked. + */ + public OneResponse lock(String owner) + { + return client.call(LOCK, id, owner); + } + + /** + * Unlocks this object + * + * @param owner String to identify the application requestiong the lock + * @return If an error occurs the error message contains the reason. + */ + public OneResponse unlock(String owner) + { + return client.call(UNLOCK, id, owner); + } + // ================================= // Helpers // ================================= diff --git a/src/oca/java/test/DocumentTest.java b/src/oca/java/test/DocumentTest.java index 775bc878ef..1730e61ffb 100644 --- a/src/oca/java/test/DocumentTest.java +++ b/src/oca/java/test/DocumentTest.java @@ -145,4 +145,19 @@ public class DocumentTest assertTrue( !foundA ); assertTrue( foundB ); } + + @Test + public void lock() + { + res = objA.lock("doctest"); + assertTrue( res.getErrorMessage(), !res.isError() ); + assertTrue( res.getMessage(), res.getBooleanMessage() == true ); + + res = objA.lock("doctest"); + assertTrue( res.getErrorMessage(), !res.isError() ); + assertTrue( res.getMessage(), res.getBooleanMessage() == false ); + + res = objA.unlock("doctest"); + assertTrue( res.getErrorMessage(), !res.isError() ); + } } diff --git a/src/oca/ruby/opennebula/document.rb b/src/oca/ruby/opennebula/document.rb index e4ff72f4ea..1faad3e2aa 100644 --- a/src/oca/ruby/opennebula/document.rb +++ b/src/oca/ruby/opennebula/document.rb @@ -44,7 +44,9 @@ module OpenNebula :chown => "document.chown", :chmod => "document.chmod", :clone => "document.clone", - :rename => "document.rename" + :rename => "document.rename", + :lock => "document.lock", + :unlock => "document.unlock" } # Creates a Document Object description with just its identifier @@ -200,6 +202,32 @@ module OpenNebula return call(DOCUMENT_METHODS[:rename], @pe_id, name) end + # Locks this object + # + # @param owner [String] String to identify the application requestiong + # the lock + # + # @return [Bool, OpenNebula::Error] In case of success, true if the + # lock was granted, and false if the object is already locked. + def lock(owner="") + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(DOCUMENT_METHODS[:lock], @pe_id, owner) + + return rc + end + + # Unlocks this object + # + # @param owner [String] String to identify the application requestiong + # the unlock + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def unlock(owner="") + return call(DOCUMENT_METHODS[:unlock], @pe_id, owner) + end + ####################################################################### # Helpers to get Document information #######################################################################