9
9
*/
10
10
11
11
import java .util .HashMap ;
12
+ import java .util .HashSet ;
12
13
import java .util .Map ;
14
+ import java .util .Set ;
13
15
import java .util .concurrent .ConcurrentHashMap ;
14
16
15
17
import nl .isaac .comp .configuration .CustomConfiguration ;
16
18
import nl .isaac .comp .configuration .types .EnvironmentType ;
17
19
import nl .isaac .dotcms .plugin .configuration .dependencies .org .apache .commons .configuration .ConfigurationException ;
18
20
import nl .isaac .dotcms .plugin .configuration .dependencies .org .apache .commons .configuration .DefaultConfigurationBuilder .ConfigurationProvider ;
21
+ import nl .isaac .dotcms .plugin .configuration .dependencies .org .apache .commons .configuration .FileConfiguration ;
19
22
import nl .isaac .dotcms .plugin .configuration .dependencies .org .apache .commons .lang .text .StrLookup ;
20
23
import nl .isaac .dotcms .plugin .configuration .dotcms .DotCMSFileConfigurationProvider ;
21
24
import nl .isaac .dotcms .plugin .configuration .exception .ConfigurationNotFoundException ;
25
+ import nl .isaac .dotcms .plugin .configuration .listener .RequestStoringListener ;
22
26
import nl .isaac .dotcms .plugin .configuration .util .EmptyConfiguration ;
23
27
24
- import com .dotmarketing .business .APILocator ;
25
- import com .dotmarketing .exception .DotDataException ;
26
28
import com .dotmarketing .util .Logger ;
27
29
28
30
/**
@@ -53,6 +55,7 @@ public class ConfigurationService {
53
55
private static final String locationConfigPlugins = "${sys:catalina.home}/conf/applications/locationConfigPlugins.xml" ;
54
56
55
57
private static final ConcurrentHashMap <String , CustomConfiguration > serverCache = new ConcurrentHashMap <String , CustomConfiguration >();
58
+ private static final Set <String > ipForwardCache = new HashSet <String >();
56
59
57
60
/**
58
61
* The type of environment
@@ -104,7 +107,6 @@ public static CustomConfiguration tryGetHostConfiguration() {
104
107
* @throws ConfigurationException
105
108
*/
106
109
public static CustomConfiguration getPluginConfiguration (String pluginName ) throws ConfigurationException , ConfigurationNotFoundException {
107
- validatePluginName (pluginName );
108
110
return retrieveFromCacheOrCreateConfiguration ("plugin_" + pluginName + '_' , pluginName , locationConfigPlugins );
109
111
}
110
112
/**
@@ -161,7 +163,6 @@ public static CustomConfiguration getHostConfiguration(String hostName, String i
161
163
* @throws ConfigurationException
162
164
*/
163
165
public static CustomConfiguration getPluginConfiguration (String pluginName , String hostName , String ipAddress , String sessionId ) throws ConfigurationException {
164
- validatePluginName (pluginName );
165
166
return retrieveFromCacheOrCreateConfiguration ("plugin_" + pluginName + '_' , hostName , ipAddress , sessionId , pluginName , locationConfigPlugins );
166
167
}
167
168
/**
@@ -182,15 +183,23 @@ private static CustomConfiguration retrieveFromCacheOrCreateConfiguration(String
182
183
ConfigurationDotCMSCacheGroupHandler .setLoaded ();
183
184
}
184
185
185
- String key = keyPrefix + hostName ;
186
+ String key ;
187
+ final String basicKey = keyPrefix + hostName ;
186
188
187
189
// On Windows the : character is not allowed, replace it with _
188
190
if (ipAddress != null ) {
189
191
ipAddress = ipAddress .replace (':' , '_' );
190
192
}
191
193
192
194
if (cacheOnIp ) {
193
- key += '_' + ipAddress + '_' + sessionId ;
195
+ String ipKey = basicKey + '_' + ipAddress + '_' + sessionId ;
196
+ if (!ipForwardCache .contains (ipKey )) {
197
+ key = ipKey ;
198
+ } else {
199
+ key = basicKey ;
200
+ }
201
+ } else {
202
+ key = basicKey ;
194
203
}
195
204
196
205
Logger .debug (ConfigurationService .class , "Looking up configuration under key: " + key );
@@ -206,11 +215,20 @@ private static CustomConfiguration retrieveFromCacheOrCreateConfiguration(String
206
215
207
216
interpolationValues .put ("hostName" , hostName );
208
217
if (pluginName != null ) {
209
- interpolationValues .put ("pluginName" , pluginName );
218
+ if (pluginName .startsWith ("osgi/" )) {
219
+ //This is an OSGi plugin
220
+ String [] splitPluginName = pluginName .split ("/" );
221
+ interpolationValues .put ("pluginName" , splitPluginName [1 ]);
222
+ if (splitPluginName .length > 2 ) {
223
+ interpolationValues .put ("osgiJarName" , splitPluginName [2 ]);
224
+ }
225
+ } else {
226
+ interpolationValues .put ("pluginName" , pluginName );
227
+ }
210
228
}
211
229
212
230
// We only do ip specific things on DEV (and local of course!)
213
- if (cacheOnIp ) {
231
+ if (cacheOnIp && ipAddress != null ) {
214
232
interpolationValues .put ("ClientIPAddress" , ipAddress );
215
233
}
216
234
@@ -222,27 +240,39 @@ private static CustomConfiguration retrieveFromCacheOrCreateConfiguration(String
222
240
providers .put ("dotcms" , dotCmsProvider );
223
241
224
242
conf = ConfigurationFactory .createConfiguration (key , configurationLocation , interpolators , providers );
225
- ConfigurationParameters params = defaultParameters .get ();
226
- if (params != null ) {
227
- params .addConfigurationToSession (key );
243
+
244
+ boolean isIpSpecific = false ;
245
+ if (cacheOnIp && ipAddress != null ) {
246
+ for (FileConfiguration fileConf : conf .getLoadedFileConfigurations ()) {
247
+ if (fileConf .getFileName () != null && fileConf .getFileName ().contains (ipAddress )) {
248
+
249
+ isIpSpecific = true ;
250
+
251
+ return conf ;
252
+ }
253
+ }
254
+ }
255
+ if (isIpSpecific ) {
256
+ ConfigurationParameters params = defaultParameters .get ();
257
+ if (params != null ) {
258
+ params .addConfigurationToSession (key );
259
+ }
260
+ }
261
+ if (cacheOnIp && !isIpSpecific ) {
262
+ ipForwardCache .add (key );
263
+ if (serverCache .contains (basicKey )) {
264
+ return serverCache .get (basicKey );
265
+ } else {
266
+ key = basicKey ;
267
+ }
228
268
}
229
269
Logger .debug (ConfigurationService .class , "Created a new configuration! " + key );
270
+
230
271
serverCache .put (key , conf );
231
272
232
273
return conf ;
233
274
}
234
275
235
- private static void validatePluginName (String pluginName ) {
236
- // First check if this plugin exists!
237
- try {
238
- if (null == APILocator .getPluginAPI ().loadPlugin (pluginName )) {
239
- throw new DotDataException ("DotCMS does not know a plugin by the name: '" + pluginName + "'" );
240
- }
241
- } catch (DotDataException e ) {
242
- throw new IllegalArgumentException ("DotCMS does not know a plugin by the name: '" + pluginName + "'" , e );
243
- }
244
-
245
- }
246
276
/**
247
277
* This is, effectively, a tuple, being able to return the required parameters needed to construct a key.
248
278
* It also contains a hook to whatever system is outside of this class to register a configuration object (or at least it's key) so that the outside system can clean up when it's no-longer needed.
@@ -272,6 +302,12 @@ public static final void storeDefaultParameters(ConfigurationParameters params)
272
302
public static final void clearDefaultParameters () {
273
303
defaultParameters .remove ();
274
304
}
305
+ /**
306
+ * Method to be used by {@link RequestStoringListener} to stack the parameters in nested requests
307
+ */
308
+ public static final ConfigurationParameters getDefaultParameters () {
309
+ return defaultParameters .get ();
310
+ }
275
311
/**
276
312
* This will invalidate a specifically cached configuration.
277
313
* @param key
@@ -285,5 +321,15 @@ public static final void removeConfigurationForKey(String key) {
285
321
*/
286
322
public static final void clearCache () {
287
323
serverCache .clear ();
324
+ ipForwardCache .clear ();
325
+ }
326
+
327
+ /**
328
+ * This determines the number of loaded configuration variants.
329
+ *
330
+ * @return
331
+ */
332
+ public static int getNumberOfLoadedConfigurations () {
333
+ return serverCache .size ();
288
334
}
289
335
}
0 commit comments