use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class EventAdminSubscriberHandler method initializeComponentFactory.
/**
* Initializes the component type.
*
* @param cd component type description to populate.
* @param metadata component type metadata.
* @throws ConfigurationException if the metadata are 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();
cd.addProperty(new PropertyDescription(TOPICS_PROPERTY, Dictionary.class.getName(), dict.toString()));
dict = new Properties();
cd.addProperty(new PropertyDescription(FILTER_PROPERTY, Dictionary.class.getName(), dict.toString()));
// Get Metadata subscribers
Element[] subscribers = metadata.getElements("subscriber", NAMESPACE);
if (subscribers != null) {
// Maps used to check name and field are unique
Set nameSet = new HashSet();
Set callbackSet = new HashSet();
// Check all subscribers are well formed
for (int i = 0; i < subscribers.length; i++) {
// Check the subscriber configuration is correct by creating an
// unused subscriber metadata
EventAdminSubscriberMetadata subscriberMetadata = new EventAdminSubscriberMetadata(getFactory().getBundleContext(), subscribers[i]);
String name = subscriberMetadata.getName();
info(LOG_PREFIX + "Checking subscriber " + name);
// Determine the event callback prototype
PojoMetadata pojoMetadata = getPojoMetadata();
String callbackType;
if (subscriberMetadata.getDataKey() == null) {
callbackType = Event.class.getName();
} else {
callbackType = subscriberMetadata.getDataType().getName();
}
// Check the event callback method is present
MethodMetadata methodMetadata = pojoMetadata.getMethod(subscriberMetadata.getCallback(), new String[] { callbackType });
String callbackSignature = subscriberMetadata.getCallback() + "(" + callbackType + ")";
if (methodMetadata == null) {
throw new ConfigurationException("Cannot find callback method " + callbackSignature);
}
// Warn if the same callback is used by several subscribers
if (callbackSet.contains(callbackSignature)) {
warn("The callback method is already used by another subscriber : " + callbackSignature);
} else {
callbackSet.add(callbackSignature);
}
// Check name is unique
if (nameSet.contains(name)) {
throw new ConfigurationException("A subscriber with the same name already exists : " + name);
}
nameSet.add(name);
}
m_description = new EventAdminSubscriberHandlerDescription(this, subscribers);
} else {
info(LOG_PREFIX + "No subscriber to check");
}
}
use of org.apache.felix.ipojo.ConfigurationException 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.ConfigurationException 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.ConfigurationException in project felix by apache.
the class Property method createArrayObject.
/**
* Creates an array object containing the type component type from
* the String array 'values'.
* @param interntype the internal type of the array.
* @param values the String array
* @return the array containing objects created from the 'values' array
* @throws ConfigurationException if the array cannot be created correctly
*/
public static Object createArrayObject(Class interntype, String[] values) throws ConfigurationException {
if (Boolean.TYPE.equals(interntype)) {
boolean[] bool = new boolean[values.length];
for (int i = 0; i < values.length; i++) {
bool[i] = Boolean.valueOf(values[i]).booleanValue();
}
return bool;
}
if (Byte.TYPE.equals(interntype)) {
byte[] byt = new byte[values.length];
for (int i = 0; i < values.length; i++) {
byt[i] = new Byte(values[i]).byteValue();
}
return byt;
}
if (Short.TYPE.equals(interntype)) {
short[] shor = new short[values.length];
for (int i = 0; i < values.length; i++) {
shor[i] = new Short(values[i]).shortValue();
}
return shor;
}
if (Integer.TYPE.equals(interntype)) {
int[] ints = new int[values.length];
for (int i = 0; i < values.length; i++) {
ints[i] = new Integer(values[i]).intValue();
}
return ints;
}
if (Long.TYPE.equals(interntype)) {
long[] longs = new long[values.length];
for (int i = 0; i < values.length; i++) {
longs[i] = new Long(values[i]).longValue();
}
return longs;
}
if (Float.TYPE.equals(interntype)) {
float[] floats = new float[values.length];
for (int i = 0; i < values.length; i++) {
floats[i] = new Float(values[i]).floatValue();
}
return floats;
}
if (Double.TYPE.equals(interntype)) {
double[] doubles = new double[values.length];
for (int i = 0; i < values.length; i++) {
doubles[i] = new Double(values[i]).doubleValue();
}
return doubles;
}
if (Character.TYPE.equals(interntype)) {
char[] chars = new char[values.length];
for (int i = 0; i < values.length; i++) {
chars[i] = values[i].toCharArray()[0];
}
return chars;
}
// object by calling a constructor with a string in argument.
try {
Constructor cst = interntype.getConstructor(new Class[] { String.class });
Object[] object = (Object[]) Array.newInstance(interntype, values.length);
for (int i = 0; i < values.length; i++) {
object[i] = cst.newInstance(new Object[] { values[i].trim() });
}
return object;
} catch (NoSuchMethodException e) {
throw new ConfigurationException("Constructor not found exception in setValue on " + interntype.getName(), e);
} catch (IllegalArgumentException e) {
throw new ConfigurationException("Argument issue when calling the constructor of the type " + interntype.getName(), e);
} catch (InstantiationException e) {
throw new ConfigurationException("Instantiation problem " + interntype.getName(), e);
} catch (IllegalAccessException e) {
throw new ConfigurationException("Illegal Access Exception in " + interntype.getName(), e);
} catch (InvocationTargetException e) {
throw new ConfigurationException("Invocation problem " + interntype.getName(), e.getTargetException());
}
}
use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.
the class PojoMetadataTest method testGetMetadata.
public void testGetMetadata() {
Element elem = null;
try {
elem = ManifestMetadataParser.parseHeaderMetadata(header);
} catch (ParseException e) {
fail("Parse Exception when parsing iPOJO-Component");
}
assertNotNull("Check elem not null", elem);
Element manip = getMetadataForComponent(elem, "ManipulationMetadata-FooProviderType-1");
assertNotNull("Check manipulation metadata not null for " + "Manipulation-FooProviderType-1", manip);
PojoMetadata mm;
try {
mm = new PojoMetadata(manip);
assertNotNull("Check mm not null", mm);
} catch (ConfigurationException e) {
fail("The creation of pojo metadata has failed");
}
}
Aggregations