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 package com.sun.solaris.service.pools;
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate import java.util.List;
307c478bd9Sstevel@tonic-gate import java.util.ArrayList;
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /**
337c478bd9Sstevel@tonic-gate  * The <code>Resource</code> class represents a resource.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate public class Resource extends Element
367c478bd9Sstevel@tonic-gate {
377c478bd9Sstevel@tonic-gate 	/**
387c478bd9Sstevel@tonic-gate 	 * The type of the resource.
397c478bd9Sstevel@tonic-gate 	 */
407c478bd9Sstevel@tonic-gate 	private final String type;
417c478bd9Sstevel@tonic-gate 	/**
427c478bd9Sstevel@tonic-gate 	 * The system id of the resource.
437c478bd9Sstevel@tonic-gate 	 */
447c478bd9Sstevel@tonic-gate 	private final String name;
457c478bd9Sstevel@tonic-gate 	/**
467c478bd9Sstevel@tonic-gate 	 * The key of the resource.
477c478bd9Sstevel@tonic-gate 	 */
487c478bd9Sstevel@tonic-gate 	private final String key;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	/**
517c478bd9Sstevel@tonic-gate 	 * Constructor
527c478bd9Sstevel@tonic-gate 	 * @param conf The configuration to which this pool belongs.
537c478bd9Sstevel@tonic-gate 	 * @param resource The pointer to the native resource which
547c478bd9Sstevel@tonic-gate 	 * this object wraps.
557c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If accessing the proxy fails.
567c478bd9Sstevel@tonic-gate 	 */
Resource(Configuration conf, long resource)577c478bd9Sstevel@tonic-gate 	Resource(Configuration conf, long resource) throws PoolsException
587c478bd9Sstevel@tonic-gate 	{
597c478bd9Sstevel@tonic-gate 		_conf = conf;
607c478bd9Sstevel@tonic-gate 		Value val = getProperty("type", resource);
617c478bd9Sstevel@tonic-gate 		type = val.getString();
627c478bd9Sstevel@tonic-gate 		val.close();
637c478bd9Sstevel@tonic-gate 		val = getProperty(type + ".name", resource);
647c478bd9Sstevel@tonic-gate 		name = val.getString();
657c478bd9Sstevel@tonic-gate 		val.close();
667c478bd9Sstevel@tonic-gate 		key = type + "." + name;
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate         /**
70*99308ed0SPeter Tribble          * Returns a pointer to the native resource represented by this resource
717c478bd9Sstevel@tonic-gate 	 * object.
727c478bd9Sstevel@tonic-gate          *
737c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If the pool cannot be located.
747c478bd9Sstevel@tonic-gate          * @return a pointer to the native resource represented by this
757c478bd9Sstevel@tonic-gate 	 * resource object.
767c478bd9Sstevel@tonic-gate          */
getResource()777c478bd9Sstevel@tonic-gate 	long getResource() throws PoolsException
787c478bd9Sstevel@tonic-gate 	{
797c478bd9Sstevel@tonic-gate 		return (_conf.checkResource(type, name));
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate         /**
837c478bd9Sstevel@tonic-gate          * Transfer the requested quantity of resource from the donor to this
847c478bd9Sstevel@tonic-gate 	 * resource.
857c478bd9Sstevel@tonic-gate          *
867c478bd9Sstevel@tonic-gate          * @param donor A donating resource.
877c478bd9Sstevel@tonic-gate          * @param qty Amount of resource to be donated.
887c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If there is an error whilst donating the
897c478bd9Sstevel@tonic-gate 	 * resource.
907c478bd9Sstevel@tonic-gate          */
transfer(Resource donor, long qty)917c478bd9Sstevel@tonic-gate 	public void transfer(Resource donor, long qty) throws PoolsException
927c478bd9Sstevel@tonic-gate 	{
937c478bd9Sstevel@tonic-gate 		if (PoolInternal.pool_resource_transfer(_conf.getConf(),
947c478bd9Sstevel@tonic-gate 		    donor.getResource(), getResource(), qty) !=
957c478bd9Sstevel@tonic-gate 		    PoolInternal.PO_SUCCESS)
967c478bd9Sstevel@tonic-gate 			throw new PoolsException();
97*99308ed0SPeter Tribble 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate         /**
1007c478bd9Sstevel@tonic-gate          * Transfer the specified resource components from the donor to this
1017c478bd9Sstevel@tonic-gate 	 * resource.
1027c478bd9Sstevel@tonic-gate          *
1037c478bd9Sstevel@tonic-gate          * @param donor A donating resource.
1047c478bd9Sstevel@tonic-gate          * @param components A list of resource components to be donated.
1057c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If there is an error whilst donating the
1067c478bd9Sstevel@tonic-gate 	 * resource components.
1077c478bd9Sstevel@tonic-gate          */
transfer(Resource donor, List components)1087c478bd9Sstevel@tonic-gate 	public void transfer(Resource donor, List components)
1097c478bd9Sstevel@tonic-gate 	    throws PoolsException
1107c478bd9Sstevel@tonic-gate 	{
1117c478bd9Sstevel@tonic-gate 		if (PoolInternal.pool_resource_xtransfer(_conf.getConf(),
1127c478bd9Sstevel@tonic-gate 		    donor.getResource(), getResource(), components) !=
1137c478bd9Sstevel@tonic-gate 		    PoolInternal.PO_SUCCESS)
1147c478bd9Sstevel@tonic-gate 			throw new PoolsException();
115*99308ed0SPeter Tribble 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/**
1187c478bd9Sstevel@tonic-gate 	 * Get a list of components which match the supplied selection
1197c478bd9Sstevel@tonic-gate 	 * criteria in values.  Only components which are controlled by
1207c478bd9Sstevel@tonic-gate 	 * this resource are searched.
1217c478bd9Sstevel@tonic-gate 	 *
1227c478bd9Sstevel@tonic-gate 	 * @param values A list of values to be used to qualify the search.
123*99308ed0SPeter Tribble 	 * @throws PoolsException If there is an error executing the query.
1247c478bd9Sstevel@tonic-gate 	 * @return a list of components which match the supplied criteria
1257c478bd9Sstevel@tonic-gate 	 */
getComponents(List values)1267c478bd9Sstevel@tonic-gate 	public List getComponents(List values) throws PoolsException
1277c478bd9Sstevel@tonic-gate 	{
1287c478bd9Sstevel@tonic-gate 		List components;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		if ((components = PoolInternal.pool_query_resource_components(
1317c478bd9Sstevel@tonic-gate 		    _conf.getConf(), getResource(), values)) == null) {
13226d8ba22Sgarypen 			if (PoolInternal.pool_error() ==
1337c478bd9Sstevel@tonic-gate 			    PoolInternal.POE_INVALID_SEARCH)
1347c478bd9Sstevel@tonic-gate 				return new ArrayList();
1357c478bd9Sstevel@tonic-gate 			else
1367c478bd9Sstevel@tonic-gate 				throw new PoolsException();
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 		ArrayList aList = new ArrayList(components.size());
1397c478bd9Sstevel@tonic-gate 		for (int i = 0; i < components.size(); i++)
1407c478bd9Sstevel@tonic-gate 			aList.add(new Component(_conf,
1417c478bd9Sstevel@tonic-gate 			    ((Long)components.get(i)).longValue()));
1427c478bd9Sstevel@tonic-gate 		return (aList);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/**
1467c478bd9Sstevel@tonic-gate 	 * Returns a descriptive string which describes the resource.
1477c478bd9Sstevel@tonic-gate 	 *
1487c478bd9Sstevel@tonic-gate 	 * @param deep Whether the information should contain information about
1497c478bd9Sstevel@tonic-gate 	 * all contained elements.
1507c478bd9Sstevel@tonic-gate 	 * @throws PoolsException If the resource cannot be located.
1517c478bd9Sstevel@tonic-gate 	 * @return a descriptive string which describes the resource.
1527c478bd9Sstevel@tonic-gate 	 */
getInformation(int deep)1537c478bd9Sstevel@tonic-gate 	public String getInformation(int deep) throws PoolsException
1547c478bd9Sstevel@tonic-gate 	{
1557c478bd9Sstevel@tonic-gate 		return (PoolInternal.pool_resource_info(_conf.getConf(),
1567c478bd9Sstevel@tonic-gate 			getResource(), deep));
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate         /**
1607c478bd9Sstevel@tonic-gate          * Returns a string representation of this resource.
1617c478bd9Sstevel@tonic-gate          *
1627c478bd9Sstevel@tonic-gate          * @return  a string representation of this resource.
1637c478bd9Sstevel@tonic-gate          */
toString()1647c478bd9Sstevel@tonic-gate 	public String toString()
1657c478bd9Sstevel@tonic-gate 	{
1667c478bd9Sstevel@tonic-gate 		StringBuffer buf = new StringBuffer();
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		buf.append(type);
1697c478bd9Sstevel@tonic-gate 		buf.append(" ");
1707c478bd9Sstevel@tonic-gate 		buf.append(name);
1717c478bd9Sstevel@tonic-gate 		return (buf.toString());
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	/**
1757c478bd9Sstevel@tonic-gate 	 * Indicates whether some other Resource is "equal to this one.
1767c478bd9Sstevel@tonic-gate 	 * @param o the reference object with which to compare.
1777c478bd9Sstevel@tonic-gate 	 * @return <code>true</code> if this object is the same as the
1787c478bd9Sstevel@tonic-gate 	 * o argument; <code>false</code> otherwise.
1797c478bd9Sstevel@tonic-gate 	 * @see	#hashCode()
1807c478bd9Sstevel@tonic-gate 	 */
equals(Object o)1817c478bd9Sstevel@tonic-gate 	public boolean equals(Object o)
1827c478bd9Sstevel@tonic-gate 	{
1837c478bd9Sstevel@tonic-gate 		if (o == this)
1847c478bd9Sstevel@tonic-gate 			return (true);
1857c478bd9Sstevel@tonic-gate 		if (!(o instanceof Resource))
1867c478bd9Sstevel@tonic-gate 			return (false);
1877c478bd9Sstevel@tonic-gate 		Resource other = (Resource) o;
1887c478bd9Sstevel@tonic-gate 		if (type.compareTo(other.getType()) != 0 ||
1897c478bd9Sstevel@tonic-gate 		    name.compareTo(other.getName()) != 0)
1907c478bd9Sstevel@tonic-gate 			return (false);
1917c478bd9Sstevel@tonic-gate 		return (true);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/**
1957c478bd9Sstevel@tonic-gate 	 * Returns a hash code value for the object. This method is
1967c478bd9Sstevel@tonic-gate 	 * supported for the benefit of hashtables such as those provided by
1977c478bd9Sstevel@tonic-gate 	 * <code>java.util.Hashtable</code>.
1987c478bd9Sstevel@tonic-gate 	 *
1997c478bd9Sstevel@tonic-gate 	 * @return a hash code value for this object.
2007c478bd9Sstevel@tonic-gate 	 * @see	#equals(java.lang.Object)
2017c478bd9Sstevel@tonic-gate 	 * @see	java.util.Hashtable
2027c478bd9Sstevel@tonic-gate 	 */
hashCode()2037c478bd9Sstevel@tonic-gate 	public int hashCode()
2047c478bd9Sstevel@tonic-gate 	{
2057c478bd9Sstevel@tonic-gate 		return (type.hashCode() + name.hashCode());
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/**
2097c478bd9Sstevel@tonic-gate 	 * Return the pointer to this resource as an element.
2107c478bd9Sstevel@tonic-gate 	 *
2117c478bd9Sstevel@tonic-gate 	 * @return The pointer to the native resource which this object wraps.
212*99308ed0SPeter Tribble 	 * @throws PoolsException If there is an error converting the native
2137c478bd9Sstevel@tonic-gate 	 * resource pointer to a native elem pointer.
2147c478bd9Sstevel@tonic-gate 	 */
getElem()2157c478bd9Sstevel@tonic-gate 	protected long getElem() throws PoolsException
2167c478bd9Sstevel@tonic-gate 	{
2177c478bd9Sstevel@tonic-gate 		long elem;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		if ((elem = PoolInternal.pool_resource_to_elem(_conf.getConf(),
2207c478bd9Sstevel@tonic-gate 		    getResource())) == 0)
2217c478bd9Sstevel@tonic-gate 			throw new PoolsException();
2227c478bd9Sstevel@tonic-gate 		return (elem);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/**
2267c478bd9Sstevel@tonic-gate 	 * Return the type of the resource
2277c478bd9Sstevel@tonic-gate 	 */
getType()2287c478bd9Sstevel@tonic-gate 	String getType()
2297c478bd9Sstevel@tonic-gate 	{
2307c478bd9Sstevel@tonic-gate 		return (type);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/**
2347c478bd9Sstevel@tonic-gate 	 * Return the name of the resource.
2357c478bd9Sstevel@tonic-gate 	 */
getName()2367c478bd9Sstevel@tonic-gate 	String getName()
2377c478bd9Sstevel@tonic-gate 	{
2387c478bd9Sstevel@tonic-gate 		return (name);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/**
2427c478bd9Sstevel@tonic-gate 	 * Return the key of the resource.
2437c478bd9Sstevel@tonic-gate 	 */
getKey()2447c478bd9Sstevel@tonic-gate 	String getKey()
2457c478bd9Sstevel@tonic-gate 	{
2467c478bd9Sstevel@tonic-gate 		return (key);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate }
249