Search in sources :

Example 6 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class ProvidedServiceDescription method getProperties.

/**
 * Gets the list of properties.
 * A copy of the actual property set is returned.
 * @return the properties.
 */
public Properties getProperties() {
    Properties props = new Properties();
    org.apache.felix.ipojo.util.Property[] ps = m_ps.getProperties();
    for (int i = 0; i < ps.length; i++) {
        if (ps[i].getValue() != Property.NO_VALUE) {
            props.put(ps[i].getName(), ps[i].getValue());
        }
    }
    return props;
}
Also used : Properties(java.util.Properties) Property(org.apache.felix.ipojo.util.Property)

Example 7 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class ProvidedServiceHandler method onSet.

/**
 * Setter Callback Method.
 * Check if the modified field is a property to update the value.
 * @param pojo : the pojo object on which the field is accessed
 * @param fieldName : field name
 * @param value : new value
 * @see org.apache.felix.ipojo.FieldInterceptor#onSet(Object, String, Object)
 */
public void onSet(Object pojo, String fieldName, Object value) {
    // Verify that the field name correspond to a dependency
    for (ProvidedService svc : m_providedServices) {
        boolean update = false;
        // Retrieve a copy of the properties.
        final Property[] properties = svc.getProperties();
        for (Property prop : properties) {
            if (fieldName.equals(prop.getField()) && !prop.getValue().equals(value)) {
                // it is the associated property
                prop.setValue(value);
                update = true;
            }
        }
        if (update) {
            svc.update();
        }
        ServiceController ctrl = svc.getController(fieldName);
        if (ctrl != null) {
            if (value instanceof Boolean) {
                ctrl.setValue((Boolean) value);
            } else {
                warn("Boolean value expected for the service controller " + fieldName);
            }
        }
    }
// Else do nothing
}
Also used : ServiceController(org.apache.felix.ipojo.handlers.providedservice.ProvidedService.ServiceController) Property(org.apache.felix.ipojo.util.Property)

Example 8 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class ProvidedServiceHandler method configure.

/**
 * Configure the handler.
 * @param componentMetadata : the component type metadata
 * @param configuration : the instance configuration
 * @throws ConfigurationException : the metadata are not correct.
 * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
 */
public void configure(Element componentMetadata, Dictionary configuration) throws ConfigurationException {
    m_providedServices.clear();
    // Create the dependency according to the component metadata
    Element[] providedServices = componentMetadata.getElements("Provides");
    for (Element providedService : providedServices) {
        // Set by the initialize component factory.
        String[] serviceSpecifications = ParseUtils.parseArrays(providedService.getAttribute("specifications"));
        // Get the factory policy
        int factory = ProvidedService.SINGLETON_STRATEGY;
        Class custom = null;
        String strategy = providedService.getAttribute("strategy");
        if (strategy == null) {
            strategy = providedService.getAttribute("factory");
        }
        if (strategy != null) {
            if ("singleton".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.SINGLETON_STRATEGY;
            } else if ("service".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.SERVICE_STRATEGY;
            } else if ("method".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.STATIC_STRATEGY;
            } else if ("instance".equalsIgnoreCase(strategy)) {
                factory = ProvidedService.INSTANCE_STRATEGY;
            } else {
                // Customized policy
                try {
                    custom = getInstanceManager().getContext().getBundle().loadClass(strategy);
                    if (!CreationStrategy.class.isAssignableFrom(custom)) {
                        throw new ConfigurationException("The custom creation policy class " + custom.getName() + " does not implement " + CreationStrategy.class.getName());
                    }
                } catch (ClassNotFoundException e) {
                    throw new ConfigurationException("The custom creation policy class " + strategy + " cannot be loaded ", e);
                }
            }
        }
        // Then create the provided service
        ProvidedService svc = new ProvidedService(this, serviceSpecifications, factory, custom, configuration);
        // Post-Registration callback
        String post = providedService.getAttribute("post-registration");
        if (post != null) {
            Callback cb = new Callback(post, new Class[] { ServiceReference.class }, false, getInstanceManager());
            svc.setPostRegistrationCallback(cb);
        }
        post = providedService.getAttribute("post-unregistration");
        if (post != null) {
            // TODO Can we really send the service reference here ?
            Callback cb = new Callback(post, new Class[] { ServiceReference.class }, false, getInstanceManager());
            svc.setPostUnregistrationCallback(cb);
        }
        Element[] props = providedService.getElements("Property");
        if (props != null) {
            // Property[] properties = new Property[props.length];
            Property[] properties = new Property[props.length];
            for (int j = 0; j < props.length; j++) {
                String name = props[j].getAttribute("name");
                String value = props[j].getAttribute("value");
                String type = props[j].getAttribute("type");
                String field = props[j].getAttribute("field");
                Property prop = new Property(name, field, null, value, type, getInstanceManager(), this);
                properties[j] = prop;
                // Check if the instance configuration has a value for this property
                Object object = configuration.get(prop.getName());
                if (object != null) {
                    prop.setValue(object);
                }
                if (field != null) {
                    getInstanceManager().register(new FieldMetadata(field, type), this);
                // Cannot register the property as the interception is necessary
                // to deal with registration update.
                }
            }
            // Attach to properties to the provided service
            svc.setProperties(properties);
        }
        Element[] controllers = providedService.getElements("Controller");
        if (controllers != null) {
            for (Element controller : controllers) {
                String field = controller.getAttribute("field");
                if (field == null) {
                    throw new ConfigurationException("The field attribute of a controller is mandatory");
                }
                String v = controller.getAttribute("value");
                boolean value = !(v != null && v.equalsIgnoreCase("false"));
                String s = controller.getAttribute("specification");
                if (s == null) {
                    s = "ALL";
                }
                svc.setController(field, value, s);
                getInstanceManager().register(new FieldMetadata(field, "boolean"), this);
            }
        }
        if (checkProvidedService(svc)) {
            m_providedServices.add(svc);
        } else {
            StringBuilder itfs = new StringBuilder();
            for (String serviceSpecification : serviceSpecifications) {
                itfs.append(' ');
                itfs.append(serviceSpecification);
            }
            throw new ConfigurationException("The provided service" + itfs + " is not valid");
        }
        // Initialize the description.
        m_description = new ProvidedServiceHandlerDescription(this, getProvidedServices());
    }
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element) Callback(org.apache.felix.ipojo.util.Callback) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Property(org.apache.felix.ipojo.util.Property)

