public synchronized ObjectName preRegister(MBeanServer server, ObjectName objectName) throws Exception {
ObjectName returnValue = super.preRegister(server, objectName);
// get the constructor
GConstructorInfo constructorInfo = gbeanInfo.getConstructor();
Class[] parameterTypes = (Class[]) constructorInfo.getTypes().toArray(new Class[constructorInfo.getTypes().size()]);
Constructor constructor = type.getConstructor(parameterTypes);
// create the instance
Object[] parameters = new Object[parameterTypes.length];
Iterator names = constructorInfo.getAttributeNames().iterator();
Iterator assertedTypes = constructorInfo.getTypes().iterator();
for (int i = 0; i < parameters.length; i++) {
String name = (String) names.next();
if (attributeMap.containsKey(name)) {
parameters[i] = getAttribute(name);
} else if (referenceMap.containsKey(name)) {
GBeanMBeanReference reference = (GBeanMBeanReference) referenceMap.get(name);
reference.online();
parameters[i] = reference.getProxy();
} else {
throw new InvalidConfigurationException("Unknown attribute or reference name in constructor: name=" + name);
}
Class assertedType = (Class) assertedTypes.next();
assert parameters[i] == null || assertedType.isPrimitive() || assertedType.isAssignableFrom(parameters[i].getClass()):
"Attempting to construct " + objectName + " of type " + gbeanInfo.getClassName()
+ ". Constructor parameter " + i + " should be " + assertedType.getName()
+ " but is " + parameters[i].getClass().getName();
}
try {
target = constructor.newInstance(parameters);
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
if(targetException instanceof Exception) {
throw (Exception)targetException;
} else if(targetException instanceof Error) {
throw (Error)targetException;
}
throw e;
} catch (IllegalArgumentException e) {
log.warn("Constructor mismatch for " + returnValue, e);
throw e;
}
// bring all of the attributes online
for (Iterator iterator = attributeMap.values().iterator(); iterator.hasNext();) {
GBeanMBeanAttribute attribute = (GBeanMBeanAttribute) iterator.next();
attribute.online();
}
// bring any reference not used in the constructor online
// @todo this code sucks, but works
for (Iterator iterator = referenceMap.values().iterator(); iterator.hasNext();) {
GBeanMBeanReference reference = (GBeanMBeanReference) iterator.next();
if (!constructorInfo.getAttributeNames().contains(reference.getName())) {
reference.online();
}
}
return returnValue;