From ffc71b6fc12638e551d933f5a608063b4e71192f Mon Sep 17 00:00:00 2001 From: "Carlos Martin and Ruben S. Montero" Date: Sat, 20 Mar 2010 22:42:32 +0100 Subject: [PATCH] feature #198: User related classes for the OCA-JAVA Bindings --- .../src/org/opennebula/client/user/User.java | 130 ++++++++++++++++++ .../org/opennebula/client/user/UserPool.java | 84 +++++++++++ 2 files changed, 214 insertions(+) create mode 100644 src/oca/java/src/org/opennebula/client/user/User.java create mode 100644 src/oca/java/src/org/opennebula/client/user/UserPool.java diff --git a/src/oca/java/src/org/opennebula/client/user/User.java b/src/oca/java/src/org/opennebula/client/user/User.java new file mode 100644 index 0000000000..01c6382b56 --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/user/User.java @@ -0,0 +1,130 @@ +/******************************************************************************* + * 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. + ******************************************************************************/ +package org.opennebula.client.user; + +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.PoolElement; +import org.w3c.dom.Node; + + +/** + * This class represents an OpenNebula User. + * It also offers static XML-RPC call wrappers. + */ +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); + } + + /** + * @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, + String username, + String 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) + { + 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); + } + + // ================================= + // 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); + + super.processInfo(response); + + return response; + } + + /** + * Deletes the user from OpenNebula. + * + * @see User#delete(Client, int) + */ + public OneResponse delete() + { + 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 new file mode 100644 index 0000000000..3fb8988c7b --- /dev/null +++ b/src/oca/java/src/org/opennebula/client/user/UserPool.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * 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. + ******************************************************************************/ +package org.opennebula.client.user; + +import java.util.AbstractList; +import java.util.Iterator; + + +import org.opennebula.client.Client; +import org.opennebula.client.OneResponse; +import org.opennebula.client.Pool; +import org.opennebula.client.PoolElement; +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 +{ + + private static final String ELEMENT_NAME = "USER"; + private static final String INFO_METHOD = "userpool.info"; + + /** + * Creates a new user pool + * @param client XML-RPC Client. + */ + public UserPool(Client client) + { + super(ELEMENT_NAME, client); + } + + @Override + public PoolElement factory(Node node) + { + return new User(node, client); + } + + /** + * Loads the xml representation of the user pool. + * + * @see UserPool#info(Client) + */ + public OneResponse info() + { + OneResponse response = client.call(INFO_METHOD); + + super.processInfo(response); + + return response; + } + + public Iterator iterator() + { + AbstractList ab = new AbstractList() + { + public int size() + { + return getLength(); + } + + public User get(int index) + { + return (User) item(index); + } + }; + + return ab.iterator(); + } +}