You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2018/11/20 22:58:40 UTC
[jspwiki] 02/07: backwards-breaking change: move
TextUtil.getRequiredProperty method to WikiEngine
This is an automated email from the ASF dual-hosted git repository.
juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 374ec0b1b1bb2ab1fad97135446452ffb069f399
Author: juanpablo <ju...@apache.org>
AuthorDate: Tue Nov 20 23:51:15 2018 +0100
backwards-breaking change: move TextUtil.getRequiredProperty method to WikiEngine
---
.../src/main/java/org/apache/wiki/PageManager.java | 2 +-
.../src/main/java/org/apache/wiki/WikiEngine.java | 34 ++++++++++++------
.../java/org/apache/wiki/auth/UserManager.java | 5 ++-
.../wiki/providers/CachingAttachmentProvider.java | 3 +-
.../org/apache/wiki/providers/CachingProvider.java | 3 +-
.../main/java/org/apache/wiki/util/TextUtil.java | 18 ----------
.../test/java/org/apache/wiki/WikiEngineTest.java | 42 ++++++++++++++++------
.../providers/BasicAttachmentProviderTest.java | 3 +-
.../java/org/apache/wiki/util/TextUtilTest.java | 22 ------------
jspwiki-war/src/main/webapp/rss.jsp | 21 ++++-------
10 files changed, 69 insertions(+), 84 deletions(-)
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/PageManager.java b/jspwiki-main/src/main/java/org/apache/wiki/PageManager.java
index 35605ed..a9cfea6 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/PageManager.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/PageManager.java
@@ -179,7 +179,7 @@ public class PageManager extends ModuleManager implements WikiEventListener {
if (useCache) {
classname = "org.apache.wiki.providers.CachingProvider";
} else {
- classname = TextUtil.getRequiredProperty(props, PROP_PAGEPROVIDER);
+ classname = m_engine.getRequiredProperty(props, PROP_PAGEPROVIDER);
}
try {
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
index 98a88d0..c781693 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiEngine.java
@@ -27,10 +27,8 @@ import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
@@ -39,6 +37,7 @@ import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeSet;
+import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
@@ -52,6 +51,7 @@ import org.apache.wiki.api.engine.AdminBeanManager;
import org.apache.wiki.api.engine.FilterManager;
import org.apache.wiki.api.engine.PluginManager;
import org.apache.wiki.api.exceptions.FilterException;
+import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
import org.apache.wiki.api.exceptions.NoSuchVariableException;
import org.apache.wiki.api.exceptions.ProviderException;
import org.apache.wiki.api.exceptions.WikiException;
@@ -293,7 +293,7 @@ public class WikiEngine
private AdminBeanManager m_adminBeanManager;
/** Stores wikiengine attributes. */
- private Map<String,Object> m_attributes = Collections.synchronizedMap(new HashMap<String,Object>());
+ private Map<String,Object> m_attributes = new ConcurrentHashMap<>();
/**
* Gets a WikiEngine related to this servlet. Since this method
@@ -1959,17 +1959,31 @@ public class WikiEngine
* @return Variable value, or null, if there is no such variable.
* @since 2.2
*/
- public String getVariable( WikiContext context, String name )
- {
- try
- {
+ public String getVariable( WikiContext context, String name ) {
+ try {
return m_variableManager.getValue( context, name );
- }
- catch( NoSuchVariableException e )
- {
+ } catch( NoSuchVariableException e ) {
return null;
}
}
+
+ /**
+ * Throws an exception if a property is not found.
+ *
+ * @param props A set of properties to search the key in.
+ * @param key The key to look for.
+ * @return The required property
+ *
+ * @throws NoRequiredPropertyException If the search key is not in the property set.
+ * @since 2.0.26 (on TextUtils, moved To WikiEngine on 2.11.0-M1)
+ */
+ public String getRequiredProperty( Properties props, String key ) throws NoRequiredPropertyException {
+ String value = TextUtil.getStringProperty( props, key, null );
+ if( value == null ) {
+ throw new NoRequiredPropertyException( "Required property not found", key );
+ }
+ return value;
+ }
/**
* Returns the current PageManager which is responsible for storing
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/UserManager.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/UserManager.java
index 6ea4de3..9b7a99d 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/auth/UserManager.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/UserManager.java
@@ -62,7 +62,6 @@ import org.apache.wiki.preferences.Preferences;
import org.apache.wiki.ui.InputValidator;
import org.apache.wiki.util.ClassUtil;
import org.apache.wiki.util.MailUtil;
-import org.apache.wiki.util.TextUtil;
import org.apache.wiki.workflow.Decision;
import org.apache.wiki.workflow.DecisionRequiredException;
import org.apache.wiki.workflow.Fact;
@@ -106,7 +105,7 @@ public class UserManager {
// private static final String PROP_ACLMANAGER = "jspwiki.aclManager";
/** Associates wiki sessions with profiles */
- private final Map<WikiSession,UserProfile> m_profiles = new WeakHashMap<WikiSession,UserProfile>();
+ private final Map<WikiSession,UserProfile> m_profiles = new WeakHashMap<>();
/** The user database loads, manages and persists user identities */
private UserDatabase m_database;
@@ -155,7 +154,7 @@ public class UserManager {
try
{
- dbClassName = TextUtil.getRequiredProperty( m_engine.getWikiProperties(), PROP_DATABASE );
+ dbClassName = m_engine.getRequiredProperty( m_engine.getWikiProperties(), PROP_DATABASE );
log.info("Attempting to load user database class "+dbClassName);
final Class<?> dbClass = ClassUtil.findClass( USERDATABASE_PACKAGE, dbClassName );
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java
index 17e3e73..2b7fb1f 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java
@@ -37,7 +37,6 @@ import org.apache.wiki.attachment.Attachment;
import org.apache.wiki.attachment.AttachmentManager;
import org.apache.wiki.search.QueryItem;
import org.apache.wiki.util.ClassUtil;
-import org.apache.wiki.util.TextUtil;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
@@ -123,7 +122,7 @@ public class CachingAttachmentProvider
//
// Find and initialize real provider.
//
- String classname = TextUtil.getRequiredProperty( properties, AttachmentManager.PROP_PROVIDER );
+ String classname = engine.getRequiredProperty( properties, AttachmentManager.PROP_PROVIDER );
try
{
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingProvider.java
index c4fa161..26c1f96 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingProvider.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingProvider.java
@@ -37,7 +37,6 @@ import org.apache.wiki.parser.MarkupParser;
import org.apache.wiki.render.RenderingManager;
import org.apache.wiki.search.QueryItem;
import org.apache.wiki.util.ClassUtil;
-import org.apache.wiki.util.TextUtil;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
@@ -145,7 +144,7 @@ public class CachingProvider implements WikiPageProvider {
//
// Find and initialize real provider.
//
- String classname = TextUtil.getRequiredProperty( properties, PageManager.PROP_PAGEPROVIDER );
+ String classname = m_engine.getRequiredProperty( properties, PageManager.PROP_PAGEPROVIDER );
try
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/util/TextUtil.java b/jspwiki-main/src/main/java/org/apache/wiki/util/TextUtil.java
index 90508c9..77f5e2e 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/util/TextUtil.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/util/TextUtil.java
@@ -27,7 +27,6 @@ import java.util.Properties;
import java.util.Random;
import org.apache.commons.lang.StringUtils;
-import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
/**
@@ -430,23 +429,6 @@ public final class TextUtil {
}
/**
- * Throws an exception if a property is not found.
- *
- * @param props A set of properties to search the key in.
- * @param key The key to look for.
- * @return The required property
- *
- * @throws NoRequiredPropertyException If the search key is not in the property set.
- */
- public static String getRequiredProperty( Properties props, String key ) throws NoRequiredPropertyException {
- String value = getStringProperty( props, key, null );
- if( value == null ) {
- throw new NoRequiredPropertyException( "Required property not found", key );
- }
- return value;
- }
-
- /**
* Returns true, if the string "val" denotes a positive string. Allowed values are "yes", "on", and "true".
* Comparison is case-insignificant. Null values are safe.
*
diff --git a/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java b/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java
index b3b6bfb..56db38f 100644
--- a/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java
+++ b/jspwiki-main/src/test/java/org/apache/wiki/WikiEngineTest.java
@@ -18,14 +18,13 @@
*/
package org.apache.wiki;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.AfterEach;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
+import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
import org.apache.wiki.attachment.Attachment;
import org.apache.wiki.attachment.AttachmentManager;
import org.apache.wiki.providers.BasicAttachmentProvider;
@@ -33,9 +32,11 @@ import org.apache.wiki.providers.CachingProvider;
import org.apache.wiki.providers.FileSystemProvider;
import org.apache.wiki.providers.VerySimpleProvider;
import org.apache.wiki.util.TextUtil;
-
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
import net.sf.ehcache.CacheManager;
public class WikiEngineTest
@@ -454,13 +455,13 @@ public class WikiEngineTest
// check a few pre-conditions
Collection< String > c = refMgr.findReferrers( "TestAtt.txt" );
- Assertions.assertTrue( c!=null && ((String)c.iterator().next()).equals( NAME1 ), "normal, unexisting page" );
+ Assertions.assertTrue( c!=null && c.iterator().next().equals( NAME1 ), "normal, unexisting page" );
c = refMgr.findReferrers( NAME1+"/TestAtt.txt" );
Assertions.assertTrue( c==null || c.size()==0, "no attachment" );
c = refMgr.findUncreated();
- Assertions.assertTrue( c!=null && c.size()==1 && ((String)c.iterator().next()).equals( "TestAtt.txt" ), "unknown attachment" );
+ Assertions.assertTrue( c!=null && c.size()==1 && c.iterator().next().equals( "TestAtt.txt" ), "unknown attachment" );
// now we create the attachment
@@ -477,10 +478,10 @@ public class WikiEngineTest
Assertions.assertTrue( c==null || c.size()==0, "no normal page" );
c = refMgr.findReferrers( NAME1+"/TestAtt.txt" );
- Assertions.assertTrue( c!=null && ((String)c.iterator().next()).equals( NAME1 ), "attachment exists now" );
+ Assertions.assertTrue( c!=null && c.iterator().next().equals( NAME1 ), "attachment exists now" );
c = refMgr.findUnreferenced();
- Assertions.assertTrue( c.size()==1 && ((String)c.iterator().next()).equals( NAME1 ), "unreferenced" );
+ Assertions.assertTrue( c.size()==1 && c.iterator().next().equals( NAME1 ), "unreferenced" );
}
finally
{
@@ -516,7 +517,7 @@ public class WikiEngineTest
c = refMgr.findUnreferenced();
Assertions.assertEquals( c.size(), 1, "unreferenced count" );
- Assertions.assertTrue( ((String)c.iterator().next()).equals( NAME1 ), "unreferenced" );
+ Assertions.assertTrue( c.iterator().next().equals( NAME1 ), "unreferenced" );
}
finally
{
@@ -552,7 +553,7 @@ public class WikiEngineTest
c = refMgr.findUnreferenced();
Assertions.assertEquals( c.size(), 1, "unreferenced count" );
- Assertions.assertTrue( ((String)c.iterator().next()).equals( NAME1 ), "unreferenced" );
+ Assertions.assertTrue( c.iterator().next().equals( NAME1 ), "unreferenced" );
}
finally
{
@@ -845,5 +846,26 @@ public class WikiEngineTest
Assertions.assertEquals( TextUtil.normalizePostData( "" ), m_engine.getText( name ), "wrong content" );
}
+
+ @Test
+ public void testGetRequiredProperty() throws Exception
+ {
+ String[] vals = { "foo", " this is a property ", "bar", "60" };
+ Properties props = TextUtil.createProperties(vals);
+ Assertions.assertEquals( "60", m_engine.getRequiredProperty( props, "bar" ) );
+ }
+
+ @Test
+ public void testGetRequiredPropertyNRPE()
+ {
+ String[] vals = { "foo", " this is a property ", "bar", "60" };
+ Properties props = TextUtil.createProperties(vals);
+ try
+ {
+ m_engine.getRequiredProperty( props, "ber" );
+ Assertions.fail( "NoRequiredPropertyException should've been thrown!" );
+ }
+ catch (NoRequiredPropertyException nrpe) {}
+ }
}
diff --git a/jspwiki-main/src/test/java/org/apache/wiki/providers/BasicAttachmentProviderTest.java b/jspwiki-main/src/test/java/org/apache/wiki/providers/BasicAttachmentProviderTest.java
index 561e566..a36aa85 100644
--- a/jspwiki-main/src/test/java/org/apache/wiki/providers/BasicAttachmentProviderTest.java
+++ b/jspwiki-main/src/test/java/org/apache/wiki/providers/BasicAttachmentProviderTest.java
@@ -30,7 +30,6 @@ import java.util.Properties;
import org.apache.wiki.TestEngine;
import org.apache.wiki.attachment.Attachment;
import org.apache.wiki.util.FileUtil;
-import org.apache.wiki.util.TextUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -58,7 +57,7 @@ public class BasicAttachmentProviderTest
{
m_engine = new TestEngine(props);
- TestEngine.deleteAll( new File(TextUtil.getRequiredProperty( props, BasicAttachmentProvider.PROP_STORAGEDIR )) );
+ TestEngine.deleteAll( new File(m_engine.getRequiredProperty( props, BasicAttachmentProvider.PROP_STORAGEDIR )) );
m_provider = new BasicAttachmentProvider();
m_provider.initialize( m_engine, props );
diff --git a/jspwiki-main/src/test/java/org/apache/wiki/util/TextUtilTest.java b/jspwiki-main/src/test/java/org/apache/wiki/util/TextUtilTest.java
index 6f7bd8d..265e943 100644
--- a/jspwiki-main/src/test/java/org/apache/wiki/util/TextUtilTest.java
+++ b/jspwiki-main/src/test/java/org/apache/wiki/util/TextUtilTest.java
@@ -21,7 +21,6 @@ package org.apache.wiki.util;
import java.io.File;
import java.util.Properties;
-import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -382,27 +381,6 @@ public class TextUtilTest
}
@Test
- public void testGetRequiredProperty() throws Exception
- {
- String[] vals = { "foo", " this is a property ", "bar", "60" };
- Properties props = TextUtil.createProperties(vals);
- Assertions.assertEquals( "60", TextUtil.getRequiredProperty( props, "bar" ) );
- }
-
- @Test
- public void testGetRequiredPropertyNRPE()
- {
- String[] vals = { "foo", " this is a property ", "bar", "60" };
- Properties props = TextUtil.createProperties(vals);
- try
- {
- TextUtil.getRequiredProperty( props, "ber" );
- Assertions.fail( "NoRequiredPropertyException should've been thrown!" );
- }
- catch (NoRequiredPropertyException nrpe) {}
- }
-
- @Test
public void testGetStringProperty()
{
String[] vals = { "foo", " this is a property " };
diff --git a/jspwiki-war/src/main/webapp/rss.jsp b/jspwiki-war/src/main/webapp/rss.jsp
index 4a3dbf8..19e0e3a 100644
--- a/jspwiki-war/src/main/webapp/rss.jsp
+++ b/jspwiki-war/src/main/webapp/rss.jsp
@@ -90,24 +90,21 @@
SimpleDateFormat iso8601fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Properties properties = wiki.getWikiProperties();
- String channelDescription = TextUtil.getRequiredProperty( properties, RSSGenerator.PROP_CHANNEL_DESCRIPTION );
- String channelLanguage = TextUtil.getRequiredProperty( properties, RSSGenerator.PROP_CHANNEL_LANGUAGE );
+ String channelDescription = wiki.getRequiredProperty( properties, RSSGenerator.PROP_CHANNEL_DESCRIPTION );
+ String channelLanguage = wiki.getRequiredProperty( properties, RSSGenerator.PROP_CHANNEL_LANGUAGE );
//
// Now, list items.
//
List< WikiPage > changed;
- if( "blog".equals( mode ) )
- {
+ if( "blog".equals( mode ) ) {
org.apache.wiki.plugin.WeblogPlugin plug = new org.apache.wiki.plugin.WeblogPlugin();
changed = plug.findBlogEntries(wiki,
wikipage.getName(),
new Date(0L),
new Date());
- }
- else
- {
+ } else {
changed = ( List< WikiPage > )wiki.getVersionHistory( wikipage.getName() );
}
@@ -117,16 +114,14 @@
boolean hasChanged = false;
Date latest = new Date(0);
- for( Iterator< WikiPage > i = changed.iterator(); i.hasNext(); )
- {
+ for( Iterator< WikiPage > i = changed.iterator(); i.hasNext(); ) {
WikiPage p = i.next();
if( !HttpUtil.checkFor304( request, p.getName(), p.getLastModified() ) ) hasChanged = true;
if( p.getLastModified().after( latest ) ) latest = p.getLastModified();
}
- if( !hasChanged && changed.size() > 0 )
- {
+ if( !hasChanged && changed.size() > 0 ) {
response.sendError( HttpServletResponse.SC_NOT_MODIFIED );
w.exitState();
return;
@@ -150,9 +145,7 @@
Element element = m_rssCache.get(hashKey);
if (element != null) {
rss = (String) element.getObjectValue();
- }
- else
- {
+ } else {
rss = wiki.getRSSGenerator().generateFeed( wikiContext, changed, mode, type );
m_rssCache.put(new Element(hashKey,rss));
}