Development Guide

Extension Helper

Extension Helper allows extensions to:

  • get information about current project,
  • get information state of PETEP core and obtain PetepHelper,
  • register modules,
  • register listeners,
  • register receivers or send data to other receivers,
  • work with other extensions / modules.

You can get instance of ExtensionHelper inside Extension.init() method.

It is recommended to register module factories in Extension.init() method only!

ExtensionHelper interface

com.warxim.petep.helper.ExtensionHelper
/**
 * Helper for extensions that allows extensions to use internal components, register module
 * factories and get information about the project.
 */
@PetepAPI
public interface ExtensionHelper {
    /*
     * PROJECT INFO
     */
    /**
     * Obtains project name.
     * @return Name of the opened project
     */
    String getProjectName();

    /**
     * Obtains project description.
     * @return Description of the opened project
     */
    String getProjectDescription();

    /*
     * APPLICATION INFO
     */
    /**
     * Obtains context type of current application instance (whether it is running in GUI or command line).
     * @return Context type (GUI / COMMAND_LINE)
     */
    ContextType getContextType();

    /**
     * CORE INFO
     */
    /**
     * Obtains PETEP core state.
     * @return PETEP core state
     */
    PetepState getPetepState();

    /**
     * Obtains PETEP helper for currently running core.
     * @return  PETEP helper if core is running;
     *          {@code Optional.empty()} otherwise
     */
    Optional<PetepHelper> getPetepHelper();

    /*
     * MODULES
     */
    /**
     * Registers proxy module factory for creating custom proxies.
     * @param factory Proxy module factory to be registered
     * @return {@code true} if the factory was successfully registered (code is available)
     */
    boolean registerProxyModuleFactory(ProxyModuleFactory factory);

    /**
     * Unregisters proxy module factory.
     * @param factory Proxy module factory to be unregistered
     * @return {@code true} if the factory was successfully unregistered (factory was registered before)
     */
    boolean unregisterProxyModuleFactory(ProxyModuleFactory factory);

    /**
     * Registers interceptor module factory for creating custom interceptors.
     * @param factory Interceptor module factory to be registered
     * @return {@code true} if the factory was successfully registered (code is available)
     */
    boolean registerInterceptorModuleFactory(InterceptorModuleFactory factory);

    /**
     * Unregisters interceptor module factory.
     * @param factory Interceptor module factory to be unregistered
     * @return {@code true} if the factory was successfully unregistered (factory was registered before)
     */
    boolean unregisterInterceptorModuleFactory(InterceptorModuleFactory factory);

    /*
     * LISTENERS
     */
    /**
     * Registers listener for PETEP core events (start, stop, ...).
     * <p>
     *     Listeners are persisted using weak reference, so the strong reference has to be saved somewhere.
     *     Otherwise the GC would get rid of it.
     * </p>
     * <p>
     *     Registered listeners are automatically freed after they are only weakly referenced,
     *     however, it is possible to unregister them using {@link ExtensionHelper#unregisterPetepListener}
     * </p>
     * @param listener Listener to be registered
     */
    void registerPetepListener(PetepListener listener);

    /**
     * Unregisters listener for PETEP core events.
     * @param listener Listener to be unregistered
     */
    void unregisterPetepListener(PetepListener listener);

    /*
     * EXTENSIONS
     */
    /**
     * Obtains list of loaded extensions.
     * @return List of extensions, which are loaded in the application
     */
    List<Extension> getExtensions();

    /**
     * Obtains extension by its code.
     * @param code Code of the extension to find
     * @return Extension of a given code;
     *         Empty optional if that extension is not loaded in the project
     */
    Optional<Extension> getExtension(String code);

    /*
     * RECEIVERS
     */
    /**
     * Registers receiver for receiving data from extensions.
     * <p>
     *     Receivers are persisted using weak reference, so the strong reference has to be saved somewhere.
     *     Otherwise the GC would get rid of it.
     * </p>
     * <p>
     *     Registered receivers are automatically freed after they are only weakly referenced,
     *     however, it is possible to unregister them using {@link ExtensionHelper#unregisterReceiver}
     * </p>
     * @param receiver Receiver to be registered
     * @return {@code true} if the receiver was successfully registered (code is available)
     */
    boolean registerReceiver(Receiver receiver);

    /**
     * Unregisters receiver.
     * @param receiver Receiver to be unregistered
     */
    void unregisterReceiver(Receiver receiver);

    /**
     * Sends data to receiver with specified code.
     * @param code Code of target receiver
     * @param data Data to be sent to the receiver
     */
    void sendToReceiver(String code, Object data);

    /**
     * Obtains list of all registered receivers.
     * @return List of all receivers
     */
    List<Receiver> getReceivers();

    /**
     * Obtains list of registered receivers that support given class.
     * @param clazz Class to check
     * @return List of receivers that support given class
     */
    List<Receiver> getReceivers(Class<?> clazz);

    /*
     * MODULES
     */
    /**
     * Obtains list of interceptor modules in direction C2S (Client -&gt; Server).
     * @return List of interceptor modules
     */
    List<InterceptorModule> getInterceptorModulesC2S();

    /**
     * Obtains list of interceptor modules in direction S2C (Client &lt;- Server).
     * @return List of interceptor modules
     */
    List<InterceptorModule> getInterceptorModulesS2C();

    /**
     * Obtains list of proxy modules.
     * @return List of proxy modules
     */
    List<ProxyModule> getProxyModules();

}