public DeploymentPolicyReader(File file) {
super(file);
}
public DeploymentPolicy read() throws InvalidPolicyException{
DeploymentPolicy policy = new DeploymentPolicy();
try {
OMElement docEle = getDocument();
if("deploymentPolicy".equalsIgnoreCase(docEle.getLocalName())){
policy.setId(docEle.getAttributeValue(new QName("id")));
//Partition-Groups
Iterator<?> partitionGroupItr = docEle.getChildrenWithLocalName("partitionGroup");
while(partitionGroupItr.hasNext()){
List<PartitionGroup> partitionGroups = new ArrayList<PartitionGroup>();
Object nextGroup = partitionGroupItr.next();
if(nextGroup instanceof OMElement){
OMElement groupEle = (OMElement) nextGroup;
PartitionGroup group = new PartitionGroup();
group.setId(groupEle.getAttributeValue(new QName("id")));
group.setPartitionAlgo(readValue(groupEle, "partitionAlgo"));
List<Partition> partitions = new ArrayList<Partition>() ;
//Partitions
Iterator<?> partitionItr = groupEle.getChildrenWithLocalName("partition");
while(partitionItr.hasNext()){
Object next = partitionItr.next();
if(next instanceof OMElement){
OMElement partitionElt = (OMElement) next;
String partitionId = partitionElt.getAttributeValue(new QName("id"));
if (partitionId != null) {
Partition partition = new Partition();
partition.setId(partitionId);
String maxValue = readValue(partitionElt, "max");
if (maxValue != null) {
partition.setPartitionMax(Integer.valueOf(maxValue));
}
String minValue = readValue(partitionElt, "min");
if (minValue != null) {
partition.setPartitionMin(Integer.valueOf(minValue));
}
String providerValue = readValue(partitionElt, "provider");
if (providerValue != null) {
partition.setProvider(providerValue);
}
Properties properties = AutoscalerUtil.getProperties(partitionElt);
if (properties != null) {
partition.setProperties(properties);
}
partitions.add(partition);
} else {
log.warn("Invalid Partition id: null. Partition will be ignored.");
}
}
}
if(group.getPartitions() == null) {
group.setPartitions(new Partition[0]);
}
group.setPartitions(partitions.toArray(group.getPartitions()));
partitionGroups.add(group);
}
if(policy.getPartitionGroups() == null) {
policy.setPartitionGroups(new PartitionGroup[0]);
}
policy.setPartitionGroups(partitionGroups.toArray(policy.getPartitionGroups()));
}
} else{
throw new DeploymentException("File is not a valid deployment policy");
}