Search in sources :

Example 1 with NullableObject

use of org.apache.felix.ipojo.handlers.dependency.NullableObject 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)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Nullable (org.apache.felix.ipojo.Nullable)1 NullableObject (org.apache.felix.ipojo.handlers.dependency.NullableObject)1