use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class CmNamespaceHandler method parsePropertyPlaceholder.
private ComponentMetadata parsePropertyPlaceholder(ParserContext context, Element element) {
MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
metadata.setProcessor(true);
metadata.setId(getId(context, element));
metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
metadata.setRuntimeClass(CmPropertyPlaceholder.class);
metadata.setInitMethod("init");
metadata.setDestroyMethod("destroy");
metadata.addProperty("blueprintContainer", createRef(context, "blueprintContainer"));
metadata.addProperty("configAdmin", createConfigurationAdminRef(context));
metadata.addProperty("persistentId", createValue(context, element.getAttribute(PERSISTENT_ID_ATTRIBUTE)));
String prefix = element.hasAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_PREFIX_ATTRIBUTE) : "${";
metadata.addProperty("placeholderPrefix", createValue(context, prefix));
String suffix = element.hasAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_SUFFIX_ATTRIBUTE) : "}";
metadata.addProperty("placeholderSuffix", createValue(context, suffix));
String nullValue = element.hasAttribute(PLACEHOLDER_NULL_VALUE_ATTRIBUTE) ? element.getAttribute(PLACEHOLDER_NULL_VALUE_ATTRIBUTE) : null;
if (nullValue != null) {
metadata.addProperty("nullValue", createValue(context, nullValue));
}
String defaultsRef = element.hasAttribute(DEFAULTS_REF_ATTRIBUTE) ? element.getAttribute(DEFAULTS_REF_ATTRIBUTE) : null;
if (defaultsRef != null) {
metadata.addProperty("defaultProperties", createRef(context, defaultsRef));
}
String ignoreMissingLocations = extractIgnoreMissingLocations(element);
if (ignoreMissingLocations != null) {
metadata.addProperty("ignoreMissingLocations", createValue(context, ignoreMissingLocations));
}
String systemProperties = extractSystemPropertiesAttribute(element);
if (systemProperties == null) {
systemProperties = SYSTEM_PROPERTIES_NEVER;
}
metadata.addProperty("systemProperties", createValue(context, systemProperties));
String updateStrategy = element.getAttribute(UPDATE_STRATEGY_ATTRIBUTE);
if (updateStrategy != null) {
metadata.addProperty("updateStrategy", createValue(context, updateStrategy));
}
metadata.addProperty("managedObjectManager", createRef(context, MANAGED_OBJECT_MANAGER_NAME));
// Parse elements
List<String> locations = new ArrayList<String>();
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element e = (Element) node;
if (isCmNamespace(e.getNamespaceURI())) {
if (nodeNameEquals(e, DEFAULT_PROPERTIES_ELEMENT)) {
if (defaultsRef != null) {
throw new ComponentDefinitionException("Only one of " + DEFAULTS_REF_ATTRIBUTE + " attribute or " + DEFAULT_PROPERTIES_ELEMENT + " element is allowed");
}
Metadata props = parseDefaultProperties(context, metadata, e);
metadata.addProperty("defaultProperties", props);
}
} else if (isExtNamespace(e.getNamespaceURI())) {
if (nodeNameEquals(e, LOCATION_ELEMENT)) {
locations.add(getTextValue(e));
}
}
}
}
if (!locations.isEmpty()) {
metadata.addProperty("locations", createList(context, locations));
}
PlaceholdersUtils.validatePlaceholder(metadata, context.getComponentDefinitionRegistry());
return metadata;
}
use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class ReferenceListRecipe method internalCreate.
@Override
protected Object internalCreate() throws ComponentDefinitionException {
try {
if (explicitDependencies != null) {
for (Recipe recipe : explicitDependencies) {
recipe.create();
}
}
ProvidedObject object = new ProvidedObject();
addPartialObject(object);
// Handle initial references
createListeners();
updateListeners();
return object;
} catch (ComponentDefinitionException t) {
throw t;
} catch (Throwable t) {
throw new ComponentDefinitionException(t);
}
}
use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class ServiceRecipe method createService.
private void createService() {
try {
if (service == null) {
LOGGER.debug("Creating service instance");
// We can't use the BlueprintRepository because we don't know what interfaces
// to use yet! We have to be a bit smarter.
ExecutionContext old = ExecutionContext.Holder.setContext(blueprintContainer.getRepository());
try {
Object o = serviceRecipe.create();
if (o instanceof Convertible) {
o = blueprintContainer.getRepository().convert(o, new ReifiedType(Object.class));
validateClasses(o);
} else if (o instanceof UnwrapperedBeanHolder) {
UnwrapperedBeanHolder holder = (UnwrapperedBeanHolder) o;
if (holder.unwrapperedBean instanceof ServiceFactory) {
// If a service factory is used, make sure the proxy classes implement this
// interface so that later on, internalGetService will create the real
// service from it.
LOGGER.debug("{} implements ServiceFactory, creating proxy that also implements this", holder.unwrapperedBean);
Collection<Class<?>> cls = getClassesForProxying(holder.unwrapperedBean);
cls.add(blueprintContainer.loadClass("org.osgi.framework.ServiceFactory"));
o = BeanRecipe.wrap(holder, cls);
} else {
validateClasses(holder.unwrapperedBean);
o = BeanRecipe.wrap(holder, getClassesForProxying(holder.unwrapperedBean));
}
} else if (!(o instanceof ServiceFactory)) {
validateClasses(o);
}
service = o;
} catch (Exception e) {
LOGGER.error("Error retrieving service from " + this, e);
throw new ComponentDefinitionException(e);
} finally {
ExecutionContext.Holder.setContext(old);
}
LOGGER.debug("Service created: {}", service);
}
// When the service is first requested, we need to create listeners and call them
if (!initialServiceRegistration && listeners == null) {
LOGGER.debug("Creating listeners");
if (listenersRecipe != null) {
listeners = (List) createRecipe(listenersRecipe);
} else {
listeners = Collections.emptyList();
}
LOGGER.debug("Listeners created: {}", listeners);
if (registration.get() != null) {
LOGGER.debug("Calling listeners for initial service registration");
for (ServiceListener listener : listeners) {
listener.register(service, registrationProperties);
}
} else {
LOGGER.debug("Calling listeners for initial service unregistration");
for (ServiceListener listener : listeners) {
listener.unregister(service, registrationProperties);
}
}
}
} catch (RuntimeException e) {
LOGGER.error("Error retrieving service from " + this, e);
throw e;
}
}
use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class BeanRecipe method createProxyBean.
private Object createProxyBean(ReferenceRecipe rr) {
try {
VoidableCallable vc = new VoidableCallable();
rr.addVoidableChild(vc);
return blueprintContainer.getProxyManager().createDelegatingProxy(blueprintContainer.getBundleContext().getBundle(), rr.getProxyChildBeanClasses(), vc, vc.call());
} catch (UnableToProxyException e) {
throw new ComponentDefinitionException(e);
}
}
use of org.osgi.service.blueprint.container.ComponentDefinitionException in project aries by apache.
the class BeanRecipe method getInstanceFromFactory.
private Object getInstanceFromFactory(List<Object> args, List<ReifiedType> argTypes) {
Object factoryObj = getFactoryObj();
// Map of matching methods
Map<Method, List<Object>> matches = findMatchingMethods(factoryObj.getClass(), factoryMethod, true, args, argTypes);
if (matches.size() == 1) {
try {
Map.Entry<Method, List<Object>> match = matches.entrySet().iterator().next();
return invoke(match.getKey(), factoryObj, match.getValue().toArray());
} catch (Throwable e) {
throw wrapAsCompDefEx(e);
}
} else if (matches.size() == 0) {
throw new ComponentDefinitionException("Unable to find a matching factory method " + factoryMethod + " on class " + factoryObj.getClass().getName() + " for arguments " + argsToString(args) + " when instanciating bean " + getName());
} else {
throw new ComponentDefinitionException("Multiple matching factory methods " + factoryMethod + " found on class " + factoryObj.getClass().getName() + " for arguments " + argsToString(args) + " when instanciating bean " + getName() + ": " + matches.keySet());
}
}
Aggregations