1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
51
52
53
54 public enum DbPageContextService
55 {
56
57
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
68
69
70
71
72
73
74 public static Object getBean( String strName )
75 {
76 ApplicationContext context = getContext( CORE );
77
78 return context.getBean( strName );
79 }
80
81
82
83
84
85
86
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
97
98
99
100 private static ApplicationContext getContext( String strContextName )
101 {
102
103 ApplicationContext context = (ApplicationContext) _mapContext.get( strContextName );
104
105 if ( context == null )
106 {
107
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
165
166
167
168
169
170
171
172
173
174
175 }