use of org.osgi.service.component.ComponentException in project felix by apache.
the class XmlHandlerTest method test_duplicate_implementation_class_10.
public void test_duplicate_implementation_class_10() throws Exception {
final List metadataList10 = readMetadata("/components_duplicate_implementation_10.xml");
assertEquals("Component Descriptors", 1, metadataList10.size());
final ComponentMetadata cm10 = (ComponentMetadata) metadataList10.get(0);
try {
cm10.validate(logger);
fail("Expect validation failure for duplicate implementation element");
} catch (ComponentException ce) {
// expected
}
}
use of org.osgi.service.component.ComponentException in project felix by apache.
the class XmlHandlerTest method test_component_no_name.
public void test_component_no_name() throws Exception {
final List metadataList10 = readMetadata("/components_anonymous_10.xml");
assertEquals("Component Descriptors", 1, metadataList10.size());
final ComponentMetadata cm10 = (ComponentMetadata) metadataList10.get(0);
try {
cm10.validate(logger);
fail("Expected validation failure for component without name");
} catch (ComponentException ce) {
// expected !!
}
final List metadataList11 = readMetadata("/components_anonymous_11.xml");
assertEquals("Component Descriptors", 1, metadataList11.size());
final ComponentMetadata cm11 = (ComponentMetadata) metadataList11.get(0);
cm11.validate(logger);
assertEquals("Expected name equals class", cm11.getImplementationClassName(), cm11.getName());
}
use of org.osgi.service.component.ComponentException in project felix by apache.
the class XmlHandlerTest method test_duplicate_service_11.
public void test_duplicate_service_11() throws Exception {
final List metadataList11 = readMetadata("/components_duplicate_service_11.xml");
assertEquals("Component Descriptors", 1, metadataList11.size());
final ComponentMetadata cm11 = (ComponentMetadata) metadataList11.get(0);
try {
cm11.validate(logger);
fail("Expect validation failure for duplicate service element");
} catch (ComponentException ce) {
// expected
}
}
use of org.osgi.service.component.ComponentException in project felix by apache.
the class ComponentRegistry method checkComponentName.
// ---------- ComponentHolder registration by component name
/**
* Checks whether the component name is "globally" unique or not. If it is
* unique, it is reserved until the actual component is registered with
* {@link #registerComponentHolder(String, ComponentHolder)} or until
* it is unreserved by calling {@link #unregisterComponentHolder(String)}.
* If a component with the same name has already been reserved or registered
* a ComponentException is thrown with a descriptive message.
*
* @param bundle the bundle registering the component
* @param name the component name to check and reserve
* @throws ComponentException if the name is already in use by another
* component.
*/
final ComponentRegistryKey checkComponentName(final Bundle bundle, final String name) {
// register the name if no registration for that name exists already
final ComponentRegistryKey key = new ComponentRegistryKey(bundle, name);
ComponentHolder<?> existingRegistration = null;
boolean present;
synchronized (m_componentHoldersByName) {
present = m_componentHoldersByName.containsKey(key);
if (!present) {
m_componentHoldersByName.put(key, null);
} else {
existingRegistration = m_componentHoldersByName.get(key);
}
}
// existing registration to provide more information if possible
if (present) {
String message = "The component name '" + name + "' has already been registered";
if (existingRegistration != null) {
Bundle cBundle = existingRegistration.getActivator().getBundleContext().getBundle();
ComponentMetadata cMeta = existingRegistration.getComponentMetadata();
StringBuffer buf = new StringBuffer(message);
buf.append(" by Bundle ").append(cBundle.getBundleId());
if (cBundle.getSymbolicName() != null) {
buf.append(" (").append(cBundle.getSymbolicName()).append(")");
}
buf.append(" as Component of Class ").append(cMeta.getImplementationClassName());
message = buf.toString();
}
throw new ComponentException(message);
}
return key;
}
use of org.osgi.service.component.ComponentException in project felix by apache.
the class Annotations method toObject.
public static <T> T toObject(Class<T> clazz, Map<String, Object> props, Bundle b, boolean supportsInterfaces) {
Map<String, Object> m = new HashMap<String, Object>();
Method[] methods = clazz.getMethods();
Map<String, Method> complexFields = new HashMap<String, Method>();
for (Method method : methods) {
String name = method.getName();
String key = fixup(name);
Object raw = props.get(key);
Class<?> returnType = method.getReturnType();
Object cooked;
if (returnType.isInterface() || returnType.isAnnotation()) {
complexFields.put(key, method);
continue;
}
try {
if (returnType.isArray()) {
Class<?> componentType = returnType.getComponentType();
if (componentType.isInterface() || componentType.isAnnotation()) {
complexFields.put(key, method);
continue;
}
cooked = coerceToArray(componentType, raw, b);
} else {
cooked = Coercions.coerce(returnType, raw, b);
}
} catch (ComponentException e) {
cooked = new Invalid(e);
}
m.put(name, cooked);
}
if (!complexFields.isEmpty()) {
if (supportsInterfaces) {
Map<String, List<Map<String, Object>>> nested = extractSubMaps(complexFields.keySet(), props);
for (Map.Entry<String, Method> entry : complexFields.entrySet()) {
List<Map<String, Object>> proplist = nested.get(entry.getKey());
if (proplist == null) {
proplist = Collections.emptyList();
}
Method method = entry.getValue();
Class<?> returnType = method.getReturnType();
if (returnType.isArray()) {
Class<?> componentType = returnType.getComponentType();
Object result = Array.newInstance(componentType, proplist.size());
for (int i = 0; i < proplist.size(); i++) {
Map<String, Object> rawElement = proplist.get(i);
Object cooked = toObject(componentType, rawElement, b, supportsInterfaces);
Array.set(result, i, cooked);
}
m.put(method.getName(), result);
} else {
if (!proplist.isEmpty()) {
Object cooked = toObject(returnType, proplist.get(0), b, supportsInterfaces);
m.put(method.getName(), cooked);
}
}
}
} else {
for (Method method : complexFields.values()) {
m.put(method.getName(), new Invalid("Invalid annotation member type" + method.getReturnType().getName() + " for member: " + method.getName()));
}
}
}
InvocationHandler h = new Handler(m);
return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[] { clazz }, h);
}
Aggregations