|
From: <nri...@us...> - 2013-07-08 14:35:17
|
Revision: 239
http://sourceforge.net/p/xmlfield/code/239
Author: nricheton
Date: 2013-07-08 14:35:13 +0000 (Mon, 08 Jul 2013)
Log Message:
-----------
Rework on XmlFieldFactory
Modified Paths:
--------------
trunk/xmlfield-core/src/main/java/org/xmlfield/core/CleanThreadLocalFilter.java
trunk/xmlfield-core/src/main/java/org/xmlfield/core/XmlFieldFactory.java
trunk/xmlfield-core/src/test/java/org/xmlfield/core/XmlFieldFactoryTest.java
Modified: trunk/xmlfield-core/src/main/java/org/xmlfield/core/CleanThreadLocalFilter.java
===================================================================
--- trunk/xmlfield-core/src/main/java/org/xmlfield/core/CleanThreadLocalFilter.java 2013-07-07 18:08:58 UTC (rev 238)
+++ trunk/xmlfield-core/src/main/java/org/xmlfield/core/CleanThreadLocalFilter.java 2013-07-08 14:35:13 UTC (rev 239)
@@ -27,6 +27,16 @@
XmlFieldFactory factory = null;
+ /**
+ * This method is intended to be overridden to setup global configuration.
+ *
+ * @param factory
+ * Global XmlFieldFactory
+ */
+ public void configure(XmlFieldFactory factory) {
+ // No special configuration
+ }
+
@Override
public void destroy() {
@@ -46,8 +56,10 @@
@Override
public void init(FilterConfig filterConfig) throws ServletException {
- factory = new XmlFieldFactory();
- factory.setUseThreadLocal(true);
+ factory = new XmlFieldFactory(true);
+
+ // Call custom configuration if any.
+ configure(factory);
}
}
Modified: trunk/xmlfield-core/src/main/java/org/xmlfield/core/XmlFieldFactory.java
===================================================================
--- trunk/xmlfield-core/src/main/java/org/xmlfield/core/XmlFieldFactory.java 2013-07-07 18:08:58 UTC (rev 238)
+++ trunk/xmlfield-core/src/main/java/org/xmlfield/core/XmlFieldFactory.java 2013-07-08 14:35:13 UTC (rev 239)
@@ -7,7 +7,11 @@
* <p>
* This factory is thread safe and can be configured to use ThreadLocal to reuse
* XmlField instances for performance reasons.
+ * <p>
+ * When using ThreadLocal, configuration ( parserConfiguration and getter cache)
+ * is unique (static) across all XmlFieldFactory instances.
*
+ *
* @see CleanThreadLocalFilter
*
* @author Nicolas Richeton
@@ -15,29 +19,55 @@
*/
public class XmlFieldFactory {
- private Boolean getterCache = null;
- private Map<String, String> parserConfiguration = null;
- private boolean useThreadLocal = false;
-
+ private static Boolean staticGetterCache = null;
+ private static Map<String, String> staticParserConfiguration = null;
/**
* XmlFiled instances associated to threads. Used only if useThreadLocal is
* true.
*/
- private final ThreadLocal<XmlField> xmlFieldInstances = new ThreadLocal<XmlField>() {
+ private static final ThreadLocal<XmlField> xmlFieldInstances = new ThreadLocal<XmlField>() {
@Override
protected XmlField initialValue() {
- XmlField xf = new XmlField(parserConfiguration);
- if (getterCache != null) {
- xf.setGetterCache(getterCache);
+ XmlField xf = new XmlField(staticParserConfiguration);
+ if (staticGetterCache != null) {
+ xf.setGetterCache(staticGetterCache);
}
return xf;
}
};
+ private Boolean getterCache = null;
+ private Map<String, String> parserConfiguration = null;
+
+ private final boolean useThreadLocal;
+
public XmlFieldFactory() {
+ this(false);
}
/**
+ * @param useThreadLocal
+ * When enabled, the factory will create only one XmlField object
+ * per thread and reuse it each time {@link #getXmlField()} is
+ * called.
+ * <p>
+ * This improves performance if {@link #getXmlField()} is called
+ * several times in the same thread. But it is still better to
+ * keep the XmlField object and reuse it because it it not
+ * subject to synchronization.
+ *
+ * <p>
+ * When used in a web application, it is recommended to use a
+ * servlet filter which clears the context after each call to
+ * prevent leaks in application servers.
+ *
+ * @see CleanThreadLocalFilter
+ */
+ public XmlFieldFactory(boolean useThreadLocal) {
+ this.useThreadLocal = useThreadLocal;
+ }
+
+ /**
* Clean the XmlField instance of the current thread.
*/
public void cleanThreadLocal() {
@@ -72,7 +102,11 @@
* @param enabled
*/
public void setGetterCache(boolean enabled) {
- this.getterCache = enabled;
+ if (useThreadLocal) {
+ staticGetterCache = enabled;
+ } else {
+ this.getterCache = enabled;
+ }
}
/**
@@ -82,28 +116,11 @@
* @param configuration
*/
public void setParserConfiguration(Map<String, String> configuration) {
- parserConfiguration = configuration;
+ if (useThreadLocal) {
+ staticParserConfiguration = configuration;
+ } else {
+ parserConfiguration = configuration;
+ }
}
- /**
- * When enabled, the factory will create only one XmlField object per thread
- * and reuse it each time {@link #getXmlField()} is called.
- * <p>
- * This improves performance if {@link #getXmlField()} is called several
- * times in the same thread. But it is still better to keep the XmlField
- * object and reuse it because it it not subject to synchronization.
- *
- * <p>
- * When used in a web application, it is recommended to use a servlet filter
- * which clears the context after each call to prevent leaks in application
- * servers.
- *
- * @see CleanThreadLocalFilter
- *
- * @param useThreadLocal
- */
- public void setUseThreadLocal(boolean useThreadLocal) {
- this.useThreadLocal = useThreadLocal;
- }
-
}
Modified: trunk/xmlfield-core/src/test/java/org/xmlfield/core/XmlFieldFactoryTest.java
===================================================================
--- trunk/xmlfield-core/src/test/java/org/xmlfield/core/XmlFieldFactoryTest.java 2013-07-07 18:08:58 UTC (rev 238)
+++ trunk/xmlfield-core/src/test/java/org/xmlfield/core/XmlFieldFactoryTest.java 2013-07-08 14:35:13 UTC (rev 239)
@@ -58,12 +58,11 @@
@Test
public void testFactoryThreadLocal() throws Exception {
- XmlFieldFactory factory = new XmlFieldFactory();
+ XmlFieldFactory factory = new XmlFieldFactory(true);
Map<String, String> conf = new HashMap<String, String>();
conf.put(OutputKeys.INDENT, "true");
factory.setParserConfiguration(conf);
- factory.setUseThreadLocal(true);
XmlField xf = factory.getXmlField();
Assert.assertNotNull(xf);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|