How to Add Server Side Hooks
Contents |
Overview
Hooks provide an easy mechanism to extend functionality from external module. It basically consists on a lightweight mechanism (build using WELD) that allows to inject dependencies from third party modules.
There are two roles for hooks:
- Base module that provides the standard functionality and defines some point in its flow that can be extended by external modules
- External modules that provide extra functionality to the base one. Logic for this extra functionality is executed in the point the base module defines for this purpose.
How to define a server side hook in the base module
Base module needs to define which are the points other modules can extend through hooks. This is done with the following code:
step 1:
Define the abstract class for the new hook:
package org.openbravo.retail.posterminal; public abstract class MyNewHook { public abstract void exec(Object param1, Object param2) throws Exception; }
step 2:
Define a function to call to every classes which are registered in the defined hooks.
private void executeHooks(Instance<? extends Object> hooks, Object param1, Object param2) throws Exception { for (Iterator<? extends Object> procIter = hooks.iterator(); procIter.hasNext();) { Object proc = procIter.next(); ((MyNewHook) proc).exec(param1, param2); } }
step 3:
Execute hooks in the desired point of your module.
@Inject @Any private Instance<MyNewHook> myNewHook; //your code executeHooks(myNewHook, myParam, null);
When this code is reached, all the hooks defined by external modules for this hookName
will be sequentially executed.
How to provide logic in external modules
When there is a base module that defines a hook as mentioned in the previous section, external modules can provide logic to be executed at that point creating a new class in following manner:
package org.openbravo.retail.posterminal; import javax.enterprise.context.ApplicationScoped; @ApplicationScoped public class MyNewHookExtension extends MyNewHook { @Override public void exec(Object param1, Object param2) throws Exception { // Business Logic } }
Server Side Hooks in Web POS
This document list of server side hooks available in Web POS.