Example 9 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class InstanceManager method handleBCInjections.

/**
 * BundleContext injection is not registered with the InstanceManager.
 * We're iterating through factory's all constructors and register first
 * BundleContext parameter as constructor injection. So rest of the code
 * don't have to do anything to handle BundleContext mixed with other
 * injections.
 *
 * @throws ConfigurationException
 */
private void handleBCInjections() throws ConfigurationException {
    MethodMetadata[] constructors = getFactory().getPojoMetadata().getConstructors();
    for (MethodMetadata constructor : constructors) {
        String[] ctorArguments = constructor.getMethodArguments();
        for (int index = 0; index < ctorArguments.length; index++) {
            if (ctorArguments[index].equals(BundleContext.class.getName()) && (m_constructorRegistration == null || !m_constructorRegistration.containsKey(index))) {
                // Check if its used with only other injections.
                boolean injectionsConsistent = true;
                for (int siblingIndex = 0; siblingIndex < ctorArguments.length; siblingIndex++) {
                    if (siblingIndex == index) {
                        continue;
                    }
                    String injectionType = ctorArguments[siblingIndex];
                    if (m_constructorRegistration != null && m_constructorRegistration.containsKey(siblingIndex)) {
                        ConstructorInjector siblingInjector = (ConstructorInjector) m_constructorRegistration.get(siblingIndex);
                        Class injectorClass = siblingInjector.getConstructorParameterType(siblingIndex);
                        if (injectorClass != null && !injectorClass.getName().equals(injectionType)) {
                            injectionsConsistent = false;
                            break;
                        }
                    } else {
                        injectionsConsistent = false;
                        break;
                    }
                }
                if (injectionsConsistent) {
                    Property contextInjection = new Property("__context", null, null, index, null, BundleContext.class.getName(), this, null);
                    contextInjection.setValue(getContext());
                    register(index, contextInjection);
                    // We register the first valid BC injection.
                    break;
                }
            }
        }
    }
}
Also used : MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata) Property(org.apache.felix.ipojo.util.Property) BundleContext(org.osgi.framework.BundleContext)

Example 10 with Property

use of org.apache.felix.ipojo.util.Property in project felix by apache.

the class ConfigurationHandler method configure.

