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 2003 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 /**
31  * The <code>Component</code> class represents a configuration
32  * resource component.
33  */
34 public class Component extends Element
35 {
36 	/**
37 	 * The type of the component.
38 	 */
39 	private final String type;
40 
41 	/**
42 	 * The system id of the component.
43 	 */
44 	private final long sys_id;
45 
46 	/**
47 	 * The key of the component.
48 	 */
49 	private final String key;
50 
51 	/**
52 	 * Constructor
53 	 *
54 	 * @param conf The configuration to which this component belongs
55 	 * @param comp The pointer to the native component
56 	 * @throws PoolsException If accessing the proxy fails.
57 	 */
Component(Configuration conf, long comp)58 	Component(Configuration conf, long comp) throws PoolsException
59 	{
60 		_conf = conf;
61 		Value val = getProperty("type", comp);
62 		type = val.getString();
63 		val.close();
64 		val = getProperty(type + ".sys_id", comp);
65 		sys_id = val.getLong();
66 		val.close();
67 		key = type + "." + sys_id;
68 	}
69 
70 	/**
71 	 * Return the pointer to the component represented by this object.
72 	 *
73 	 * @return conf the pointer to the component represented by this object
74 	 * @throws PoolsException If the component cannot be located.
75 	 */
getComponent()76 	long getComponent() throws PoolsException
77 	{
78 		return (_conf.checkComponent(type, sys_id));
79 	}
80 
81 	/**
82 	 * Returns a descriptive string which describes the component.
83 	 *
84 	 * @param deep Whether the information should contain information about
85 	 * all contained elements.
86 	 * @throws PoolsException If the component cannot be located.
87 	 * @return a descriptive string which describes the component.
88 	 */
getInformation(int deep)89 	public String getInformation(int deep) throws PoolsException
90 	{
91 		return (PoolInternal.pool_component_info(_conf.getConf(),
92 			getComponent(), deep));
93 	}
94 
95         /**
96          * Returns a string representation of this component.
97          *
98          * @return  a string representation of this component.
99          */
toString()100 	public String toString()
101 	{
102 		StringBuffer buf = new StringBuffer();
103 
104 		buf.append(type);
105 		buf.append(" ");
106 		buf.append(sys_id);
107 		return (buf.toString());
108 	}
109 
110 	/**
111 	 * Indicates whether some other Component is "equal to this one.
112 	 * @param o the reference object with which to compare.
113 	 * @return <code>true</code> if this object is the same as the
114 	 * o argument; <code>false</code> otherwise.
115 	 * @see	#hashCode()
116 	 */
equals(Object o)117 	public boolean equals(Object o)
118 	{
119 		if (o == this)
120 			return (true);
121 		if (!(o instanceof Component))
122 			return (false);
123 		Component other = (Component) o;
124 		if (type.compareTo(other.getType()) != 0 ||
125 		    sys_id != other.getSysId())
126 			return (false);
127 		return (true);
128 	}
129 
130 	/**
131 	 * Returns a hash code value for the object. This method is
132 	 * supported for the benefit of hashtables such as those provided by
133 	 * <code>java.util.Hashtable</code>.
134 	 *
135 	 * @return a hash code value for this object.
136 	 * @see	#equals(java.lang.Object)
137 	 * @see	java.util.Hashtable
138 	 */
hashCode()139 	public int hashCode()
140 	{
141 		return (type.hashCode() + (int) sys_id);
142 	}
143 
144 	/**
145 	 * Return the pointer to this component as an element.
146 	 *
147 	 * @return The pointer to the native component which this object wraps.
148 	 * @throws PoolsException If there is an error converting the native
149 	 * component pointer to a native elem pointer.
150 	 */
getElem()151 	protected long getElem() throws PoolsException
152 	{
153 		long elem;
154 
155 		if ((elem = PoolInternal.pool_component_to_elem(_conf.getConf(),
156 		    getComponent())) == 0)
157 			throw new PoolsException();
158 		return (elem);
159 	}
160 
161 	/**
162 	 * Return the type of the component
163 	 */
getType()164 	String getType()
165 	{
166 		return (type);
167 	}
168 
169 	/**
170 	 * Return the system id of the component.
171 	 */
getSysId()172 	long getSysId()
173 	{
174 		return (sys_id);
175 	}
176 
177 	/**
178 	 * Return the key of the component.
179 	 */
getKey()180 	String getKey()
181 	{
182 		return (key);
183 	}
184 }
185