View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.plugins.jasper.service;
35  
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileNotFoundException;
39  
40  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
41  import net.sf.jasperreports.engine.JRException;
42  import net.sf.jasperreports.engine.JasperCompileManager;
43  import net.sf.jasperreports.engine.JasperReport;
44  
45  /**
46   * The Class CompiledJasperTemplateCacheService.
47   */
48  public class CompiledJasperTemplateCacheService extends AbstractCacheableService
49  {
50  
51      /** The Constant SERVICE_NAME. */
52      private static final String SERVICE_NAME = "CompiledJasperTemplateCacheService";
53  
54      /**
55       * Instantiates a new compiled jasper template cache service.
56       */
57      public CompiledJasperTemplateCacheService( )
58      {
59          initCache( );
60      }
61  
62      /**
63       * Gets the name.
64       *
65       * @return the name
66       */
67      public String getName( )
68      {
69          return SERVICE_NAME;
70      }
71  
72      /** The compiled jasper template cache service. */
73      private static CompiledJasperTemplateCacheService _compiledJasperTemplateCacheService;
74  
75      /**
76       * Gets the single instance of CompiledJasperTemplateCacheService.
77       *
78       * @return single instance of CompiledJasperTemplateCacheService
79       */
80      public static CompiledJasperTemplateCacheService getInstance( )
81      {
82          if ( _compiledJasperTemplateCacheService == null )
83          {
84              _compiledJasperTemplateCacheService = new CompiledJasperTemplateCacheService( );
85          }
86          return _compiledJasperTemplateCacheService;
87      }
88  
89      /**
90       * Gets the compiled jasper template.
91       *
92       * @param code
93       *            the code
94       * @param reportFile
95       *            the report file
96       * @return the compiled jasper template
97       * @throws FileNotFoundException
98       *             the file not found exception
99       * @throws JRException
100      *             the JR exception
101      */
102     public JasperReport getCompiledJasperTemplate( String code, File reportFile ) throws FileNotFoundException, JRException
103     {
104         String cacheKey = getCacheKey( code );
105         JasperReport r = ( JasperReport ) getFromCache( cacheKey );
106         if ( r == null )
107         {
108             r = JasperCompileManager.compileReport( new FileInputStream( reportFile ) );
109             putInCache( cacheKey, r );
110         }
111         return r;
112     }
113 
114     /**
115      * Gets the cache key.
116      *
117      * @param type
118      *            the type
119      * @return the cache key
120      */
121     private String getCacheKey( String type )
122     {
123         StringBuilder sbKey = new StringBuilder( );
124         sbKey.append( "[code:" ).append( type ).append( "]" );
125         return sbKey.toString( );
126     }
127 
128 }