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