boolean merged = false;
ManagementComponent mc = managementObject.componentType();
boolean isMC = !(mc.type().length() == 0 && mc.subtype().length() == 0);
// Merge this with the ManagedObject
ManagedObject parentMO = moRegistry.get(key);
if (parentMO == null && isMC == false)
{
log.debug("Deferring resolution of runtime ManagedObject: "+managementObject);
// Save the runtime mo for merging
runtimeMOs.put(key, mo);
}
else
{
mergeRuntimeMO(parentMO, mo);
merged = true;
runtimeMOs.remove(key);
}
// Update the runtime state of any ManagedComponent associated with this runtime mo
ManagedComponent comp = md.getComponent(mo.getName());
if (comp != null)
{
RunState state = updateRunState(mo, comp);
log.debug("Updated component: "+comp+" run state to: "+state);
}
// There is no further processing of runtime ManagedObjects, unless its marked as a component
if (isMC == false)
return;
//
else if (merged == false)
{
Set<ManagedOperation> runtimeOps = mo.getOperations();
runtimeOps = createOperationProxies(mo, runtimeOps);
MutableManagedObject moi = (MutableManagedObject) mo;
moi.setOperations(runtimeOps);
}
}
else
{
// See if there is runtime info to merge
ManagedObject runtimeMO = runtimeMOs.get(key);
if (runtimeMO != null)
{
mergeRuntimeMO(mo, runtimeMO);
runtimeMOs.remove(key);
// Update the runtime state of any ManagedComponent associated with this runtime mo
ManagedComponent comp = md.getComponent(mo.getName());
if (comp != null)
{
RunState state = updateRunState(runtimeMO, comp);
log.debug("Updated component: "+comp+" run state to: "+state);
}
}
}
// Update the MO registry
// TODO - does this make sense? In case of a MetaType.isCollection we could get different results then
// ManagedObject prevMO = moRegistry.put(key, mo);
// if( prevMO != null )
// {
// // This should only matter for ManagedObjects that have a ManagementObjectID
// log.debug("Duplicate mo for key: "+key+", prevMO: "+prevMO);
// return;
// }
// Check for unresolved refs
checkForReferences(key, mo);
// Map any existing ManagedComponent types
for(ManagedComponent comp : md.getComponents().values())
{
log.debug("Updating ManagementComponent: "+comp);
ComponentType type = comp.getType();
Set<ManagedComponent> typeComps = compByCompType.get(type);
if (typeComps == null)
{
typeComps = new HashSet<ManagedComponent>();
compByCompType.put(type, typeComps);
}
typeComps.add(comp);
}
// Create ManagedComponents for ManagedObjects annotated with ManagementComponent
ManagementComponent mc = (ManagementComponent) moAnns.get(ManagementComponent.class.getName());
if (mc != null && md.getComponent(mo.getName()) == null)
{
ComponentType type = new ComponentType(mc.type(), mc.subtype());
ManagedComponentImpl comp = new TempManagedComponentImpl(type, md, mo);
md.addComponent(mo.getName(), comp);
log.debug("Processing ManagementComponent("+mo.getName()+"): "+comp);
Set<ManagedComponent> typeComps = compByCompType.get(type);
if (typeComps == null)
{
typeComps = new HashSet<ManagedComponent>();
compByCompType.put(type, typeComps);
}
typeComps.add(comp);
RunState state = updateRunState(mo, comp);
}
// Scan for @ManagementObjectRef
for(ManagedProperty prop : mo.getProperties().values())
{
log.debug("Checking property: "+prop);
// See if this is a ManagementObjectID
Map<String, Annotation> pannotations = prop.getAnnotations();
if (pannotations != null && pannotations.isEmpty() == false)
{
ManagementObjectID id = (ManagementObjectID) pannotations.get(ManagementObjectID.class.getName());
if (id != null)
{
Object refName = getRefName(prop.getValue());
if (refName == null)
refName = id.name();
String propKey = refName + "/" + id.type();
log.debug("ManagedProperty level ID for ManagedObject: "+propKey+", attachmentName: "+mo.getAttachmentName());
moRegistry.put(propKey, mo);
checkForReferences(propKey, mo);
}
// See if this is a ManagementObjectRef
ManagementObjectRef ref = (ManagementObjectRef) pannotations.get(ManagementObjectRef.class.getName());
if ( ref != null )
{
// The reference key is the prop value + ref.type()
log.debug("Property("+prop.getName()+") references: "+ref);
Object refName = getRefName(prop.getValue());
if (refName == null)
refName = ref.name();
String targetKey = refName + "/" + ref.type();
ManagedObject target = moRegistry.get(targetKey);
if (target != null)
{
log.debug("Resolved property("+prop.getName()+") reference to: "+targetKey);
prop.setTargetManagedObject(target);
}
else
{
Set<ManagedProperty> referers = unresolvedRefs.get(targetKey);
if (referers == null)
{
referers = new HashSet<ManagedProperty>();
unresolvedRefs.put(targetKey, referers);
}
referers.add(prop);
}
}
}
MetaType propType = prop.getMetaType();
if (propType == AbstractManagedObjectFactory.MANAGED_OBJECT_META_TYPE)
{
processGenericValue ((GenericValue)prop.getValue(), md);
}
else if (propType.isArray())
{
ArrayMetaType amt = (ArrayMetaType) propType;
MetaType etype = amt.getElementType();
if (etype == AbstractManagedObjectFactory.MANAGED_OBJECT_META_TYPE)
{
ArrayValue avalue = (ArrayValue) prop.getValue();
int length = avalue != null ? avalue.getLength() : 0;
for(int n = 0; n < length; n ++)
processGenericValue((GenericValue) avalue.getValue(n), md);
}
}
else if (propType.isCollection())
{
CollectionMetaType amt = (CollectionMetaType) propType;
MetaType etype = amt.getElementType();
if (etype == AbstractManagedObjectFactory.MANAGED_OBJECT_META_TYPE)
{
CollectionValue avalue = (CollectionValue) prop.getValue();
if(avalue != null)
{
MetaValue[] elements = avalue.getElements();
for(int n = 0; n < avalue.getSize(); n ++)
{
GenericValue gv = (GenericValue) elements[n];
ManagedObject propMO = (ManagedObject) gv.getValue();
if(propMO != null)
processManagedObject(propMO, md);
}
}
}