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() == 135 PoolInternal.POE_INVALID_SEARCH) 136 return new ArrayList(); 137 else 138 throw new PoolsException(); 139 } 140 ArrayList aList = new ArrayList(components.size()); 141 for (int i = 0; i < components.size(); i++) 142 aList.add(new Component(_conf, 143 ((Long)components.get(i)).longValue())); 144 return (aList); 145 } 146 147 /** 148 * Returns a descriptive string which describes the resource. 149 * 150 * @param deep Whether the information should contain information about 151 * all contained elements. 152 * @throws PoolsException If the resource cannot be located. 153 * @return a descriptive string which describes the resource. 154 */ 155 public String getInformation(int deep) throws PoolsException 156 { 157 return (PoolInternal.pool_resource_info(_conf.getConf(), 158 getResource(), deep)); 159 } 160 161 /** 162 * Returns a string representation of this resource. 163 * 164 * @return a string representation of this resource. 165 */ 166 public String toString() 167 { 168 StringBuffer buf = new StringBuffer(); 169 170 buf.append(type); 171 buf.append(" "); 172 buf.append(name); 173 return (buf.toString()); 174 } 175 176 /** 177 * Indicates whether some other Resource is "equal to this one. 178 * @param o the reference object with which to compare. 179 * @return <code>true</code> if this object is the same as the 180 * o argument; <code>false</code> otherwise. 181 * @see #hashCode() 182 */ 183 public boolean equals(Object o) 184 { 185 if (o == this) 186 return (true); 187 if (!(o instanceof Resource)) 188 return (false); 189 Resource other = (Resource) o; 190 if (type.compareTo(other.getType()) != 0 || 191 name.compareTo(other.getName()) != 0) 192 return (false); 193 return (true); 194 } 195 196 /** 197 * Returns a hash code value for the object. This method is 198 * supported for the benefit of hashtables such as those provided by 199 * <code>java.util.Hashtable</code>. 200 * 201 * @return a hash code value for this object. 202 * @see #equals(java.lang.Object) 203 * @see java.util.Hashtable 204 */ 205 public int hashCode() 206 { 207 return (type.hashCode() + name.hashCode()); 208 } 209 210 /** 211 * Return the pointer to this resource as an element. 212 * 213 * @return The pointer to the native resource which this object wraps. 214 * @throws PoolsExecption If there is an error converting the native 215 * resource pointer to a native elem pointer. 216 */ 217 protected long getElem() throws PoolsException 218 { 219 long elem; 220 221 if ((elem = PoolInternal.pool_resource_to_elem(_conf.getConf(), 222 getResource())) == 0) 223 throw new PoolsException(); 224 return (elem); 225 } 226 227 /** 228 * Return the type of the resource 229 */ 230 String getType() 231 { 232 return (type); 233 } 234 235 /** 236 * Return the name of the resource. 237 */ 238 String getName() 239 { 240 return (name); 241 } 242 243 /** 244 * Return the key of the resource. 245 */ 246 String getKey() 247 { 248 return (key); 249 } 250 } 251