Skip to content

Commit

Permalink
Updated the code for dotCMS 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
yasindilekci committed Sep 27, 2016
1 parent 36ea4b3 commit 42e4e06
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Configuration/conf/Language-ext.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
javax.portlet.title.EXT_CONFIGURATION=Configuration Cache
com.dotcms.repackage.javax.portlet.title.EXT_CONFIGURATION=Configuration Cache
18 changes: 4 additions & 14 deletions Configuration/conf/web-ext.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
<filter>
<filter-name>ConfigurationRequestStoringFilter</filter-name>
<filter-class>nl.isaac.dotcms.plugin.configuration.filter.RequestStoringFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>ConfigurationRequestStoringFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>

<listener>
<listener-class>nl.isaac.dotcms.plugin.configuration.filter.SessionListener</listener-class>
<listener-class>nl.isaac.dotcms.plugin.configuration.listener.RequestStoringListener</listener-class>
</listener>
<listener>
<listener-class>nl.isaac.dotcms.plugin.configuration.listener.SessionListener</listener-class>
</listener>

<servlet>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
<xml fileName="${sys:catalina.home}/conf/applications/${param:pluginName}/pluginConfig.xml" optional="true" />
<properties fileName="${sys:catalina.home}/conf/applications/${param:pluginName}/plugin.properties" optional="true"/>

<!-- Indien een plugin gebruik makt van de configuratie plugin dan MOET een van onderstaande bestanden bestaan -->
<!-- Indien een plugin gebruik maakt van de configuratie plugin dan MOET een van onderstaande bestanden bestaan -->
<xml fileName="${sys:catalina.home}/../plugins/${param:pluginName}/conf/pluginConfig.xml" optional="true"/>
<properties fileName="${sys:catalina.home}/../plugins/${param:pluginName}/conf/plugin.properties" optional="true"/>

<!-- Default osgi plugin.properties location when putting the plugin.properties in the resources/ext folder -->
<properties fileName="${sys:catalina.home}/webapps/ROOT/WEB-INF/velocity/osgi/${param:osgiJarName}/ext/plugin.properties" optional="true"/>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import nl.isaac.dotcms.plugin.configuration.dependencies.org.apache.commons.configuration.tree.ConfigurationNode;
import nl.isaac.dotcms.plugin.configuration.dependencies.org.apache.commons.configuration.tree.ConfigurationNodeVisitor;



