package com.sun.solaris.service.pools;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class Configuration extends Element
{
private boolean _valid = false;
private long _this;
private String name;
private Map elements;
private String key;
public Configuration(String location, int perms) throws PoolsException
{
if (((_this = PoolInternal.pool_conf_alloc())) == 0)
throw new PoolsException();
_conf = this;
open(location, perms);
elements = new HashMap();
}
protected void finalize() throws Throwable
{
try
{
close();
if (_this != 0) {
PoolInternal.pool_conf_free(_this);
_this = 0;
}
}
finally
{
super.finalize();
}
}
final long getConf()
{
return (_this);
}
public void open(String location, int perms) throws PoolsException
{
if (_valid == false) {
if (PoolInternal.pool_conf_open(getConf(), location,
perms) != PoolInternal.PO_SUCCESS) {
throw new PoolsException();
}
_valid = true;
name = getStringProperty("system.name");
key = "system." + name;
}
}
public void close()
{
if (_valid == true) {
elements.clear();
PoolInternal.pool_conf_close(getConf());
name = key = null;
}
_valid = false;
}
public String getLocation()
{
return (PoolInternal.pool_conf_location(getConf()));
}
public int status()
{
return (PoolInternal.pool_conf_status(getConf()));
}
public void remove() throws PoolsException
{
if (PoolInternal.pool_conf_remove(getConf()) !=
PoolInternal.PO_SUCCESS)
throw new PoolsException();
}
public void rollback() throws PoolsException
{
if (PoolInternal.pool_conf_rollback(getConf()) !=
PoolInternal.PO_SUCCESS)
throw new PoolsException();
}
public void commit(int active) throws PoolsException
{
if (PoolInternal.pool_conf_commit(getConf(), active) !=
PoolInternal.PO_SUCCESS)
throw new PoolsException();
}
public void export(String location, int format) throws PoolsException
{
if (PoolInternal.pool_conf_export(getConf(), location, format)
!= PoolInternal.PO_SUCCESS)
throw new PoolsException();
}
public void validate(int level) throws PoolsException
{
if (PoolInternal.pool_conf_validate(getConf(), level)
!= PoolInternal.PO_SUCCESS)
throw new PoolsException();
}
public int update() throws PoolsException
{
return (PoolInternal.pool_conf_update(getConf()));
}
public Pool createPool(String name) throws PoolsException
{
long aPool;
if ((aPool = PoolInternal.pool_create(getConf(), name)) == 0) {
throw new PoolsException();
}
Pool p = new Pool(this, aPool);
elements.put(p.getKey(), p);
return (p);
}
public void destroyPool(Pool aPool) throws PoolsException
{
elements.remove(aPool.getKey());
PoolInternal.pool_destroy(getConf(), aPool.getPool());
}
public Pool getPool(String name) throws PoolsException
{
long aPool;
if ((aPool = PoolInternal.pool_get_pool(getConf(), name)) ==
0) {
throw new PoolsException();
}
if (elements.containsKey("PoolInternal." + name))
return ((Pool) elements.get("PoolInternal." + name));
else {
Pool p = new Pool(this, aPool);
elements.put(p.getKey(), p);
return (p);
}
}
long checkPool(String name) throws PoolsException
{
long aPool;
if ((aPool = PoolInternal.pool_get_pool(getConf(), name)) ==
0) {
throw new PoolsException();
}
return (aPool);
}
public List getPools(List values) throws PoolsException
{
List pools;
if ((pools = PoolInternal.pool_query_pools(getConf(), values))
== null) {
if (PoolInternal.pool_error() ==
PoolInternal.POE_INVALID_SEARCH)
return new ArrayList();
else
throw new PoolsException();
}
ArrayList aList = new ArrayList(pools.size());
for (int i = 0; i < pools.size(); i++)
aList.add(new Pool(this,
((Long)pools.get(i)).longValue()));
return (aList);
}
public Resource createResource(String type, String name)
throws PoolsException
{
long aResource;
if ((aResource = PoolInternal.pool_resource_create(getConf(),
type, name)) == 0) {
throw new PoolsException();
}
Resource res = new Resource(this, aResource);
elements.put(res.getKey(), res);
return (res);
}
public void destroyResource(Resource res) throws PoolsException
{
elements.remove(res.getKey());
PoolInternal.pool_resource_destroy(getConf(),
res.getResource());
}
public Resource getResource(String type, String name)
throws PoolsException
{
long res;
if ((res = PoolInternal.pool_get_resource(getConf(), type,
name)) == 0) {
throw new PoolsException();
}
if (elements.containsKey(type + "." + name))
return ((Resource) elements.get(type + "." + name));
else {
Resource r = new Resource(this, res);
elements.put(r.getKey(), r);
return (r);
}
}
long checkResource(String type, String name) throws PoolsException
{
long res;
if ((res = PoolInternal.pool_get_resource(getConf(), type,
name)) == 0) {
throw new PoolsException();
}
return (res);
}
public List getResources(List values) throws PoolsException
{
List resources;
if ((resources = PoolInternal.pool_query_resources(getConf(),
values)) == null) {
if (PoolInternal.pool_error() ==
PoolInternal.POE_INVALID_SEARCH)
return new ArrayList();
else
throw new PoolsException();
}
ArrayList aList = new ArrayList(resources.size());
for (int i = 0; i < resources.size(); i++)
aList.add(new Resource(this,
((Long)resources.get(i)).longValue()));
return (aList);
}
public Component getComponent(String type, long sys_id)
throws PoolsException
{
List props = new ArrayList();
Value ptype = new Value("type", type);
Value psys_id = new Value(type + ".sys_id", sys_id);
props.add(ptype);
props.add(psys_id);
List comps = getComponents(props);
ptype.close();
psys_id.close();
if (comps.size() != 1)
throw new PoolsException();
return ((Component) comps.get(0));
}
long checkComponent(String type, long sys_id)
throws PoolsException
{
List props = new ArrayList();
Value ptype = new Value("type", type);
Value psys_id = new Value(type + ".sys_id", sys_id);
props.add(ptype);
props.add(psys_id);
List comps = checkComponents(props);
ptype.close();
psys_id.close();
if (comps.size() != 1)
throw new PoolsException();
return (((Long)comps.get(0)).longValue());
}
public List getComponents(List values) throws PoolsException
{
List components;
if ((components = PoolInternal.pool_query_components(getConf(),
values)) == null) {
if (PoolInternal.pool_error() ==
PoolInternal.POE_INVALID_SEARCH)
return new ArrayList();
else
throw new PoolsException();
}
ArrayList aList = new ArrayList(components.size());
for (int i = 0; i < components.size(); i++) {
Value typeVal = new Value(name);
if (PoolInternal.pool_get_property(getConf(),
((Long)components.get(i)).longValue(), "type",
typeVal.getValue()) == PoolInternal.POC_INVAL)
throw new PoolsException();
if (typeVal == null)
throw new PoolsException();
String type = typeVal.getString();
typeVal.close();
Value idValue = new Value(name);
if (PoolInternal.pool_get_property(getConf(),
((Long)components.get(i)).longValue(),
type + ".sys_id", idValue.getValue()) ==
PoolInternal.POC_INVAL)
throw new PoolsException();
if (idValue == null)
throw new PoolsException();
long sys_id = idValue.getLong();
idValue.close();
if (elements.containsKey(type + "." + sys_id))
aList.add((Component)elements.get(type + "." +
sys_id));
else
aList.add(new Component(this, ((Long)components.
get(i)).longValue()));
}
return (aList);
}
List checkComponents(List values) throws PoolsException
{
List components;
if ((components = PoolInternal.pool_query_components(getConf(),
values)) == null) {
if (PoolInternal.pool_error() ==
PoolInternal.POE_INVALID_SEARCH)
return new ArrayList();
else
throw new PoolsException();
}
return (components);
}
public String getInformation(int deep)
{
return (PoolInternal.pool_conf_info(_conf.getConf(), deep));
}
public String toString()
{
StringBuffer buf = new StringBuffer();
buf.append("system: ");
buf.append(name);
return (buf.toString());
}
public boolean equals(Object o)
{
if (o == this)
return (true);
if (!(o instanceof Configuration))
return (false);
Configuration other = (Configuration) o;
if (name.compareTo(other.getName()) != 0)
return (false);
return (true);
}
public int hashCode()
{
return name.hashCode();
}
protected long getElem() throws PoolsException
{
long elem;
if ((elem = PoolInternal.pool_conf_to_elem(getConf())) == 0)
throw new PoolsException();
return (elem);
}
String getName()
{
return (name);
}
String getKey()
{
return (key);
}
}