ResourceService.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.service.resource;

  35. import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
  36. import fr.paris.lutece.portal.service.util.AppLogService;
  37. import fr.paris.lutece.portal.service.util.AppPropertiesService;

  38. import java.util.ArrayList;
  39. import java.util.Collection;
  40. import java.util.Iterator;
  41. import java.util.List;
  42. import java.util.StringTokenizer;

  43. /**
  44.  * This abstract Class provides a standard way for application to deliver resources using multiple loaders and an optional cache.
  45.  *
  46.  * @since v1.4.1
  47.  */
  48. public abstract class ResourceService extends AbstractCacheableService
  49. {
  50.     // Constants
  51.     private static final String DELIMITER = ",";
  52.     private static final String UNDEFINED_SERVICE_NAME = "Undefined Service Name";

  53.     // Variables
  54.     private String _strName = UNDEFINED_SERVICE_NAME;
  55.     private List<ResourceLoader> _listLoaders = new ArrayList<>( );

  56.     /**
  57.      *
  58.      */
  59.     protected ResourceService( )
  60.     {
  61.         String strLoadersProperty = getLoadersProperty( );

  62.         if ( ( strLoadersProperty != null ) && ( !strLoadersProperty.equals( "" ) ) )
  63.         {
  64.             initLoaders( strLoadersProperty );
  65.         }
  66.         else
  67.         {
  68.             AppLogService.error( "Resource service : Loaders property key is missing" );
  69.         }
  70.     }

  71.     // Methods to overide
  72.     /**
  73.      * Gets the name of the property that list all loaders
  74.      *
  75.      * @return The property key
  76.      */
  77.     protected abstract String getLoadersProperty( );

  78.     /**
  79.      * Initialize loaders
  80.      *
  81.      * @param strKey
  82.      *            The property key that contains all loaders class
  83.      */
  84.     protected void initLoaders( String strKey )
  85.     {
  86.         String strLoaders = AppPropertiesService.getProperty( strKey );
  87.         StringTokenizer st = new StringTokenizer( strLoaders, DELIMITER );

  88.         while ( st.hasMoreTokens( ) )
  89.         {
  90.             String strLoaderClassName = st.nextToken( );
  91.             addLoader( strLoaderClassName );
  92.         }
  93.     }

  94.     /**
  95.      * Set the service name
  96.      *
  97.      * @param strName
  98.      *            The service name
  99.      */
  100.     protected void setName( String strName )
  101.     {
  102.         _strName = strName;
  103.     }

  104.     /**
  105.      * Get the service name
  106.      *
  107.      * @return The service name
  108.      */
  109.     public String getName( )
  110.     {
  111.         return _strName;
  112.     }

  113.     /**
  114.      * Set the service name by reading a property
  115.      *
  116.      * @param strKey
  117.      *            The name key
  118.      */
  119.     protected void setNameKey( String strKey )
  120.     {
  121.         setName( AppPropertiesService.getProperty( strKey, UNDEFINED_SERVICE_NAME ) );
  122.     }

  123.     /**
  124.      * Defines whether the cache is enable or disable reading a property
  125.      *
  126.      * @param strKey
  127.      *            The key name of the cache
  128.      */
  129.     protected void setCacheKey( String strKey )
  130.     {
  131.         String strCache = AppPropertiesService.getProperty( strKey, "false" );

  132.         if ( strCache.equals( "true" ) )
  133.         {
  134.             initCache( getName( ) );
  135.         }
  136.     }

  137.     /**
  138.      * Add a new loader to the service
  139.      *
  140.      * @param strLoaderClassName
  141.      *            The loader class name
  142.      */
  143.     protected void addLoader( String strLoaderClassName )
  144.     {
  145.         try
  146.         {
  147.             ResourceLoader loader = (ResourceLoader) Class.forName( strLoaderClassName ).newInstance( );
  148.             _listLoaders.add( loader );
  149.         }
  150.         catch( IllegalAccessException | InstantiationException | ClassNotFoundException e )
  151.         {
  152.             AppLogService.error( e.getMessage( ), e );
  153.         }
  154.     }

  155.     /**
  156.      * Returns a resource by its Id
  157.      *
  158.      * @param strId
  159.      *            The resource Id
  160.      * @return A resource
  161.      */
  162.     protected Resource getResource( String strId )
  163.     {
  164.         Resource resource = null;

  165.         if ( isCacheEnable( ) )
  166.         {
  167.             resource = (Resource) getFromCache( strId );

  168.             if ( resource == null )
  169.             {
  170.                 resource = loadResource( strId );

  171.                 if ( resource != null )
  172.                 {
  173.                     putInCache( strId, resource );
  174.                 }
  175.             }
  176.         }
  177.         else
  178.         {
  179.             resource = loadResource( strId );
  180.         }

  181.         return resource;
  182.     }

  183.     /**
  184.      * Load a resource by its Id
  185.      *
  186.      * @param strId
  187.      *            The resource Id
  188.      * @return A resource
  189.      */
  190.     private Resource loadResource( String strId )
  191.     {
  192.         Resource resource = null;
  193.         Iterator<ResourceLoader> i = _listLoaders.iterator( );

  194.         while ( i.hasNext( ) && ( resource == null ) )
  195.         {
  196.             ResourceLoader loader = i.next( );
  197.             resource = loader.getResource( strId );
  198.         }

  199.         return resource;
  200.     }

  201.     /**
  202.      * Load all resources
  203.      *
  204.      * @return A collection of resources
  205.      */
  206.     protected Collection<Resource> getResources( )
  207.     {
  208.         List<Resource> listResources = new ArrayList<>( );
  209.         Iterator<ResourceLoader> i = _listLoaders.iterator( );

  210.         while ( i.hasNext( ) )
  211.         {
  212.             ResourceLoader loader = i.next( );
  213.             Collection<Resource> colResources = loader.getResources( );
  214.             Iterator<Resource> j = colResources.iterator( );

  215.             while ( j.hasNext( ) )
  216.             {
  217.                 Resource resource = j.next( );
  218.                 listResources.add( resource );
  219.             }
  220.         }

  221.         return listResources;
  222.     }
  223. }