xref: /illumos-gate/usr/src/cmd/pools/poold/com/sun/solaris/service/pools/Element.java (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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  *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>Element</code> class represents a pools configuration
37  * element.  The class is an abstract, base class for concrete
38  * implementation elements, such as pool and resource.
39  */
40 public abstract class Element implements Property, PropertyWalk
41 {
42 	/**
43 	 * The configuration to which this element belongs.
44 	 */
45 	protected Configuration _conf;
46 
47         /**
48          * Returns a string representation of this element.
49          *
50          * @return  a string representation of this element.
51          */
52 	public String toString()
53 	{
54 		try {
55 			return (getInformation(1));
56 		} catch (PoolsException pe) {
57 			return (pe.toString());
58 		}
59 	}
60 
61 	/**
62 	 * Returns a descriptive string which describes the element.
63 	 *
64 	 * @param deep Whether the information should contain information about
65 	 * all contained elements.
66 	 * @throws PoolsException If the element cannot be located.
67 	 * @return a descriptive string which describes the element.
68 	 */
69 	public abstract String getInformation(int deep) throws PoolsException;
70 
71 	/**
72 	 * Get the property with the supplied name.
73 	 *
74 	 * @param name The name of the property to be retrieved.
75 	 * @throws PoolsExecption If there is an error accessing the property.
76 	 * @return a value containing the property details.
77 	 */
78         private Value getProperty(String name) throws PoolsException
79 	{
80 		Value value = new Value(name);
81 
82 		if (PoolInternal.pool_get_property(_conf.getConf(), getElem(),
83 			name, value.getValue()) == PoolInternal.POC_INVAL)
84 			throw new PoolsException();
85 		return (value);
86 	}
87 
88 	/**
89 	 * Get the property with the supplied name using the supplied
90 	 * proxy.
91 	 *
92 	 * @param name The name of the property to be retrieved.
93 	 * @param proxy The proxy item used to retrieve the property.
94 	 * @throws PoolsExecption If there is an error accessing the property.
95 	 * @return a value containing the property details.
96 	 */
97         protected Value getProperty(String name, long proxy)
98 	    throws PoolsException
99 	{
100 		Value value = new Value(name);
101 
102 		if (PoolInternal.pool_get_property(_conf.getConf(), proxy, name,
103 		    value.getValue()) == PoolInternal.POC_INVAL)
104 			throw new PoolsException();
105 		return (value);
106 	}
107 
108 	/**
109 	 * Put the supplied value as an element property with the supplied
110 	 * name.
111 	 *
112 	 * @param name The name of the property to be updated.
113 	 * @param value The value of the property to be updated.
114 	 * @throws PoolsExecption If there is an error accessing the property.
115 	 */
116         public void putProperty(String name, Value value) throws PoolsException
117 	{
118 		if (PoolInternal.pool_put_property(_conf.getConf(), getElem(),
119 			name, value.getValue()) != PoolInternal.PO_SUCCESS)
120 			throw new PoolsException();
121 	}
122 
123 	/**
124 	 * Remove the element property with the supplied name.
125 	 *
126 	 * @param name The name of the property to be removed.
127 	 * @throws PoolsExecption If there is an error removing the property.
128 	 */
129         public void rmProperty(String name) throws PoolsException
130 	{
131 		if (PoolInternal.pool_rm_property(_conf.getConf(), getElem(),
132 			name) != PoolInternal.PO_SUCCESS)
133 			throw new PoolsException();
134 	}
135 
136 	/**
137 	 * Get a String property.
138 	 *
139 	 * If the type of the property does not match, i.e. it's not a
140 	 * String, or the element does not have such a property then a
141 	 * PoolsException is thrown.
142 	 *
143 	 * @param name The name of the property to be retrieved.
144 	 * @throws PoolsException If getting the String property fails.
145 	 */
146 	public String getStringProperty(String name) throws PoolsException
147 	{
148 		Value val = getProperty(name);
149 
150 		if (val != null) {
151 			String ret = val.getString();
152 			val.close();
153 			return (ret);
154 		}
155 		throw new PoolsException();
156 	}
157 
158 	/**
159 	 * Get a long property.
160 	 *
161 	 * If the type of the property does not match, i.e. it's not a
162 	 * long, or the element does not have such a property then a
163 	 * PoolsException is thrown.
164 	 *
165 	 * @param name The name of the property to be retrieved.
166 	 * @throws PoolsException If getting the long property fails.
167 	 */
168 	public long getLongProperty(String name) throws PoolsException
169 	{
170 		Value val = getProperty(name);
171 
172 		if (val != null) {
173 			long ret = val.getLong();
174 			val.close();
175 			return (ret);
176 		}
177 		throw new PoolsException();
178 	}
179 
180 	/**
181 	 * Get a double property.
182 	 *
183 	 * If the type of the property does not match, i.e. it's not a
184 	 * double, or the element does not have such a property then a
185 	 * PoolsException is thrown.
186 	 *
187 	 * @param name The name of the property to be retrieved.
188 	 * @throws PoolsException If getting the double property fails.
189 	 */
190 	public double getDoubleProperty(String name) throws PoolsException
191 	{
192 		Value val = getProperty(name);
193 
194 		if (val != null) {
195 			double ret = val.getDouble();
196 			val.close();
197 			return (ret);
198 		}
199 		throw new PoolsException();
200 	}
201 
202 	/**
203 	 * Get a boolean property.
204 	 *
205 	 * If the type of the property does not match, i.e. it's not a
206 	 * boolean, or the element does not have such a property then
207 	 * a PoolsException is thrown.
208 	 *
209 	 * @param name The name of the property to be retrieved.
210 	 * @throws PoolsException If getting the boolean property fails.
211 	 */
212 	public boolean getBoolProperty(String name) throws PoolsException
213 	{
214 		Value val = getProperty(name);
215 
216 		if (val != null) {
217 			boolean ret = val.getBool();
218 			val.close();
219 			return (ret);
220 		}
221 		throw new PoolsException();
222 	}
223 
224 	/**
225 	 * Walk all properties of the invoking object.
226 	 *
227 	 * @param elem The element to whom the property belongs.
228 	 * @param val The value representing the current element.
229 	 * @param user User supplied data, provided when the walk is invoked.
230 	 * @throws PoolsExecption If there is an error walking the property.
231 	 * @return 0 to continue the walk, anything else to terminate it.
232 	 */
233 	public int walk(Element elem, Value val, Object user)
234 	    throws PoolsException
235 	{
236 		System.out.println("Property name: " + val.getName() +
237 		    ", value: "+val.toString());
238 		val.close();
239 		return (0);
240 	}
241 
242 	/**
243 	 * Return the pointer to this subtype as an element.
244 	 *
245 	 * @return The pointer to the native subtype which this object wraps.
246 	 * @throws PoolsExecption If there is an error converting the native
247 	 * subtype pointer to a native elem pointer.
248 	 */
249 	protected abstract long getElem() throws PoolsException;
250 
251 	/**
252 	 * Walk all the properties of this element.
253 	 *
254 	 * @param handler The object which will receive the callbacks.
255 	 * @param user Data supplied by the user for use in the callback.
256 	 * @return 0 for a successful walk, else 1.
257 	 * @throws PoolsExecption If there is an error during the walk.
258 	 */
259 	public int walkProperties(PropertyWalk handler, Object user)
260 	    throws PoolsException
261 	{
262 		return (walkProps(_conf.getConf(), getElem(), handler, user));
263 	}
264 
265 	/**
266 	 * Walk the properties of the supplied element using the
267 	 * supplied handler.
268 	 *
269 	 * @param conf native reference to the configuration in which
270 	 * the element belongs.
271 	 * @param elem native reference to the element whose
272 	 * properties are to be walked.
273 	 * @param handler a method to be invoked with each property of
274 	 * the elem.
275 	 * @param user a user parameter which is passed to handler on
276 	 * each invocation.
277 	 * @throws PoolsException if there is an error accessing the
278 	 * element properties.
279 	 */
280 	private native int walkProps(long conf, long elem,
281 	    PropertyWalk handler, Object user) throws PoolsException;
282 
283 	/**
284 	 * Return the key of the element.
285 	 */
286 	abstract String getKey();
287 }
288