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 2e4934a6229df6952c5b638d7b91b4d01140fcd2 Author: juanpablo <[email protected]> AuthorDate: Thu Feb 20 17:50:24 2020 +0100 JSPWIKI-120: remove Engine#getCurrentWatchDog() use WatchDog.getCurrentWathDog( Engine ) instead --- .../src/main/java/org/apache/wiki/WatchDog.java | 150 ++++++++++----------- .../java/org/apache/wiki/WikiBackgroundThread.java | 11 +- .../main/java/org/apache/wiki/api/core/Engine.java | 9 -- .../main/java/org/apache/wiki/rss/RSSThread.java | 25 ++-- .../apache/wiki/search/LuceneSearchProvider.java | 2 +- .../java/org/apache/wiki/ui/WikiJSPFilter.java | 14 +- 6 files changed, 97 insertions(+), 114 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WatchDog.java b/jspwiki-main/src/main/java/org/apache/wiki/WatchDog.java index 94d236f..ad261bd 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/WatchDog.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/WatchDog.java @@ -18,6 +18,9 @@ */ package org.apache.wiki; +import org.apache.log4j.Logger; +import org.apache.wiki.api.core.Engine; + import java.lang.ref.WeakReference; import java.util.Iterator; import java.util.Map; @@ -25,64 +28,53 @@ import java.util.Set; import java.util.Stack; import java.util.concurrent.ConcurrentHashMap; -import org.apache.log4j.Logger; - /** - * WatchDog is a general system watchdog. You can attach any Watchable or a - * Thread object to it, and it will notify you if a timeout has been exceeded. + * WatchDog is a general system watchdog. You can attach any Watchable or a Thread object to it, and it will notify you + * if a timeout has been exceeded. * <p> - * The notification of the timeouts is done from a separate WatchDog thread, - * of which there is one per watched thread. This Thread is named 'WatchDog for - * XXX', where XXX is your Thread name. + * The notification of the timeouts is done from a separate WatchDog thread, of which there is one per watched thread. + * This Thread is named 'WatchDog for XXX', where XXX is your Thread name. * <p> - * The suggested method of obtaining a WatchDog is via the static factory - * method, since it will return you the correct watchdog for the current - * thread. However, we do not prevent you from creating your own watchdogs - * either. + * The suggested method of obtaining a WatchDog is via the static factory method, since it will return you the correct + * watchdog for the current thread. However, we do not prevent you from creating your own watchdogs either. * <p> - * If you create a WatchDog for a Thread, the WatchDog will figure out when - * the Thread is dead, and will stop itself accordingly. However, this object - * is not automatically released, so you might want to check it out after a while. + * If you create a WatchDog for a Thread, the WatchDog will figure out when the Thread is dead, and will stop itself + * accordingly. However, this object is not automatically released, so you might want to check it out after a while. * * @since 2.4.92 */ public final class WatchDog { private Watchable m_watchable; - private Stack< State > m_stateStack = new Stack< State >(); + private final Stack< State > m_stateStack = new Stack<>(); private boolean m_enabled = true; - private WikiEngine m_engine; + private Engine m_engine; private static final Logger log = Logger.getLogger( WatchDog.class ); - private static Map< Integer, WeakReference< WatchDog > > c_kennel = new ConcurrentHashMap< Integer, WeakReference< WatchDog > >(); - + private static Map< Integer, WeakReference< WatchDog > > c_kennel = new ConcurrentHashMap<>(); private static WikiBackgroundThread c_watcherThread; /** - * Returns the current watchdog for the current thread. This - * is the preferred method of getting you a Watchdog, since it - * keeps an internal list of Watchdogs for you so that there - * won't be more than one watchdog per thread. + * Returns the current watchdog for the current thread. This is the preferred method of getting you a Watchdog, since it + * keeps an internal list of Watchdogs for you so that there won't be more than one watchdog per thread. * * @param engine The WikiEngine to which the Watchdog should be bonded to. * @return A usable WatchDog object. */ - public static WatchDog getCurrentWatchDog( WikiEngine engine ) { - Thread t = Thread.currentThread(); - WatchDog wd = null; + public static WatchDog getCurrentWatchDog( final Engine engine ) { + final Thread t = Thread.currentThread(); WeakReference< WatchDog > w = c_kennel.get( t.hashCode() ); - + WatchDog wd = null; if( w != null ) { wd = w.get(); } if( w == null || wd == null ) { wd = new WatchDog( engine, t ); - w = new WeakReference< WatchDog >( wd ); - + w = new WeakReference<>( wd ); c_kennel.put( t.hashCode(), w ); } @@ -92,14 +84,14 @@ public final class WatchDog { /** * Creates a new WatchDog for a Watchable. * - * @param engine The WikiEngine. - * @param watch A Watchable object. + * @param engine The Engine. + * @param watch A Watchable object. */ - public WatchDog( WikiEngine engine, Watchable watch ) { + public WatchDog( final Engine engine, final Watchable watch ) { m_engine = engine; m_watchable = watch; - synchronized( this.getClass() ) { + synchronized( WatchDog.class ) { if( c_watcherThread == null ) { c_watcherThread = new WatchDogThread( engine ); c_watcherThread.start(); @@ -113,30 +105,24 @@ public final class WatchDog { * @param engine The WikiEngine * @param thread A Thread for watching. */ - public WatchDog(WikiEngine engine, Thread thread) { - this( engine, new ThreadWrapper(thread) ); + public WatchDog( final Engine engine, final Thread thread ) { + this( engine, new ThreadWrapper( thread ) ); } /** * Hopefully finalizes this properly. This is rather untested for now... */ private static void scrub() { - // - // During finalization, the object may already be cleared (depending - // on the finalization order). Therefore, it's possible that this - // method is called from another thread after the WatchDog itself - // has been cleared. - // + // During finalization, the object may already be cleared (depending on the finalization order). Therefore, it's + // possible that this method is called from another thread after the WatchDog itself has been cleared. if( c_kennel == null ) { return; } - for( Map.Entry< Integer, WeakReference< WatchDog > > e : c_kennel.entrySet() ) { - WeakReference< WatchDog > w = e.getValue(); + for( final Map.Entry< Integer, WeakReference< WatchDog > > e : c_kennel.entrySet() ) { + final WeakReference< WatchDog > w = e.getValue(); - // // Remove expired as well - // if( w.get() == null ) { c_kennel.remove( e.getKey() ); scrub(); @@ -149,7 +135,7 @@ public final class WatchDog { * Can be used to enable the WatchDog. Will cause a new Thread to be created, if none was existing previously. */ public void enable() { - synchronized( this.getClass() ) { + synchronized( WatchDog.class ) { if( !m_enabled ) { m_enabled = true; c_watcherThread = new WatchDogThread( m_engine ); @@ -162,7 +148,7 @@ public final class WatchDog { * Is used to disable a WatchDog. The watchdog thread is shut down and resources released. */ public void disable() { - synchronized( this.getClass() ) { + synchronized( WatchDog.class ) { if( m_enabled ) { m_enabled = false; c_watcherThread.shutdown(); @@ -177,7 +163,7 @@ public final class WatchDog { * * @param state A free-form string description of your state. */ - public void enterState( String state ) { + public void enterState( final String state ) { enterState( state, Integer.MAX_VALUE ); } @@ -197,14 +183,14 @@ public final class WatchDog { * @param state A free-form string description of the state * @param expectedCompletionTime The timeout in seconds. */ - public void enterState( String state, int expectedCompletionTime ) { + public void enterState( final String state, final int expectedCompletionTime ) { if( log.isDebugEnabled() ){ log.debug( m_watchable.getName() + ": Entering state " + state + ", expected completion in " + expectedCompletionTime + " s"); } synchronized( m_stateStack ) { - State st = new State( state, expectedCompletionTime ); + final State st = new State( state, expectedCompletionTime ); m_stateStack.push( st ); } } @@ -223,10 +209,10 @@ public final class WatchDog { * * @param state The state you wish to exit. */ - public void exitState( String state ) { + public void exitState( final String state ) { if( !m_stateStack.empty() ) { synchronized( m_stateStack ) { - State st = m_stateStack.peek(); + final State st = m_stateStack.peek(); if( state == null || st.getState().equals( state ) ) { m_stateStack.pop(); @@ -249,7 +235,7 @@ public final class WatchDog { * @return {@code true} if not empty, {@code false} otherwise. */ public boolean isStateStackNotEmpty() { - return m_stateStack != null && !m_stateStack.isEmpty(); + return !m_stateStack.isEmpty(); } /** @@ -268,8 +254,8 @@ public final class WatchDog { synchronized( m_stateStack ) { if( !m_stateStack.empty() ) { - State st = m_stateStack.peek(); - long now = System.currentTimeMillis(); + final State st = m_stateStack.peek(); + final long now = System.currentTimeMillis(); if( now > st.getExpiryTime() ) { log.info( "Watchable '" + m_watchable.getName() + "' exceeded timeout in state '" + st.getState() + @@ -277,7 +263,7 @@ public final class WatchDog { ( log.isDebugEnabled() ? "" : "Enable DEBUG-level logging to see stack traces." ) ); dumpStackTraceForWatchable(); - m_watchable.timeoutExceeded(st.getState()); + m_watchable.timeoutExceeded( st.getState() ); } } else { log.warn( "Stack for " + m_watchable.getName() + " is empty!" ); @@ -293,22 +279,22 @@ public final class WatchDog { return; } - Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces(); - Set<Thread> threads = stackTraces.keySet(); - Iterator<Thread> threadIterator = threads.iterator(); - StringBuilder stacktrace = new StringBuilder(); + final Map< Thread, StackTraceElement[] > stackTraces = Thread.getAllStackTraces(); + final Set< Thread > threads = stackTraces.keySet(); + final Iterator< Thread > threadIterator = threads.iterator(); + final StringBuilder stacktrace = new StringBuilder(); while ( threadIterator.hasNext() ) { - Thread t = threadIterator.next(); + final Thread t = threadIterator.next(); if( t.getName().equals( m_watchable.getName() ) ) { if( t.getName().equals( m_watchable.getName() ) ) { - stacktrace.append( "dumping stacktrace for too long running thread : " + t ); + stacktrace.append( "dumping stacktrace for too long running thread : " ).append( t ); } else { - stacktrace.append( "dumping stacktrace for other running thread : " + t ); + stacktrace.append( "dumping stacktrace for other running thread : " ).append( t ); } - StackTraceElement[] ste = stackTraces.get( t ); - for( int i = 0; i < ste.length; i++ ) { - stacktrace.append( "\n" + ste[i] ); + final StackTraceElement[] ste = stackTraces.get( t ); + for( final StackTraceElement stackTraceElement : ste ) { + stacktrace.append( "\n" ).append( stackTraceElement ); } } } @@ -321,12 +307,13 @@ public final class WatchDog { * * @return Random ramblings. */ + @Override public String toString() { synchronized( m_stateStack ) { String state = "Idle"; if( !m_stateStack.empty() ) { - State st = m_stateStack.peek(); + final State st = m_stateStack.peek(); state = st.getState(); } return "WatchDog state=" + state; @@ -340,33 +327,34 @@ public final class WatchDog { /** How often the watchdog thread should wake up (in seconds) */ private static final int CHECK_INTERVAL = 30; - public WatchDogThread( WikiEngine engine ) { + public WatchDogThread( final Engine engine ) { super( engine, CHECK_INTERVAL ); setName( "WatchDog for '" + engine.getApplicationName() + "'" ); } + @Override public void startupTask() { } + @Override public void shutdownTask() { WatchDog.scrub(); } /** - * Checks if the watchable is alive, and if it is, checks if - * the stack is finished. + * Checks if the watchable is alive, and if it is, checks if the stack is finished. * - * If the watchable has been deleted in the mean time, will - * simply shut down itself. + * If the watchable has been deleted in the mean time, will simply shut down itself. */ - public void backgroundTask() throws Exception { + @Override + public void backgroundTask() { if( c_kennel == null ) { return; } - for( Map.Entry< Integer, WeakReference< WatchDog > > entry : c_kennel.entrySet() ) { - WeakReference< WatchDog > wr = entry.getValue(); - WatchDog w = wr.get(); + for( final Map.Entry< Integer, WeakReference< WatchDog > > entry : c_kennel.entrySet() ) { + final WeakReference< WatchDog > wr = entry.getValue(); + final WatchDog w = wr.get(); if( w != null ) { if( w.isWatchableAlive() && w.isStateStackNotEmpty() ) { w.check(); @@ -391,10 +379,10 @@ public final class WatchDog { protected long m_enterTime; protected long m_expiryTime; - protected State( String state, int expiry ) { + protected State( final String state, final int expiry ) { m_state = state; m_enterTime = System.currentTimeMillis(); - m_expiryTime = m_enterTime + (expiry * 1000L); + m_expiryTime = m_enterTime + ( expiry * 1_000L ); } protected String getState() { @@ -404,6 +392,7 @@ public final class WatchDog { protected long getExpiryTime() { return m_expiryTime; } + } /** @@ -412,18 +401,21 @@ public final class WatchDog { private static class ThreadWrapper implements Watchable { private Thread m_thread; - public ThreadWrapper( Thread thread ) { + public ThreadWrapper( final Thread thread ) { m_thread = thread; } - public void timeoutExceeded( String state ) { + @Override + public void timeoutExceeded( final String state ) { // TODO: Figure out something sane to do here. } + @Override public String getName() { return m_thread.getName(); } + @Override public boolean isAlive() { return m_thread.isAlive(); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiBackgroundThread.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiBackgroundThread.java index 18e48e3..8bda0fa 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/WikiBackgroundThread.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiBackgroundThread.java @@ -19,6 +19,7 @@ package org.apache.wiki; import org.apache.log4j.Logger; +import org.apache.wiki.api.core.Engine; import org.apache.wiki.event.WikiEngineEvent; import org.apache.wiki.event.WikiEvent; import org.apache.wiki.event.WikiEventListener; @@ -34,7 +35,7 @@ public abstract class WikiBackgroundThread extends Thread implements WikiEventLi private static final Logger LOG = Logger.getLogger( WikiBackgroundThread.class ); private volatile boolean m_killMe = false; - private final WikiEngine m_engine; + private final Engine m_engine; private final int m_interval; private static final long POLLING_INTERVAL = 1_000L; @@ -46,7 +47,7 @@ public abstract class WikiBackgroundThread extends Thread implements WikiEventLi * @param sleepInterval the interval between invocations of * the thread's {@link Thread#run()} method, in seconds */ - public WikiBackgroundThread( final WikiEngine engine, final int sleepInterval ) { + public WikiBackgroundThread( final Engine engine, final int sleepInterval ) { super(); m_engine = engine; m_interval = sleepInterval; @@ -60,7 +61,7 @@ public abstract class WikiBackgroundThread extends Thread implements WikiEventLi * @param event {@inheritDoc} * @see org.apache.wiki.event.WikiEventListener#actionPerformed(org.apache.wiki.event.WikiEvent) */ - public final void actionPerformed( final WikiEvent event ) { + @Override public final void actionPerformed( final WikiEvent event ) { if ( event instanceof WikiEngineEvent ) { if ( event.getType() == WikiEngineEvent.SHUTDOWN ) { LOG.warn( "Detected wiki engine shutdown: killing " + getName() + "." ); @@ -81,7 +82,7 @@ public abstract class WikiBackgroundThread extends Thread implements WikiEventLi * * @return the wiki engine */ - public WikiEngine getEngine() { + public Engine getEngine() { return m_engine; } @@ -103,7 +104,7 @@ public abstract class WikiBackgroundThread extends Thread implements WikiEventLi * * @see java.lang.Thread#run() */ - public final void run() { + @Override public final void run() { try { // Perform the initial startup task final String name = getName(); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/api/core/Engine.java b/jspwiki-main/src/main/java/org/apache/wiki/api/core/Engine.java index f88eb88..bab095c 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/api/core/Engine.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/api/core/Engine.java @@ -19,7 +19,6 @@ package org.apache.wiki.api.core; import org.apache.log4j.Logger; -import org.apache.wiki.WatchDog; import org.apache.wiki.api.exceptions.ProviderException; import org.apache.wiki.event.WikiEventListener; @@ -381,14 +380,6 @@ public interface Engine { < T > T removeAttribute( String key ); /** - * Returns a WatchDog for current thread. - * - * @return The current thread WatchDog. - * @since 2.4.92 - */ - WatchDog getCurrentWatchDog(); - - /** * Signals that the Engine will be shut down by the servlet container. */ void shutdown(); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSThread.java b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSThread.java index 4d0d303..9707314 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSThread.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/rss/RSSThread.java @@ -19,6 +19,12 @@ package org.apache.wiki.rss; +import org.apache.log4j.Logger; +import org.apache.wiki.WatchDog; +import org.apache.wiki.WikiBackgroundThread; +import org.apache.wiki.WikiEngine; +import org.apache.wiki.util.FileUtil; + import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; @@ -28,12 +34,6 @@ import java.io.Reader; import java.io.StringReader; import java.io.Writer; -import org.apache.log4j.Logger; -import org.apache.wiki.WatchDog; -import org.apache.wiki.WikiBackgroundThread; -import org.apache.wiki.WikiEngine; -import org.apache.wiki.util.FileUtil; - /** * Runs the RSS generation thread. * FIXME: MUST be somewhere else, this is not a good place. @@ -54,7 +54,7 @@ public class RSSThread extends WikiBackgroundThread * @param rssFile A File to write the RSS data to. * @param rssInterval How often the RSS should be generated. */ - public RSSThread( WikiEngine engine, File rssFile, int rssInterval ) + public RSSThread( final WikiEngine engine, final File rssFile, final int rssInterval ) { super( engine, rssInterval ); m_generator = engine.getRSSGenerator(); @@ -68,9 +68,8 @@ public class RSSThread extends WikiBackgroundThread * {@inheritDoc} */ @Override - public void startupTask() throws Exception - { - m_watchdog = getEngine().getCurrentWatchDog(); + public void startupTask() { + m_watchdog = WatchDog.getCurrentWatchDog( getEngine() ); } /** @@ -99,14 +98,14 @@ public class RSSThread extends WikiBackgroundThread // log.debug("Regenerating RSS feed to "+m_rssFile); - String feed = m_generator.generate(); + final String feed = m_generator.generate(); in = new StringReader(feed); out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( m_rssFile ), "UTF-8") ); FileUtil.copyContents( in, out ); } - catch( IOException e ) + catch( final IOException e ) { log.error("Cannot generate RSS feed to "+m_rssFile.getAbsolutePath(), e ); m_generator.setEnabled( false ); @@ -118,7 +117,7 @@ public class RSSThread extends WikiBackgroundThread if( in != null ) in.close(); if( out != null ) out.close(); } - catch( IOException e ) + catch( final IOException e ) { log.fatal("Could not close I/O for RSS", e ); m_generator.setEnabled( false ); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/search/LuceneSearchProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/search/LuceneSearchProvider.java index 1cfcc30..c6ab12a 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/search/LuceneSearchProvider.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/search/LuceneSearchProvider.java @@ -564,7 +564,7 @@ public class LuceneSearchProvider implements SearchProvider { @Override public void startupTask() throws Exception { - m_watchdog = getEngine().getCurrentWatchDog(); + m_watchdog = WatchDog.getCurrentWatchDog( getEngine() ); // Sleep initially... try { diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiJSPFilter.java b/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiJSPFilter.java index d7d1fe2..03e16cd 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiJSPFilter.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/WikiJSPFilter.java @@ -77,15 +77,15 @@ public class WikiJSPFilter extends WikiServletFilter { private boolean useEncoding; /** {@inheritDoc} */ - public void init( final FilterConfig config ) throws ServletException { + @Override public void init( final FilterConfig config ) throws ServletException { super.init( config ); m_wiki_encoding = m_engine.getWikiProperties().getProperty(WikiEngine.PROP_ENCODING); useEncoding = !( Boolean.valueOf( m_engine.getWikiProperties().getProperty( WikiEngine.PROP_NO_FILTER_ENCODING, "false" ).trim() ).booleanValue() ); } - public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain ) throws ServletException, IOException { - final WatchDog w = m_engine.getCurrentWatchDog(); + @Override public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain ) throws ServletException, IOException { + final WatchDog w = WatchDog.getCurrentWatchDog( m_engine ); try { NDC.push( m_engine.getApplicationName()+":"+((HttpServletRequest)request).getRequestURI() ); w.enterState("Filtering for URL "+((HttpServletRequest)request).getRequestURI(), 90 ); @@ -229,15 +229,15 @@ public class WikiJSPFilter extends WikiServletFilter { /** * Returns a writer for output; this wraps the internal buffer into a PrintWriter. */ - public PrintWriter getWriter() { + @Override public PrintWriter getWriter() { return m_writer; } - public ServletOutputStream getOutputStream() { + @Override public ServletOutputStream getOutputStream() { return m_servletOut; } - public void flushBuffer() throws IOException { + @Override public void flushBuffer() throws IOException { m_writer.flush(); super.flushBuffer(); } @@ -272,7 +272,7 @@ public class WikiJSPFilter extends WikiServletFilter { } /** Returns whatever was written so far into the Writer. */ - public String toString() { + @Override public String toString() { try { flushBuffer(); } catch( final IOException e ) {
