View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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  
36  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
37  import fr.paris.lutece.portal.service.util.AppLogService;
38  import fr.paris.lutece.portal.service.util.AppPropertiesService;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  import java.util.Iterator;
43  import java.util.List;
44  import java.util.StringTokenizer;
45  
46  
47  /**
48   * This abstract Class provides a standard way for application to deliver resources
49   * using multiple loaders and an optional cache.
50   * @since v1.4.1
51   */
52  public abstract class ResourceService extends AbstractCacheableService
53  {
54      // Constants
55      private static final String DELIMITER = ",";
56      private static final String UNDEFINED_SERVICE_NAME = "Undefined Service Name";
57  
58      // Variables
59      private String _strName = UNDEFINED_SERVICE_NAME;
60      private List<ResourceLoader> _listLoaders = new ArrayList<ResourceLoader>(  );
61  
62      /**
63       *
64       */
65      protected ResourceService(  )
66      {
67          String strLoadersProperty = getLoadersProperty(  );
68  
69          if ( ( strLoadersProperty != null ) && ( !strLoadersProperty.equals( "" ) ) )
70          {
71              initLoaders( strLoadersProperty );
72  
73              //            initCache( getName() );
74          }
75          else
76          {
77              AppLogService.error( "Resource service : Loaders property key is missing" );
78          }
79      }
80  
81      // Methods to overide
82      /**
83       * Gets the name of the property that list all loaders
84       * @return The property key
85       */
86      protected abstract String getLoadersProperty(  );
87  
88      /**
89       * Initialize loaders
90       * @param strKey The property key that contains all loaders class
91       */
92      protected void initLoaders( String strKey )
93      {
94          String strLoaders = AppPropertiesService.getProperty( strKey );
95          StringTokenizer st = new StringTokenizer( strLoaders, DELIMITER );
96  
97          while ( st.hasMoreTokens(  ) )
98          {
99              String strLoaderClassName = st.nextToken(  );
100             addLoader( strLoaderClassName );
101         }
102     }
103 
104     /**
105      * Set the service name
106      * @param strName The service name
107      */
108     protected void setName( String strName )
109     {
110         _strName = strName;
111     }
112 
113     /**
114      * Get the service name
115      * @return The service name
116      */
117     public String getName(  )
118     {
119         return _strName;
120     }
121 
122     /**
123      * Set the service name by reading a property
124      * @param strKey The name key
125      */
126     protected void setNameKey( String strKey )
127     {
128         setName( AppPropertiesService.getProperty( strKey, UNDEFINED_SERVICE_NAME ) );
129     }
130 
131     /**
132      * Defines whether the cache is enable or disable reading a property
133      * @param strKey The key name of the cache
134      */
135     protected void setCacheKey( String strKey )
136     {
137         String strCache = AppPropertiesService.getProperty( strKey, "false" );
138 
139         if ( strCache.equals( "true" ) )
140         {
141             initCache( getName(  ) );
142         }
143     }
144 
145     /**
146      * Add a new loader to the service
147      * @param strLoaderClassName The loader class name
148      */
149     protected void addLoader( String strLoaderClassName )
150     {
151         try
152         {
153             ResourceLoader loader = (ResourceLoader) Class.forName( strLoaderClassName ).newInstance(  );
154             _listLoaders.add( loader );
155         }
156         catch ( IllegalAccessException e )
157         {
158             AppLogService.error( e.getMessage(  ), e );
159         }
160         catch ( InstantiationException e )
161         {
162             AppLogService.error( e.getMessage(  ), e );
163         }
164         catch ( ClassNotFoundException e )
165         {
166             AppLogService.error( e.getMessage(  ), e );
167         }
168     }
169 
170     /**
171      * Returns a resource by its Id
172      * @param strId The resource Id
173      * @return A resource
174      */
175     protected Resource getResource( String strId )
176     {
177         Resource resource = null;
178 
179         if ( isCacheEnable(  ) )
180         {
181             resource = (Resource) getFromCache( strId );
182 
183             if ( resource == null )
184             {
185                 resource = loadResource( strId );
186 
187                 if ( resource != null )
188                 {
189                     putInCache( strId, resource );
190                 }
191             }
192         }
193         else
194         {
195             resource = loadResource( strId );
196         }
197 
198         return resource;
199     }
200 
201     /**
202      * Load a resource by its Id
203      * @param strId The resource Id
204      * @return A resource
205      */
206     private Resource loadResource( String strId )
207     {
208         Resource resource = null;
209         Iterator<ResourceLoader> i = _listLoaders.iterator(  );
210 
211         while ( i.hasNext(  ) && ( resource == null ) )
212         {
213             ResourceLoader loader = (ResourceLoader) i.next(  );
214             resource = loader.getResource( strId );
215         }
216 
217         return resource;
218     }
219 
220     /**
221      * Load all resources
222      * @return A collection of resources
223      */
224     protected Collection<Resource> getResources(  )
225     {
226         List<Resource> listResources = new ArrayList<Resource>(  );
227         Iterator<ResourceLoader> i = _listLoaders.iterator(  );
228 
229         while ( i.hasNext(  ) )
230         {
231             ResourceLoader loader = (ResourceLoader) i.next(  );
232             Collection<Resource> colResources = loader.getResources(  );
233             Iterator<Resource> j = colResources.iterator(  );
234 
235             while ( j.hasNext(  ) )
236             {
237                 Resource resource = (Resource) j.next(  );
238                 listResources.add( resource );
239             }
240         }
241 
242         return listResources;
243     }
244 }