17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate package com.sun.solaris.service.pools;
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate import java.util.List;
317c478bd9Sstevel@tonic-gate import java.util.ArrayList;
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /**
347c478bd9Sstevel@tonic-gate  * The <code>Pool</code> class represents a Resource Pool.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate public class Pool extends Element {
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 	/**
397c478bd9Sstevel@tonic-gate 	 * The name of this instance.
407c478bd9Sstevel@tonic-gate 	 */
417c478bd9Sstevel@tonic-gate 	private final String name;
427c478bd9Sstevel@tonic-gate 	/**
437c478bd9Sstevel@tonic-gate 	 * The key of the pool.
447c478bd9Sstevel@tonic-gate 	 */
457c478bd9Sstevel@tonic-gate 	private final String key;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	/**
487c478bd9Sstevel@tonic-gate 	 * Constructor
497c478bd9Sstevel@tonic-gate 	 * @param conf The configuration to which this pool belongs.
507c478bd9Sstevel@tonic-gate 	 * @param pool The pointer to the native pool which this object wraps.
517c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If accessing the proxy fails.
527c478bd9Sstevel@tonic-gate 	 */
Pool(Configuration conf, long pool)537c478bd9Sstevel@tonic-gate 	Pool(Configuration conf, long pool) throws PoolsException
547c478bd9Sstevel@tonic-gate 	{
557c478bd9Sstevel@tonic-gate 		_conf = conf;
567c478bd9Sstevel@tonic-gate 		Value val = getProperty("pool.name", pool);
577c478bd9Sstevel@tonic-gate 		name = val.getString();
587c478bd9Sstevel@tonic-gate 		val.close();
597c478bd9Sstevel@tonic-gate 		key = "pool." + name;
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate         /**
637c478bd9Sstevel@tonic-gate          * Returns a pointer to the native pool represented by this
647c478bd9Sstevel@tonic-gate          * pool object.
657c478bd9Sstevel@tonic-gate 	 *
667c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If the pool cannot be located.
677c478bd9Sstevel@tonic-gate          * @return a pointer to the native pool represented by this
687c478bd9Sstevel@tonic-gate          * pool object.
697c478bd9Sstevel@tonic-gate 	 */
getPool()707c478bd9Sstevel@tonic-gate 	long getPool() throws PoolsException
717c478bd9Sstevel@tonic-gate 	{
727c478bd9Sstevel@tonic-gate 		return (_conf.checkPool(name));
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate         /**
767c478bd9Sstevel@tonic-gate          * Associate this pool with the supplied resource.
777c478bd9Sstevel@tonic-gate          *
787c478bd9Sstevel@tonic-gate          * @param res A resource in the same configuration as this pool.
797c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If there is an error whilst associating the
807c478bd9Sstevel@tonic-gate 	 * resource with the pool.
817c478bd9Sstevel@tonic-gate          */
associate(Resource res)827c478bd9Sstevel@tonic-gate 	public void associate(Resource res) throws PoolsException
837c478bd9Sstevel@tonic-gate 	{
847c478bd9Sstevel@tonic-gate 		if (PoolInternal.pool_associate(_conf.getConf(), getPool(),
857c478bd9Sstevel@tonic-gate 		    res.getResource()) != PoolInternal.PO_SUCCESS)
867c478bd9Sstevel@tonic-gate 			throw new PoolsException();
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate         /**
907c478bd9Sstevel@tonic-gate          * Dissociate this pool from the supplied resource.
917c478bd9Sstevel@tonic-gate          *
927c478bd9Sstevel@tonic-gate          * @param res A resource in the same configuration as this pool.
937c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If there is an error whilst dissociating the
947c478bd9Sstevel@tonic-gate 	 * resource from the pool.
957c478bd9Sstevel@tonic-gate          */
dissociate(Resource res)967c478bd9Sstevel@tonic-gate 	public void dissociate(Resource res) throws PoolsException
977c478bd9Sstevel@tonic-gate 	{
987c478bd9Sstevel@tonic-gate 		if (PoolInternal.pool_dissociate(_conf.getConf(), getPool(),
997c478bd9Sstevel@tonic-gate 		    res.getResource()) != PoolInternal.PO_SUCCESS)
1007c478bd9Sstevel@tonic-gate 			throw new PoolsException();
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/**
1047c478bd9Sstevel@tonic-gate 	 * Get a list of resources which match the supplied selection criteria
1057c478bd9Sstevel@tonic-gate 	 * in values. Only resources which are associated with this pool are
1067c478bd9Sstevel@tonic-gate 	 * searched.
1077c478bd9Sstevel@tonic-gate 	 *
1087c478bd9Sstevel@tonic-gate 	 * @param values A list of values to be used to qualify the search.
109*99308ed0SPeter Tribble 	 * @throws PoolsException If there is an error executing the query.
1107c478bd9Sstevel@tonic-gate 	 * @return a list of resources which match the supplied criteria
1117c478bd9Sstevel@tonic-gate 	 */
getResources(List values)1127c478bd9Sstevel@tonic-gate 	public List getResources(List values) throws PoolsException
1137c478bd9Sstevel@tonic-gate 	{
1147c478bd9Sstevel@tonic-gate 		List resources;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		if ((resources = PoolInternal.pool_query_pool_resources(
1177c478bd9Sstevel@tonic-gate 			 _conf.getConf(), getPool(), values)) == null) {
11826d8ba22Sgarypen 			if (PoolInternal.pool_error() ==
1197c478bd9Sstevel@tonic-gate 			    PoolInternal.POE_INVALID_SEARCH)
1207c478bd9Sstevel@tonic-gate 				return new ArrayList();
1217c478bd9Sstevel@tonic-gate 			else
1227c478bd9Sstevel@tonic-gate 				throw new PoolsException();
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 		ArrayList aList = new ArrayList(resources.size());
1257c478bd9Sstevel@tonic-gate 		for (int i = 0; i < resources.size(); i++)
1267c478bd9Sstevel@tonic-gate 			aList.add(new Resource(_conf,
1277c478bd9Sstevel@tonic-gate 			    ((Long)resources.get(i)).longValue()));
1287c478bd9Sstevel@tonic-gate 		return (aList);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/**
1327c478bd9Sstevel@tonic-gate 	 * Returns a descriptive string which describes the pool.
1337c478bd9Sstevel@tonic-gate 	 *
1347c478bd9Sstevel@tonic-gate 	 * @param deep Whether the information should contain information about
1357c478bd9Sstevel@tonic-gate 	 * all contained elements.
1367c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If the pool cannot be located.
1377c478bd9Sstevel@tonic-gate 	 * @return a descriptive string which describes the pool.
1387c478bd9Sstevel@tonic-gate 	 */
getInformation(int deep)1397c478bd9Sstevel@tonic-gate 	public String getInformation(int deep) throws PoolsException
1407c478bd9Sstevel@tonic-gate 	{
1417c478bd9Sstevel@tonic-gate 		return (PoolInternal.pool_info(_conf.getConf(), getPool(),
1427c478bd9Sstevel@tonic-gate 			    deep));
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate         /**
1467c478bd9Sstevel@tonic-gate          * Returns a string representation of this pool.
1477c478bd9Sstevel@tonic-gate          *
1487c478bd9Sstevel@tonic-gate          * @return  a string representation of this pool.
1497c478bd9Sstevel@tonic-gate          */
toString()1507c478bd9Sstevel@tonic-gate 	public String toString()
1517c478bd9Sstevel@tonic-gate 	{
1527c478bd9Sstevel@tonic-gate 		StringBuffer buf = new StringBuffer();
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		buf.append("pool: ");
1557c478bd9Sstevel@tonic-gate 		buf.append(name);
1567c478bd9Sstevel@tonic-gate 		return (buf.toString());
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/**
1607c478bd9Sstevel@tonic-gate 	 * Indicates whether some other Pool is "equal to this one.
1617c478bd9Sstevel@tonic-gate 	 * @param o the reference object with which to compare.
1627c478bd9Sstevel@tonic-gate 	 * @return <code>true</code> if this object is the same as the
1637c478bd9Sstevel@tonic-gate 	 * o argument; <code>false</code> otherwise.
1647c478bd9Sstevel@tonic-gate 	 * @see	#hashCode()
1657c478bd9Sstevel@tonic-gate 	 */
equals(Object o)1667c478bd9Sstevel@tonic-gate 	public boolean equals(Object o)
1677c478bd9Sstevel@tonic-gate 	{
1687c478bd9Sstevel@tonic-gate 		if (o == this)
1697c478bd9Sstevel@tonic-gate 			return (true);
1707c478bd9Sstevel@tonic-gate 		if (!(o instanceof Pool))
1717c478bd9Sstevel@tonic-gate 			return (false);
1727c478bd9Sstevel@tonic-gate 		Pool other = (Pool) o;
1737c478bd9Sstevel@tonic-gate 		if (name.compareTo(other.getName()) != 0)
1747c478bd9Sstevel@tonic-gate 			return (false);
1757c478bd9Sstevel@tonic-gate 		return (true);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/**
1797c478bd9Sstevel@tonic-gate 	 * Returns a hash code value for the object. This method is
1807c478bd9Sstevel@tonic-gate 	 * supported for the benefit of hashtables such as those provided by
1817c478bd9Sstevel@tonic-gate 	 * <code>java.util.Hashtable</code>.
1827c478bd9Sstevel@tonic-gate 	 *
1837c478bd9Sstevel@tonic-gate 	 * @return a hash code value for this object.
1847c478bd9Sstevel@tonic-gate 	 * @see	#equals(java.lang.Object)
1857c478bd9Sstevel@tonic-gate 	 * @see	java.util.Hashtable
1867c478bd9Sstevel@tonic-gate 	 */
hashCode()1877c478bd9Sstevel@tonic-gate 	public int hashCode()
1887c478bd9Sstevel@tonic-gate 	{
1897c478bd9Sstevel@tonic-gate 		return (name.hashCode());
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/**
1937c478bd9Sstevel@tonic-gate 	 * Return the pointer to this pool as an element.
1947c478bd9Sstevel@tonic-gate 	 *
1957c478bd9Sstevel@tonic-gate 	 * @return The pointer to the native pool which this object wraps.
196*99308ed0SPeter Tribble 	 * @throws PoolsException If there is an error converting the native
1977c478bd9Sstevel@tonic-gate 	 * pool pointer to a native elem pointer.
1987c478bd9Sstevel@tonic-gate 	 */
getElem()1997c478bd9Sstevel@tonic-gate 	protected long getElem() throws PoolsException
2007c478bd9Sstevel@tonic-gate 	{
2017c478bd9Sstevel@tonic-gate 		long elem;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		if ((elem = PoolInternal.pool_to_elem(_conf.getConf(),
2047c478bd9Sstevel@tonic-gate 		    getPool())) == 0)
2057c478bd9Sstevel@tonic-gate 			throw new PoolsException();
2067c478bd9Sstevel@tonic-gate 		return (elem);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/**
2107c478bd9Sstevel@tonic-gate 	 * Return the name of the pool.
2117c478bd9Sstevel@tonic-gate 	 */
getName()2127c478bd9Sstevel@tonic-gate 	String getName()
2137c478bd9Sstevel@tonic-gate 	{
2147c478bd9Sstevel@tonic-gate 		return (name);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/**
2187c478bd9Sstevel@tonic-gate 	 * Return the key of the pool.
2197c478bd9Sstevel@tonic-gate 	 */
getKey()2207c478bd9Sstevel@tonic-gate 	String getKey()
2217c478bd9Sstevel@tonic-gate 	{
2227c478bd9Sstevel@tonic-gate 		return (key);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate }
225