this.kernel = kernel;
this.lifecycleBroadcaster = lifecycleBroadcaster;
this.gbeanInstanceState = new GBeanInstanceState(abstractName, kernel, dependencyManager, this, lifecycleBroadcaster);
this.classLoader = classLoader;
GBeanInfo gbeanInfo = gbeanData.getGBeanInfo();
try {
type = classLoader.loadClass(gbeanInfo.getClassName());
} catch (ClassNotFoundException e) {
throw new InvalidConfigurationException("Could not load GBeanInfo class from classloader: " + classLoader +
" className=" + gbeanInfo.getClassName(), e);
}
name = gbeanInfo.getName();
//
Set constructorArgs = new HashSet(gbeanInfo.getConstructor().getAttributeNames());
// interfaces
interfaces = (String[]) gbeanInfo.getInterfaces().toArray(new String[0]);
// attributes
Map attributesMap = new HashMap();
for (Iterator iterator = gbeanInfo.getAttributes().iterator(); iterator.hasNext();) {
GAttributeInfo attributeInfo = (GAttributeInfo) iterator.next();
attributesMap.put(attributeInfo.getName(), new GBeanAttribute(this, attributeInfo, constructorArgs.contains(attributeInfo.getName())));
}
addManagedObjectAttributes(attributesMap);
attributes = (GBeanAttribute[]) attributesMap.values().toArray(new GBeanAttribute[attributesMap.size()]);
for (int i = 0; i < attributes.length; i++) {
attributeIndex.put(attributes[i].getName(), new Integer(i));
}
// references
Set referencesSet = new HashSet();
Set dependencySet = new HashSet();
// add the references
Map dataReferences = gbeanData.getReferences();
for (Iterator iterator = gbeanInfo.getReferences().iterator(); iterator.hasNext();) {
GReferenceInfo referenceInfo = (GReferenceInfo) iterator.next();
String referenceName = referenceInfo.getName();
ReferencePatterns referencePatterns = (ReferencePatterns) dataReferences.remove(referenceName);
if (referenceInfo.getProxyType().equals(Collection.class.getName())) {
referencesSet.add(new GBeanCollectionReference(this, referenceInfo, kernel, referencePatterns));
} else {
referencesSet.add(new GBeanSingleReference(this, referenceInfo, kernel, referencePatterns));
if (referencePatterns != null) {
dependencySet.add(new GBeanDependency(this, referencePatterns.getAbstractName(), kernel));
}
}
}
if (!dataReferences.isEmpty()) {
throw new IllegalStateException("Attempting to set unknown references: " + dataReferences.keySet());
}
references = (GBeanReference[]) referencesSet.toArray(new GBeanReference[referencesSet.size()]);
for (int i = 0; i < references.length; i++) {
referenceIndex.put(references[i].getName(), new Integer(i));
}
//dependencies
for (Iterator iterator = gbeanData.getDependencies().iterator(); iterator.hasNext();) {
AbstractName dependencyName = ((ReferencePatterns) iterator.next()).getAbstractName();
dependencySet.add(new GBeanDependency(this, dependencyName, kernel));
}
dependencies = (GBeanDependency[]) dependencySet.toArray(new GBeanDependency[dependencySet.size()]);
// framework operations -- all framework operations have currently been removed
// operations
Map operationsMap = new HashMap();
for (Iterator iterator = gbeanInfo.getOperations().iterator(); iterator.hasNext();) {
GOperationInfo operationInfo = (GOperationInfo) iterator.next();
GOperationSignature signature = new GOperationSignature(operationInfo.getName(), operationInfo.getParameterList());
// do not allow overriding of framework operations
if (!operationsMap.containsKey(signature)) {
GBeanOperation operation = new GBeanOperation(this, operationInfo);
operationsMap.put(signature, operation);
}
}
operations = new GBeanOperation[operationsMap.size()];
int opCounter = 0;
for (Iterator iterator = operationsMap.entrySet().iterator(); iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
operations[opCounter] = (GBeanOperation) entry.getValue();
operationIndex.put(entry.getKey(), new Integer(opCounter));
opCounter++;
}
// get the constructor
List arguments = gbeanInfo.getConstructor().getAttributeNames();
Class[] parameterTypes = new Class[arguments.size()];
for (int i = 0; i < parameterTypes.length; i++) {
String argumentName = (String) arguments.get(i);
if (referenceIndex.containsKey(argumentName)) {
Integer index = (Integer) referenceIndex.get(argumentName);
GBeanReference reference = references[index.intValue()];
parameterTypes[i] = reference.getProxyType();
} else if (attributeIndex.containsKey(argumentName)) {
Integer index = (Integer) attributeIndex.get(argumentName);
GBeanAttribute attribute = attributes[index.intValue()];
parameterTypes[i] = attribute.getType();
}
}
try {
constructor = type.getConstructor(parameterTypes);
} catch (NoSuchMethodException e) {
StringBuffer buf = new StringBuffer("Could not find a valid constructor for GBean: ").append(gbeanInfo.getName()).append("\n");
buf.append("ParameterTypes: ").append(Arrays.asList(parameterTypes)).append("\n");
Constructor[] constructors = type.getConstructors();
for (int i = 0; i < constructors.length; i++) {
Constructor testConstructor = constructors[i];
buf.append("constructor types: ").append(Arrays.asList(testConstructor.getParameterTypes())).append("\n");
if (testConstructor.getParameterTypes().length == parameterTypes.length) {
Class[] testParameterTypes = testConstructor.getParameterTypes();
for (int k = 0; k < testParameterTypes.length; k++) {
Class testParameterType = testParameterTypes[k];
if (parameterTypes[k].getName().equals(testParameterType.getName())) {
if (parameterTypes[k].getClassLoader() != testParameterType.getClassLoader()) {
buf.append("different classloaders in position: ").append(k).append(" class name: ").append(testParameterType.getName()).append("\n");
buf.append("parameter type classloader: ").append(parameterTypes[k].getClassLoader()).append("\n");
buf.append("constructor type classloader: ").append(testParameterType.getClassLoader()).append("\n");
}
} else {
buf.append("different type in position: ").append(k).append("\n");
}
}
}
}
throw new InvalidConfigurationException(buf.toString());
} catch (NoClassDefFoundError e) {
throw new InvalidConfigurationException(e);
}
// rebuild the gbean info based on the current attributes, operations, and references because
// the above code add new attributes and operations
this.gbeanInfo = rebuildGBeanInfo(gbeanInfo.getConstructor(), gbeanInfo.getJ2eeType());
// create the raw invokers
rawInvoker = new RawInvoker(this);
// set the initial attribute values