xref: /illumos-gate/usr/src/cmd/pools/poold/com/sun/solaris/service/pools/Value.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 /**
30  * The <code>Value</code> class represents a pools value.
31  */
32 public class Value {
33 
34 	private long _this;
35 
36 	/**
37 	 * Constructor. Only for use from native code.
38 	 * @param pointer A pointer to a C value.
39 	 */
Value(long pointer)40 	private Value(long pointer)
41 	{
42 		_this = pointer;
43 	}
44 
45 	/**
46 	 * Constructor
47 	 * @param name The name of the value.
48 	 * @throws PoolsException If there is an error whilst
49 	 * allocating the value.
50 	 */
Value(String name)51 	public Value(String name) throws PoolsException
52 	{
53 		if ((_this = PoolInternal.pool_value_alloc()) == 0)
54 			throw new PoolsException();
55 		setName(name);
56 	}
57 
58 	/**
59 	 * Constructor
60 	 * @param name The name of the value.
61 	 * @param value The value of the value.
62 	 * @throws PoolsException If there is an error whilst
63 	 * allocating the value.
64 	 */
Value(String name, long value)65 	public Value(String name, long value) throws PoolsException
66 	{
67 		this(name);
68 		setValue(value);
69 	}
70 
71 	/**
72 	 * Constructor
73 	 * @param name The name of the value.
74 	 * @param value The value of the value.
75 	 * @param s Indicates if the value is signed or not.
76 	 * @throws PoolsException If there is an error whilst
77 	 * allocating the value.
78 	 */
Value(String name, long value, boolean s)79 	public Value(String name, long value, boolean s) throws PoolsException
80 	{
81 		this(name);
82 		setValue(value, s);
83 	}
84 
85 	/**
86 	 * Constructor
87 	 * @param name The name of the value.
88 	 * @param value The value of the value.
89 	 * @throws PoolsException If there is an error whilst
90 	 * allocating the value.
91 	 */
Value(String name, String value)92 	public Value(String name, String value) throws PoolsException
93 	{
94 		this(name);
95 		setValue(value);
96 	}
97 
98 	/**
99 	 * Constructor
100 	 * @param name The name of the value.
101 	 * @param value The value of the value.
102 	 * @throws PoolsException If there is an error whilst
103 	 * allocating the value.
104 	 */
Value(String name, boolean value)105 	public Value(String name, boolean value) throws PoolsException
106 	{
107 		this(name);
108 		setValue(value);
109 	}
110 
111 	/**
112 	 * Constructor
113 	 * @param name The name of the value.
114 	 * @param value The value of the value.
115 	 * @throws PoolsException If there is an error whilst
116 	 * allocating the value.
117 	 */
Value(String name, double value)118 	public Value(String name, double value) throws PoolsException
119 	{
120 		this(name);
121 		setValue(value);
122 	}
123 
124 
125 	private boolean _locked = false;
126 
127 	/**
128 	 * Check whether the value is locked or not
129 	 * @return returns the value of _locked
130 	 */
islocked()131 	public boolean islocked() throws PoolsException
132         {
133                 return (_locked);
134         }
135 
136 	/**
137 	 * Lock the value
138 	 */
lock()139 	public void lock() throws PoolsException
140 	{
141 		_locked = true;
142 	}
143 
144 	/**
145 	 * Unlock the value
146 	 */
unlock()147 	public void unlock() throws PoolsException
148 	{
149 		_locked = false;
150 	}
151 
152 	/**
153 	 * Explicitly reclaim the memory (if not locked)
154 	 * allocated for this value by the C proxy.
155 	 */
close()156 	public void close()
157 	{
158 		if (_locked == false) {
159 			if (_this != 0) {
160 				PoolInternal.pool_value_free(_this);
161 				_this = 0;
162 			}
163 		}
164 	}
165 
166 	/**
167 	 * Reclaim the memory allocated for this value by the C
168 	 * proxy.
169 	 *
170 	 * @throws Throwable If freeing this configuration fails.
171 	 */
finalize()172 	protected void finalize() throws Throwable
173 	{
174 		try
175 		{
176 			unlock();
177 			close();
178 		}
179 		finally
180 		{
181 			super.finalize();
182 		}
183 	}
184 
185 	/**
186 	 * Name this value.
187 	 *
188 	 * @param name The name to set for this value.
189 	 */
setName(String name)190 	public void setName(String name)
191 	{
192 		PoolInternal.pool_value_set_name(_this, name);
193 	}
194 
195 	/**
196 	 * Set this value to take the supplied signed long value.
197 	 *
198 	 * @param value The value to which this value should be set.
199 	 */
setValue(long value)200 	public void setValue(long value)
201 	{
202 		PoolInternal.pool_value_set_int64(_this, value);
203 	}
204 
205 	/**
206 	 * Set this value to take the supplied long value.
207 	 *
208 	 * @param value The value to which this value should be set.
209 	 * @param s Is the value signed or unsigned.
210 	 */
setValue(long value, boolean s)211 	public void setValue(long value, boolean s)
212 	{
213 		if (s)
214 			setValue(value);
215 		PoolInternal.pool_value_set_uint64(_this, value);
216 	}
217 
218 	/**
219 	 * Set this value to take the supplied string value.
220 	 *
221 	 * @param value The value to which this value should be set.
222 	 * @throws PoolsException If the setting of the value fails.
223 	 */
setValue(String value)224 	public void setValue(String value) throws PoolsException
225 	{
226 		if (PoolInternal.pool_value_set_string(_this, value) !=
227 		    PoolInternal.PO_SUCCESS)
228 			throw new PoolsException();
229 	}
230 
231 	/**
232 	 * Set this value to take the supplied boolean value.
233 	 *
234 	 * @param value The value to which this value should be set.
235 	 */
setValue(boolean value)236 	public void setValue(boolean value)
237 	{
238 		if (value == true)
239 			PoolInternal.pool_value_set_bool(_this, (short)1);
240 		else
241 			PoolInternal.pool_value_set_bool(_this, (short)0);
242 	}
243 
244 	/**
245 	 * Set this value to take the supplied double value.
246 	 *
247 	 * @param value The value to which this value should be set.
248 	 */
setValue(double value)249 	public void setValue(double value)
250 	{
251 		PoolInternal.pool_value_set_double(_this, value);
252 	}
253 
254 	/**
255 	 * Returns the name of the value.
256 	 *
257 	 * @return the name of the value.
258 	 */
getName()259 	public String getName()
260 	{
261 		return (PoolInternal.pool_value_get_name(_this));
262 	}
263 
264 	/**
265 	 * Returns the pointer to the native value represented by this
266 	 * object.
267 	 *
268 	 * @return the pointer to the native value represented by this
269 	 * object.
270 	 */
getValue()271 	public long getValue()
272 	{
273 		return (_this);
274 	}
275 
276 	/**
277 	 * Returns the type of this object.
278 	 *
279 	 * @return the type of this object.
280 	 */
getType()281 	public int getType()
282 	{
283 		return (PoolInternal.pool_value_get_type(_this));
284 	}
285 
286 	/**
287 	 * Returns a string representation of this value.
288 	 *
289 	 * @return a string representation of this value.
290 	 */
toString()291 	public String toString()
292 	{
293 		int type = PoolInternal.pool_value_get_type(_this);
294 
295 		try {
296 			if (type == PoolInternal.POC_INT ||
297 			    type == PoolInternal.POC_UINT)
298 				return (String.valueOf(getLong()));
299 			if (type == PoolInternal.POC_STRING)
300 				return getString();
301 			if (type == PoolInternal.POC_BOOL)
302 				return (String.valueOf(getBool()));
303 			if (type == PoolInternal.POC_DOUBLE)
304 				return (String.valueOf(getDouble()));
305 		}
306 		catch (PoolsException pe) {
307 			return pe.toString();
308 		}
309 		return "";	/* Stop the compiler complaining */
310 	}
311 
312         /**
313          * Returns the value as a UnsignedInt64.
314          *
315          * @return the value as a UnsignedInt64.
316          * @throws PoolsException if the value is not an
317          * UnsignedInt64.
318          */
getUnsignedInt64()319 	public final UnsignedInt64 getUnsignedInt64() throws PoolsException
320 	{
321 		return (getUnsignedInt64Value(_this));
322 	}
323 
324         /**
325          * Returns the value as a long.
326          *
327          * @return the value as a long.
328          * @throws PoolsException if the value is not a long.
329          */
getLong()330 	public final long getLong() throws PoolsException
331 	{
332 		return (getLongValue(_this));
333 	}
334 
335         /**
336          * Returns the value as a String.
337          *
338          * @return the value as a String.
339          * @throws PoolsException if the value is not a String.
340          */
getString()341 	public final String getString() throws PoolsException
342 	{
343 		return (getStringValue(_this));
344 	}
345 
346         /**
347          * Returns the value as a boolean.
348          *
349          * @return the value as a boolean.
350          * @throws PoolsException if the value is not a boolean.
351          */
getBool()352 	public final boolean getBool() throws PoolsException
353 	{
354 		return (getBoolValue(_this));
355 	}
356 
357         /**
358          * Returns the value as a double.
359          *
360          * @return the value as a double.
361          * @throws PoolsException if the value is not a double.
362          */
getDouble()363 	public final double getDouble() throws PoolsException
364 	{
365 		return (getDoubleValue(_this));
366 	}
367 
368         /**
369          * Returns the value as a UnsignedInt64.
370          *
371          * @param pointer the native value to be accessed.
372          * @return the value as a UnsignedInt64.
373          */
getUnsignedInt64Value( long pointer)374 	private final static native UnsignedInt64 getUnsignedInt64Value(
375 	    long pointer);
376 
377         /**
378          * Returns the value as a long.
379          *
380          * @param pointer the native value to be accessed.
381          * @return the value as a long.
382          */
getLongValue(long pointer)383 	private final static native long getLongValue(long pointer);
384 
385         /**
386          * Returns the value as a String.
387          *
388          * @param pointer the native value to be accessed.
389          * @return the value as a String.
390          */
getStringValue(long pointer)391 	private final static native String getStringValue(long pointer);
392 
393         /**
394          * Returns the value as a boolean.
395          *
396          * @param pointer the native value to be accessed.
397          * @return the value as a boolean.
398          */
getBoolValue(long pointer)399 	private final static native boolean getBoolValue(long pointer);
400 
401         /**
402          * Returns the value as a double.
403          *
404          * @param pointer the native value to be accessed.
405          * @return the value as a double.
406          */
getDoubleValue(long pointer)407 	private final static native double getDoubleValue(long pointer);
408 }
409