public class ToStringMultiLineVisitor implements ConfigurationNodeVisitor {

private Stack<String> parentStack = new Stack<String>();
Expand All @@ -22,16 +20,16 @@ public void visitBeforeChildren(ConfigurationNode node) {
parentStack.add(node.getName());

if (node.getChildrenCount() == 0) {
buf.append("\n");
if (buf.length() > 0) {
buf.append("\n");
}

for (int i = 0; i < parentStack.size() - 1; i++) {
buf.append(parentStack.get(i)).append(".");
}
}

buf.append(parentStack.peek());
buf.append(parentStack.peek());

if (node.getChildrenCount() == 0) {
buf.append(" = ").append(node.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@
*/

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import nl.isaac.comp.configuration.CustomConfiguration;
import nl.isaac.comp.configuration.types.EnvironmentType;
import nl.isaac.dotcms.plugin.configuration.dependencies.org.apache.commons.configuration.ConfigurationException;
import nl.isaac.dotcms.plugin.configuration.dependencies.org.apache.commons.configuration.DefaultConfigurationBuilder.ConfigurationProvider;
import nl.isaac.dotcms.plugin.configuration.dependencies.org.apache.commons.configuration.FileConfiguration;
import nl.isaac.dotcms.plugin.configuration.dependencies.org.apache.commons.lang.text.StrLookup;
import nl.isaac.dotcms.plugin.configuration.dotcms.DotCMSFileConfigurationProvider;
import nl.isaac.dotcms.plugin.configuration.exception.ConfigurationNotFoundException;
import nl.isaac.dotcms.plugin.configuration.listener.RequestStoringListener;
import nl.isaac.dotcms.plugin.configuration.util.EmptyConfiguration;

import com.dotmarketing.business.APILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.util.Logger;

/**
Expand Down Expand Up @@ -53,6 +55,7 @@ public class ConfigurationService {
private static final String locationConfigPlugins = "${sys:catalina.home}/conf/applications/locationConfigPlugins.xml";

private static final ConcurrentHashMap<String, CustomConfiguration> serverCache = new ConcurrentHashMap<String, CustomConfiguration>();
private static final Set<String> ipForwardCache = new HashSet<String>();

/**
* The type of environment
Expand Down Expand Up @@ -104,7 +107,6 @@ public static CustomConfiguration tryGetHostConfiguration() {
* @throws ConfigurationException
*/
public static CustomConfiguration getPluginConfiguration(String pluginName) throws ConfigurationException, ConfigurationNotFoundException {
validatePluginName(pluginName);
return retrieveFromCacheOrCreateConfiguration("plugin_" + pluginName + '_', pluginName, locationConfigPlugins);
}
/**
Expand Down Expand Up @@ -161,7 +163,6 @@ public static CustomConfiguration getHostConfiguration(String hostName, String i
* @throws ConfigurationException
*/
public static CustomConfiguration getPluginConfiguration(String pluginName, String hostName, String ipAddress, String sessionId) throws ConfigurationException {
validatePluginName(pluginName);
return retrieveFromCacheOrCreateConfiguration("plugin_" + pluginName + '_', hostName, ipAddress, sessionId, pluginName, locationConfigPlugins);
}
/**
Expand All @@ -182,15 +183,23 @@ private static CustomConfiguration retrieveFromCacheOrCreateConfiguration(String
ConfigurationDotCMSCacheGroupHandler.setLoaded();
}

String key = keyPrefix + hostName;
String key;
final String basicKey = keyPrefix + hostName;

// On Windows the : character is not allowed, replace it with _
if (ipAddress != null) {
ipAddress = ipAddress.replace(':', '_');
}

if (cacheOnIp) {
key += '_' + ipAddress + '_' + sessionId;
String ipKey = basicKey + '_' + ipAddress + '_' + sessionId;
if (!ipForwardCache.contains(ipKey)) {
key = ipKey;
} else {
key = basicKey;
}
} else {
key = basicKey;
}

Logger.debug(ConfigurationService.class, "Looking up configuration under key: " + key);
Expand All @@ -206,11 +215,20 @@ private static CustomConfiguration retrieveFromCacheOrCreateConfiguration(String

interpolationValues.put("hostName", hostName);
if (pluginName != null) {
interpolationValues.put("pluginName", pluginName);
if(pluginName.startsWith("osgi/")) {
//This is an OSGi plugin
String[] splitPluginName = pluginName.split("/");
interpolationValues.put("pluginName", splitPluginName[1]);
if(splitPluginName.length > 2) {
interpolationValues.put("osgiJarName", splitPluginName[2]);
}
} else {
interpolationValues.put("pluginName", pluginName);
}
}

// We only do ip specific things on DEV (and local of course!)
if (cacheOnIp) {
if (cacheOnIp && ipAddress != null) {
interpolationValues.put("ClientIPAddress", ipAddress);
}

Expand All @@ -222,27 +240,39 @@ private static CustomConfiguration retrieveFromCacheOrCreateConfiguration(String
providers.put("dotcms", dotCmsProvider);

conf = ConfigurationFactory.createConfiguration(key, configurationLocation, interpolators, providers);
ConfigurationParameters params = defaultParameters.get();
if (params != null) {
params.addConfigurationToSession(key);

boolean isIpSpecific = false;
if (cacheOnIp && ipAddress != null) {
for (FileConfiguration fileConf : conf.getLoadedFileConfigurations()) {
if (fileConf.getFileName() != null && fileConf.getFileName().contains(ipAddress)) {

isIpSpecific = true;

return conf;
}
}
}
if (isIpSpecific) {
ConfigurationParameters params = defaultParameters.get();
if (params != null) {
params.addConfigurationToSession(key);
}
}
if (cacheOnIp && !isIpSpecific) {
ipForwardCache.add(key);
if (serverCache.contains(basicKey)) {
return serverCache.get(basicKey);
} else {
key = basicKey;
}
}
Logger.debug(ConfigurationService.class, "Created a new configuration! " + key);

serverCache.put(key, conf);

return conf;
}

private static void validatePluginName(String pluginName) {
// First check if this plugin exists!
try {
if (null == APILocator.getPluginAPI().loadPlugin(pluginName)) {
throw new DotDataException("DotCMS does not know a plugin by the name: '" + pluginName + "'");
}
} catch (DotDataException e) {
throw new IllegalArgumentException("DotCMS does not know a plugin by the name: '" + pluginName + "'", e);
}

}
/**
* This is, effectively, a tuple, being able to return the required parameters needed to construct a key.
* 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.
Expand Down Expand Up @@ -272,6 +302,12 @@ public static final void storeDefaultParameters(ConfigurationParameters params)
public static final void clearDefaultParameters() {
defaultParameters.remove();
}
/**
* Method to be used by {@link RequestStoringListener} to stack the parameters in nested requests
*/
public static final ConfigurationParameters getDefaultParameters() {
return defaultParameters.get();
}
/**
* This will invalidate a specifically cached configuration.
* @param key
Expand All @@ -285,5 +321,15 @@ public static final void removeConfigurationForKey(String key) {
*/
public static final void clearCache() {
serverCache.clear();
ipForwardCache.clear();
}

/**
* This determines the number of loaded configuration variants.
*
* @return
*/
public static int getNumberOfLoadedConfigurations() {
return serverCache.size();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nl.isaac.dotcms.plugin.configuration.filter;
package nl.isaac.dotcms.plugin.configuration.listener;
/**
* dotCMS Configuration plugin by ISAAC - The Full Service Internet Agency is licensed
* under a Creative Commons Attribution 3.0 Unported License
Expand All @@ -13,6 +13,7 @@
import javax.servlet.http.HttpServletRequest;

import nl.isaac.dotcms.plugin.configuration.ConfigurationService;
import nl.isaac.dotcms.plugin.configuration.ConfigurationService.ConfigurationParameters;

import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.exception.DotDataException;
Expand All @@ -28,8 +29,13 @@
public class RequestBasedConfigurationParameters implements ConfigurationService.ConfigurationParameters {

private final HttpServletRequest request;
public RequestBasedConfigurationParameters(HttpServletRequest request) {
private final ConfigurationParameters previous;
public RequestBasedConfigurationParameters(HttpServletRequest request, ConfigurationService.ConfigurationParameters previous) {
this.request = request;
this.previous = previous;
}
public ConfigurationParameters getPrevious() {
return previous;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package nl.isaac.dotcms.plugin.configuration.listener;
/**
* dotCMS Configuration plugin by ISAAC - The Full Service Internet Agency is licensed
* under a Creative Commons Attribution 3.0 Unported License
* - http://creativecommons.org/licenses/by/3.0/
* - http://www.geekyplugins.com/
*
* @copyright Copyright (c) 2011 ISAAC Software Solutions B.V. (http://www.isaac.nl)
*/

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

import nl.isaac.dotcms.plugin.configuration.ConfigurationService;
import nl.isaac.dotcms.plugin.configuration.ConfigurationService.ConfigurationParameters;

/**
* The only purpose of this class is to set and clear the default parameters for {@link ConfigurationService} while the request is active.
* @author maarten
*
*/
public class RequestStoringListener implements ServletRequestListener {

@Override
public void requestInitialized(ServletRequestEvent sre) {
if (sre.getServletRequest() instanceof HttpServletRequest) {
final ConfigurationParameters previousParams = ConfigurationService.getDefaultParameters();
ConfigurationService.storeDefaultParameters(new RequestBasedConfigurationParameters((HttpServletRequest) sre.getServletRequest(), previousParams));
}
}

@Override
public void requestDestroyed(ServletRequestEvent sre) {
final ConfigurationParameters current = ConfigurationService.getDefaultParameters();
if (current instanceof RequestBasedConfigurationParameters
&& ((RequestBasedConfigurationParameters) current).getPrevious() != null) {
ConfigurationService.storeDefaultParameters(((RequestBasedConfigurationParameters) current).getPrevious());
} else {
ConfigurationService.clearDefaultParameters();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nl.isaac.dotcms.plugin.configuration.filter;
package nl.isaac.dotcms.plugin.configuration.listener;
/**
* dotCMS Configuration plugin by ISAAC - The Full Service Internet Agency is licensed
* under a Creative Commons Attribution 3.0 Unported License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import nl.isaac.comp.configuration.CustomConfiguration;
import nl.isaac.dotcms.plugin.configuration.ConfigurationService;
import nl.isaac.dotcms.plugin.configuration.dependencies.org.apache.commons.configuration.ConfigurationException;
import nl.isaac.dotcms.plugin.configuration.filter.RequestStoringFilter;
import nl.isaac.dotcms.plugin.configuration.listener.RequestStoringListener;
import nl.isaac.dotcms.plugin.configuration.util.BasicMap;

import org.apache.velocity.tools.view.context.ViewContext;
Expand All @@ -30,7 +30,7 @@

/**
* This view tool will always use {@link ConfigurationService#getHostConfiguration()} for host configuration and {@link ConfigurationService#getPluginConfiguration(String)} for plugin configurations.
* So ensure that something like {@link RequestStoringFilter} is deployed!
* So ensure that something like {@link RequestStoringListener} is deployed!
*
* @author maarten
*
Expand Down
Loading

0 comments on commit 42e4e06

Please sign in to comment.