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 27 package com.sun.solaris.service.pools; 28 29 import java.util.List; 30 import java.util.ArrayList; 31 32 /** 33 * The <code>Resource</code> class represents a resource. 34 */ 35 public class Resource extends Element 36 { 37 /** 38 * The type of the resource. 39 */ 40 private final String type; 41 /** 42 * The system id of the resource. 43 */ 44 private final String name; 45 /** 46 * The key of the resource. 47 */ 48 private final String key; 49 50 /** 51 * Constructor 52 * @param conf The configuration to which this pool belongs. 53 * @param resource The pointer to the native resource which 54 * this object wraps. 55 * @throws PoolsException If accessing the proxy fails. 56 */ 57 Resource(Configuration conf, long resource) throws PoolsException 58 { 59 _conf = conf; 60 Value val = getProperty("type", resource); 61 type = val.getString(); 62 val.close(); 63 val = getProperty(type + ".name", resource); 64 name = val.getString(); 65 val.close(); 66 key = type + "." + name; 67 } 68 69 /** 70 * Returns a pointer to the native resource represented by this resource 71 * object. 72 * 73 * @throws PoolsException If the pool cannot be located. 74 * @return a pointer to the native resource represented by this 75 * resource object. 76 */ 77 long getResource() throws PoolsException 78 { 79 return (_conf.checkResource(type, name)); 80 } 81 82 /** 83 * Transfer the requested quantity of resource from the donor to this 84 * resource. 85 * 86 * @param donor A donating resource. 87 * @param qty Amount of resource to be donated. 88 * @throws PoolsException If there is an error whilst donating the 89 * resource. 90 */ 91 public void transfer(Resource donor, long qty) throws PoolsException 92 { 93 if (PoolInternal.pool_resource_transfer(_conf.getConf(), 94 donor.getResource(), getResource(), qty) != 95 PoolInternal.PO_SUCCESS) 96 throw new PoolsException(); 97 } 98 99 /** 100 * Transfer the specified resource components from the donor to this 101 * resource. 102 * 103 * @param donor A donating resource. 104 * @param components A list of resource components to be donated. 105 * @throws PoolsException If there is an error whilst donating the 106 * resource components. 107 */ 108 public void transfer(Resource donor, List components) 109 throws PoolsException 110 { 111 if (PoolInternal.pool_resource_xtransfer(_conf.getConf(), 112 donor.getResource(), getResource(), components) != 113 PoolInternal.PO_SUCCESS) 114 throw new PoolsException(); 115 } 116 117 /** 118 * Get a list of components which match the supplied selection 119 * criteria in values. Only components which are controlled by 120 * this resource are searched. 121 * 122 * @param values A list of values to be used to qualify the search. 123 * @throws PoolsException If there is an error executing the query. 124 * @return a list of components which match the supplied criteria 125 */ 126 public List getComponents(List values) throws PoolsException 127 { 128 List components; 129 130 if ((components = PoolInternal.pool_query_resource_components( 131 _conf.getConf(), getResource(), values)) == null) { 132 if (PoolInternal.pool_error() == 133 PoolInternal.POE_INVALID_SEARCH) 134 return new ArrayList(); 135 else 136 throw new PoolsException(); 137 } 138 ArrayList aList = new ArrayList(components.size()); 139 for (int i = 0; i < components.size(); i++) 140 aList.add(new Component(_conf, 141 ((Long)components.get(i)).longValue())); 142 return (aList); 143 } 144 145 /** 146 * Returns a descriptive string which describes the resource. 147 * 148 * @param deep Whether the information should contain information about 149 * all contained elements. 150 * @throws PoolsException If the resource cannot be located. 151 * @return a descriptive string which describes the resource. 152 */ 153 public String getInformation(int deep) throws PoolsException 154 { 155 return (PoolInternal.pool_resource_info(_conf.getConf(), 156 getResource(), deep)); 157 } 158 159 /** 160 * Returns a string representation of this resource. 161 * 162 * @return a string representation of this resource. 163 */ 164 public String toString() 165 { 166 StringBuffer buf = new StringBuffer(); 167 168 buf.append(type); 169 buf.append(" "); 170 buf.append(name); 171 return (buf.toString()); 172 } 173 174 /** 175 * Indicates whether some other Resource is "equal to this one. 176 * @param o the reference object with which to compare. 177 * @return <code>true</code> if this object is the same as the 178 * o argument; <code>false</code> otherwise. 179 * @see #hashCode() 180 */ 181 public boolean equals(Object o) 182 { 183 if (o == this) 184 return (true); 185 if (!(o instanceof Resource)) 186 return (false); 187 Resource other = (Resource) o; 188 if (type.compareTo(other.getType()) != 0 || 189 name.compareTo(other.getName()) != 0) 190 return (false); 191 return (true); 192 } 193 194 /** 195 * Returns a hash code value for the object. This method is 196 * supported for the benefit of hashtables such as those provided by 197 * <code>java.util.Hashtable</code>. 198 * 199 * @return a hash code value for this object. 200 * @see #equals(java.lang.Object) 201 * @see java.util.Hashtable 202 */ 203 public int hashCode() 204 { 205 return (type.hashCode() + name.hashCode()); 206 } 207 208 /** 209 * Return the pointer to this resource as an element. 210 * 211 * @return The pointer to the native resource which this object wraps. 212 * @throws PoolsException If there is an error converting the native 213 * resource pointer to a native elem pointer. 214 */ 215 protected long getElem() throws PoolsException 216 { 217 long elem; 218 219 if ((elem = PoolInternal.pool_resource_to_elem(_conf.getConf(), 220 getResource())) == 0) 221 throw new PoolsException(); 222 return (elem); 223 } 224 225 /** 226 * Return the type of the resource 227 */ 228 String getType() 229 { 230 return (type); 231 } 232 233 /** 234 * Return the name of the resource. 235 */ 236 String getName() 237 { 238 return (name); 239 } 240 241 /** 242 * Return the key of the resource. 243 */ 244 String getKey() 245 { 246 return (key); 247 } 248 } 249