Search in sources :

Example 6 with ComponentException

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
    }
}
Also used : ComponentException(org.osgi.service.component.ComponentException) List(java.util.List)

Example 7 with ComponentException

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());
}
Also used : ComponentException(org.osgi.service.component.ComponentException) List(java.util.List)

Example 8 with ComponentException

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
    }
}
Also used : ComponentException(org.osgi.service.component.ComponentException) List(java.util.List)

Example 9 with ComponentException

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;
}
Also used : Bundle(org.osgi.framework.Bundle) ComponentException(org.osgi.service.component.ComponentException) ComponentMetadata(org.apache.felix.scr.impl.metadata.ComponentMetadata)

Example 10 with ComponentException

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);
}
Also used : HashMap(java.util.HashMap) InvocationHandler(java.lang.reflect.InvocationHandler) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) ComponentException(org.osgi.service.component.ComponentException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ComponentException (org.osgi.service.component.ComponentException)16 List (java.util.List)7 IOException (java.io.IOException)4 InputStream (java.io.InputStream)3 Properties (java.util.Properties)3 ArrayList (java.util.ArrayList)2 CommonEventCatalogUIAdapter (org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter)2 ComponentInstance (org.osgi.service.component.ComponentInstance)2 Listener (com.google.common.util.concurrent.Service.Listener)1 State (com.google.common.util.concurrent.Service.State)1 ShutdownException (io.fabric8.common.util.ShutdownTracker.ShutdownException)1 VertxService (io.fabric8.gateway.fabric.support.vertx.VertxService)1 DetectingGateway (io.fabric8.gateway.handlers.detecting.DetectingGateway)1 Protocol (io.fabric8.gateway.handlers.detecting.Protocol)1 AmqpProtocol (io.fabric8.gateway.handlers.detecting.protocol.amqp.AmqpProtocol)1 HttpProtocol (io.fabric8.gateway.handlers.detecting.protocol.http.HttpProtocol)1 MqttProtocol (io.fabric8.gateway.handlers.detecting.protocol.mqtt.MqttProtocol)1 OpenwireProtocol (io.fabric8.gateway.handlers.detecting.protocol.openwire.OpenwireProtocol)1 SslConfig (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslConfig)1 SslProtocol (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslProtocol)1