Map<String, KernelDeployment> serverDeployments = null;
if(server instanceof MCServer)
{
// Build ManagedDeployments for the KernelDeployments
MCServer mcserver = MCServer.class.cast(server);
Kernel kernel = mcserver.getKernel();
serverDeployments = mcserver.getDeployments();
ManagedDeployment firstDeployment = null;
for(KernelDeployment kd : serverDeployments.values())
{
BootstrapDeployment deploymentUnit = new BootstrapDeployment(kd);
KernelDeploymentVisitor visitor = new KernelDeploymentVisitor();
try
{
visitor.deploy(deploymentUnit, kd);
}
catch(DeploymentException e)
{
log.debug("Failed to build ManagedDeployment for: "+kd, e);
continue;
}
/* Create minimal deployment ManagedObject. Don't use the ManagedObjectFactory
* as this will create ManagedObjects for the beans via the beansFactory
* property. We handle the beans below.
*/
Set<ManagedProperty> kdProperties = new HashSet<ManagedProperty>();
HashSet<ManagedOperation> ops = null;
ManagedObject kdMO = new ManagedObjectImpl(kd.getName(), "",
KernelDeployment.class.getName(),
kdProperties, ops, (Serializable) kd);
Map<String, ManagedObject> kdMOs = new HashMap<String, ManagedObject>();
kdMOs.put(kd.getName(), kdMO);
// Traverse the deployment components
for(DeploymentUnit compUnit : deploymentUnit.getComponents())
{
BeanMetaData bmd = compUnit.getAttachment(BeanMetaData.class);
ManagedObject bmdMO = mof.initManagedObject(bmd, compUnit.getMetaData());
if(bmdMO == null)
continue;
Map<String, Annotation> moAnns = bmdMO.getAnnotations();
ManagementObject mo = (ManagementObject) moAnns.get(ManagementObject.class.getName());
// Reset the name to the bean name rather than the attachment name
if(bmdMO instanceof MutableManagedObject)
{
MutableManagedObject mmo = (MutableManagedObject) bmdMO;
// Reset the name to the bean name if its the attachment name
if(mmo.getName().equals(mmo.getAttachmentName()))
mmo.setName(bmd.getName());
mmo.setParent(kdMO);
// Add an alias property
Set<Object> bmdAliases = bmd.getAliases();
Map<String, ManagedProperty> oldProps = mmo.getProperties();
Map<String, ManagedProperty> newProps = new HashMap<String, ManagedProperty>(oldProps);
if(bmdAliases != null && bmdAliases.size() > 0)
{
ArrayMetaType aliasType = new ArrayMetaType(SimpleMetaType.STRING, false);
DefaultFieldsImpl fields = getFields("alias", aliasType);
fields.setDescription("Aliases of the bean");
String[] aliases = new String[bmdAliases.size()];
Iterator<?> i = bmdAliases.iterator();
for(int n = 0; i.hasNext(); n++)
{
aliases[n] = i.next().toString();
}
ArrayValueSupport value = new ArrayValueSupport(aliasType, aliases);
fields.setValue(value);
ManagedPropertyImpl aliasesMP = new ManagedPropertyImpl(bmdMO, fields);
newProps.put("alias", aliasesMP);
}
// Add a state property
DefaultFieldsImpl stateFields = getFields("state", ControllerStateMetaType.TYPE);
stateFields.setViewUse(new ViewUse[]{ViewUse.STATISTIC});
EnumValue stateValue = getState(bmd.getName(), kernel);
stateFields.setValue(stateValue);
stateFields.setDescription("The bean controller state");
ManagedPropertyImpl stateMP = new ManagedPropertyImpl(mmo, stateFields);
newProps.put("state", stateMP);
// Update the properties
mmo.setProperties(newProps);
}
log.debug("Created ManagedObject: "+bmdMO+" for bean: "+bmd.getName());
kdMOs.put(bmd.getName(), bmdMO);
}
// Create the ManagedDeployment
ManagedDeployment md = mgtDeploymentCreator.build(deploymentUnit, kdMOs, null);
if(firstDeployment == null)
firstDeployment = md;
// Create the ManagedComponents
for(ManagedObject bmdMO : kdMOs.values())
{
if(bmdMO.getAttachmentName().equals(KernelDeployment.class.getName()))
continue;
ComponentType type = KnownComponentTypes.MCBean.Any.getType();
Map<String, Annotation> moAnns = bmdMO.getAnnotations();
ManagementComponent mc = (ManagementComponent) moAnns.get(ManagementComponent.class.getName());
if(mc != null)
{
type = new ComponentType(mc.type(), mc.subtype());
}
ManagedComponentImpl comp = new ManagedComponentImpl(type, md, bmdMO);
md.addComponent(bmdMO.getName(), comp);
log.debug("Created ManagedComponent("+comp.getName()+") of type: "
+type
+" for MO: "+bmdMO.getName()
+", componentName: "+bmdMO.getComponentName());
}
if(md != null)
bootstrapMDs.put(kd.getName(), md);
}
// Add other Server managed objects
if(firstDeployment != null)
{
ComponentType type = new ComponentType("MCBean", "MCServer");
ManagedObject serverMO = mof.initManagedObject(mcserver, null);
if (serverMO.getOperations() != null && serverMO.getOperations().size() == 0)
{
ManagedOperationImpl shutdown = new ManagedOperationImpl("Shutdown the server", "shutdown");
if(serverMO instanceof MutableManagedObject)
{
HashSet<ManagedOperation> ops = new HashSet<ManagedOperation>();
ops.add(shutdown);
MutableManagedObject mmo = MutableManagedObject.class.cast(serverMO);
mmo.setOperations(ops);
}
}
ManagedComponentImpl serverComp = new ManagedComponentImpl(type, firstDeployment, serverMO);
firstDeployment.addComponent("MCServer", serverComp);
try
{
BeanInfo mcserverInfo = configurator.getBeanInfo(mcserver.getClass());
BeanKernelRegistryEntry entry = new BeanKernelRegistryEntry(mcserver, mcserverInfo);
bootstrapEntries.put(serverComp.getComponentName(), entry);
}
catch(Throwable t)
{
log.error("Failed to create BeanInfo for: "+serverComp, t);
}
// ServerConfig
type = new ComponentType("MCBean", "ServerConfig");
ServerConfig config = mcserver.getConfig();
ManagedObject mo = mof.initManagedObject(config, null);
ManagedComponentImpl configComp = new ManagedComponentImpl(type, firstDeployment, mo);
firstDeployment.addComponent("ServerConfig", configComp);
log.debug("Created ManagedComponent of type: "+type+" for ServerConfig");
}