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 4e6eed4c72c5fcde4d151a4a35c540b2131acc18 Author: juanpablo <[email protected]> AuthorDate: Sun Mar 15 18:42:42 2020 +0100 add suggested fixes and format by IntelliJ --- .../wiki/providers/BasicAttachmentProvider.java | 96 +++++------- .../wiki/providers/CachingAttachmentProvider.java | 174 ++++++++------------- 2 files changed, 108 insertions(+), 162 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/providers/BasicAttachmentProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/providers/BasicAttachmentProvider.java index ad698d3..7447374 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/providers/BasicAttachmentProvider.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/providers/BasicAttachmentProvider.java @@ -148,7 +148,6 @@ public class BasicAttachmentProvider implements WikiAttachmentProvider { wikipage = mangleName( wikipage ); final File f = new File( m_storageDir, wikipage + DIR_EXTENSION ); - if( f.exists() && !f.isDirectory() ) { throw new ProviderException( "Storage dir '" + f.getAbsolutePath() + "' is not a directory!" ); } @@ -189,27 +188,21 @@ public class BasicAttachmentProvider implements WikiAttachmentProvider { } /** - * Goes through the repository and decides which version is - * the newest one in that directory. + * Goes through the repository and decides which version is the newest one in that directory. * - * @return Latest version number in the repository, or 0, if - * there is no page in the repository. + * @return Latest version number in the repository, or 0, if there is no page in the repository. */ private int findLatestVersion( final Attachment att ) throws ProviderException { final File attDir = findAttachmentDir( att ); - - // log.debug("Finding pages in "+attDir.getAbsolutePath()); final String[] pages = attDir.list( new AttachmentVersionFilter() ); - if( pages == null ) { return 0; // No such thing found. } int version = 0; - for( int i = 0; i < pages.length; i++ ) { - // log.debug("Checking: "+pages[i]); - final int cutpoint = pages[ i ].indexOf( '.' ); - final String pageNum = ( cutpoint > 0 ) ? pages[ i ].substring( 0, cutpoint ) : pages[ i ]; + for( final String page : pages ) { + final int cutpoint = page.indexOf( '.' ); + final String pageNum = ( cutpoint > 0 ) ? page.substring( 0, cutpoint ) : page; try { final int res = Integer.parseInt( pageNum ); @@ -363,37 +356,35 @@ public class BasicAttachmentProvider implements WikiAttachmentProvider { public List< Attachment > listAttachments( final Page page ) throws ProviderException { final List< Attachment > result = new ArrayList<>(); final File dir = findPageDir( page.getName() ); - if( dir != null ) { - final String[] attachments = dir.list(); - if( attachments != null ) { - // We now have a list of all potential attachments in the directory. - for( int i = 0; i < attachments.length; i++ ) { - final File f = new File( dir, attachments[i] ); - if( f.isDirectory() ) { - String attachmentName = unmangleName( attachments[i] ); - - // Is it a new-stylea attachment directory? If yes, we'll just deduce the name. If not, however, - // we'll check if there's a suitable property file in the directory. - if( attachmentName.endsWith( ATTDIR_EXTENSION ) ) { - attachmentName = attachmentName.substring( 0, attachmentName.length() - ATTDIR_EXTENSION.length() ); - } else { - final File propFile = new File( f, PROPERTY_FILE ); - if( !propFile.exists() ) { - // This is not obviously a JSPWiki attachment, so let's just skip it. - continue; - } - } - - final Attachment att = getAttachmentInfo( page, attachmentName, WikiProvider.LATEST_VERSION ); - // Sanity check - shouldn't really be happening, unless you mess with the repository directly. - if( att == null ) { - throw new ProviderException("Attachment disappeared while reading information:"+ - " if you did not touch the repository, there is a serious bug somewhere. "+ - "Attachment = " + attachments[ i ] + ", decoded = " + attachmentName ); + final String[] attachments = dir.list(); + if( attachments != null ) { + // We now have a list of all potential attachments in the directory. + for( final String attachment : attachments ) { + final File f = new File( dir, attachment ); + if( f.isDirectory() ) { + String attachmentName = unmangleName( attachment ); + + // Is it a new-stylea attachment directory? If yes, we'll just deduce the name. If not, however, + // we'll check if there's a suitable property file in the directory. + if( attachmentName.endsWith( ATTDIR_EXTENSION ) ) { + attachmentName = attachmentName.substring( 0, attachmentName.length() - ATTDIR_EXTENSION.length() ); + } else { + final File propFile = new File( f, PROPERTY_FILE ); + if( !propFile.exists() ) { + // This is not obviously a JSPWiki attachment, so let's just skip it. + continue; } + } - result.add( att ); + final Attachment att = getAttachmentInfo( page, attachmentName, WikiProvider.LATEST_VERSION ); + // Sanity check - shouldn't really be happening, unless you mess with the repository directly. + if( att == null ) { + throw new ProviderException( "Attachment disappeared while reading information:" + + " if you did not touch the repository, there is a serious bug somewhere. " + "Attachment = " + attachment + + ", decoded = " + attachmentName ); } + + result.add( att ); } } } @@ -405,8 +396,7 @@ public class BasicAttachmentProvider implements WikiAttachmentProvider { * {@inheritDoc} */ @Override - public Collection< Attachment > findAttachments( final QueryItem[] query ) - { + public Collection< Attachment > findAttachments( final QueryItem[] query ) { return new ArrayList<>(); } @@ -461,26 +451,25 @@ public class BasicAttachmentProvider implements WikiAttachmentProvider { att.setVersion( version ); // Should attachment be cachable by the client (browser)? - if (m_disableCache != null) { - final Matcher matcher = m_disableCache.matcher(name); - if (matcher.matches()) { - att.setCacheable(false); + if( m_disableCache != null ) { + final Matcher matcher = m_disableCache.matcher( name ); + if( matcher.matches() ) { + att.setCacheable( false ); } } // System.out.println("Fetching info on version "+version); try { - final Properties props = getPageProperties(att); + final Properties props = getPageProperties( att ); att.setAuthor( props.getProperty( version+".author" ) ); final String changeNote = props.getProperty( version+".changenote" ); if( changeNote != null ) { - att.setAttribute(WikiPage.CHANGENOTE, changeNote); + att.setAttribute( WikiPage.CHANGENOTE, changeNote ); } final File f = findFile( dir, att ); - att.setSize( f.length() ); - att.setLastModified( new Date(f.lastModified()) ); + att.setLastModified( new Date( f.lastModified() ) ); } catch( final FileNotFoundException e ) { log.error( "Can't get attachment properties for " + att, e ); return null; @@ -499,10 +488,8 @@ public class BasicAttachmentProvider implements WikiAttachmentProvider { @Override public List< Attachment > getVersionHistory( final Attachment att ) { final ArrayList< Attachment > list = new ArrayList<>(); - try { final int latest = findLatestVersion( att ); - for( int i = latest; i >= 1; i-- ) { final Attachment a = getAttachmentInfo( new WikiPage( m_engine, att.getParentName() ), att.getFileName(), i ); @@ -533,9 +520,8 @@ public class BasicAttachmentProvider implements WikiAttachmentProvider { public void deleteAttachment( final Attachment att ) throws ProviderException { final File dir = findAttachmentDir( att ); final String[] files = dir.list(); - - for( int i = 0; i < files.length; i++ ) { - final File file = new File( dir.getAbsolutePath() + "/" + files[ i ] ); + for( final String s : files ) { + final File file = new File( dir.getAbsolutePath() + "/" + s ); file.delete(); } dir.delete(); 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 6fe99b4..c29a3ac 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 @@ -38,21 +38,17 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Date; -import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import java.util.Properties; /** - * Provides a caching attachment provider. This class rests on top of a - * real provider class and provides a cache to speed things up. Only the - * Attachment objects are cached; the actual attachment contents are - * fetched always from the provider. + * Provides a caching attachment provider. This class rests on top of a real provider class and provides a cache to speed things up. + * Only the Attachment objects are cached; the actual attachment contents are fetched always from the provider. * * @since 2.1.64. */ - // EntryRefreshPolicy for that. public class CachingAttachmentProvider implements WikiAttachmentProvider { @@ -65,11 +61,11 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { /** Default cache capacity for now. */ public static final int m_capacity = 1000; - /** - * The cache contains Collection objects which contain Attachment objects. - * The key is the parent wiki page name (String). - */ + /** The cache contains Collection objects which contain Attachment objects. The key is the parent wiki page name (String). */ private Cache m_cache; + + /** Name of the attachment cache. */ + public static final String ATTCACHE_NAME = "jspwiki.attachmentsCache"; /** Name of the attachment cache. */ public static final String ATTCOLLCACHE_NAME = "jspwiki.attachmentCollectionsCache"; @@ -78,8 +74,6 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { * This provides for quickly giving recently changed attachments (for the RecentChanges plugin) */ private Cache m_attCache; - /** Name of the attachment cache. */ - public static final String ATTCACHE_NAME = "jspwiki.attachmentsCache"; private long m_cacheMisses = 0; private long m_cacheHits = 0; @@ -87,8 +81,6 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { /** The extension to append to directory names to denote an attachment directory. */ public static final String DIR_EXTENSION = "-att"; - /** Property that supplies the directory used to store attachments. */ - public static final String PROP_STORAGEDIR = "jspwiki.basicAttachmentProvider.storageDir"; private boolean m_gotall = false; @@ -97,25 +89,24 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { */ @Override public void initialize( final Engine engine, final Properties properties ) throws NoRequiredPropertyException, IOException { - log.info("Initing CachingAttachmentProvider"); - + log.info( "Initing CachingAttachmentProvider" ); final String attCollCacheName = engine.getApplicationName() + "." + ATTCOLLCACHE_NAME; - if (m_cacheManager.cacheExists(attCollCacheName)) { - m_cache = m_cacheManager.getCache(attCollCacheName); + if( m_cacheManager.cacheExists( attCollCacheName ) ) { + m_cache = m_cacheManager.getCache( attCollCacheName ); } else { - m_cache = new Cache(attCollCacheName, m_capacity, false, false, 0, 0); - m_cacheManager.addCache(m_cache); + m_cache = new Cache( attCollCacheName, m_capacity, false, false, 0, 0 ); + m_cacheManager.addCache( m_cache ); } // // cache for the individual Attachment objects, attachment name is key, the Attachment object is the cached object // final String attCacheName = engine.getApplicationName() + "." + ATTCACHE_NAME; - if (m_cacheManager.cacheExists(attCacheName)) { - m_attCache = m_cacheManager.getCache(attCacheName); + if( m_cacheManager.cacheExists( attCacheName ) ) { + m_attCache = m_cacheManager.getCache( attCacheName ); } else { - m_attCache = new Cache(attCacheName, m_capacity, false, false, 0, 0); - m_cacheManager.addCache(m_attCache); + m_attCache = new Cache( attCacheName, m_capacity, false, false, 0, 0 ); + m_cacheManager.addCache( m_attCache ); } // // Find and initialize real provider. @@ -127,31 +118,22 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { throw new NoRequiredPropertyException( e.getMessage(), AttachmentManager.PROP_PROVIDER ); } - try - { - final Class<?> providerclass = ClassUtil.findClass( "org.apache.wiki.providers", classname); - - m_provider = (WikiAttachmentProvider)providerclass.newInstance(); + try { + final Class< ? > providerclass = ClassUtil.findClass( "org.apache.wiki.providers", classname ); + m_provider = ( WikiAttachmentProvider )providerclass.newInstance(); - log.debug("Initializing real provider class "+m_provider); + log.debug( "Initializing real provider class " + m_provider ); m_provider.initialize( engine, properties ); + } catch( final ClassNotFoundException e ) { + log.error( "Unable to locate provider class " + classname, e ); + throw new IllegalArgumentException( "no provider class", e ); + } catch( final InstantiationException e ) { + log.error( "Unable to create provider class " + classname, e ); + throw new IllegalArgumentException( "faulty provider class", e ); + } catch( final IllegalAccessException e ) { + log.error( "Illegal access to provider class " + classname, e ); + throw new IllegalArgumentException( "illegal provider class", e ); } - catch( final ClassNotFoundException e ) - { - log.error("Unable to locate provider class "+classname,e); - throw new IllegalArgumentException("no provider class", e); - } - catch( final InstantiationException e ) - { - log.error("Unable to create provider class "+classname,e); - throw new IllegalArgumentException("faulty provider class", e); - } - catch( final IllegalAccessException e ) - { - log.error("Illegal access to provider class "+classname,e); - throw new IllegalArgumentException("illegal provider class", e); - } - } /** @@ -195,13 +177,8 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { return refresh(page); } - private <T> List<T> cloneCollection( final Collection<T> c ) - { - final ArrayList<T> list = new ArrayList<>(); - - list.addAll( c ); - - return list; + private < T > List< T > cloneCollection( final Collection< T > c ) { + return new ArrayList<>( c ); } /** @@ -217,19 +194,16 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { * {@inheritDoc} */ @Override - public List<Attachment> listAllChanged( final Date timestamp) throws ProviderException { - List< Attachment > all = null; - // + public List<Attachment> listAllChanged( final Date timestamp ) throws ProviderException { + final List< Attachment > all; // we do a one-time build up of the cache, after this the cache is updated for every attachment add/delete - if (m_gotall == false) { + if ( !m_gotall ) { all = m_provider.listAllChanged(timestamp); // Put all pages in the cache : - synchronized (this) { - for ( final Iterator< Attachment > i = all.iterator(); i.hasNext(); ) { - final Attachment att = i.next(); - m_attCache.put(new Element(att.getName(), att)); + for( final Attachment att : all ) { + m_attCache.put( new Element( att.getName(), att ) ); } m_gotall = true; } @@ -269,11 +243,9 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { * * @return The newly fetched object from the provider. */ - private List<Attachment> refresh( final Page page ) throws ProviderException - { - final List<Attachment> c = m_provider.listAttachments( page ); - m_cache.put(new Element(page.getName(), c)); - + private List< Attachment > refresh( final Page page ) throws ProviderException { + final List< Attachment > c = m_provider.listAttachments( page ); + m_cache.put( new Element( page.getName(), c ) ); return c; } @@ -283,34 +255,32 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { @SuppressWarnings("unchecked") @Override public Attachment getAttachmentInfo( final Page page, final String name, final int version) throws ProviderException { - if (log.isDebugEnabled()) { - log.debug("Getting attachments for " + page + ", name=" + name + ", version=" + version); + if( log.isDebugEnabled() ) { + log.debug( "Getting attachments for " + page + ", name=" + name + ", version=" + version ); } - // // We don't cache previous versions - // - if (version != WikiProvider.LATEST_VERSION) { - log.debug("...we don't cache old versions"); - return m_provider.getAttachmentInfo(page, name, version); + if( version != WikiProvider.LATEST_VERSION ) { + log.debug( "...we don't cache old versions" ); + return m_provider.getAttachmentInfo( page, name, version ); } - Collection<Attachment> c = null; - final Element element = m_cache.get(page.getName()); + final Collection< Attachment > c; + final Element element = m_cache.get( page.getName() ); - if (element == null) { - log.debug(page.getName() + " wasn't in the cache"); - c = refresh(page); + if( element == null ) { + log.debug( page.getName() + " wasn't in the cache" ); + c = refresh( page ); - if (c == null) { + if( c == null ) { return null; // No such attachment } } else { - log.debug(page.getName() + " FOUND in the cache"); - c = (Collection<Attachment>) element.getObjectValue(); + log.debug( page.getName() + " FOUND in the cache" ); + c = ( Collection< Attachment > )element.getObjectValue(); } - return findAttachmentFromCollection(c, name); + return findAttachmentFromCollection( c, name ); } /** @@ -326,10 +296,9 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { * {@inheritDoc} */ @Override - public void deleteVersion( final Attachment att ) throws ProviderException - { + public void deleteVersion( final Attachment att ) throws ProviderException { // This isn't strictly speaking correct, but it does not really matter - m_cache.remove(att.getParentName()); + m_cache.remove( att.getParentName() ); m_provider.deleteVersion( att ); } @@ -337,10 +306,9 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { * {@inheritDoc} */ @Override - public void deleteAttachment( final Attachment att ) throws ProviderException - { - m_cache.remove(att.getParentName()); - m_attCache.remove(att.getName()); + public void deleteAttachment( final Attachment att ) throws ProviderException { + m_cache.remove( att.getParentName() ); + m_attCache.remove( att.getName() ); m_provider.deleteAttachment( att ); } @@ -351,11 +319,10 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { * @return A plain string with all the above mentioned values. */ @Override - public synchronized String getProviderInfo() - { - return "Real provider: "+m_provider.getClass().getName()+ - ". Cache misses: "+m_cacheMisses+ - ". Cache hits: "+m_cacheHits; + public synchronized String getProviderInfo() { + return "Real provider: " + m_provider.getClass().getName() + + ". Cache misses: " + m_cacheMisses + + ". Cache hits: " + m_cacheHits; } /** @@ -363,8 +330,7 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { * * @return The real provider underneath this one. */ - public WikiAttachmentProvider getRealProvider() - { + public WikiAttachmentProvider getRealProvider() { return m_provider; } @@ -372,24 +338,18 @@ public class CachingAttachmentProvider implements WikiAttachmentProvider { * {@inheritDoc} */ @Override - public void moveAttachmentsForPage( final String oldParent, final String newParent ) throws ProviderException - { + public void moveAttachmentsForPage( final String oldParent, final String newParent ) throws ProviderException { m_provider.moveAttachmentsForPage(oldParent, newParent); m_cache.remove(newParent); m_cache.remove(oldParent); - // - // This is a kludge to make sure that the pages are removed - // from the other cache as well. - // + // This is a kludge to make sure that the pages are removed from the other cache as well. final String checkName = oldParent + "/"; @SuppressWarnings("unchecked") final List< String > names = m_cache.getKeysWithExpiryCheck(); - for( final String name : names ) - { - if( name.startsWith( checkName ) ) - { - m_attCache.remove(name); + for( final String name : names ) { + if( name.startsWith( checkName ) ) { + m_attCache.remove( name ); } } }
