*/
@SuppressWarnings("unchecked")
public ManagedObject buildManagedObject(Class<? extends Serializable> clazz)
{
boolean trace = log.isTraceEnabled();
BeanInfo beanInfo = configuration.getBeanInfo(clazz);
ClassInfo classInfo = beanInfo.getClassInfo();
ManagementObject managementObject = classInfo.getUnderlyingAnnotation(ManagementObject.class);
if( managementObject == null )
{
if (trace)
log.trace("No ManagementObject annotation, skipping ManagedObject for class: "+clazz);
// Skip the ManagedObject creation
return null;
}
HashMap<String, Annotation> moAnnotations = new HashMap<String, Annotation>();
moAnnotations.put(ManagementObject.class.getName(), managementObject);
ManagementObjectID moID = classInfo.getUnderlyingAnnotation(ManagementObjectID.class);
if (moID != null)
moAnnotations.put(ManagementObjectID.class.getName(), moID);
// Process the ManagementObject fields
boolean isRuntime = managementObject.isRuntime();
String name = classInfo.getName();
String nameType = null;
String attachmentName = classInfo.getName();
Class<? extends Fields> moFieldsFactory = null;
Class<? extends ManagedPropertyConstraintsPopulatorFactory> moConstraintsFactory = null;
Class<? extends ManagedProperty> moPropertyFactory = null;
if (managementObject != null)
{
name = managementObject.name();
if (name.length() == 0 || name.equals(ManagementConstants.GENERATED))
name = classInfo.getName();
nameType = managementObject.type();
if (nameType.length() == 0)
nameType = null;
attachmentName = managementObject.attachmentName();
if (attachmentName.length() == 0)
attachmentName = classInfo.getName();
// Check for a component specification
ManagementComponent mc = managementObject.componentType();
if (mc.equals(AnnotationDefaults.COMP_TYPE) == false)
moAnnotations.put(ManagementComponent.class.getName(), mc);
// ManagementObject level default factory classes
moFieldsFactory = managementObject.fieldsFactory();
moConstraintsFactory = managementObject.constraintsFactory();
moPropertyFactory = managementObject.propertyFactory();
}
if (trace)
{
log.trace("Building MangedObject(name="+name+",nameType="+nameType
+",attachmentName="+attachmentName+",isRuntime="+isRuntime+")");
}
ManagementProperties propertyType = ManagementProperties.ALL;
if (managementObject != null)
propertyType = managementObject.properties();
// Build the ManagedProperties
Set<ManagedProperty> properties = new HashSet<ManagedProperty>();
Set<PropertyInfo> propertyInfos = beanInfo.getProperties();
if (propertyInfos != null && propertyInfos.isEmpty() == false)
{
for (PropertyInfo propertyInfo : propertyInfos)
{
// Ignore the "class" property
if ("class".equals(propertyInfo.getName()))
continue;
ManagementProperty managementProperty = propertyInfo.getUnderlyingAnnotation(ManagementProperty.class);
ManagementObjectID id = propertyInfo.getUnderlyingAnnotation(ManagementObjectID.class);
ManagementObjectRef ref = propertyInfo.getUnderlyingAnnotation(ManagementObjectRef.class);
ManagementRuntimeRef runtimeRef = propertyInfo.getUnderlyingAnnotation(ManagementRuntimeRef.class);
HashMap<String, Annotation> propAnnotations = new HashMap<String, Annotation>();
if (managementProperty != null)
propAnnotations.put(ManagementProperty.class.getName(), managementProperty);
if (id != null)
{
propAnnotations.put(ManagementObjectID.class.getName(), id);
// This overrides the MO nameType
nameType = id.type();
}
if (ref != null)
propAnnotations.put(ManagementObjectRef.class.getName(), ref);
if (runtimeRef != null)
propAnnotations.put(ManagementRuntimeRef.class.getName(), runtimeRef);
// Check for a simple property
boolean includeProperty = (propertyType == ManagementProperties.ALL);
if (managementProperty != null)
includeProperty = (managementProperty.ignored() == false);
if (includeProperty)
{
Fields fields = null;
if (managementProperty != null)
{
Class<? extends Fields> factory = moFieldsFactory;
if (factory == ManagementProperty.NULL_FIELDS_FACTORY.class)
factory = managementProperty.fieldsFactory();
if (factory != ManagementProperty.NULL_FIELDS_FACTORY.class)
{
try
{
fields = factory.newInstance();
}
catch (Exception e)
{
log.debug("Failed to created Fields", e);
}
}
}
if (fields == null)
fields = new DefaultFieldsImpl();
if( propertyInfo instanceof Serializable )
{
Serializable info = Serializable.class.cast(propertyInfo);
fields.setField(Fields.PROPERTY_INFO, info);
}
String propertyName = propertyInfo.getName();
if (managementProperty != null)
propertyName = managementProperty.name();
if( propertyName.length() == 0 )
propertyName = propertyInfo.getName();
fields.setField(Fields.NAME, propertyName);
// This should probably always the the propertyInfo name?
String mappedName = propertyInfo.getName();
if (managementProperty != null)
mappedName = managementProperty.mappedName();
if( mappedName.length() == 0 )
mappedName = propertyInfo.getName();
fields.setField(Fields.MAPPED_NAME, mappedName);
String description = ManagementConstants.GENERATED;
if (managementProperty != null)
description = managementProperty.description();
if (description.equals(ManagementConstants.GENERATED))
description = propertyName;
fields.setField(Fields.DESCRIPTION, description);
if (trace)
{
log.trace("Building MangedProperty(name="+propertyName
+",mappedName="+mappedName
+") ,annotations="+propAnnotations);
}
boolean mandatory = false;
if (managementProperty != null)
mandatory = managementProperty.mandatory();
if (mandatory)
fields.setField(Fields.MANDATORY, Boolean.TRUE);
boolean managed = false;
if (managementProperty != null)
managed = managementProperty.managed();
MetaType metaType;
if (managed)
{
TypeInfo typeInfo = propertyInfo.getType();
if(typeInfo.isArray())
metaType = new ArrayMetaType(1, MANAGED_OBJECT_META_TYPE);
else if (typeInfo.isCollection())
metaType = new CollectionMetaType(typeInfo.getName(), MANAGED_OBJECT_META_TYPE);
else
metaType = MANAGED_OBJECT_META_TYPE;
}
else
{
metaType = metaTypeFactory.resolve(propertyInfo.getType());
}
fields.setField(Fields.META_TYPE, metaType);
if (propAnnotations.isEmpty() == false)
fields.setField(Fields.ANNOTATIONS, propAnnotations);
// Delegate others (legal values, min/max etc.) to the constraints factory
try
{
Class<? extends ManagedPropertyConstraintsPopulatorFactory> factoryClass = moConstraintsFactory;
if (factoryClass == ManagementProperty.NULL_CONSTRAINTS.class)
{
if (managementProperty != null)
factoryClass = managementProperty.constraintsFactory();
}
ManagedPropertyConstraintsPopulatorFactory factory = factoryClass.newInstance();
ManagedPropertyConstraintsPopulator populator = factory.newInstance();
if (populator != null)
populator.populateManagedProperty(clazz, propertyInfo, fields);
}
catch(Exception e)
{
log.debug("Failed to populate constraints for: "+propertyInfo, e);
}
ManagedProperty property = null;
if (managementProperty != null)
{
Class<? extends ManagedProperty> factory = moPropertyFactory;
if (factory == ManagementProperty.NULL_PROPERTY_FACTORY.class)
factory = managementProperty.propertyFactory();
if (factory != ManagementProperty.NULL_PROPERTY_FACTORY.class)
property = getManagedProperty(factory, fields);
}
// we should have write-through by default
// use factory to change this default behavior
if (property == null)
property = createDefaultManagedProperty(fields);
properties.add(property);
}
else if (trace)
log.trace("Ignoring property: " + propertyInfo);
}
}
/* TODO: Operations. In general the bean metadata does not contain
operation information.
*/
Set<ManagedOperation> operations = new HashSet<ManagedOperation>();
Set<MethodInfo> methodInfos = beanInfo.getMethods();
if (methodInfos != null && methodInfos.isEmpty() == false)
{
for (MethodInfo methodInfo : methodInfos)
{
ManagementOperation managementOp = methodInfo.getUnderlyingAnnotation(ManagementOperation.class);