Search in sources :

Example 1 with Nullable

use of org.apache.felix.ipojo.Nullable in project felix by apache.

the class TemporalDependency method start.

/**
 * Start method. Initializes the nullable object.
 * @see org.apache.felix.ipojo.util.DependencyModel#start()
 */
public void start() {
    super.start();
    switch(m_policy) {
        case TemporalHandler.NULL:
            m_nullableObject = null;
            break;
        case TemporalHandler.NULLABLE:
            // access to the service specification.
            try {
                m_nullableObject = Proxy.newProxyInstance(m_handler.getInstanceManager().getClazz().getClassLoader(), new Class[] { getSpecification(), Nullable.class }, // NOPMD
                new NullableObject());
                if (isAggregate()) {
                    if (m_collection) {
                        List list = new ArrayList(1);
                        list.add(m_nullableObject);
                        m_nullableObject = list;
                    } else {
                        Object[] array = (Object[]) Array.newInstance(getSpecification(), 1);
                        array[0] = m_nullableObject;
                        m_nullableObject = array;
                    }
                }
            } catch (NoClassDefFoundError e) {
                // It generally comes from a missing import.
                throw new IllegalStateException("Cannot create the Nullable object, a referenced class cannot be loaded: " + e.getMessage());
            }
            break;
        case TemporalHandler.DEFAULT_IMPLEMENTATION:
            // Create the default-implementation object.
            try {
                Class clazz = m_handler.getInstanceManager().getContext().getBundle().loadClass(m_di);
                m_nullableObject = clazz.newInstance();
            } catch (IllegalAccessException e) {
                throw new IllegalStateException("Cannot load the default-implementation " + m_di + " : " + e.getMessage());
            } catch (InstantiationException e) {
                throw new IllegalStateException("Cannot load the default-implementation " + m_di + " : " + e.getMessage());
            } catch (ClassNotFoundException e) {
                throw new IllegalStateException("Cannot load the default-implementation " + m_di + " : " + e.getMessage());
            }
            if (isAggregate()) {
                if (m_collection) {
                    List list = new ArrayList(1);
                    list.add(m_nullableObject);
                    m_nullableObject = list;
                } else {
                    Object[] array = (Object[]) Array.newInstance(getSpecification(), 1);
                    array[0] = m_nullableObject;
                    m_nullableObject = array;
                }
            }
            break;
        case TemporalHandler.EMPTY:
            if (!m_collection) {
                m_nullableObject = Array.newInstance(getSpecification(), 0);
            } else {
                // Empty collection
                m_nullableObject = new ArrayList(0);
            }
            break;
        default:
            // Cannot occurs
            break;
    }
}
Also used : NullableObject(org.apache.felix.ipojo.handlers.dependency.NullableObject) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NullableObject(org.apache.felix.ipojo.handlers.dependency.NullableObject) Nullable(org.apache.felix.ipojo.Nullable)

Example 2 with Nullable

use of org.apache.felix.ipojo.Nullable in project felix by apache.

the class NullableTest method testOnGet_whenNullableEnabled_returnsAnInstanceOfNullableAndSpecification.

public void testOnGet_whenNullableEnabled_returnsAnInstanceOfNullableAndSpecification() {
    Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
    BundleContext context = Mockito.mock(BundleContext.class);
    InstanceManager im = Mockito.mock(InstanceManager.class);
    Mockito.when(im.getClazz()).thenReturn(ComponentTestWithSuperClass.class);
    DependencyHandler handler = Mockito.mock(DependencyHandler.class);
    Mockito.when(handler.getInstanceManager()).thenReturn(im);
    Dependency dependency = new Dependency(handler, "a_field", TestSpecification.class, null, true, false, true, false, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    Object service = dependency.onGet(null, null, null);
    Assert.assertTrue(service instanceof Nullable);
    Assert.assertTrue(service instanceof TestSpecification);
}
Also used : MockBundle(org.apache.felix.ipojo.test.MockBundle) Bundle(org.osgi.framework.Bundle) InstanceManager(org.apache.felix.ipojo.InstanceManager) MockBundle(org.apache.felix.ipojo.test.MockBundle) Nullable(org.apache.felix.ipojo.Nullable) BundleContext(org.osgi.framework.BundleContext)

Example 3 with Nullable

use of org.apache.felix.ipojo.Nullable in project felix by apache.

the class FilterCheckSubscriber method getProps.

public Properties getProps() {
    Properties props = new Properties();
    props.put("Bind", new Integer(bound));
    props.put("Nullable", new Boolean(m_foo instanceof Nullable));
    return props;
}
Also used : Properties(java.util.Properties) Nullable(org.apache.felix.ipojo.Nullable)

Aggregations

Nullable (org.apache.felix.ipojo.Nullable)3 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1 InstanceManager (org.apache.felix.ipojo.InstanceManager)1 NullableObject (org.apache.felix.ipojo.handlers.dependency.NullableObject)1 MockBundle (org.apache.felix.ipojo.test.MockBundle)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1