package test.rewolucja.resources.physical.base; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Stack; import test.rewolucja.energy.extension.EnergyExtension; import test.rewolucja.energy.profile.PowerInterface; import test.rewolucja.extensions.Extension; import test.rewolucja.extensions.ExtensionList; import test.rewolucja.extensions.ExtensionListImpl; import test.rewolucja.extensions.ExtensionType; import test.rewolucja.resources.Resource; import test.rewolucja.resources.ResourceCharacteristics; import test.rewolucja.resources.ResourceStatus; import test.rewolucja.resources.ResourceType; import test.rewolucja.resources.StructureEntityInterface; import test.rewolucja.resources.description.ExecResourceDescription; import test.rewolucja.resources.exception.ResourceException; import test.rewolucja.resources.logical.base.LogicalResource; import test.rewolucja.resources.properties.DefaultPropertiesBuilder; import test.rewolucja.resources.properties.PropertiesDirector; import test.rewolucja.resources.test.Event; import test.rewolucja.resources.utils.validator.ResourceNameValidator; import test.rewolucja.resources.utils.validator.ResourcePropertiesValidator; import test.rewolucja.resources.utils.validator.ResourceStatusValidator; import test.rewolucja.resources.utils.validator.ResourceTypeValidator; import test.rewolucja.resources.utils.validator.ResourceValidator; public class ComputingResource implements Resource, StructureEntityInterface { protected String name; protected ResourceType type; protected ResourceStatus status; protected ComputingResource parent; protected List children; protected ResourceCharacteristics resourceCharacteristic; //protected Properties properties; protected ExtensionList extensionList; public ExtensionList getExtensionList() { return extensionList; } /*public ComputingResource(String resourceName, ResourceType type, ResourceCharacteristics resourceCharacteristic) { super(); this.type = type; this.name = resourceName; this.status = ResourceStatus.FREE; this.resourceCharacteristic = resourceCharacteristic; this.extensionList = new ExtensionListImpl(1); }*/ public ComputingResource(ExecResourceDescription resDesc) { this.type = resDesc.getType(); this.name = resDesc.getResourceId(); this.status = ResourceStatus.FREE; this.extensionList = new ExtensionListImpl(1); initCharacteristics(resDesc); //this(resDesc.getResourceId(), resDesc.getType(), new ResourceCharacteristics( // resDesc.getResourceUnits())); } protected void initCharacteristics(ExecResourceDescription resDesc){ resourceCharacteristic = new ResourceCharacteristics(resDesc.getResourceUnits()); } public ComputingResource getParent() { return parent; } public void setParent(ComputingResource newParent) { parent = newParent; } public List getChildren() { return children; } public void addChild(ComputingResource child) { child.setParent(this); if (children == null) children = new ArrayList(); children.add(child); } public String getName() { return name; } public ResourceType getType() { return type; } public ResourceCharacteristics getResourceCharacteristic() { return resourceCharacteristic; } public ResourceStatus getStatus() { return status; } public void setStatus(ResourceStatus newStatus) { status = newStatus; if(children != null) { for(ComputingResource child: children){ child.setStatus(status); } } } public void triggerEventUp(Event event) { if(parent != null) parent.handleEvent(event); } public void handleEvent(Event event){ if (extensionList != null) { for (Extension extension : extensionList) { if (extension.supportsEvent(event)) { extension.handleEvent(event); } } } triggerEventUp(event); } public List getDescendantsByType(ResourceType type) throws ResourceException { List validators = new ArrayList(); validators.add(new ResourceTypeValidator(type)); return searchDescendants(validators); } public List getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) throws ResourceException { List validators = new ArrayList(); validators.add(new ResourceStatusValidator(status)); validators.add(new ResourceTypeValidator(type)); return searchDescendants(validators); } public ComputingResource getDescendantsByName(String resourceName) throws ResourceException { List validators = new ArrayList(); validators.add(new ResourceNameValidator(resourceName)); List resources = searchDescendants(validators); return resources.size() == 0 ? null : resources.get(0); } public List filterDescendants(Properties properties) { List validators = new ArrayList(); validators.add(new ResourcePropertiesValidator(properties)); return searchDescendants(validators); } protected List searchDescendants(List validators) { List descendants = new ArrayList(); if (children != null) { Stack toExamine = new Stack(); toExamine.push(this); while (!toExamine.isEmpty()) { ComputingResource resource = toExamine.pop(); List resources = resource.getChildren(); if (resources == null) continue; int numberOfRes = resources.size(); for (int i = 0; i < numberOfRes; i++) { ComputingResource resourceChild = resources.get(i); if (resourceChild.match(validators)) { descendants.add(resourceChild); } else toExamine.insertElementAt(resourceChild, 0); //toExamine.push(resourceChild); } } } return descendants; } protected boolean match(List validators){ for(ResourceValidator validator: validators){ if(validator.validate(this) == false) return false; } return true; } public Properties getProperties(){ PropertiesDirector propDirector = new PropertiesDirector(); propDirector.setPropertiesBuilder(new DefaultPropertiesBuilder()); propDirector.constructProperties(this); return propDirector.getProperties(); } public PowerInterface getPowerInterface(){ if (extensionList != null) { for (Extension extension : extensionList) { if (extension.getType() == ExtensionType.ENERGY_EXTENSION) { EnergyExtension ee = (EnergyExtension)extension; return ee.getPowerInterface(); } } } return null; } protected LogicalResource logicalResource; public LogicalResource getLogicalResource() { return logicalResource; } public void setLogicalResource(LogicalResource logRes) { logicalResource = logRes; } /*public void addLogicalResource(LogicalResource logicalResource) { if (logicalResources == null) logicalResources = new ArrayList(1); logicalResources.add(logicalResource); }*/ }