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 4e130737bdfcae92b67f4edafd24a0118e9505a4 Author: juanpablo <[email protected]> AuthorDate: Wed Feb 26 13:12:37 2020 +0100 JSPWIKI-120: InitializablePlugin and implementing classes use Engine instead of WikiEngine --- .../wiki/api/plugin/InitializablePlugin.java | 20 +- .../org/apache/wiki/plugin/PageViewPlugin.java | 300 +++++++-------------- .../org/apache/wiki/plugin/JavaScriptPlugin.java | 15 +- 3 files changed, 115 insertions(+), 220 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/api/plugin/InitializablePlugin.java b/jspwiki-main/src/main/java/org/apache/wiki/api/plugin/InitializablePlugin.java index 6287ec7..1f4d33e 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/api/plugin/InitializablePlugin.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/api/plugin/InitializablePlugin.java @@ -18,28 +18,24 @@ */ package org.apache.wiki.api.plugin; -import org.apache.wiki.WikiEngine; +import org.apache.wiki.api.core.Engine; import org.apache.wiki.api.exceptions.PluginException; /** - * If a plugin defines this interface, it is called exactly once - * prior to the actual execute() routine. If the plugin has its - * own declaration in jspwiki_modules.xml, then it is called during - * startup - otherwise it is called the first time the plugin is + * If a plugin defines this interface, it is called exactly once prior to the actual execute() routine. If the plugin has its + * own declaration in jspwiki_modules.xml, then it is called during startup - otherwise it is called the first time the plugin is * encountered. * <p> - * This method did not actually work until 2.5.30. The method signature - * has been changed in 2.6 to reflect the new operation. + * This method did not actually work until 2.5.30. The method signature has been changed in 2.6 to reflect the new operation. */ -public interface InitializablePlugin -{ +public interface InitializablePlugin { + /** - * Called whenever the plugin is being instantiated for - * the first time. + * Called whenever the plugin is being instantiated for the first time. * * @param engine The WikiEngine. * @throws PluginException If something goes wrong. */ + void initialize( Engine engine ) throws PluginException; - void initialize( WikiEngine engine ) throws PluginException; } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/plugin/PageViewPlugin.java b/jspwiki-main/src/main/java/org/apache/wiki/plugin/PageViewPlugin.java index 58777b4..7572450 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/plugin/PageViewPlugin.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/plugin/PageViewPlugin.java @@ -29,7 +29,6 @@ import org.apache.oro.text.regex.PatternMatcher; import org.apache.oro.text.regex.Perl5Matcher; import org.apache.wiki.WikiBackgroundThread; import org.apache.wiki.WikiContext; -import org.apache.wiki.WikiEngine; import org.apache.wiki.WikiPage; import org.apache.wiki.api.core.Engine; import org.apache.wiki.api.exceptions.PluginException; @@ -134,16 +133,12 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin * * @param engine The wiki engine. */ - @Override public void initialize( final WikiEngine engine ) - { - + @Override + public void initialize( final Engine engine ) { log.info( "initializing PageViewPlugin" ); - - synchronized( this ) - { - if( c_singleton == null ) - { - c_singleton = new PageViewManager( ); + synchronized( this ) { + if( c_singleton == null ) { + c_singleton = new PageViewManager(); } c_singleton.initialize( engine ); } @@ -152,23 +147,20 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin /** * Cleanup the singleton reference. */ - private void cleanup() - { + private void cleanup() { log.info( "cleaning up PageView Manager" ); - c_singleton = null; } /** * {@inheritDoc} */ - @Override public String execute( final WikiContext context, final Map<String, String> params ) throws PluginException - { + @Override + public String execute( final WikiContext context, final Map< String, String > params ) throws PluginException { final PageViewManager manager = c_singleton; String result = STR_EMPTY; - if( manager != null ) - { + if( manager != null ) { result = manager.execute( context, params ); } @@ -178,8 +170,7 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin /** * Page view manager, handling all storage. */ - public final class PageViewManager implements WikiEventListener - { + public final class PageViewManager implements WikiEventListener { /** Are we initialized? */ private boolean m_initialized = false; @@ -199,13 +190,10 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin private String m_workDir = null; /** Comparator for descending sort on page count. */ - private final Comparator<Object> m_compareCountDescending = new Comparator<Object>() { - @Override public int compare( final Object o1, final Object o2 ) - { - final int v1 = getCount( o1 ); - final int v2 = getCount( o2 ); - return (v1 == v2) ? ((String) o1).compareTo( (String) o2 ) : (v1 < v2) ? 1 : -1; - } + private final Comparator< Object > m_compareCountDescending = ( o1, o2 ) -> { + final int v1 = getCount( o1 ); + final int v2 = getCount( o2 ); + return ( v1 == v2 ) ? ( ( String )o1 ).compareTo( ( String )o2 ) : ( v1 < v2 ) ? 1 : -1; }; /** @@ -213,16 +201,11 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin * * @param engine The wiki engine. */ - public synchronized void initialize( final WikiEngine engine ) - { + public synchronized void initialize( final Engine engine ) { log.info( "initializing PageView Manager" ); - m_workDir = engine.getWorkDir(); - engine.addWikiEventListener( this ); - - if( m_counters == null ) - { + if( m_counters == null ) { // Load the counters into a collection m_storage = new Properties(); m_counters = new TreeMap<>(); @@ -231,8 +214,7 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin } // backup counters every 5 minutes - if( m_pageCountSaveThread == null ) - { + if( m_pageCountSaveThread == null ) { m_pageCountSaveThread = new CounterSaveThread( engine, 5 * STORAGE_INTERVAL, this ); m_pageCountSaveThread.start(); } @@ -242,16 +224,13 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin /** * Handle the shutdown event via the page counter thread. - * */ - private synchronized void handleShutdown() - { + private synchronized void handleShutdown() { log.info( "handleShutdown: The counter store thread was shut down." ); cleanup(); - if( m_counters != null ) - { + if( m_counters != null ) { m_dirty = true; storeCounters(); @@ -273,35 +252,28 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin * * @param event The wiki event to inspect. */ - @Override public void actionPerformed( final WikiEvent event ) - { - if( event instanceof WikiEngineEvent ) - { - if( event.getType() == WikiEngineEvent.SHUTDOWN ) - { + @Override + public void actionPerformed( final WikiEvent event ) { + if( event instanceof WikiEngineEvent ) { + if( event.getType() == WikiEngineEvent.SHUTDOWN ) { log.info( "Detected wiki engine shutdown" ); handleShutdown(); } - } - else if( (event instanceof WikiPageRenameEvent) && (event.getType() == WikiPageRenameEvent.PAGE_RENAMED) ) - { - final String oldPageName = ((WikiPageRenameEvent) event).getOldPageName(); - final String newPageName = ((WikiPageRenameEvent) event).getNewPageName(); - final Counter oldCounter = m_counters.get(oldPageName); - if ( oldCounter != null ) - { - m_storage.remove(oldPageName); - m_counters.put(newPageName, oldCounter); - m_storage.setProperty(newPageName, oldCounter.toString()); - m_counters.remove(oldPageName); + } else if( ( event instanceof WikiPageRenameEvent ) && ( event.getType() == WikiPageRenameEvent.PAGE_RENAMED ) ) { + final String oldPageName = ( ( WikiPageRenameEvent )event ).getOldPageName(); + final String newPageName = ( ( WikiPageRenameEvent )event ).getNewPageName(); + final Counter oldCounter = m_counters.get( oldPageName ); + if( oldCounter != null ) { + m_storage.remove( oldPageName ); + m_counters.put( newPageName, oldCounter ); + m_storage.setProperty( newPageName, oldCounter.toString() ); + m_counters.remove( oldPageName ); m_dirty = true; } - } - else if( (event instanceof WikiPageEvent) && (event.getType() == WikiPageEvent.PAGE_DELETED) ) - { - final String pageName = ((WikiPageEvent) event).getPageName(); - m_storage.remove(pageName); - m_counters.remove(pageName); + } else if( ( event instanceof WikiPageEvent ) && ( event.getType() == WikiPageEvent.PAGE_DELETED ) ) { + final String pageName = ( ( WikiPageEvent )event ).getPageName(); + m_storage.remove( pageName ); + m_counters.remove( pageName ); } } @@ -313,13 +285,12 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin * @return String Wiki page snippet * @throws PluginException Malformed pattern parameter. */ - public String execute( final WikiContext context, final Map<String, String> params ) throws PluginException { + public String execute( final WikiContext context, final Map< String, String > params ) throws PluginException { final Engine engine = context.getEngine(); final WikiPage page = context.getPage(); String result = STR_EMPTY; - if( page != null ) - { + if( page != null ) { // get parameters final String pagename = page.getName(); String count = params.get( PARAM_COUNT ); @@ -336,48 +307,32 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin boolean increment = false; // increment counter? - if( STR_YES.equals( count ) ) - { + if( STR_YES.equals( count ) ) { increment = true; - } - else - { + } else { count = null; } // default increment counter? - if( (show == null || STR_NONE.equals( show )) && count == null ) - { + if( ( show == null || STR_NONE.equals( show ) ) && count == null ) { increment = true; } // filter on referring pages? - Collection<String> referrers = null; + Collection< String > referrers = null; - if( refer != null ) - { + if( refer != null ) { final ReferenceManager refManager = engine.getManager( ReferenceManager.class ); - - final Iterator< String > iter = refManager.findCreated().iterator(); - - while ( iter != null && iter.hasNext() ) - { - final String name = iter.next(); + for( final String name : refManager.findCreated() ) { boolean use = false; - - for( int n = 0; !use && n < refer.length; n++ ) - { - use = matcher.matches( name, refer[n] ); + for( int n = 0; !use && n < refer.length; n++ ) { + use = matcher.matches( name, refer[ n ] ); } - if( use ) - { + if( use ) { final Collection< String > refs = engine.getManager( ReferenceManager.class ).findReferrers( name ); - - if( refs != null && !refs.isEmpty() ) - { - if( referrers == null ) - { + if( refs != null && !refs.isEmpty() ) { + if( referrers == null ) { referrers = new HashSet<>(); } referrers.addAll( refs ); @@ -386,15 +341,12 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin } } - synchronized( this ) - { + synchronized( this ) { Counter counter = m_counters.get( pagename ); // only count in view mode, keep storage values in sync - if( increment && WikiContext.VIEW.equalsIgnoreCase( context.getRequestContext() ) ) - { - if( counter == null ) - { + if( increment && WikiContext.VIEW.equalsIgnoreCase( context.getRequestContext() ) ) { + if( counter == null ) { counter = new Counter(); m_counters.put( pagename, counter ); } @@ -403,16 +355,12 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin m_dirty = true; } - if( show == null || STR_NONE.equals( show ) ) - { + if( show == null || STR_NONE.equals( show ) ) { // nothing to show - } - else if( PARAM_COUNT.equals( show ) ) - { + } else if( PARAM_COUNT.equals( show ) ) { // show page count - if( counter == null ) - { + if( counter == null ) { counter = new Counter(); m_counters.put( pagename, counter ); m_storage.setProperty( pagename, counter.toString() ); @@ -420,47 +368,31 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin } result = counter.toString(); - } - else if( body != null && 0 < body.length() && STR_LIST.equals( show ) ) - { + } else if( body != null && 0 < body.length() && STR_LIST.equals( show ) ) { // show list of counts String header = STR_EMPTY; String line = body; String footer = STR_EMPTY; int start = body.indexOf( STR_SEPARATOR ); - // split body into header, line, footer on ---- - // separator - if( 0 < start ) - { + // split body into header, line, footer on ---- separator + if( 0 < start ) { header = body.substring( 0, start ); - start = skipWhitespace( start + STR_SEPARATOR.length(), body ); - int end = body.indexOf( STR_SEPARATOR, start ); - - if( start >= end ) - { + if( start >= end ) { line = body.substring( start ); - - } - else - { + } else { line = body.substring( start, end ); - end = skipWhitespace( end + STR_SEPARATOR.length(), body ); - footer = body.substring( end ); } } // sort on name or count? - Map<String, Counter> sorted = m_counters; - - if( sort != null && PARAM_COUNT.equals( sort ) ) - { + Map< String, Counter > sorted = m_counters; + if( PARAM_COUNT.equals( sort ) ) { sorted = new TreeMap<>( m_compareCountDescending ); - sorted.putAll( m_counters ); } @@ -470,8 +402,7 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin final Object[] args = new Object[] { pagename, STR_EMPTY, STR_EMPTY }; final Iterator< Entry< String, Counter > > iter = sorted.entrySet().iterator(); - while ( iter != null && 0 < entries && iter.hasNext() ) - { + while( 0 < entries && iter.hasNext() ) { final Entry< String, Counter > entry = iter.next(); final String name = entry.getKey(); @@ -480,35 +411,29 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin boolean use = min <= value && value <= max; // did we specify a refer-to page? - if( use && referrers != null ) - { + if( use && referrers != null ) { use = referrers.contains( name ); } // did we specify what pages to include? - if( use && include != null ) - { + if( use && include != null ) { use = false; - for( int n = 0; !use && n < include.length; n++ ) - { - use = matcher.matches( name, include[n] ); + for( int n = 0; !use && n < include.length; n++ ) { + use = matcher.matches( name, include[ n ] ); } } // did we specify what pages to exclude? - if( use && null != exclude ) - { - for( int n = 0; use && n < exclude.length; n++ ) - { - use &= !matcher.matches( name, exclude[n] ); + if( use && null != exclude ) { + for( int n = 0; use && n < exclude.length; n++ ) { + use &= !matcher.matches( name, exclude[ n ] ); } } - if( use ) - { - args[1] = engine.getManager( RenderingManager.class ).beautifyTitle( name ); - args[2] = entry.getValue(); + if( use ) { + args[ 1 ] = engine.getManager( RenderingManager.class ).beautifyTitle( name ); + args[ 2 ] = entry.getValue(); fmt.format( args, buf, null ); @@ -533,27 +458,18 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin * @return Pattern[] The compiled patterns, or <code>null</code>. * @throws PluginException On malformed patterns. */ - private Pattern[] compileGlobs( final String name, final String value ) throws PluginException - { + private Pattern[] compileGlobs( final String name, final String value ) throws PluginException { Pattern[] result = null; - - if( value != null && 0 < value.length() && !STR_GLOBSTAR.equals( value ) ) - { - try - { + if( value != null && 0 < value.length() && !STR_GLOBSTAR.equals( value ) ) { + try { final PatternCompiler pc = new GlobCompiler(); - final String[] ptrns = StringUtils.split( value, STR_COMMA ); + result = new Pattern[ ptrns.length ]; - result = new Pattern[ptrns.length]; - - for( int n = 0; n < ptrns.length; n++ ) - { - result[n] = pc.compile( ptrns[n] ); + for( int n = 0; n < ptrns.length; n++ ) { + result[ n ] = pc.compile( ptrns[ n ] ); } - } - catch( final MalformedPatternException e ) - { + } catch( final MalformedPatternException e ) { throw new PluginException( "Parameter " + name + " has a malformed pattern: " + e.getMessage() ); } } @@ -568,10 +484,8 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin * @param value String in which offset points. * @return int Adjusted offset into value. */ - private int skipWhitespace( int offset, final String value ) - { - while ( Character.isWhitespace( value.charAt( offset ) ) ) - { + private int skipWhitespace( int offset, final String value ) { + while( Character.isWhitespace( value.charAt( offset ) ) ) { offset++; } return offset; @@ -602,11 +516,8 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin } // Copy the collection into a sorted map - final Iterator< Entry< Object, Object > > iter = m_storage.entrySet().iterator(); - - while ( iter != null && iter.hasNext() ) { - final Entry< ?, ? > entry = iter.next(); - m_counters.put( (String) entry.getKey(), new Counter( (String) entry.getValue() ) ); + for( final Entry< ?, ? > entry : m_storage.entrySet() ) { + m_counters.put( ( String )entry.getKey(), new Counter( ( String )entry.getValue() ) ); } log.info( "Loaded " + m_counters.size() + " counter values." ); @@ -636,10 +547,9 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin /** * Is the given thread still current? - * - * @return boolean <code>true</code> if the thread is still the current - * background thread. - * @param thrd + * + * @param thrd thread that can be the current background thread. + * @return boolean <code>true</code> if the thread is still the current background thread. */ private synchronized boolean isRunning( final Thread thrd ) { @@ -648,12 +558,8 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin } - - /** - * Counter for page hits collection. - */ - private static final class Counter - { + /** Counter for page hits collection. */ + private static final class Counter { /** The count value. */ private int m_count = 0; @@ -661,8 +567,7 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin /** * Create a new counter. */ - public Counter() - { + public Counter() { } /** @@ -706,17 +611,18 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin /** * @return String String representation of the count. */ - @Override public String toString() + @Override + public String toString() { return String.valueOf( m_count ); } + } /** * Background thread storing the page counters. */ - static final class CounterSaveThread extends WikiBackgroundThread - { + static final class CounterSaveThread extends WikiBackgroundThread { /** The page view manager. */ private final PageViewManager m_manager; @@ -726,15 +632,11 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin * * @param engine The wiki engine. * @param interval Delay in seconds between saves. - * @param pageViewManager + * @param pageViewManager page view manager. */ - public CounterSaveThread( final WikiEngine engine, final int interval, final PageViewManager pageViewManager ) - { - + public CounterSaveThread( final Engine engine, final int interval, final PageViewManager pageViewManager ) { super( engine, interval ); - - if( pageViewManager == null ) - { + if( pageViewManager == null ) { throw new IllegalArgumentException( "Manager cannot be null" ); } @@ -744,11 +646,9 @@ public class PageViewPlugin extends AbstractReferralPlugin implements WikiPlugin /** * Save the page counters to file. */ - @Override public void backgroundTask() - { - - if( m_manager.isRunning( this ) ) - { + @Override + public void backgroundTask() { + if( m_manager.isRunning( this ) ) { m_manager.storeCounters(); } } diff --git a/jspwiki-main/src/test/java/org/apache/wiki/plugin/JavaScriptPlugin.java b/jspwiki-main/src/test/java/org/apache/wiki/plugin/JavaScriptPlugin.java index 5e2a51c..5ab8b01 100644 --- a/jspwiki-main/src/test/java/org/apache/wiki/plugin/JavaScriptPlugin.java +++ b/jspwiki-main/src/test/java/org/apache/wiki/plugin/JavaScriptPlugin.java @@ -18,30 +18,29 @@ */ package org.apache.wiki.plugin; -import java.util.Map; - import org.apache.wiki.WikiContext; -import org.apache.wiki.WikiEngine; +import org.apache.wiki.api.core.Engine; import org.apache.wiki.api.exceptions.PluginException; import org.apache.wiki.api.plugin.InitializablePlugin; import org.apache.wiki.api.plugin.WikiPlugin; +import java.util.Map; + /** * Implements a simple plugin that just returns a piece of Javascript * <P> * Parameters: text - text to return. - * */ -public class JavaScriptPlugin implements WikiPlugin, InitializablePlugin -{ +public class JavaScriptPlugin implements WikiPlugin, InitializablePlugin { + protected static boolean c_inited = false; - public String execute( WikiContext context, Map< String, String > params ) throws PluginException { + @Override public String execute( final WikiContext context, final Map< String, String > params ) throws PluginException { return "<script language=\"JavaScript\"><!--\nfoo='';\n--></script>\n"; } - public void initialize( WikiEngine engine ) throws PluginException { + @Override public void initialize( final Engine engine ) throws PluginException { c_inited = true; }