/**
 * Configures the handler.
 * Access to field does not require synchronization as this method is executed
 * before any thread access to this object.
 *
 * @param metadata      the metadata of the component
 * @param configuration the instance configuration
 * @throws ConfigurationException one property metadata is not correct
 * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.metadata.Element, java.util.Dictionary)
 */
public void configure(Element metadata, Dictionary configuration) throws ConfigurationException {
    // Build the map
    Element[] confs = metadata.getElements("Properties", "");
    Element[] configurables = confs[0].getElements("Property");
    // Check if the component is dynamically configurable
    // Propagation enabled by default.
    m_mustPropagate = true;
    // We must create a copy as the Config Admin dictionary has some limitations
    m_toPropagate = new Hashtable<String, Object>();
    if (configuration != null) {
        Enumeration keys = configuration.keys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            // we don't propagate properties starting with .
            if (!excluded(key)) {
                m_toPropagate.put(key, configuration.get(key));
            }
        }
    }
    String propa = confs[0].getAttribute("propagation");
    if (propa != null && propa.equalsIgnoreCase("false")) {
        m_mustPropagate = false;
        m_toPropagate = null;
    }
    // Check if the component support ConfigurationADmin reconfiguration
    // Look inside the component type description
    m_managedServicePID = confs[0].getAttribute("pid");
    // Look inside the instance configuration.
    String instanceMSPID = (String) configuration.get(MANAGED_SERVICE_PID);
    if (instanceMSPID != null) {
        m_managedServicePID = instanceMSPID;
    }
    // updated method
    String upd = confs[0].getAttribute("updated");
    if (upd != null) {
        MethodMetadata method = getPojoMetadata().getMethod(upd);
        if (method == null) {
            throw new ConfigurationException("The updated method is not found in the class " + getInstanceManager().getClassName());
        } else if (method.getMethodArguments().length == 0) {
            m_updated = new Callback(upd, new Class[0], false, getInstanceManager());
        } else if (method.getMethodArguments().length == 1 && method.getMethodArguments()[0].equals(Dictionary.class.getName())) {
            m_updated = new Callback(upd, new Class[] { Dictionary.class }, false, getInstanceManager());
        } else {
            throw new ConfigurationException("The updated method is found in the class " + getInstanceManager().getClassName() + " must have either no argument or a Dictionary");
        }
    }
    for (int i = 0; configurables != null && i < configurables.length; i++) {
        String fieldName = configurables[i].getAttribute("field");
        String methodName = configurables[i].getAttribute("method");
        String paramIndex = configurables[i].getAttribute("constructor-parameter");
        int index = -1;
        // The initialize method has fixed the property name.
        String name = configurables[i].getAttribute("name");
        String value = configurables[i].getAttribute("value");
        // The initialize method has fixed the property name.
        String type = configurables[i].getAttribute("type");
        Property prop;
        if (paramIndex == null) {
            prop = new Property(name, fieldName, methodName, value, type, getInstanceManager(), this);
        } else {
            index = Integer.parseInt(paramIndex);
            prop = new Property(name, fieldName, methodName, index, value, type, getInstanceManager(), this);
        }
        addProperty(prop);
        // Check if the instance configuration contains value for the current property :
        if (configuration.get(name) == null) {
            if (fieldName != null && configuration.get(fieldName) != null) {
                prop.setValue(configuration.get(fieldName));
            }
        } else {
            prop.setValue(configuration.get(name));
        }
        if (fieldName != null) {
            FieldMetadata field = new FieldMetadata(fieldName, type);
            getInstanceManager().register(field, prop);
        }
        if (index != -1) {
            getInstanceManager().register(index, prop);
        }
    }
    m_description = new ConfigurationHandlerDescription(this, m_configurableProperties, m_managedServicePID);
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element) Callback(org.apache.felix.ipojo.util.Callback) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata) Property(org.apache.felix.ipojo.util.Property)

Aggregations

Property (org.apache.felix.ipojo.util.Property)14 Element (org.apache.felix.ipojo.metadata.Element)2 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)2 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)2 Callback (org.apache.felix.ipojo.util.Callback)2 Properties (java.util.Properties)1 ConfigurationException (org.apache.felix.ipojo.ConfigurationException)1 ServiceController (org.apache.felix.ipojo.handlers.providedservice.ProvidedService.ServiceController)1 BundleContext (org.osgi.framework.BundleContext)1