* @param builder
*/
protected void parseListeners(Element element, ParserContext context, BeanDefinitionBuilder builder) {
List listeners = DomUtils.getChildElementsByTagName(element, LISTENER);
ManagedList listenersRef = new ManagedList();
// loop on listeners
for (Iterator iter = listeners.iterator(); iter.hasNext();) {
Element listnr = (Element) iter.next();
// wrapper target object
Object target = null;
// target bean name (in case of a reference)
String targetName = null;
// filter elements
NodeList nl = listnr.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element beanDef = (Element) node;
// check inline ref
if (listnr.hasAttribute(REF))
context.getReaderContext().error(
"nested bean declaration is not allowed if 'ref' attribute has been specified", beanDef);
target = context.getDelegate().parsePropertySubElement(beanDef, builder.getBeanDefinition());
// if this is a bean reference (nested <ref>), extract the name
if (target instanceof RuntimeBeanReference) {
targetName = ((RuntimeBeanReference) target).getBeanName();
}
}
}
// extract bind/unbind attributes from <osgi:listener>
// Element
MutablePropertyValues vals = new MutablePropertyValues();
NamedNodeMap attrs = listnr.getAttributes();
for (int x = 0; x < attrs.getLength(); x++) {
Attr attribute = (Attr) attrs.item(x);
String name = attribute.getLocalName();
// extract ref value
if (REF.equals(name))
targetName = attribute.getValue();
else
vals.addPropertyValue(Conventions.attributeNameToPropertyName(name), attribute.getValue());
}
// create serviceListener adapter
RootBeanDefinition wrapperDef = new RootBeanDefinition(OsgiServiceLifecycleListenerAdapter.class);
// set the target name (if we have one)
if (targetName != null)
vals.addPropertyValue(TARGET_BEAN_NAME_PROP, targetName);
// else set the actual target
else
vals.addPropertyValue(TARGET_PROP, target);
wrapperDef.setPropertyValues(vals);
// add listener to list
listenersRef.add(wrapperDef);
}
builder.addPropertyValue(LISTENERS_PROP, listenersRef);
}