// find the composite that will be updated
Composite composite = composites.get(compositeQName);
if (composite == null) {
throw new NodeException("trying to update composite " + compositeQName.toString() +
" which can't be found in node " + nodeURI);
}
// parse the XML into an composite object
Composite newComposite = null;
ExtensionPointRegistry registry = nodeRuntime.getExtensionPointRegistry();
StAXArtifactProcessorExtensionPoint staxProcessors =
registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class);
StAXArtifactProcessor<Composite> processor = staxProcessors.getProcessor(Composite.class);
try {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader = inputFactory.createXMLStreamReader(bais);
newComposite = processor.read(reader);
reader.close();
} catch (Exception ex) {
throw new NodeException(ex);
}
// for each component in the composite compare it against the live component
for (Component newComponent : newComposite.getComponents()){
for (Component component : composite.getComponents()){
if (component.getName().equals(newComponent.getName())){
// compare the component references
for (Reference newReference : newComponent.getReferences()){
for (Reference reference : component.getReferences()) {
if (reference.getName().equals(newReference.getName())) {
boolean referenceChanged = false;
List<Binding> removeCandidates = new ArrayList<Binding>();
List<Binding> addCandidates = new ArrayList<Binding>();
removeCandidates.addAll(reference.getBindings());
for (Binding newBinding : newReference.getBindings()){
boolean bindingFound = false;
for (Binding binding : reference.getBindings()){
// find the matching target service binding
if (binding.getName().equals(newBinding.getName())){
if ((binding.getURI() != null) &&
(newBinding.getURI() != null) &&
!binding.getURI().equals(newBinding.getURI())){
binding.setURI(newBinding.getURI());
referenceChanged = true;
logger.log(Level.INFO, "Updating binding " +
component.getName() +
" reference " +
reference.getName() +
" binding " +
binding.getClass().getName() +
" URI " +
binding.getURI());
}
bindingFound = true;
removeCandidates.remove(binding);
}
}
if (bindingFound == false){
addCandidates.add(newBinding);
}
}
for (Binding addBinding : addCandidates){
reference.getBindings().add(addBinding);
referenceChanged = true;
logger.log(Level.INFO, "Adding binding " +
component.getName() +
" reference " +
reference.getName() +
" binding " +
addBinding.getClass().getName() +
" URI " +
addBinding.getURI());
}
// remove all of the old bindings
for (Binding removeBinding : removeCandidates){
reference.getBindings().remove(removeBinding);
referenceChanged = true;
logger.log(Level.INFO, "Removing binding " +
component.getName() +
" reference " +
reference.getName() +
" binding " +
removeBinding.getClass().getName() +
" URI " +
removeBinding.getURI());
}
// if the node is running restart the reference and the component that holds it
if (referenceChanged && nodeStarted){
try {
nodeRuntime.getCompositeActivator().stop((RuntimeComponent)component);
nodeRuntime.getCompositeActivator().deactivate((RuntimeComponent)component,
(RuntimeComponentReference)reference);
nodeRuntime.getCompositeActivator().start((RuntimeComponent)component,
(RuntimeComponentReference)reference);
nodeRuntime.getCompositeActivator().start((RuntimeComponent)component);
} catch (Exception ex) {
throw new NodeException(ex);
}
}
}
}