OSGI HengSin/DS

From ADempiere
Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.

Why Services?

  • Firstly Eclipse has its own Extensions, then why this? Isn't DS more of an OSGI borrowed thingy? Well you are not the first one to think so. Read this to know more. Good news is that as of Eclipse 3.5 onwards, DS is supported with good configuration screens under the Plugin Perspective.

How is it set up?

  • Configuration.xml under OSGI-INF folder
  • Requires org.eclipse.equinox.event and org.eclipse.osgi.services plugins
  • Coding in user class
  • EventManager bundle

Configuration.xml

  • In the Base bundle, under the OSGI-INF folder is an xml, in HengSin's case it is eventmanager.xml
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" 
                              name="org.adempiere.base">
   <implementation class="org.adempiere.base.event.EventManager"/>
   <reference bind="bindEventAdmin" cardinality="1..1"  
           interface="org.osgi.service.event.EventAdmin" name="EventAdmin" policy="static" 
           unbind="unbindEventAdmin"/>
   <service>
      <provide interface="org.adempiere.base.event.IEventManager"/>
   </service>
</scr:component>

User Class

  • In ModelValidationEngine class under the fireDocValidate method:
//now process osgi event handlers
Event event = EventManager.newEvent(ModelValidator.documentEventTopics[docTiming],
	new EventProperty(EventManager.EVENT_DATA, po), new EventProperty("tableName", po.get_TableName()));
EventManager.getInstance().sendEvent(event);
@SuppressWarnings("unchecked")
List<String> errors = (List<String>) event.getProperty(IEventManager.EVENT_ERROR_MESSAGES);
	if (errors != null && !errors.isEmpty())
			return errors.get(0);

EventManager Reference

  • The EventManager package in Base bundle requires org.eclipse.equinox.event and org.eclipse.osgi.services plugins.
  • In EventManager package, there are:
    • EventManager class to bind/unbind, register/unregister all Events from available services
    • IEventTopics interface to define all the Model Events
    • Factory classes to handle group of services such as:
      • ImportEvent object reference for importing of external files
      • FactsEventData object reference to handle Accounting Facts

Resolution

  • Before the User Class can call the EventManager, there must be instantiation done by the eventmanager.xml to bindEventAdmin(). Otherwise getInstance() will return null and thus causing the issue we had earlier.
  • The OSGI execution does not detect the absence of required plugins. After ensuring the missing Event bundle is present, the eventmanager.xml execution of bindEventAdmin occurs.

How is it used?

  • For example, let's say we want to define new ModelValidator events
  • We can create a new bundle to register its ModelValidation events (See event.test plugin in project space).
  • The event.xml calls MyEventHandler class to do that.