Update of
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache
In directory
james.mmbase.org:/tmp/cvs-serv1609/applications/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache
Modified Files:
CacheWrapper.java BasicCacheHandlerInterceptor.java
OSCacheWrapper.java CacheNameResolver.java
TokenizerCacheNameResolver.java PrefixSuffixModifier.java
Removed Files:
FlushHintCacheNameResolverFactory.java
CacheNameResolverFactory.java
Log Message:
work in progress on the cache flush code (and tests)
See also:
http://cvs.mmbase.org/viewcvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache
Index: CacheWrapper.java
===================================================================
RCS file:
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache/CacheWrapper.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- CacheWrapper.java 3 Nov 2008 12:10:07 -0000 1.3
+++ CacheWrapper.java 6 Nov 2008 10:17:23 -0000 1.4
@@ -9,6 +9,8 @@
*/
package org.mmbase.applications.vprowizards.spring.cache;
+import javax.servlet.http.HttpServletRequest;
+
public interface CacheWrapper {
public void flushForName(String flushname);
Index: BasicCacheHandlerInterceptor.java
===================================================================
RCS file:
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache/BasicCacheHandlerInterceptor.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- BasicCacheHandlerInterceptor.java 3 Nov 2008 13:14:38 -0000 1.4
+++ BasicCacheHandlerInterceptor.java 6 Nov 2008 10:17:23 -0000 1.5
@@ -8,12 +8,14 @@
*/
package org.mmbase.applications.vprowizards.spring.cache;
+import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
+import org.mmbase.applications.vprowizards.spring.util.ClassInstanceFactory;
import org.mmbase.util.logging.Logger;
import org.mmbase.util.logging.Logging;
import org.springframework.web.bind.ServletRequestUtils;
@@ -29,9 +31,10 @@
public class BasicCacheHandlerInterceptor extends CacheHandlerInterceptor {
private static Logger log =
Logging.getLoggerInstance(BasicCacheHandlerInterceptor.class);
+ private List<Modifier> modifiers = new ArrayList<Modifier>();
-
- private CacheNameResolverFactory cacheNameResolverFactory = null;
+// private Class<? extends CacheNameResolver> cachNameResolverClass;
+ private ClassInstanceFactory<CacheNameResolver> cacheNameResolverFactory;
private CacheWrapper cacheWrapper = null;
BasicCacheHandlerInterceptor() {
@@ -43,11 +46,10 @@
throws Exception {
log.debug("handling request type flush hint");
- if (shouldFlush(request)) {
- TokenizerCacheNameResolver resolver =
(TokenizerCacheNameResolver) cacheNameResolverFactory.getCacheNameResolver();
+ if (shouldFlush(request) &&
request.getParameterMap().get("flushname") != null) {
+ CacheNameResolver resolver =
cacheNameResolverFactory.newInstance();
resolver.setInput(ServletRequestUtils.getStringParameter(request, "flushname",
""));
- resolver.setNameSpace("request");
- flushForName(resolver.getNames());
+ flushForNames(resolver.getNames("request"));
}
}
@@ -60,10 +62,9 @@
log.debug("handling node type flush hint");
if (shouldFlush(request)) {
- TokenizerCacheNameResolver resolver =
(TokenizerCacheNameResolver) cacheNameResolverFactory.getCacheNameResolver();
+ CacheNameResolver resolver =
cacheNameResolverFactory.newInstance();
resolver.setInput(ServletRequestUtils.getStringParameter(request, "flushname",
""));
- resolver.setNameSpace("node");
- flushForName(resolver.getNames());
+ flushForNames(resolver.getNames("node"));
}
}
});
@@ -75,41 +76,86 @@
log.debug("handling relation type flush hint");
if (shouldFlush(request)) {
- TokenizerCacheNameResolver resolver =
(TokenizerCacheNameResolver) cacheNameResolverFactory.getCacheNameResolver();
+ CacheNameResolver resolver =
cacheNameResolverFactory.newInstance();
resolver.setInput(ServletRequestUtils.getStringParameter(request, "flushname",
""));
- resolver.setNameSpace("relation");
- flushForName(resolver.getNames());
+ flushForNames(resolver.getNames("relation"));
}
}
});
}
- /**
- * flush the given cache groups.
- * @param request
- * @param flushnames a comma separated list of cache groups.
- */
- private void flushForName(List<String> flushnames) {
- for(String name: flushnames) {
- cacheWrapper.flushForName(name);
+
+
+ public void setCacheWrapper(CacheWrapper cacheWrapper) {
+ this.cacheWrapper = cacheWrapper;
+ }
+
+
+ public CacheWrapper getCacheWrapper() {
+ return cacheWrapper;
}
+ public void addModifier(Modifier modifier) {
+ modifiers.add(modifier);
+ }
+
+ public void setModifiers(List<Modifier> modifiers) {
+ this.modifiers.addAll(modifiers);
}
private boolean shouldFlush(HttpServletRequest request) {
return ! StringUtils.isEmpty(request.getParameter("flushname"));
}
+ public ClassInstanceFactory<CacheNameResolver>
getCacheNameResolverFactory() {
+ return cacheNameResolverFactory;
+ }
- public void setCacheNameResolverFactory(CacheNameResolverFactory
cacheNameResolverFactory) {
- this.cacheNameResolverFactory = cacheNameResolverFactory;
+
+ public void
setCacheNameResolverFactory(ClassInstanceFactory<CacheNameResolver>
classInstanceFactory) {
+ this.cacheNameResolverFactory = classInstanceFactory;
}
- public void setCacheWrapper(CacheWrapper cacheWrapper) {
- this.cacheWrapper = cacheWrapper;
+
+
+ /**
+ * flush the given cache groups.
+ * @param request
+ * @param flushnames a comma separated list of cache groups.
+ * @param request
+ */
+ private void flushForNames(List<String> flushnames) {
+ for(String name: flushnames) {
+ cacheWrapper.flushForName(applyModifiers(name));
+ }
}
+ private String applyModifiers(String input){
+ for(Modifier modifier: modifiers){
+ input = modifier.modify(input);
+ }
+ return input;
+ }
+
+ /**
+ * apply all the modifiers to a list of strings.
+ * All the cache names that are resolved will be put through all the
registered modifiers
+ * before they are returned.
+ *
+ * @param items
+ * @return
+ */
+ private List<String> modify(List<String> items) {
+ List<String> result = new ArrayList<String>();
+ for (String item: items) {
+ for (Modifier modifier : modifiers) {
+ item = modifier.modify(item);
+ }
+ result.add(item);
+ }
+ return result;
+ }
}
Index: OSCacheWrapper.java
===================================================================
RCS file:
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache/OSCacheWrapper.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- OSCacheWrapper.java 3 Nov 2008 12:10:07 -0000 1.3
+++ OSCacheWrapper.java 6 Nov 2008 10:17:23 -0000 1.4
@@ -9,18 +9,31 @@
*/
package org.mmbase.applications.vprowizards.spring.cache;
+import javax.servlet.http.HttpServletRequest;
+
+import org.mmbase.module.core.MMBaseContext;
+
import com.opensymphony.oscache.base.Cache;
+import com.opensymphony.oscache.web.ServletCacheAdministrator;
+/**
+ * this wraps an oscahe instance.
+ * @author ebunders
+ *
+ */
public class OSCacheWrapper implements CacheWrapper {
- private Cache cache = null;
+ private static Cache cache = null;
+ /**
+ * This method flushes an oscache cache group with given name.
+ * @see
org.mmbase.applications.vprowizards.spring.cache.CacheWrapper#flushForName(java.lang.String,
javax.servlet.http.HttpServletRequest)
+ */
public void flushForName(String flushname) {
if (cache == null) {
- //TODO:sort this out
-// cache =
ServletCacheAdministrator.getInstance(MMBaseContext.getServletContext()).getCache(request,
-// PageContext.APPLICATION_SCOPE);
+ cache =
ServletCacheAdministrator.getInstance(MMBaseContext.getServletContext()).getAppScopeCache(MMBaseContext.getServletContext());
}
+ cache.flushGroup(flushname);
}
}
Index: CacheNameResolver.java
===================================================================
RCS file:
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache/CacheNameResolver.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- CacheNameResolver.java 27 Oct 2008 15:12:29 -0000 1.2
+++ CacheNameResolver.java 6 Nov 2008 10:17:23 -0000 1.3
@@ -11,12 +11,30 @@
import java.util.List;
+/**
+ * This interface expresses the concept that the name(s) of cache keys that
need to
+ * be flushed can be coded and need to be processed before the cache(s) can be
flushed.
+ * You can think about a http request parameter containing any number of cache
keys that need
+ * to be split up in a specific way.
+ *
+ * At least on of the getNames() menthods need to be implemented. If one is
not implemented it should
+ * throw a [EMAIL PROTECTED] UnsupportedOperationException}
+ * @author ebunders
+ *
+ */
public interface CacheNameResolver {
/**
* Obtain the cache names for the given namespace or those without a
namespace (global)
*
- * @param namespace
- * @return
+ * @param qualifier this can be some implementation-specific qualifier or
null if the implementation
+ * does not need it.
+ * @return a list of specific cache keys
+ */
+ public List<String> getNames(String qualifier);
+
+ /**
+ * Obtain the cache names for the given namespace or those without a
namespace (global)
+ * @return a list of specific cache keys
*/
public List<String> getNames();
Index: TokenizerCacheNameResolver.java
===================================================================
RCS file:
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache/TokenizerCacheNameResolver.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- TokenizerCacheNameResolver.java 3 Nov 2008 12:10:07 -0000 1.3
+++ TokenizerCacheNameResolver.java 6 Nov 2008 10:17:23 -0000 1.4
@@ -21,28 +21,61 @@
import org.mmbase.util.logging.Logging;
/**
- * This class helps you to handle strings that are formatted in a certain way.
The idear is that you have a comma
- * seperated list of values attached to a namespace, like
'namespace:value1,value2,value3'.<br>
+ * This class helps you to handle strings that are formatted in a certain way.
The idea is that you have a comma
+ * separated list of values attached to a namespace, like
'namespace:value1,value2,value3'.<br>
* the input string can contain more that one of these constructs for
different namespaces, separated by a
- * space. When using this class you have to register the namespaces you look
for. If one of the sections of
- * the input string dous not start with any of the registered namespaces, it
will assume the values are 'global',
- * and they are added to the values for all the namespaces you have registered.
- * In this way you can define various cache groups to be flushed for request,
node and/or relation type of cache flush hints.
+ * space. When using this class you have to give an input string, and then you
can get values for different namespaces.
+ * The input string will be parsed once.
*
* @author ebunders
*
*/
public class TokenizerCacheNameResolver implements CacheNameResolver {
- private List<String> nameSpaces = new ArrayList<String>();
- private List<Modifier> modifiers = new ArrayList<Modifier>();
+
private Map<String, List<String>> namesForNamespace = null;
+ private List<String> globalValues;
private String input = null;
- private String nameSpace = null;
+
+ private final String reNamespace = "^[\\w_]+:";
+ private final String reValue = "[\\w_]+";
+ private final String reTemplate = "(" + reValue +
"(\\[[\\w_]+(:[0-9])?\\])?)?";
+ private final String reComposite = reNamespace + reTemplate + "(," +
reTemplate + ")*";
private static Logger log =
Logging.getLoggerInstance(TokenizerCacheNameResolver.class);
+ /* (non-Javadoc)
+ * @see
org.mmbase.applications.vprowizard.spring.cache.CacheNameResolver#getNamesForNamespace(java.lang.String)
+ */
+ public List<String> getNames(String nameSpace) {
+ if(StringUtils.isEmpty(nameSpace)) {
+ throw new IllegalStateException("attribute namespace is empty");
+ }
+
+ if (namesForNamespace == null) {
+ tokenize();
+ }
+ List<String> result = new ArrayList<String>();
+ if(namesForNamespace.get(nameSpace) != null){
+ result.addAll(namesForNamespace.get(nameSpace));
+ }
+ result.addAll(globalValues);
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see
org.mmbase.applications.vprowizard.spring.cache.CacheNameResolver#setInput(java.lang.String)
+ */
+ public void setInput(String input) {
+ reset();
+ this.input = input;
+ }
+
+ public String toString() {
+ return ReflectionToStringBuilder.toString(this);
+ }
+
/**
* Tokenize the input string with all the configured tokens. All values
for each token are then put thrugh all the
* modifiers
@@ -53,118 +86,44 @@
if(StringUtils.isEmpty(input)) {
throw new IllegalStateException("set input first");
}
- if(StringUtils.isEmpty(nameSpace)) {
- throw new IllegalStateException("set nameSpace first");
- }
- if(nameSpaces.size() == 0) {
- throw new IllegalStateException("set namespaces first");
- }
+
+ //init
namesForNamespace = new HashMap<String, List<String>>();
+ globalValues = new ArrayList<String>();
+
List<String> parts = Arrays.asList(input.trim().split(" "));
- List<String> globals = new ArrayList<String>();
for (String part : parts) {
part = part.trim();
boolean partHasNamespace = false;
- for (String namespace : nameSpaces) {
- String _namespace = namespace + ":";
- if (part.startsWith(_namespace)) {
+ // boolean matches = part.matches("^[\\w_]+:[\\w,]+");
+ boolean matches = part.matches(reComposite);
+ if (matches) {
partHasNamespace = true;
- part = part.substring(_namespace.length());
- //TODO: het zou kunnen gebeuren date er twee 'parts' zijn
voor dezelfde namespace
- namesForNamespace.put(namespace,
modify(Arrays.asList(part.split(","))));
- }
- }
- if (!partHasNamespace) {
- globals.addAll(modify(Arrays.asList(part.split(","))));
+ String nameSpace = part.substring(0, part.indexOf(":"));
+ part = part.substring(part.indexOf(":")+1);
+ if(namesForNamespace.get(nameSpace) == null){
+ namesForNamespace.put(nameSpace, new
ArrayList<String>());
}
+
namesForNamespace.get(nameSpace).addAll(Arrays.asList(part.split(",")));
}
- // are there globals? add them to all the namespaces
- if (globals.size() > 0) {
- for (String namespace : namesForNamespace.keySet()) {
- namesForNamespace.get(namespace).addAll(globals);
- }
- }
- }
- /**
- * apply all the modifiers to a list of strings.
- * All the cache names that are resolved will be put through all the
registered modifiers
- * before they are returned.
- *
- * @param items
- * @return
- */
- private List<String> modify(List<String> items) {
- List<String> result = new ArrayList<String>();
- for (String item: items) {
- for (Modifier modifier : modifiers) {
- item = modifier.modify(item);
+ if (!partHasNamespace) {
+ globalValues.addAll(Arrays.asList(part.split(",")));
}
- result.add(item);
}
- return result;
}
- public TokenizerCacheNameResolver addModifier(Modifier modifier) {
- modifiers.add(modifier);
- return this;
- }
- public void addModifiers(List<Modifier> modifiers) {
- this.modifiers.addAll(modifiers);
- }
-
- /* (non-Javadoc)
- * @see
org.mmbase.applications.vprowizard.spring.cache.CacheNameResolver#getNamesForNamespace(java.lang.String)
- */
- public List<String> getNames() {
- if(StringUtils.isEmpty(nameSpace)) {
- throw new IllegalStateException("attribute namespace is empty");
- }
- if(!nameSpaces.contains(nameSpace)) {
- throw new IllegalStateException("namespace '"+nameSpace+"' is not
known" );
- }
- if (namesForNamespace == null) {
- tokenize();
- }
- return namesForNamespace.get(nameSpace);
- }
private void reset() {
namesForNamespace = null;
}
- /* (non-Javadoc)
- * @see
org.mmbase.applications.vprowizard.spring.cache.CacheNameResolver#setInput(java.lang.String)
- */
- public void setInput(String input) {
- reset();
- this.input = input;
- }
-
- /**
- * Set the nameSpace that you want to fetch the cache names for
- * @param nameSpace
- */
- public void setNameSpace(String nameSpace) {
- this.nameSpace = nameSpace;
+ public List<String> getNames() {
+ throw new UnsupportedOperationException("this method is not supported
for this cache name resolver");
}
- public String toString() {
- return ReflectionToStringBuilder.toString(this);
- }
- /**
- * Add the namespaces that are used to tokenize the input string
- * TODO: why? why not tokenize on all the namespaces you find?
- * @param namespaces
- */
- public void addNameSpaces(String[] namespaces) {
- List<String> disco = Arrays.asList(namespaces);
- for (String string : disco) {
- nameSpaces.add(string);
- }
- }
}
Index: PrefixSuffixModifier.java
===================================================================
RCS file:
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache/PrefixSuffixModifier.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- PrefixSuffixModifier.java 3 Nov 2008 12:10:07 -0000 1.3
+++ PrefixSuffixModifier.java 6 Nov 2008 10:17:23 -0000 1.4
@@ -32,18 +32,24 @@
}
public void setPrefix(String prefix) {
- this.prefix = prefix;
+ if(prefix == null){
+ prefix = "";
+ }
+ this.prefix = prefix.trim();
}
public void setSuffix(String suffix) {
- this.suffix = suffix;
+ if(suffix == null){
+ suffix = "";
+ }
+ this.suffix = suffix.trim();
}
public String modify(String input) {
- if (!StringUtils.isEmpty(prefix)) {
+ if (!StringUtils.isBlank(prefix)) {
input = prefix + input;
}
- if (!StringUtils.isEmpty(suffix)) {
+ if (!StringUtils.isBlank(suffix)) {
input = input + suffix;
}
return input;
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs