use of org.apache.felix.ipojo.util.Callback in project felix by apache.
the class EventAdminSubscriberHandler method handleEvent.
/**
*************************************************************************
* OSGi EventHandler callback
*************************************************************************
*/
/**
* Receives an event. The event is dispatch to attached subscribers.
*
* @param event the received event.
* @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event)
*/
public void handleEvent(final Event event) {
EventAdminSubscriberMetadata subscriberMetadata = null;
// Retrieve the event's topic
String topic = event.getTopic();
// For each subscribers
Collection subscribers = m_subscribersByName.values();
for (Iterator i = subscribers.iterator(); i.hasNext(); ) {
subscriberMetadata = (EventAdminSubscriberMetadata) i.next();
// Stack confinement
boolean isListening = false;
Object callbackParam = null;
Callback callback = null;
synchronized (this) {
isListening = m_isListening;
}
// Check if the subscriber's topic and filter match
Filter filter = subscriberMetadata.getFilter();
if (EventUtil.matches(topic, subscriberMetadata.getTopics()) && (filter == null || event.matches(filter))) {
String name = subscriberMetadata.getName();
callback = (Callback) m_callbacksByName.get(name);
try {
// Depending on the subscriber type...
callbackParam = getCallbackParameter(event, subscriberMetadata);
} catch (ClassCastException e) {
// Ignore the data event if type doesn't match
warn(LOG_PREFIX + "Ignoring data event : Bad data type", e);
} catch (NoSuchFieldException e) {
// Ignore events without data field for data events
// subscriber
warn(LOG_PREFIX + "Ignoring data event : No data", e);
}
}
// NullPointerExceptions)
if (isListening && callback != null && callbackParam != null) {
try {
callback.call(new Object[] { callbackParam });
} catch (InvocationTargetException e) {
error(LOG_PREFIX + "The callback has thrown an exception", e.getTargetException());
} catch (Exception e) {
error(LOG_PREFIX + "Unexpected exception when calling callback", e);
}
}
}
}
use of org.apache.felix.ipojo.util.Callback in project felix by apache.
the class EventAdminSubscriberHandler method configure.
/**
* Constructor.
*
* @param metadata the omponent type metadata
* @param conf the instance configuration
* @throws ConfigurationException if one event subscription 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 and filter instance configuration
Dictionary instanceTopics = (Dictionary) conf.get(TOPICS_PROPERTY);
Dictionary instanceFilter = (Dictionary) conf.get(FILTER_PROPERTY);
// Get Metadata subscribers
Element[] subscribers = metadata.getElements("subscriber", NAMESPACE);
// The topics to listen
Set topics = new TreeSet();
if (subscribers != null) {
// Configure all subscribers
for (int i = 0; i < subscribers.length; i++) {
// Extract the subscriber configuration
EventAdminSubscriberMetadata subscriberMetadata = new EventAdminSubscriberMetadata(m_manager.getContext(), subscribers[i]);
String name = subscriberMetadata.getName();
info(LOG_PREFIX + "Configuring subscriber " + name);
// Get the topics instance configuration if redefined
String topicsString = (instanceTopics != null) ? (String) instanceTopics.get(name) : null;
if (topicsString != null) {
subscriberMetadata.setTopics(topicsString);
}
// Get the filter instance configuration if redefined
String filterString = (instanceFilter != null) ? (String) instanceFilter.get(name) : null;
if (filterString != null) {
subscriberMetadata.setFilter(filterString);
}
// Check the publisher is correctly configured
subscriberMetadata.check();
// Add this subscriber's topics to the global list
String[] subscriberTopics = subscriberMetadata.getTopics();
for (int j = 0; j < subscriberTopics.length; j++) {
topics.add(subscriberTopics[j]);
}
// Determine the event callback prototype
PojoMetadata pojoMetadata = getPojoMetadata();
String callbackType;
if (subscriberMetadata.getDataKey() == null) {
callbackType = Event.class.getName();
} else {
callbackType = subscriberMetadata.getDataType().getName();
}
// Create the specified callback and register it
MethodMetadata methodMetadata = pojoMetadata.getMethod(subscriberMetadata.getCallback(), new String[] { callbackType });
Callback callback = new Callback(methodMetadata, m_manager);
m_callbacksByName.put(name, callback);
// Add the subscriber list gloal map
m_subscribersByName.put(name, subscriberMetadata);
}
// Construct the global topic list
m_topics = new String[topics.size()];
int i = 0;
for (Iterator iterator = topics.iterator(); iterator.hasNext(); ) {
String tmp = (String) iterator.next();
m_topics[i++] = tmp;
}
m_description = new EventAdminSubscriberHandlerDescription(this, subscribers);
} else {
info(LOG_PREFIX + "No subscriber to configure");
}
}
use of org.apache.felix.ipojo.util.Callback in project felix by apache.
the class DynamicMBeanImpl method invoke.
/**
* Invokes the required method on the targeted POJO.
*
* @param operationName the name of the method called
* @param params the parameters given to the method
* @param signature the determine which method called
* @return the object return by the method
* @throws MBeanException if something bad occures
* @throws ReflectionException if something bad occures
*/
public Object invoke(String operationName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
MethodField method = m_configMap.getMethodFromName(operationName, signature);
if (method != null) {
MethodMetadata methodCall = method.getMethod();
Callback mc = new Callback(methodCall, m_instanceManager);
try {
return mc.call(params);
} catch (NoSuchMethodException e) {
throw new ReflectionException(e);
} catch (IllegalAccessException e) {
throw new ReflectionException(e);
} catch (InvocationTargetException e) {
throw new MBeanException(e);
}
} else {
throw new ReflectionException(new NoSuchMethodException(operationName), "Cannot find the operation " + operationName + " in " + m_className);
}
}
use of org.apache.felix.ipojo.util.Callback 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());
}
}
use of org.apache.felix.ipojo.util.Callback in project felix by apache.
the class TransactionHandler method configure.
public void configure(Element arg0, Dictionary arg1) throws ConfigurationException {
Element[] elements = arg0.getElements(NAME, NAMESPACE);
if (elements.length > 1) {
throw new ConfigurationException("The handler " + NAMESPACE + ":" + NAME + " cannot be declared several times");
}
String field = elements[0].getAttribute(FIELD_ATTRIBUTE);
if (field != null) {
FieldMetadata meta = getPojoMetadata().getField(field);
if (meta == null) {
throw new ConfigurationException("The transaction field does not exist in the pojo class : " + field);
}
if (!meta.getFieldType().equals(Transaction.class.getName())) {
throw new ConfigurationException("The transaction field type must be " + Transaction.class.getName());
}
// Register the interceptor
getInstanceManager().register(meta, this);
}
String oncommit = elements[0].getAttribute(ONCOMMIT_ATTRIBUTE);
if (oncommit != null) {
m_onCommit = new Callback(oncommit, new String[] { Transaction.class.getName() }, false, getInstanceManager());
}
String onrollback = elements[0].getAttribute(ONROLLBACK_ATTRIBUTE);
if (onrollback != null) {
m_onRollback = new Callback(onrollback, new String[] { Transaction.class.getName() }, false, getInstanceManager());
}
Element[] sub = elements[0].getElements(TRANSACTIONAL_ELEMENT);
if (sub == null || sub.length == 0) {
throw new ConfigurationException("The handler " + NAMESPACE + ":" + NAME + " must have " + TRANSACTIONAL_ELEMENT + " subelement");
}
for (int i = 0; i < sub.length; i++) {
String method = sub[i].getAttribute(METHOD_ATTRIBUTE);
String to = sub[i].getAttribute(TIMEOUT_ATTRIBUTE);
String propa = sub[i].getAttribute(PROPAGATION_ATTRIBUTE);
String nrbf = sub[i].getAttribute(NOROLLBACKFOR_ATTRIBUTE);
String eorb = sub[i].getAttribute(EXCEPTIONONROLLBACK_ATTRIBUTE);
if (method == null) {
throw new ConfigurationException("A transactional element must specified the method attribute");
}
MethodMetadata meta = this.getPojoMetadata().getMethod(method);
if (meta == null) {
throw new ConfigurationException("A transactional method is not in the pojo class : " + method);
}
int timeout = 0;
if (to != null) {
timeout = new Integer(to).intValue();
}
int propagation = DEFAULT_PROPAGATION;
if (propa != null) {
propagation = parsePropagation(propa);
}
List<String> exceptions = new ArrayList<String>();
if (nrbf != null) {
exceptions = (List<String>) ParseUtils.parseArraysAsList(nrbf);
}
boolean exceptionOnRollback = false;
if (eorb != null) {
exceptionOnRollback = new Boolean(eorb).booleanValue();
}
TransactionalMethod tm = new TransactionalMethod(method, propagation, timeout, exceptions, exceptionOnRollback, this);
m_methods.add(tm);
this.getInstanceManager().register(meta, tm);
}
}
Aggregations