diff --git a/src/oca/java/build.sh b/src/oca/java/build.sh
index 875f68a438..967aa00e3f 100755
--- a/src/oca/java/build.sh
+++ b/src/oca/java/build.sh
@@ -76,7 +76,7 @@ do_documentation()
-subpackages org.opennebula \
-windowtitle 'OpenNebula Cloud API' \
-doctitle 'OpenNebula Cloud API Specification' \
- -header 'OpenNebula
Java API' \
+ -header 'OpenNebula
Cloud API' \
-bottom 'Visit OpenNebula.org
Copyright 2002-2010 ©
OpenNebula Project Leads (OpenNebula.org).'
diff --git a/src/oca/java/share/examples/SessionInit.java b/src/oca/java/share/examples/SessionInit.java
index b98c1712df..9bce826bd5 100644
--- a/src/oca/java/share/examples/SessionInit.java
+++ b/src/oca/java/share/examples/SessionInit.java
@@ -24,22 +24,35 @@ public class SessionInit {
try
{
Client oneClient = new Client();
+ System.out.println(" ok");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
- System.out.println("ok");
- System.out.println("Wrong session initialization...");
+ System.out.println("Forcing a wrong user/password initialization...");
try
{
+ // The secret string should be user:password. The url is null, so it
+ // will be set to default.
Client oneClient = new Client("wrong_password_token",null);
}
catch (Exception e)
{
System.out.println("\t" + e.getMessage());
}
+
+ System.out.println("Forcing a wrong url initialization...");
+ try
+ {
+ // The HTTP is misspelled
+ Client oneClient = new Client(null,"HTP://localhost:2633/RPC2");
+ }
+ catch (Exception e)
+ {
+ System.out.println("\t" + e.getMessage());
+ }
}
}
diff --git a/src/oca/java/share/examples/SessionInit.sh b/src/oca/java/share/examples/SessionInit.sh
new file mode 100755
index 0000000000..b362986eaa
--- /dev/null
+++ b/src/oca/java/share/examples/SessionInit.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+java -cp ../../lib/*:../../jar/*:. SessionInit
diff --git a/src/oca/java/share/examples/UserSample.java b/src/oca/java/share/examples/UserSample.java
index 8198ae9807..093b601365 100644
--- a/src/oca/java/share/examples/UserSample.java
+++ b/src/oca/java/share/examples/UserSample.java
@@ -22,6 +22,12 @@ public class UserSample
{
public static void main(String[] args)
{
+ // Let's try some of the OpenNebula Cloud API functionality.
+
+ // First of all, a Client object has to be created.
+ // Here the client will try to connect to OpenNebula using the default
+ // options: the auth. file will be assumed to be at $ONE_AUTH, and the
+ // endpoint will be set to the environment variable $ONE_XMLRPC.
Client oneClient;
try
@@ -34,17 +40,24 @@ public class UserSample
return;
}
+ // We will create a user pool and query some information.
+ // The info method retrieves and saves internally the information
+ // from OpenNebula.
UserPool userpool = new UserPool(oneClient);
OneResponse rc = userpool.info();
+ // The response can be an error, in which case we have access to a
+ // human-readable error message.
if (rc.isError())
{
System.out.println(rc.getErrorMessage());
return;
}
+ // Let's find out the current state of the users pool
printUserPool(userpool);
+ // Now we will try to allocate a new user
System.out.println("Allocating new user (javaUser,javaPassword)...");
rc = User.allocate(oneClient, "javaUser", "javaPassword");
@@ -54,22 +67,44 @@ public class UserSample
return;
}
- User javaUser = new User(Integer.parseInt(rc.getMessage()),oneClient);
+ // If the allocation was successful, then the response message contains
+ // the new user's ID.
+ int userID = Integer.parseInt( rc.getMessage() );
+ System.out.println("The allocation request returned this ID: " + userID);
+ // We can create a representation for the new user, using the returned
+ // user-ID
+ User javaUser = new User(userID, oneClient);
+
+ // And request its information
rc = javaUser.info();
+ // Alternatively we could have requested the user's info with the
+ // static info method:
+ // rc = User.info(oneClient, userID);
+ // and processed the xml returned in the message of the OneResponse.
+
if (rc.isError())
{
System.out.println(rc.getErrorMessage());
return;
}
+ // This is how the info returned looks like...
System.out.println("Info for " + javaUser.xpath("name") + "...");
System.out.println(rc.getMessage());
+ // Wait a second... what was that xpath method for?
+ // Now that we have the user's info loaded, we can use xpath expressions
+ // without parsing and initializing any xml, as simple as
+ // String name = javaUser.xpath("name");
+
+ // The user pool information is now outdated, so we need to call the
+ // info method again
userpool.info();
printUserPool(userpool);
+ // Let's delete this new user, using its ID
System.out.println("Deleting " + javaUser.getName() + "...");
rc = javaUser.delete();
@@ -79,6 +114,7 @@ public class UserSample
return;
}
+ // Now the pool information is outdated again, it is time to reload it.
userpool.info();
printUserPool(userpool);
}
@@ -89,6 +125,7 @@ public class UserSample
System.out.println("Number of users: " + up.getLength());
System.out.println("User ID\t\tName\t\tEnabled");
+ // You can use the for-each loops with the OpenNebula pools
for( User user : up )
{
String id = user.getId();
diff --git a/src/oca/java/share/examples/UserSample.sh b/src/oca/java/share/examples/UserSample.sh
new file mode 100755
index 0000000000..c664e0ac4d
--- /dev/null
+++ b/src/oca/java/share/examples/UserSample.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+java -cp ../../lib/*:../../jar/*:. UserSample
diff --git a/src/oca/java/share/examples/VMachineSample.java b/src/oca/java/share/examples/VMachineSample.java
new file mode 100644
index 0000000000..9e481b49e3
--- /dev/null
+++ b/src/oca/java/share/examples/VMachineSample.java
@@ -0,0 +1,185 @@
+/*******************************************************************************
+ * Copyright 2002-2010, 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+
+import org.opennebula.client.Client;
+import org.opennebula.client.OneResponse;
+import org.opennebula.client.vm.VirtualMachine;
+import org.opennebula.client.vm.VirtualMachinePool;
+
+public class VMachineSample{
+
+ public static void main(String[] args)
+ {
+ // Let's try some of the OpenNebula Cloud API functionality for VMs.
+
+ // First of all, a Client object has to be created.
+ // Here the client will try to connect to OpenNebula using the default
+ // options: the auth. file will be assumed to be at $ONE_AUTH, and the
+ // endpoint will be set to the environment variable $ONE_XMLRPC.
+ Client oneClient;
+
+ try
+ {
+ oneClient = new Client();
+
+ // We will try to create a new virtual machine. The first thing we
+ // need is an OpenNebula virtual machine template.
+
+ // This VM template is a valid one, but it will probably fail to run
+ // if we try to deploy it; the path for the image is unlikely to
+ // exist.
+ String vmTemplate =
+ "NAME = vm_from_java CPU = 0.1 MEMORY = 64\n"
+ + "DISK = [\n"
+ + "\tsource = \"/home/user/vmachines/ttylinux/ttylinux.img\",\n"
+ + "\ttarget = \"hda\",\n"
+ + "\treadonly = \"no\" ]\n"
+ + "# NIC = [ NETWORK = \"Non existing network\" ]\n"
+ + "FEATURES = [ acpi=\"no\" ]";
+
+ // You can try to uncomment the NIC line, in that case OpenNebula
+ // won't be able to insert this machine in the database.
+
+ System.out.println("Virtual Machine Template:\n" + vmTemplate);
+ System.out.println();
+
+ System.out.print("Trying to allocate the virtual machine... ");
+ OneResponse rc = VirtualMachine.allocate(oneClient, vmTemplate);
+
+ if( rc.isError() )
+ {
+ System.out.println( "failed!");
+ throw new Exception( rc.getErrorMessage() );
+ }
+
+ // The response message is the new VM's ID
+ int newVMID = Integer.parseInt(rc.getMessage());
+ System.out.println("ok, ID " + newVMID + ".");
+
+ // We can create a representation for the new VM, using the returned
+ // VM-ID
+ VirtualMachine vm = new VirtualMachine(newVMID, oneClient);
+
+ // Let's hold the VM, so the scheduler won't try to deploy it
+ System.out.print("Trying to hold the new VM... ");
+ rc = vm.hold();
+
+ if(rc.isError())
+ {
+ System.out.println("failed!");
+ throw new Exception( rc.getErrorMessage() );
+ }
+ else
+ System.out.println("ok.");
+
+ // And now we can request its information.
+ rc = vm.info();
+
+ if(rc.isError())
+ throw new Exception( rc.getErrorMessage() );
+
+ System.out.println();
+ System.out.println(
+ "This is the information OpenNebula stores for the new VM:");
+ System.out.println(rc.getMessage() + "\n");
+
+ // This VirtualMachine object has some helpers, so we can access its
+ // attributes easily (remember to load the data first using the info
+ // method).
+ System.out.println("The new VM " +
+ vm.getName() + " has status: " + vm.status());
+
+ // And we can also use xpath expressions
+ System.out.println("The path of the disk is");
+ System.out.println( "\t" + vm.xpath("template/disk/source") );
+
+ // Let's delete the VirtualMachine object.
+ vm = null;
+
+ // The reference is lost, but we can ask OpenNebula about the VM
+ // again. This time however, we are going to use the VM pool
+ VirtualMachinePool vmPool = new VirtualMachinePool(oneClient);
+ // Remember that we have to ask the pool to retrieve the information
+ // from OpenNebula
+ rc = vmPool.info();
+
+ if(rc.isError())
+ throw new Exception( rc.getErrorMessage() );
+
+ System.out.println(
+ "\nThese are all the Virtual Machines in the pool:");
+ for ( VirtualMachine vmachine : vmPool )
+ {
+ System.out.println("\tID :" + vmachine.getId() +
+ ", Name :" + vmachine.getName() );
+
+ // Check if we have found the VM we are looking for
+ if ( vmachine.getId().equals( ""+newVMID ) )
+ {
+ vm = vmachine;
+ }
+ }
+
+ // We have also some useful helpers for the actions you can perform
+ // on a virtual machine, like cancel:
+ rc = vm.cancel();
+ System.out.println("\nTrying to cancel the VM " + vm.getId() +
+ " (should fail)...");
+
+ // This is all the information you can get from the OneResponse:
+ System.out.println("\tOpenNebula response");
+ System.out.println("\t Error: " + rc.isError());
+ System.out.println("\t Msg: " + rc.getMessage());
+ System.out.println("\t ErrMsg: " + rc.getErrorMessage());
+
+ rc = vm.finalizeVM();
+ System.out.println("\nTrying to finalize (delete) the VM " +
+ vm.getId() + "...");
+
+ System.out.println("\tOpenNebula response");
+ System.out.println("\t Error: " + rc.isError());
+ System.out.println("\t Msg: " + rc.getMessage());
+ System.out.println("\t ErrMsg: " + rc.getErrorMessage());
+
+
+ }
+ catch (Exception e)
+ {
+ System.out.println(e.getMessage());
+ }
+
+
+ }
+
+ public static void printVMachinePool (VirtualMachinePool vmPool)
+ {
+ System.out.println("--------------------------------------------");
+ System.out.println("Number of VMs: " + vmPool.getLength());
+ System.out.println("User ID\t\tName\t\tEnabled");
+
+ // You can use the for-each loops with the OpenNebula pools
+ for( VirtualMachine vm : vmPool )
+ {
+ String id = vm.getId();
+ String name = vm.getName();
+ String enab = vm.xpath("enabled");
+
+ System.out.println(id+"\t\t"+name+"\t\t"+enab);
+ }
+
+ System.out.println("--------------------------------------------");
+ }
+}
diff --git a/src/oca/java/share/examples/VMachineSample.sh b/src/oca/java/share/examples/VMachineSample.sh
new file mode 100755
index 0000000000..b6dc89e7a5
--- /dev/null
+++ b/src/oca/java/share/examples/VMachineSample.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+java -cp ../../lib/*:../../jar/*:. VMachineSample
diff --git a/src/oca/java/src/org/opennebula/client/Client.java b/src/oca/java/src/org/opennebula/client/Client.java
index 1fea68b32d..de8b9499a9 100644
--- a/src/oca/java/src/org/opennebula/client/Client.java
+++ b/src/oca/java/src/org/opennebula/client/Client.java
@@ -36,7 +36,7 @@ import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
* xml-rpc calls.
*
*/
-public class Client {
+public class Client{
//--------------------------------------------------------------------------
// PUBLIC INTERFACE
@@ -54,7 +54,7 @@ public class Client {
public Client() throws Exception
{
setOneAuth(null);
- setOneEndPoint(null);
+ setOneEndPoint(null);
}
/**
@@ -160,7 +160,7 @@ public class Client {
if(token.length != 2 )
{
throw new Exception("Wrong format for authorization string: "
- + oneSecret);
+ + oneSecret + "\nFormat expected is user:password");
}
MessageDigest md = MessageDigest.getInstance("SHA-1");
diff --git a/src/oca/java/src/org/opennebula/client/OneResponse.java b/src/oca/java/src/org/opennebula/client/OneResponse.java
index 98201ec769..917258360c 100644
--- a/src/oca/java/src/org/opennebula/client/OneResponse.java
+++ b/src/oca/java/src/org/opennebula/client/OneResponse.java
@@ -20,7 +20,7 @@ package org.opennebula.client;
* carries a boolean indicating if it is an error. It can also contain a
* success message, or an error message.
*/
-public class OneResponse {
+public class OneResponse{
/**
* Creates a new response.
*
@@ -34,7 +34,7 @@ public class OneResponse {
this.success = success;
this.msg = message;
}
-
+
/**
* Returns true if the call resulted in error.
*
@@ -44,7 +44,7 @@ public class OneResponse {
{
return !success;
}
-
+
/**
* Returns a string containing the error message, or null
* if the response isn't an error.
@@ -56,7 +56,7 @@ public class OneResponse {
{
return success ? null : msg;
}
-
+
/**
* Returns a string containing the response information, or
* null if the response was an error. Note that the success
diff --git a/src/oca/java/src/org/opennebula/client/Pool.java b/src/oca/java/src/org/opennebula/client/Pool.java
index 8762ce5088..194e914984 100644
--- a/src/oca/java/src/org/opennebula/client/Pool.java
+++ b/src/oca/java/src/org/opennebula/client/Pool.java
@@ -99,7 +99,7 @@ public abstract class Pool{
*/
public PoolElement item(int index)
{
- PoolElement the_element = null;
+ PoolElement theElement = null;
if (poolElements != null)
{
@@ -107,11 +107,11 @@ public abstract class Pool{
if (node != null)
{
- the_element = factory(node);
+ theElement = factory(node);
}
}
- return the_element;
+ return theElement;
}
/**
@@ -122,4 +122,4 @@ public abstract class Pool{
{
return poolElements == null ? 0 : poolElements.getLength();
}
-}
\ No newline at end of file
+}
diff --git a/src/oca/java/src/org/opennebula/client/PoolElement.java b/src/oca/java/src/org/opennebula/client/PoolElement.java
index cc93e948fd..faf4c3985e 100644
--- a/src/oca/java/src/org/opennebula/client/PoolElement.java
+++ b/src/oca/java/src/org/opennebula/client/PoolElement.java
@@ -44,7 +44,6 @@ public abstract class PoolElement {
* Creates a new PoolElement with the specified attributes.
* @param id Id of the element.
* @param client XML-RPC Client.
- * @param root Name of the xml's root element.
*/
protected PoolElement(int id, Client client)
{
@@ -63,7 +62,6 @@ public abstract class PoolElement {
*
* @param client XML-RPC Client.
* @param xmlElement XML representation of the element.
- * @param root Name of the xml's root element.
*/
protected PoolElement(Node xmlElement, Client client)
{
diff --git a/src/oca/java/src/org/opennebula/client/package.html b/src/oca/java/src/org/opennebula/client/package.html
new file mode 100644
index 0000000000..bd3527ba32
--- /dev/null
+++ b/src/oca/java/src/org/opennebula/client/package.html
@@ -0,0 +1,37 @@
+
+
+
+Provides the Java bindings for the OpenNebula Cloud API (OCA). +
++The included classes can be used as simple wrappers for the XML-RPC calls +supported by the OpenNebula core, or as objects that store the information +and provide useful helpers. +
++Apart from this jar file, you need to add to the project's build-path the +Apache XML-RPC library. +
++You can download it from Apache's +distribution directory. +The jar files you need are xmlrpc-client-*.jar, ws-commons-util-*.jar and xmlrpc-common-*.jar.
+ ++Please visit the OpenNebula site for general information, +and consult the documentation section +to find up to date references, tutorials, and more. +
++You can learn about the OpenNebula XML-RPC API here. +
+ + + + + diff --git a/src/oca/java/src/org/opennebula/client/user/User.java b/src/oca/java/src/org/opennebula/client/user/User.java index 01c6382b56..931e35becb 100644 --- a/src/oca/java/src/org/opennebula/client/user/User.java +++ b/src/oca/java/src/org/opennebula/client/user/User.java @@ -25,106 +25,104 @@ import org.w3c.dom.Node; * This class represents an OpenNebula User. * It also offers static XML-RPC call wrappers. */ -public class User extends PoolElement { +public class User extends PoolElement{ -private static final String ROOT_NAME = "USER"; - - private static final String METHOD_PREFIX = "user."; - private static final String ALLOCATE = METHOD_PREFIX + "allocate"; - private static final String INFO = METHOD_PREFIX + "info"; - private static final String DELETE = METHOD_PREFIX + "delete"; - - /** - * Creates a new User representation. - * - * @param id The user id (uid). - * @param client XML-RPC Client. - */ - public User(int id, Client client) - { - super(id, client); - } + private static final String METHOD_PREFIX = "user."; + private static final String ALLOCATE = METHOD_PREFIX + "allocate"; + private static final String INFO = METHOD_PREFIX + "info"; + private static final String DELETE = METHOD_PREFIX + "delete"; + + /** + * Creates a new User representation. + * + * @param id The user id (uid). + * @param client XML-RPC Client. + */ + public User(int id, Client client) + { + super(id, client); + } - /** - * @see PoolElement - */ - protected User(Node xmlElement, Client client) - { + /** + * @see PoolElement + */ + protected User(Node xmlElement, Client client) + { super(xmlElement, client); - } - - - // ================================= - // Static XML-RPC methods - // ================================= - - /** - * Allocates a new user in OpenNebula. - * - * @param client XML-RPC Client. - * @param username Username for the new user. - * @param password Password for the new user - * @return If successful the message contains - * the associated id (int uid) generated for this user. - */ - public static OneResponse allocate(Client client, + } + + + // ================================= + // Static XML-RPC methods + // ================================= + + /** + * Allocates a new user in OpenNebula. + * + * @param client XML-RPC Client. + * @param username Username for the new user. + * @param password Password for the new user + * @return If successful the message contains + * the associated id (int uid) generated for this user. + */ + public static OneResponse allocate(Client client, String username, String password) { - return client.call(ALLOCATE, username, password); - } + return client.call(ALLOCATE, username, password); + } - /** Retrieves the information of the given user. - * - * @param client XML-RPC Client. - * @param id The user id (uid) for the user to - * retrieve the information from. - * @return if successful the message contains the - * string with the information about the user returned by OpenNebula. - */ - public static OneResponse info(Client client, int id) + /** Retrieves the information of the given user. + * + * @param client XML-RPC Client. + * @param id The user id (uid) for the user to + * retrieve the information from. + * @return if successful the message contains the + * string with the information about the user returned by OpenNebula. + */ + public static OneResponse info(Client client, int id) { - return client.call(INFO, id); - } - - /** - * Deletes a user from OpenNebula. - * - * @param client XML-RPC Client. - * @param id The user id (uid) of the target user we want to delete. - * @return If an error occurs the error message contains the reason. - */ - public static OneResponse delete(Client client, int id) + return client.call(INFO, id); + } + + /** + * Deletes a user from OpenNebula. + * + * @param client XML-RPC Client. + * @param id The user id (uid) of the target user we want to delete. + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse delete(Client client, int id) { - return client.call(DELETE, id); - } + return client.call(DELETE, id); + } - // ================================= - // Instanced object XML-RPC methods - // ================================= - - /** - * Loads the xml representation of the user. - * The info is also stored internally. - * - * @see User#info(Client, int) - */ - public OneResponse info() + // ================================= + // Instanced object XML-RPC methods + // ================================= + + /** + * Loads the xml representation of the user. + * The info is also stored internally. + * + * @see User#info(Client, int) + */ + public OneResponse info() { - OneResponse response = info(client, id); + OneResponse response = info(client, id); - super.processInfo(response); + super.processInfo(response); - return response; - } - - /** - * Deletes the user from OpenNebula. - * - * @see User#delete(Client, int) - */ - public OneResponse delete() + return response; + } + + /** + * Deletes the user from OpenNebula. + * + * @see User#delete(Client, int) + */ + public OneResponse delete() { - return delete(client, id); - } + return delete(client, id); + } } diff --git a/src/oca/java/src/org/opennebula/client/user/UserPool.java b/src/oca/java/src/org/opennebula/client/user/UserPool.java index 3fb8988c7b..893a51725a 100644 --- a/src/oca/java/src/org/opennebula/client/user/UserPool.java +++ b/src/oca/java/src/org/opennebula/client/user/UserPool.java @@ -29,56 +29,53 @@ import org.w3c.dom.Node; * This class represents an OpenNebula user pool. * It also offers static XML-RPC call wrappers. */ -public class UserPool extends Pool implements Iterable