Nov 9, 2021 3:52:58 PM Thomas Dumont avatar

Create a ResourceService

Resource load services for applications

Lutece offers a basic implementation for in-house services that allow applications to load their resources. For example, the calendar plugin manages calendars and has an AgendaService service that allows you to load a resource, in this case an agenda, from its identifier.

The objectives of the basic implementation called ResourceService are to provide:

  • an abstraction layer compared to the resource loading mode. These can be loaded from files, a database or other. Each loading device corresponds to an implementation of the ResourceLoader interface. It is possible to combine several loading devices.
  • an integrated cache system to optimize access to resources. The cache for each service is manageable from the Lutece Cache Management feature.
  • service configuration facilities (name, cache activation, ...) based on the application's properties file.

Here is the class diagram of the API:

API ResourceService Class Diagram

Implementing a ResourceService

Here is the minimal implementation of a service in the form of a singleton.

public class MyResourceService extends ResourceService
{
    private static MyResourceService _singleton = new MyResourceService
    private static final String PROPERTY_NAME = "myplugin.service.name";
    private static final String PROPERTY_CACHE = "myplugin.service.cache";
    private static final String PROPERTY_LOADERS = "myplugin.service.loaders";

    / **
     * Private constructor
     * /
    private MyResourceService
    {
        Great();
        setCacheKey (PROPERTY_CACHE);
        setNameKey (PROPERTY_NAME);
    }

    / **
     * Returns the instance of the singleton
     *
     * @return The instance of the singleton
     * /
    public static MyResourceService getInstance ()
    {
        return _singleton;
    }

    / **
     * Returns the property key that contains the loaders list
     * @return A property key
     * /
    protected String getLoadersProperty ()
    {
        return PROPERTY_LOADERS;
    }

    / **
     * Returns to the corresponding resource
     *
     * @param strResourceName The resource name
     * @return the corresponding resource
     * /
    public MyResource getMyResource (String strResourceName)
    {
        return (MyResource) getResource (strResourceName);
    }
}

The configuration of the service is done in the properties file. The basic values ​​to configure are:

Configuration element Example in the properties file
Name of the service myplugin.service.name = MyPlugin Plugin - MyResources Loading Service
Cache activation myplugin.service.cache = true
List of loaders myplugin.service.loaders = com.mycompany.myapp.service.MyResourceLoader

Implementing a ResourceLoader

The minimum implementation of a ResourceLoader is as follows:

import en.paris.lutece.portal.service.resource.Resource;
import en.paris.lutece.portal.service.resource.ResourceLoader;

import java.util.ArrayList;

/ **
 * MyResourceLoader
 * /
public class MyResourceLoader implements ResourceLoader
{

    / **
     * Implementation of the ResourceLoader interface
     * @return A collection of resources
     * /
    public Collection getResources ()
    {
        // Build a collection of resources available by this loader
        ArrayList listResources = new ArrayList ();
        ...
        return listResources
    }

    / **
     * Implementation of the ResourceLoader interface
     * @param strId The resource Id
     * @return The Resource
     * /
    public Resource getResource (String strId)
    {
        ...
    }
}