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 fr.paris.lutece.portal.service.spring.SpringContextService;
37  import fr.paris.lutece.portal.service.util.AppLogService;
38  import fr.paris.lutece.portal.service.util.AppPathService;
39  
40  import org.springframework.context.ApplicationContext;
41  import org.springframework.context.support.FileSystemXmlApplicationContext;
42  
43  import java.io.File;
44  
45  import java.util.ArrayList;
46  import java.util.HashMap;
47  import java.util.List;
48  import java.util.Map;
49  
50  /**
51   * Service fetching the declared section
52   */
53  @SuppressWarnings( "unchecked" )
54  public enum ExportFormatService
55  {
56      INSTANCE;
57  
58      private static final String CORE = "core";
59      private static Map<String, ApplicationContext> _mapContext = new HashMap<String, ApplicationContext>( );
60      private static final String PATH_CONF = "/WEB-INF/conf/";
61      private static final String DIR_PLUGINS = "plugins/";
62      private static final String SUFFIX_CONTEXT_FILE = "_context.xml";
63  
64      /**
65       * Gets a Spring Application context from a given name
66       * 
67       * @param strContextName
68       *            The context's name
69       * @return The context
70       */
71      private static ApplicationContext getContext( String strContextName )
72      {
73          // Try to get the context from the cache
74          ApplicationContext context = (ApplicationContext) _mapContext.get( strContextName );
75  
76          if ( context == null )
77          {
78              // If not found then load the context from the XML file
79              String strContextFilePath = AppPathService.getAbsolutePathFromRelativePath( PATH_CONF );
80  
81              if ( !strContextName.equals( CORE ) )
82              {
83                  strContextFilePath += DIR_PLUGINS;
84              }
85  
86              String strContextFile = strContextFilePath + strContextName + SUFFIX_CONTEXT_FILE;
87  
88              try
89              {
90                  if ( strContextName.equals( CORE ) )
91                  {
92                      context = new FileSystemXmlApplicationContext( "file:" + strContextFile );
93                  }
94                  else
95                  {
96                      List<String> listModules = new ArrayList<String>( );
97                      File dir = new File( strContextFilePath );
98                      String [ ] arrayFiles = dir.list( );
99  
100                     if ( arrayFiles != null )
101                     {
102                         for ( int i = 0; i < arrayFiles.length; i++ )
103                         {
104                             String strPath = arrayFiles [i];
105 
106                             if ( strPath.startsWith( strContextName ) && strPath.endsWith( "_context.xml" ) )
107                             {
108                                 String strFullPath = "file:" + strContextFilePath + strPath;
109                                 listModules.add( strFullPath );
110                             }
111                         }
112                     }
113 
114                     String [ ] arrayModules = new String [ listModules.size( )];
115                     listModules.toArray( arrayModules );
116                     context = new FileSystemXmlApplicationContext( arrayModules );
117                 }
118             }
119             catch( Exception e )
120             {
121                 AppLogService.error( "Error retrieving context file : " + e.getMessage( ), e );
122             }
123             finally
124             {
125                 _mapContext.put( strContextName, context );
126             }
127 
128             _mapContext.put( strContextName, context );
129         }
130 
131         return context;
132     }
133 
134     public List<ILinkJasperReport> getExportTypes( )
135     {
136         return SpringContextService.getBeansOfType( ILinkJasperReport.class );
137 
138     }
139 }