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 2005 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 import java.util.Map; 35 import java.util.HashMap; 36 37 /** 38 * The <code>Configuration</code> class represents a pools configuration. 39 */ 40 public class Configuration extends Element 41 { 42 /** 43 * Indicates whether the configuration represents a usable 44 * configuration. 45 */ 46 private boolean _valid = false; 47 48 /** 49 * A reference to the native libpool object represented by 50 * this instance. 51 */ 52 private long _this; 53 54 /** 55 * The name of this instance. 56 */ 57 private String name; 58 59 /** 60 * The cache of elements known to this configuration. 61 */ 62 private Map elements; 63 64 /** 65 * The key of the configuration. 66 */ 67 private String key; 68 69 /** 70 * Constructor 71 * @param location The location of the configuration. 72 * @param perms The OR'd permissions used when opening this 73 * configuration. 74 * @exception PoolsException The error message generated by 75 * libpool. 76 */ Configuration(String location, int perms)77 public Configuration(String location, int perms) throws PoolsException 78 { 79 if (((_this = PoolInternal.pool_conf_alloc())) == 0) 80 throw new PoolsException(); 81 _conf = this; 82 open(location, perms); 83 elements = new HashMap(); 84 } 85 86 /** 87 * Reclaim the memory allocated for this configuration by the C 88 * proxy. 89 * 90 * @throws Throwable If freeing this configuration fails. 91 */ finalize()92 protected void finalize() throws Throwable 93 { 94 try 95 { 96 close(); 97 if (_this != 0) { 98 PoolInternal.pool_conf_free(_this); 99 _this = 0; 100 } 101 } 102 finally 103 { 104 super.finalize(); 105 } 106 } 107 108 /** 109 * Returns a pointer to the native configuration, wrapped by this 110 * instance. 111 * 112 * @return the configuration pointer. 113 */ getConf()114 final long getConf() 115 { 116 return (_this); 117 } 118 119 /** 120 * Opens the configuration at the supplied location and with the 121 * supplied permissions. 122 * 123 * @throws PoolsException if there is an error opening the 124 * configuration. 125 */ open(String location, int perms)126 public void open(String location, int perms) throws PoolsException 127 { 128 if (_valid == false) { 129 if (PoolInternal.pool_conf_open(getConf(), location, 130 perms) != PoolInternal.PO_SUCCESS) { 131 throw new PoolsException(); 132 } 133 _valid = true; 134 name = getStringProperty("system.name"); 135 key = "system." + name; 136 } 137 } 138 139 /** 140 * Closes the configuration. 141 * 142 */ close()143 public void close() 144 { 145 if (_valid == true) { 146 elements.clear(); 147 PoolInternal.pool_conf_close(getConf()); 148 name = key = null; 149 } 150 _valid = false; 151 } 152 153 /** 154 * Returns the location of the configuration. 155 * 156 * @return the location of the configuration. 157 */ getLocation()158 public String getLocation() 159 { 160 return (PoolInternal.pool_conf_location(getConf())); 161 } 162 163 /** 164 * Returns the status of the configuration. 165 * 166 * @return the status of the configuration. 167 */ status()168 public int status() 169 { 170 return (PoolInternal.pool_conf_status(getConf())); 171 } 172 173 /** 174 * Remove the configuration. 175 * 176 * @throws PoolsExecption If the removal fails. 177 */ remove()178 public void remove() throws PoolsException 179 { 180 if (PoolInternal.pool_conf_remove(getConf()) != 181 PoolInternal.PO_SUCCESS) 182 throw new PoolsException(); 183 } 184 185 /** 186 * Rollback the configuration, undoing any changes which have been 187 * made since the last commit or (if there are no commits) since the 188 * configuration was opened. 189 * 190 * @throws PoolsExecption If the rollback fails. 191 */ rollback()192 public void rollback() throws PoolsException 193 { 194 if (PoolInternal.pool_conf_rollback(getConf()) != 195 PoolInternal.PO_SUCCESS) 196 throw new PoolsException(); 197 } 198 199 /** 200 * Commit the configuration, making any changes since the configuration 201 * was last committed (or opened if there have been no prior commits) 202 * permanent. 203 * 204 * @throws PoolsExecption If the commit fails. 205 */ commit(int active)206 public void commit(int active) throws PoolsException 207 { 208 if (PoolInternal.pool_conf_commit(getConf(), active) != 209 PoolInternal.PO_SUCCESS) 210 throw new PoolsException(); 211 } 212 213 /** 214 * Export the configuration, storing the current state of the 215 * configuration at the supplied location in the supplied format. 216 * 217 * @param location The location of the export. 218 * @param format The format of the export. 219 * @throws PoolsExecption If the export fails. 220 */ export(String location, int format)221 public void export(String location, int format) throws PoolsException 222 { 223 if (PoolInternal.pool_conf_export(getConf(), location, format) 224 != PoolInternal.PO_SUCCESS) 225 throw new PoolsException(); 226 } 227 228 /** 229 * Validate the configuration, ensuring that the current state of the 230 * configuration satisfies the supplied validation level. 231 * 232 * @param level The desired level of validation. 233 * @throws PoolsExecption If the validation fails. 234 */ validate(int level)235 public void validate(int level) throws PoolsException 236 { 237 if (PoolInternal.pool_conf_validate(getConf(), level) 238 != PoolInternal.PO_SUCCESS) 239 throw new PoolsException(); 240 } 241 242 /** 243 * Update the configuration, ensuring that the current state of the 244 * configuration reflects that of the kernel. 245 * 246 * @throws PoolsExecption If the update fails. 247 * @return a bitmap of the changed types. 248 */ update()249 public int update() throws PoolsException 250 { 251 return (PoolInternal.pool_conf_update(getConf())); 252 } 253 254 /** 255 * Create a pool with the supplied name. 256 * 257 * @param name The name of the PoolInternal. 258 * @throws PoolsExecption If the pool creation fails. 259 * @return a pool with the supplied name. 260 */ createPool(String name)261 public Pool createPool(String name) throws PoolsException 262 { 263 long aPool; 264 265 if ((aPool = PoolInternal.pool_create(getConf(), name)) == 0) { 266 throw new PoolsException(); 267 } 268 Pool p = new Pool(this, aPool); 269 elements.put(p.getKey(), p); 270 return (p); 271 } 272 273 /** 274 * Destroy the supplied PoolInternal. 275 * 276 * @param aPool The pool to be destroyed. 277 * @throws PoolsException If the pool cannot be located. 278 */ destroyPool(Pool aPool)279 public void destroyPool(Pool aPool) throws PoolsException 280 { 281 elements.remove(aPool.getKey()); 282 PoolInternal.pool_destroy(getConf(), aPool.getPool()); 283 } 284 285 /** 286 * Get the pool with the supplied name. 287 * 288 * @param name The name of the pool to be found. 289 * @throws PoolsExecption If the pool cannot be located. 290 * @return a pool with the supplied name. 291 */ getPool(String name)292 public Pool getPool(String name) throws PoolsException 293 { 294 long aPool; 295 296 if ((aPool = PoolInternal.pool_get_pool(getConf(), name)) == 297 0) { 298 throw new PoolsException(); 299 } 300 if (elements.containsKey("PoolInternal." + name)) 301 return ((Pool) elements.get("PoolInternal." + name)); 302 else { 303 Pool p = new Pool(this, aPool); 304 elements.put(p.getKey(), p); 305 return (p); 306 } 307 } 308 309 /** 310 * Get the proxy for the pool with the supplied name. 311 * 312 * @param name The name of the pool to be found. 313 * @throws PoolsExecption If the pool cannot be located. 314 * @return the proxy for the pool with the supplied name. 315 */ checkPool(String name)316 long checkPool(String name) throws PoolsException 317 { 318 long aPool; 319 320 if ((aPool = PoolInternal.pool_get_pool(getConf(), name)) == 321 0) { 322 throw new PoolsException(); 323 } 324 return (aPool); 325 } 326 327 /** 328 * Get a list of pools which match the supplied selection criteria 329 * in values. 330 * 331 * @param values A list of values to be used to qualify the search. 332 * @throws PoolsExecption If there is an error executing the query. 333 * @return a list of pools which match the supplied criteria 334 */ getPools(List values)335 public List getPools(List values) throws PoolsException 336 { 337 List pools; 338 339 if ((pools = PoolInternal.pool_query_pools(getConf(), values)) 340 == null) { 341 if (PoolInternal.pool_error() == 342 PoolInternal.POE_INVALID_SEARCH) 343 return new ArrayList(); 344 else 345 throw new PoolsException(); 346 } 347 ArrayList aList = new ArrayList(pools.size()); 348 for (int i = 0; i < pools.size(); i++) 349 aList.add(new Pool(this, 350 ((Long)pools.get(i)).longValue())); 351 return (aList); 352 } 353 354 /** 355 * Create a resource with the supplied type and name. 356 * 357 * @param type The type of the resource. 358 * @param name The name of the resource. 359 * @throws PoolsExecption If the resource creation fails. 360 * @return a resource of the supplied type and name. 361 */ createResource(String type, String name)362 public Resource createResource(String type, String name) 363 throws PoolsException 364 { 365 long aResource; 366 367 if ((aResource = PoolInternal.pool_resource_create(getConf(), 368 type, name)) == 0) { 369 throw new PoolsException(); 370 } 371 Resource res = new Resource(this, aResource); 372 elements.put(res.getKey(), res); 373 return (res); 374 } 375 376 /** 377 * Destroy the supplied resource. 378 * 379 * @param res The resource to be destroyed. 380 * @throws PoolsException If the resource cannot be located. 381 */ destroyResource(Resource res)382 public void destroyResource(Resource res) throws PoolsException 383 { 384 elements.remove(res.getKey()); 385 PoolInternal.pool_resource_destroy(getConf(), 386 res.getResource()); 387 } 388 389 /** 390 * Get the resource with the supplied name. 391 * 392 * @param type The type of the resource to be found. 393 * @param name The name of the resource to be found. 394 * @throws PoolsExecption If the resource cannot be located. 395 * @return a resource with the supplied name. 396 */ getResource(String type, String name)397 public Resource getResource(String type, String name) 398 throws PoolsException 399 { 400 long res; 401 402 if ((res = PoolInternal.pool_get_resource(getConf(), type, 403 name)) == 0) { 404 throw new PoolsException(); 405 } 406 if (elements.containsKey(type + "." + name)) 407 return ((Resource) elements.get(type + "." + name)); 408 else { 409 Resource r = new Resource(this, res); 410 elements.put(r.getKey(), r); 411 return (r); 412 } 413 } 414 415 /** 416 * Get the proxy for the resource with the supplied type and 417 * name. 418 * 419 * @param type The type of the resource to be found. 420 * @param name The name of the resource to be found. 421 * @throws PoolsExecption If the resource cannot be located. 422 * @return the proxy for the resource with the supplied name. 423 */ checkResource(String type, String name)424 long checkResource(String type, String name) throws PoolsException 425 { 426 long res; 427 428 if ((res = PoolInternal.pool_get_resource(getConf(), type, 429 name)) == 0) { 430 throw new PoolsException(); 431 } 432 return (res); 433 } 434 435 /** 436 * Get a list of resources which match the supplied selection criteria 437 * in values. 438 * 439 * @param values A list of values to be used to qualify the search. 440 * @throws PoolsExecption If there is an error executing the query. 441 * @return a list of resources which match the supplied criteria 442 */ getResources(List values)443 public List getResources(List values) throws PoolsException 444 { 445 List resources; 446 447 if ((resources = PoolInternal.pool_query_resources(getConf(), 448 values)) == null) { 449 if (PoolInternal.pool_error() == 450 PoolInternal.POE_INVALID_SEARCH) 451 return new ArrayList(); 452 else 453 throw new PoolsException(); 454 } 455 ArrayList aList = new ArrayList(resources.size()); 456 for (int i = 0; i < resources.size(); i++) 457 aList.add(new Resource(this, 458 ((Long)resources.get(i)).longValue())); 459 return (aList); 460 } 461 462 /** 463 * Get the component with the supplied name. 464 * 465 * @param type The type of the component to be found. 466 * @param sys_id The sys_id of the component to be found. 467 * @throws PoolsExecption If the component cannot be located. 468 * @return a component with the supplied name. 469 */ getComponent(String type, long sys_id)470 public Component getComponent(String type, long sys_id) 471 throws PoolsException 472 { 473 List props = new ArrayList(); 474 Value ptype = new Value("type", type); 475 Value psys_id = new Value(type + ".sys_id", sys_id); 476 477 props.add(ptype); 478 props.add(psys_id); 479 List comps = getComponents(props); 480 ptype.close(); 481 psys_id.close(); 482 if (comps.size() != 1) 483 throw new PoolsException(); 484 return ((Component) comps.get(0)); 485 } 486 487 /** 488 * Get the component proxy with the supplied type and sys_id. 489 * 490 * @param type The type of the component to be found. 491 * @param sys_id The sys_id of the component to be found. 492 * @throws PoolsExecption If the component cannot be located. 493 * @return a component with the supplied name. 494 */ checkComponent(String type, long sys_id)495 long checkComponent(String type, long sys_id) 496 throws PoolsException 497 { 498 List props = new ArrayList(); 499 Value ptype = new Value("type", type); 500 Value psys_id = new Value(type + ".sys_id", sys_id); 501 502 props.add(ptype); 503 props.add(psys_id); 504 List comps = checkComponents(props); 505 ptype.close(); 506 psys_id.close(); 507 if (comps.size() != 1) 508 throw new PoolsException(); 509 return (((Long)comps.get(0)).longValue()); 510 } 511 512 /** 513 * Get a list of components which match the supplied selection criteria 514 * in values. 515 * 516 * @param values A list of values to be used to qualify the search. 517 * @throws PoolsExecption If there is an error executing the query. 518 * @return a list of components which match the supplied criteria 519 */ getComponents(List values)520 public List getComponents(List values) throws PoolsException 521 { 522 List components; 523 524 if ((components = PoolInternal.pool_query_components(getConf(), 525 values)) == null) { 526 if (PoolInternal.pool_error() == 527 PoolInternal.POE_INVALID_SEARCH) 528 return new ArrayList(); 529 else 530 throw new PoolsException(); 531 } 532 ArrayList aList = new ArrayList(components.size()); 533 for (int i = 0; i < components.size(); i++) { 534 /* 535 * Extract type information 536 */ 537 Value typeVal = new Value(name); 538 539 if (PoolInternal.pool_get_property(getConf(), 540 ((Long)components.get(i)).longValue(), "type", 541 typeVal.getValue()) == PoolInternal.POC_INVAL) 542 throw new PoolsException(); 543 if (typeVal == null) 544 throw new PoolsException(); 545 String type = typeVal.getString(); 546 typeVal.close(); 547 548 Value idValue = new Value(name); 549 550 if (PoolInternal.pool_get_property(getConf(), 551 ((Long)components.get(i)).longValue(), 552 type + ".sys_id", idValue.getValue()) == 553 PoolInternal.POC_INVAL) 554 throw new PoolsException(); 555 if (idValue == null) 556 throw new PoolsException(); 557 long sys_id = idValue.getLong(); 558 idValue.close(); 559 560 if (elements.containsKey(type + "." + sys_id)) 561 aList.add((Component)elements.get(type + "." + 562 sys_id)); 563 else 564 aList.add(new Component(this, ((Long)components. 565 get(i)).longValue())); 566 } 567 return (aList); 568 569 } 570 571 /** 572 * Get a list of component proxies which match the supplied 573 * selection criteria in values. 574 * 575 * @param values A list of values to be used to qualify the search. 576 * @throws PoolsExecption If there is an error executing the query. 577 * @return a list of component proxies which match the 578 * supplied criteria 579 */ checkComponents(List values)580 List checkComponents(List values) throws PoolsException 581 { 582 List components; 583 584 if ((components = PoolInternal.pool_query_components(getConf(), 585 values)) == null) { 586 if (PoolInternal.pool_error() == 587 PoolInternal.POE_INVALID_SEARCH) 588 return new ArrayList(); 589 else 590 throw new PoolsException(); 591 } 592 return (components); 593 } 594 /** 595 * Returns a descriptive string which describes the configuration. 596 * 597 * @param deep Whether the information should contain information about 598 * all contained elements. 599 * @return a descriptive string which describes the configuration. 600 */ getInformation(int deep)601 public String getInformation(int deep) 602 { 603 return (PoolInternal.pool_conf_info(_conf.getConf(), deep)); 604 } 605 606 /** 607 * Returns a string representation of this configuration. 608 * 609 * @return a string representation of this configuration. 610 */ toString()611 public String toString() 612 { 613 StringBuffer buf = new StringBuffer(); 614 615 buf.append("system: "); 616 buf.append(name); 617 return (buf.toString()); 618 } 619 620 /** 621 * Indicates whether some other Configuration is "equal to this one. 622 * @param o the reference object with which to compare. 623 * @return <code>true</code> if this object is the same as the 624 * o argument; <code>false</code> otherwise. 625 * @see #hashCode() 626 */ equals(Object o)627 public boolean equals(Object o) 628 { 629 if (o == this) 630 return (true); 631 if (!(o instanceof Configuration)) 632 return (false); 633 Configuration other = (Configuration) o; 634 if (name.compareTo(other.getName()) != 0) 635 return (false); 636 return (true); 637 } 638 639 /** 640 * Returns a hash code value for the object. This method is 641 * supported for the benefit of hashtables such as those provided by 642 * <code>java.util.Hashtable</code>. 643 * 644 * @return a hash code value for this object. 645 * @see #equals(java.lang.Object) 646 * @see java.util.Hashtable 647 */ hashCode()648 public int hashCode() 649 { 650 return name.hashCode(); 651 } 652 653 /** 654 * Return the pointer to this configuration as an element. 655 * 656 * @return The pointer to the native configuration which this object 657 * wraps. 658 * @throws PoolsExecption If there is an error converting the native 659 * configuration pointer to a native elem pointer. 660 */ getElem()661 protected long getElem() throws PoolsException 662 { 663 long elem; 664 665 if ((elem = PoolInternal.pool_conf_to_elem(getConf())) == 0) 666 throw new PoolsException(); 667 return (elem); 668 } 669 670 /** 671 * Return the anme of the configuration. 672 */ getName()673 String getName() 674 { 675 return (name); 676 } 677 678 /** 679 * Return the key of the configuration. 680 */ getKey()681 String getKey() 682 { 683 return (key); 684 } 685 } 686