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