1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 *ident "%Z%%M% %I% %E% SMI" 27 */ 28 29 package com.sun.solaris.service.pools; 30 31 import java.util.List; 32 import java.util.ArrayList; 33 34 /** 35 * The <code>Resource</code> class represents a resource. 36 */ 37 public class Resource extends Element 38 { 39 /** 40 * The type of the resource. 41 */ 42 private final String type; 43 /** 44 * The system id of the resource. 45 */ 46 private final String name; 47 /** 48 * The key of the resource. 49 */ 50 private final String key; 51 52 /** 53 * Constructor 54 * @param conf The configuration to which this pool belongs. 55 * @param resource The pointer to the native resource which 56 * this object wraps. 57 * @throws PoolsException If accessing the proxy fails. 58 */ 59 Resource(Configuration conf, long resource) throws PoolsException 60 { 61 _conf = conf; 62 Value val = getProperty("type", resource); 63 type = val.getString(); 64 val.close(); 65 val = getProperty(type + ".name", resource); 66 name = val.getString(); 67 val.close(); 68 key = type + "." + name; 69 } 70 71 /** 72 * Returns a pointer to the native resouce represented by this resource 73 * object. 74 * 75 * @throws PoolsException If the pool cannot be located. 76 * @return a pointer to the native resource represented by this 77 * resource object. 78 */ 79 long getResource() throws PoolsException 80 { 81 return (_conf.checkResource(type, name)); 82 } 83 84 /** 85 * Transfer the requested quantity of resource from the donor to this 86 * resource. 87 * 88 * @param donor A donating resource. 89 * @param qty Amount of resource to be donated. 90 * @throws PoolsException If there is an error whilst donating the 91 * resource. 92 */ 93 public void transfer(Resource donor, long qty) throws PoolsException 94 { 95 if (PoolInternal.pool_resource_transfer(_conf.getConf(), 96 donor.getResource(), getResource(), qty) != 97 PoolInternal.PO_SUCCESS) 98 throw new PoolsException(); 99 } 100 101 /** 102 * Transfer the specified resource components from the donor to this 103 * resource. 104 * 105 * @param donor A donating resource. 106 * @param components A list of resource components to be donated. 107 * @throws PoolsException If there is an error whilst donating the 108 * resource components. 109 */ 110 public void transfer(Resource donor, List components) 111 throws PoolsException 112 { 113 if (PoolInternal.pool_resource_xtransfer(_conf.getConf(), 114 donor.getResource(), getResource(), components) != 115 PoolInternal.PO_SUCCESS) 116 throw new PoolsException(); 117 } 118 119 /** 120 * Get a list of components which match the supplied selection 121 * criteria in values. Only components which are controlled by 122 * this resource are searched. 123 * 124 * @param values A list of values to be used to qualify the search. 125 * @throws PoolsExecption If there is an error executing the query. 126 * @return a list of components which match the supplied criteria 127 */ 128 public List getComponents(List values) throws PoolsException 129 { 130 List components; 131 132 if ((components = PoolInternal.pool_query_resource_components( 133 _conf.getConf(), getResource(), values)) == null) { 134 if (PoolInternal.pool_error() == 0 || 135 PoolInternal.pool_error() == 136 PoolInternal.POE_INVALID_SEARCH) 137 return new ArrayList(); 138 else 139 throw new PoolsException(); 140 } 141 ArrayList aList = new ArrayList(components.size()); 142 for (int i = 0; i < components.size(); i++) 143 aList.add(new Component(_conf, 144 ((Long)components.get(i)).longValue())); 145 return (aList); 146 } 147 148 /** 149 * Returns a descriptive string which describes the resource. 150 * 151 * @param deep Whether the information should contain information about 152 * all contained elements. 153 * @throws PoolsException If the resource cannot be located. 154 * @return a descriptive string which describes the resource. 155 */ 156 public String getInformation(int deep) throws PoolsException 157 { 158 return (PoolInternal.pool_resource_info(_conf.getConf(), 159 getResource(), deep)); 160 } 161 162 /** 163 * Returns a string representation of this resource. 164 * 165 * @return a string representation of this resource. 166 */ 167 public String toString() 168 { 169 StringBuffer buf = new StringBuffer(); 170 171 buf.append(type); 172 buf.append(" "); 173 buf.append(name); 174 return (buf.toString()); 175 } 176 177 /** 178 * Indicates whether some other Resource is "equal to this one. 179 * @param o the reference object with which to compare. 180 * @return <code>true</code> if this object is the same as the 181 * o argument; <code>false</code> otherwise. 182 * @see #hashCode() 183 */ 184 public boolean equals(Object o) 185 { 186 if (o == this) 187 return (true); 188 if (!(o instanceof Resource)) 189 return (false); 190 Resource other = (Resource) o; 191 if (type.compareTo(other.getType()) != 0 || 192 name.compareTo(other.getName()) != 0) 193 return (false); 194 return (true); 195 } 196 197 /** 198 * Returns a hash code value for the object. This method is 199 * supported for the benefit of hashtables such as those provided by 200 * <code>java.util.Hashtable</code>. 201 * 202 * @return a hash code value for this object. 203 * @see #equals(java.lang.Object) 204 * @see java.util.Hashtable 205 */ 206 public int hashCode() 207 { 208 return (type.hashCode() + name.hashCode()); 209 } 210 211 /** 212 * Return the pointer to this resource as an element. 213 * 214 * @return The pointer to the native resource which this object wraps. 215 * @throws PoolsExecption If there is an error converting the native 216 * resource pointer to a native elem pointer. 217 */ 218 protected long getElem() throws PoolsException 219 { 220 long elem; 221 222 if ((elem = PoolInternal.pool_resource_to_elem(_conf.getConf(), 223 getResource())) == 0) 224 throw new PoolsException(); 225 return (elem); 226 } 227 228 /** 229 * Return the type of the resource 230 */ 231 String getType() 232 { 233 return (type); 234 } 235 236 /** 237 * Return the name of the resource. 238 */ 239 String getName() 240 { 241 return (name); 242 } 243 244 /** 245 * Return the key of the resource. 246 */ 247 String getKey() 248 { 249 return (key); 250 } 251 } 252