use of org.apache.felix.ipojo.parser.FieldMetadata in project felix by apache.
the class PropertiesHandler method configure.
/**
* This method is the first to be invoked.
* This method aims to configure the handler. It receives the component type metadata and the instance
* configuration. The method parses given metadata and registers fields to inject.
*
* Step 3 : when the instance configuration contains the properties.file property, it overrides the properties file location.
*
* @param metadata : component type metadata
* @param configuration : instance description
* @throws ConfigurationException : the configuration of the handler has failed.
*/
@SuppressWarnings("unchecked")
public void configure(Element metadata, Dictionary configuration) throws ConfigurationException {
// Parse metadata to get <properties file="$file"/>
// Get all elements to configure the handler
Element[] elem = metadata.getElements("properties", NAMESPACE);
switch(elem.length) {
case 0:
// It actually happen only if you force the handler to be plugged.
throw new ConfigurationException("No properties found");
case 1:
// One 'properties' found, get attributes.
m_file = elem[0].getAttribute("file");
if (m_file == null) {
// if file is null, throw a configuration error.
throw new ConfigurationException("Malformed properties element : file attribute must be set");
}
break;
default:
// To simplify we handle only one properties element.
throw new ConfigurationException("Only one properties element is supported");
}
// Look if the instance overrides file location :
String instanceFile = (String) configuration.get("properties.file");
if (instanceFile != null) {
m_file = instanceFile;
}
// Load properties
try {
loadProperties();
} catch (IOException e) {
throw new ConfigurationException("Error when reading the " + m_file + " file : " + e.getMessage());
}
// Register fields
// By convention, properties file entry are field name, so look for each property to get field list.
// First get Pojo Metadata metadata :
PojoMetadata pojoMeta = getPojoMetadata();
Enumeration e = m_properties.keys();
while (e.hasMoreElements()) {
String field = (String) e.nextElement();
FieldMetadata fm = pojoMeta.getField(field);
if (fm == null) {
// The field does not exist
throw new ConfigurationException("The field " + field + " is declared in the properties file but does not exist in the pojo");
}
// Then check that the field is a String field
if (!fm.getFieldType().equals(String.class.getName())) {
throw new ConfigurationException("The field " + field + " exists in the pojo, but is not a String");
}
// All checks are ok, register the interceptor.
getInstanceManager().register(fm, this);
}
// Finally register the field to listen
}
use of org.apache.felix.ipojo.parser.FieldMetadata in project felix by apache.
the class EventAdminPublisherHandler method initializeComponentFactory.
/**
* Initializes the component type.
*
* @param cd the component type description to populate
* @param metadata the component type metadata
* @throws ConfigurationException if the given metadata is incorrect.
* @see org.apache.felix.ipojo.Handler#initializeComponentFactory(
* org.apache.felix.ipojo.architecture.ComponentTypeDescription, org.apache.felix.ipojo.metadata.Element)
*/
public void initializeComponentFactory(ComponentTypeDescription cd, Element metadata) throws ConfigurationException {
// Update the current component description
Dictionary dict = new Properties();
PropertyDescription pd = new PropertyDescription(TOPICS_PROPERTY, Dictionary.class.getName(), dict.toString());
cd.addProperty(pd);
// Get Metadata publishers
Element[] publishers = metadata.getElements("publisher", NAMESPACE);
// if publisher is null, look for 'publishes' elements
if (publishers == null || publishers.length == 0) {
publishers = metadata.getElements("publishes", NAMESPACE);
}
if (publishers != null) {
// Maps used to check name and field are unique
Set nameSet = new HashSet();
Set fieldSet = new HashSet();
// Check all publishers are well formed
for (int i = 0; i < publishers.length; i++) {
// Check the publisher configuration is correct by creating an
// unused publisher metadata
EventAdminPublisherMetadata publisherMetadata = new EventAdminPublisherMetadata(publishers[i]);
String name = publisherMetadata.getName();
info(LOG_PREFIX + "Checking publisher " + name);
// Check field existence and type
String field = publisherMetadata.getField();
FieldMetadata fieldMetadata = getPojoMetadata().getField(publisherMetadata.getField(), Publisher.class.getName());
if (fieldMetadata == null) {
throw new ConfigurationException("Field not found in the component : " + Publisher.class.getName() + " " + field);
}
// Check name and field are unique
if (nameSet.contains(name)) {
throw new ConfigurationException("A publisher with the same name already exists : " + name);
} else if (fieldSet.contains(field)) {
throw new ConfigurationException("The field " + field + " is already associated to a publisher");
}
nameSet.add(name);
fieldSet.add(field);
}
} else {
info(LOG_PREFIX + "No publisher to check");
}
}
use of org.apache.felix.ipojo.parser.FieldMetadata in project felix by apache.
the class EventAdminPublisherHandler method configure.
/**
* Constructor.
*
* @param metadata the component type metadata
* @param conf the instance configuration
* @throws ConfigurationException if one event publication 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 conf) throws ConfigurationException {
// Store the component manager
m_manager = getInstanceManager();
// Get the topics instance configuration
Dictionary instanceTopics = (Dictionary) conf.get(TOPICS_PROPERTY);
// Get Metadata publishers
Element[] publishers = metadata.getElements("publisher", NAMESPACE);
// if publisher is null, look for 'publishes' elements
if (publishers == null || publishers.length == 0) {
publishers = metadata.getElements("publishes", NAMESPACE);
}
if (publishers != null) {
// map
for (int i = 0; i < publishers.length; i++) {
// Extract the publisher configuration
EventAdminPublisherMetadata publisherMetadata = new EventAdminPublisherMetadata(publishers[i]);
String name = publisherMetadata.getName();
info(LOG_PREFIX + "Configuring publisher " + name);
// Get the topic instance configuration if redefined
String topicsString = (instanceTopics != null) ? (String) instanceTopics.get(name) : null;
if (topicsString != null) {
publisherMetadata.setTopics(topicsString);
}
// Check the publisher is correctly configured
publisherMetadata.check();
// Create the associated Publisher and insert it in the
// publisher map
Publisher publisher = new PublisherImpl(this, publisherMetadata.getTopics(), publisherMetadata.isSynchronous(), publisherMetadata.getDataKey(), m_manager.getInstanceName());
m_publishersByField.put(publisherMetadata.getField(), publisher);
// Register the callback that return the publisher
// reference when the specified field is read by the
// POJO.
FieldMetadata fieldMetadata = getPojoMetadata().getField(publisherMetadata.getField(), Publisher.class.getName());
m_manager.register(fieldMetadata, this);
}
} else {
info(LOG_PREFIX + "No publisher to configure");
}
setValidity(true);
debug(LOG_PREFIX + "Setup description....");
// Initialize the description.
m_description = new EventAdminPublisherHandlerDescription(this, m_publishersByField.values());
}
use of org.apache.felix.ipojo.parser.FieldMetadata in project felix by apache.
the class DependencyConfigurationChecker method deduceAggregationFromTheInjectionPoints.
/**
* Determines if the dependency is aggregate from the field or constructor parameter used to inject the dependency.
* If the dependency just uses methods, this method does nothing. This method also check that dependencies set to
* aggregate have a valid injection type.
* @param dependency the dependency
* @param manipulation the manipulation metadata
* @throws ConfigurationException if the type of the field or constructor parameter used to inject the dependency
* is not suitable for aggregate dependencies.
*/
private static void deduceAggregationFromTheInjectionPoints(Dependency dependency, PojoMetadata manipulation) throws ConfigurationException {
if (dependency.getField() != null) {
FieldMetadata field = manipulation.getField(dependency.getField());
String type = field.getFieldType();
if (type.endsWith("[]")) {
dependency.setAggregateType(AggregateDependencyInjectionType.ARRAY);
} else if (Collection.class.getName().equals(type) || List.class.getName().equals(type)) {
dependency.setAggregateType(AggregateDependencyInjectionType.LIST);
} else if (Set.class.getName().equals(type)) {
dependency.setAggregateType(AggregateDependencyInjectionType.SET);
} else if (Vector.class.getName().equals(type)) {
dependency.setAggregateType(AggregateDependencyInjectionType.VECTOR);
} else if (dependency.isAggregate()) {
// Something wrong. The dependency has a field that is not suitable for aggregate dependencies
throw new ConfigurationException("The dependency " + DependencyHandler.getDependencyIdentifier(dependency) + " cannot be an aggregate dependency - reason: the type " + field.getFieldType() + " of the field " + field.getFieldName() + " is not suitable for aggregate " + "dependencies. Compatible types are array, vector, list, set and collection.");
}
}
if (dependency.getConstructorParameterIndex() != -1) {
String type = manipulation.getConstructors()[0].getMethodArguments()[dependency.getConstructorParameterIndex()];
if (type.endsWith("[]")) {
dependency.setAggregateType(AggregateDependencyInjectionType.ARRAY);
} else if (Collection.class.getName().equals(type) || List.class.getName().equals(type)) {
dependency.setAggregateType(AggregateDependencyInjectionType.LIST);
} else if (Set.class.getName().equals(type)) {
dependency.setAggregateType(AggregateDependencyInjectionType.SET);
} else if (Vector.class.getName().equals(type)) {
dependency.setAggregateType(AggregateDependencyInjectionType.VECTOR);
} else if (dependency.isAggregate()) {
// Something wrong. The dependency has a field that is not suitable for aggregate dependencies
throw new ConfigurationException("The dependency " + DependencyHandler.getDependencyIdentifier(dependency) + " cannot be an aggregate dependency - reason: the type " + type + " of the constructor parameter " + dependency.getConstructorParameterIndex() + " is not suitable for aggregate " + "dependencies. Compatible types are array, vector, list, set and collection.");
}
}
// TODO We may not cover some cases such as inconsistency between the constructor and the field. However this
// should be very rare.
}
use of org.apache.felix.ipojo.parser.FieldMetadata in project felix by apache.
the class ControllerHandler method initializeComponentFactory.
/**
* Initialize the component factory.
* The controller field is checked to avoid configure check.
* @param desc : component description
* @param metadata : component type metadata
* @throws ConfigurationException : occurs if the controller field is not in the POJO class or is not a boolean.
* @see org.apache.felix.ipojo.Handler#initializeComponentFactory(org.apache.felix.ipojo.architecture.ComponentTypeDescription, org.apache.felix.ipojo.metadata.Element)
*/
public void initializeComponentFactory(ComponentTypeDescription desc, Element metadata) throws ConfigurationException {
String field = null;
Element[] controller = metadata.getElements("controller");
// Use only the first controller
field = controller[0].getAttribute("field");
if (field == null) {
throw new ConfigurationException("Lifecycle controller : the controller element needs to contain a field attribute");
}
PojoMetadata method = getFactory().getPojoMetadata();
FieldMetadata fieldMetadata = method.getField(field);
if (fieldMetadata == null) {
throw new ConfigurationException("Lifecycle controller : The field " + field + " does not exist in the implementation class");
}
if (!fieldMetadata.getFieldType().equalsIgnoreCase("boolean")) {
throw new ConfigurationException("Lifecycle controller : The field " + field + " must be a boolean (" + fieldMetadata.getFieldType() + " found)");
}
}
Aggregations