xref: /illumos-gate/usr/src/cmd/pools/poold/com/sun/solaris/service/pools/Element.java (revision 99308ed0417a2b8ab73c5856a8a5345ce2a7aea7)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  */
26 
27 package com.sun.solaris.service.pools;
28 
29 import java.util.List;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Arrays;
33 
34 /**
35  * The <code>Element</code> class represents a pools configuration
36  * element.  The class is an abstract, base class for concrete
37  * implementation elements, such as pool and resource.
38  */
39 public abstract class Element implements Property, PropertyWalk
40 {
41 	/**
42 	 * The configuration to which this element belongs.
43 	 */
44 	protected Configuration _conf;
45 
46         /**
47          * Returns a string representation of this element.
48          *
49          * @return  a string representation of this element.
50          */
toString()51 	public String toString()
52 	{
53 		try {
54 			return (getInformation(1));
55 		} catch (PoolsException pe) {
56 			return (pe.toString());
57 		}
58 	}
59 
60 	/**
61 	 * Returns a descriptive string which describes the element.
62 	 *
63 	 * @param deep Whether the information should contain information about
64 	 * all contained elements.
65 	 * @throws PoolsException If the element cannot be located.
66 	 * @return a descriptive string which describes the element.
67 	 */
getInformation(int deep)68 	public abstract String getInformation(int deep) throws PoolsException;
69 
70 	/**
71 	 * A list of properties that are chosen to be cached.
72 	 */
73 	private static final List cachedProperties
74 			= Arrays.asList(new String[] {"cpu.sys_id",
75 			"pool.default", "pool.sys_id", "pset.default",
76 			"pset.sys_id", "pset.type", "pset.units"});
77 
78         /**
79          * hashmap of property values that are determined readonly
80          */
81         private HashMap readOnlyValues = new HashMap();
82 
83 	/**
84 	 * Get the property with the supplied name.
85 	 *
86 	 * @param name The name of the property to be retrieved.
87 	 * @throws PoolsException If there is an error accessing the property.
88 	 * @return a value containing the property details.
89 	 */
getProperty(String name)90         private Value getProperty(String name) throws PoolsException
91 	{
92             if (readOnlyValues.containsKey(name)) {
93                 Value value = (Value) readOnlyValues.get(name);
94                 return (value);
95             } else {
96                 Value value = new Value(name);
97                 if (PoolInternal.pool_get_property(_conf.getConf(), getElem(),
98                         name, value.getValue()) == PoolInternal.POC_INVAL) {
99                     throw new PoolsException();
100                 } else {
101                     if (cachedProperties.contains(name)) {
102                         value.lock();
103                         readOnlyValues.put(name, value);
104                     }
105                     return (value);
106                 }
107             }
108         }
109 
110 	/**
111 	 * Get the property with the supplied name using the supplied
112 	 * proxy.
113 	 *
114 	 * @param name The name of the property to be retrieved.
115 	 * @param proxy The proxy item used to retrieve the property.
116 	 * @throws PoolsException If there is an error accessing the property.
117 	 * @return a value containing the property details.
118 	 */
getProperty(String name, long proxy)119         protected Value getProperty(String name, long proxy)
120 	    throws PoolsException
121 	{
122             if (readOnlyValues.containsKey(name)) {
123                 Value value = (Value) readOnlyValues.get(name);
124                 return (value);
125             } else {
126                 Value value = new Value(name);
127                 if (PoolInternal.pool_get_property(_conf.getConf(), proxy, name,
128                         value.getValue()) == PoolInternal.POC_INVAL) {
129                     throw new PoolsException();
130                 } else {
131                     if (cachedProperties.contains(name)) {
132                         value.lock();
133                         readOnlyValues.put(name, value);
134                     }
135                     return (value);
136                 }
137             }
138         }
139 
140 	/**
141 	 * Put the supplied value as an element property with the supplied
142 	 * name.
143 	 *
144 	 * @param name The name of the property to be updated.
145 	 * @param value The value of the property to be updated.
146 	 * @throws PoolsException If there is an error accessing the property.
147 	 */
putProperty(String name, Value value)148         public void putProperty(String name, Value value) throws PoolsException
149 	{
150 		if (PoolInternal.pool_put_property(_conf.getConf(), getElem(),
151 			name, value.getValue()) != PoolInternal.PO_SUCCESS)
152 			throw new PoolsException();
153 	}
154 
155 	/**
156 	 * Remove the element property with the supplied name.
157 	 *
158 	 * @param name The name of the property to be removed.
159 	 * @throws PoolsException If there is an error removing the property.
160 	 */
rmProperty(String name)161         public void rmProperty(String name) throws PoolsException
162 	{
163 		if (PoolInternal.pool_rm_property(_conf.getConf(), getElem(),
164 			name) != PoolInternal.PO_SUCCESS)
165 			throw new PoolsException();
166 	}
167 
168 	/**
169 	 * Get a String property.
170 	 *
171 	 * If the type of the property does not match, i.e. it's not a
172 	 * String, or the element does not have such a property then a
173 	 * PoolsException is thrown.
174 	 *
175 	 * @param name The name of the property to be retrieved.
176 	 * @throws PoolsException If getting the String property fails.
177 	 */
getStringProperty(String name)178 	public String getStringProperty(String name) throws PoolsException
179 	{
180 		Value val = getProperty(name);
181 
182 		if (val != null) {
183 			String ret = val.getString();
184 			val.close();
185 			return (ret);
186 		}
187 		throw new PoolsException();
188 	}
189 
190 	/**
191 	 * Get a long property.
192 	 *
193 	 * If the type of the property does not match, i.e. it's not a
194 	 * long, or the element does not have such a property then a
195 	 * PoolsException is thrown.
196 	 *
197 	 * @param name The name of the property to be retrieved.
198 	 * @throws PoolsException If getting the long property fails.
199 	 */
getLongProperty(String name)200 	public long getLongProperty(String name) throws PoolsException
201 	{
202 		Value val = getProperty(name);
203 
204 		if (val != null) {
205 			long ret = val.getLong();
206 			val.close();
207 			return (ret);
208 		}
209 		throw new PoolsException();
210 	}
211 
212 	/**
213 	 * Get a double property.
214 	 *
215 	 * If the type of the property does not match, i.e. it's not a
216 	 * double, or the element does not have such a property then a
217 	 * PoolsException is thrown.
218 	 *
219 	 * @param name The name of the property to be retrieved.
220 	 * @throws PoolsException If getting the double property fails.
221 	 */
getDoubleProperty(String name)222 	public double getDoubleProperty(String name) throws PoolsException
223 	{
224 		Value val = getProperty(name);
225 
226 		if (val != null) {
227 			double ret = val.getDouble();
228 			val.close();
229 			return (ret);
230 		}
231 		throw new PoolsException();
232 	}
233 
234 	/**
235 	 * Get a boolean property.
236 	 *
237 	 * If the type of the property does not match, i.e. it's not a
238 	 * boolean, or the element does not have such a property then
239 	 * a PoolsException is thrown.
240 	 *
241 	 * @param name The name of the property to be retrieved.
242 	 * @throws PoolsException If getting the boolean property fails.
243 	 */
getBoolProperty(String name)244 	public boolean getBoolProperty(String name) throws PoolsException
245 	{
246 		Value val = getProperty(name);
247 
248 		if (val != null) {
249 			boolean ret = val.getBool();
250 			val.close();
251 			return (ret);
252 		}
253 		throw new PoolsException();
254 	}
255 
256 	/**
257 	 * Walk all properties of the invoking object.
258 	 *
259 	 * @param elem The element to whom the property belongs.
260 	 * @param val The value representing the current element.
261 	 * @param user User supplied data, provided when the walk is invoked.
262 	 * @throws PoolsException If there is an error walking the property.
263 	 * @return 0 to continue the walk, anything else to terminate it.
264 	 */
walk(Element elem, Value val, Object user)265 	public int walk(Element elem, Value val, Object user)
266 	    throws PoolsException
267 	{
268 		System.out.println("Property name: " + val.getName() +
269 		    ", value: "+val.toString());
270 		val.close();
271 		return (0);
272 	}
273 
274 	/**
275 	 * Return the pointer to this subtype as an element.
276 	 *
277 	 * @return The pointer to the native subtype which this object wraps.
278 	 * @throws PoolsException If there is an error converting the native
279 	 * subtype pointer to a native elem pointer.
280 	 */
getElem()281 	protected abstract long getElem() throws PoolsException;
282 
283 	/**
284 	 * Walk all the properties of this element.
285 	 *
286 	 * @param handler The object which will receive the callbacks.
287 	 * @param user Data supplied by the user for use in the callback.
288 	 * @return 0 for a successful walk, else 1.
289 	 * @throws PoolsException If there is an error during the walk.
290 	 */
walkProperties(PropertyWalk handler, Object user)291 	public int walkProperties(PropertyWalk handler, Object user)
292 	    throws PoolsException
293 	{
294 		return (walkProps(_conf.getConf(), getElem(), handler, user));
295 	}
296 
297 	/**
298 	 * Walk the properties of the supplied element using the
299 	 * supplied handler.
300 	 *
301 	 * @param conf native reference to the configuration in which
302 	 * the element belongs.
303 	 * @param elem native reference to the element whose
304 	 * properties are to be walked.
305 	 * @param handler a method to be invoked with each property of
306 	 * the elem.
307 	 * @param user a user parameter which is passed to handler on
308 	 * each invocation.
309 	 * @throws PoolsException if there is an error accessing the
310 	 * element properties.
311 	 */
walkProps(long conf, long elem, PropertyWalk handler, Object user)312 	private native int walkProps(long conf, long elem,
313 	    PropertyWalk handler, Object user) throws PoolsException;
314 
315 	/**
316 	 * Return the key of the element.
317 	 */
getKey()318 	abstract String getKey();
319 }
320