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.plugins.dbpage.service;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPathService;
38  
39  import org.springframework.context.ApplicationContext;
40  import org.springframework.context.support.FileSystemXmlApplicationContext;
41  
42  import java.io.File;
43  
44  import java.util.ArrayList;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  /**
50   * This class provides a way to use Spring Framework ligthweight containers
51   * offering IoC (Inversion of Control) features.
52   * @see http://www.springframework.org
53   */
54  public enum DbPageContextService
55  {
56      /**
57       * Implementation of a singleton instance
58       */
59      INSTANCE;
60      private static final String CORE = "core";
61      private static Map<String,ApplicationContext> _mapContext = new HashMap<String,ApplicationContext>(  );
62      private static final String PATH_CONF = "/WEB-INF/conf/";
63      private static final String DIR_PLUGINS = "plugins/";
64      private static final String SUFFIX_CONTEXT_FILE = "_context.xml";
65  
66      /**
67       * Return an instance, which may be shared or independent, of the given bean name.
68       * This method allows a Spring BeanFactory to be used as a replacement for
69       * the Singleton or Prototype design pattern.<br />
70       * The bean is retrieved from the main context defined in the WEB-INF/conf/core_context.xml.
71       * @param strName The bean's name
72       * @return The instance of the bean
73       */
74      public static Object getBean( String strName )
75      {
76          ApplicationContext context = getContext( CORE );
77  
78          return context.getBean( strName );
79      }
80  
81      /**
82       * Return an instance of the given bean name loaded by the a Spring BeanFactory.
83       * The bean is retreived from a plugin context defined in the WEB-INF/conf/plugins/[plugin_name]_context.xml.
84       * @param strPluginName The Plugin's name
85       * @param strName The bean's name
86       * @return The instance of the bean
87       */
88      public static Object getPluginBean( String strPluginName, String strName )
89      {
90          ApplicationContext context = getContext( strPluginName );
91  
92          return ( context != null ) ? context.getBean( strName ) : null;
93      }
94  
95      /**
96       * Gets a Spring Application context from a given name
97       * @param strContextName The context's name
98       * @return The context
99       */
100     private static ApplicationContext getContext( String strContextName )
101     {
102         // Try to get the context from the cache
103         ApplicationContext context = (ApplicationContext) _mapContext.get( strContextName );
104 
105         if ( context == null )
106         {
107             // If not found then load the context from the XML file
108             String strContextFilePath = AppPathService.getAbsolutePathFromRelativePath( PATH_CONF );
109 
110             if ( !strContextName.equals( CORE ) )
111             {
112                 strContextFilePath += DIR_PLUGINS;
113             }
114 
115             String strContextFile = strContextFilePath + strContextName + SUFFIX_CONTEXT_FILE;
116 
117             try
118             {
119                 if ( strContextName.equals( CORE ) )
120                 {
121                     context = new FileSystemXmlApplicationContext( "file:" + strContextFile );
122                 }
123                 else
124                 {
125                     List<String> listModules = new ArrayList<String>(  );
126                     File dir = new File( strContextFilePath );
127                     String[] arrayFiles = dir.list(  );
128 
129                     if ( arrayFiles != null )
130                     {
131                         for ( int i = 0; i < arrayFiles.length; i++ )
132                         {
133                             String strPath = arrayFiles[i];
134 
135                             if ( strPath.startsWith( strContextName ) && strPath.endsWith( "_context.xml" ) )
136                             {
137                                 String strFullPath = "file:" + strContextFilePath + strPath;
138                                 listModules.add( strFullPath );
139                             }
140                         }
141                     }
142 
143                     String[] arrayModules = new String[listModules.size(  )];
144                     listModules.toArray( arrayModules );
145                     context = new FileSystemXmlApplicationContext( arrayModules );
146                 }
147             }
148             catch ( Exception e )
149             {
150                 AppLogService.error( "Error retrieving context file : " + e.getMessage(  ), e );
151             }
152             finally
153             {
154                 _mapContext.put( strContextName, context );
155             }
156 
157             _mapContext.put( strContextName, context );
158         }
159 
160         return context;
161     }
162 
163     /**
164      * Fetched the subclasses of the section
165      * @param strContextName The context name
166      * @param classDef The class definition
167      * @return A map containing the beans
168      */
169    /* public static Map getListSubClasses( String strContextName, Class classDef )
170     {
171         ApplicationContext context = getContext( strContextName );
172 
173         return context.getBeansOfType( classDef );
174     }*/
175 }