package javax.management.relation;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.jboss.mx.util.Serialization;
public class RelationTypeSupport
implements RelationType
{
private String name;
private HashMap roleInfos;
private static final long serialVersionUID;
private static final ObjectStreamField[] serialPersistentFields;
static
{
switch (Serialization.version)
{
case Serialization.V1R0:
serialVersionUID = -8179019472410837190L;
serialPersistentFields = new ObjectStreamField[]
{
new ObjectStreamField("myIsInRelServFlg", Boolean.TYPE),
new ObjectStreamField("myRoleName2InfoMap", HashMap.class),
new ObjectStreamField("myTypeName", String.class),
};
break;
default:
serialVersionUID = 4611072955724144607L;
serialPersistentFields = new ObjectStreamField[]
{
new ObjectStreamField("isInRelationService", Boolean.TYPE),
new ObjectStreamField("roleName2InfoMap", HashMap.class),
new ObjectStreamField("typeName", String.class),
};
}
}
protected RelationTypeSupport(String name)
{
this.name = name;
roleInfos = new HashMap();
}
public RelationTypeSupport(String name, RoleInfo[] infos)
throws IllegalArgumentException, InvalidRelationTypeException
{
if (name == null)
throw new IllegalArgumentException("Null name");
if (infos == null)
throw new IllegalArgumentException("No role information");
if (infos.length == 0)
throw new InvalidRelationTypeException("No role information");
this.name = name;
roleInfos = new HashMap();
for (int i = 0; i < infos.length; i++)
{
if (infos[i] == null)
throw new InvalidRelationTypeException("Null role");
if (roleInfos.containsKey(infos[i].getName()))
throw new InvalidRelationTypeException(
"Duplicate role name" + infos[i].getName());
roleInfos.put(infos[i].getName(), infos[i]);
}
}
public String getRelationTypeName()
{
return name;
}
public List getRoleInfos()
{
return new ArrayList(roleInfos.values());
}
public RoleInfo getRoleInfo(String roleInfoName)
throws IllegalArgumentException, RoleInfoNotFoundException
{
if (roleInfoName == null)
throw new IllegalArgumentException("Null role info name");
RoleInfo result = (RoleInfo) roleInfos.get(roleInfoName);
if (result == null)
throw new RoleInfoNotFoundException(roleInfoName);
return result;
}
protected void addRoleInfo(RoleInfo roleInfo)
throws IllegalArgumentException, InvalidRelationTypeException
{
if (roleInfo == null)
throw new IllegalArgumentException("No role information");
String newName = roleInfo.getName();
if (roleInfos.containsKey(newName))
throw new InvalidRelationTypeException("Duplicate role name");
roleInfos.put(newName, roleInfo);
}
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException
{
ObjectInputStream.GetField getField = ois.readFields();
switch (Serialization.version)
{
case Serialization.V1R0:
roleInfos = (HashMap) getField.get("myRoleName2InfoMap", null);
name = (String) getField.get("myTypeName", null);
break;
default:
roleInfos = (HashMap) getField.get("roleName2InfoMap", null);
name = (String) getField.get("typeName", null);
}
}
private void writeObject(ObjectOutputStream oos)
throws IOException
{
ObjectOutputStream.PutField putField = oos.putFields();
switch (Serialization.version)
{
case Serialization.V1R0:
putField.put("myTypeName", name);
putField.put("myRoleName2InfoMap", roleInfos);
break;
default:
putField.put("typeName", name);
putField.put("roleName2InfoMap", roleInfos);
}
oos.writeFields();
}
}