This is an automated email from the ASF dual-hosted git repository.
martin_s pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/archiva-components.git
The following commit(s) were added to refs/heads/master by this push:
new 6b54260 Refactoring ehcache module
6b54260 is described below
commit 6b5426052c1a3021e5087a6f46008f0d380ac1c3
Author: Martin Stockhammer <[email protected]>
AuthorDate: Mon Nov 18 22:59:51 2019 +0100
Refactoring ehcache module
---
pom.xml | 12 ++
.../components/cache/factory/CacheFactory.java | 4 +-
.../spring-cache-ehcache/pom.xml | 17 ++-
.../components/cache/ehcache/EhcacheCache.java | 164 ++++++++++++++-------
.../components/cache/ehcache/EhcacheCreator.java | 22 ++-
...s-cache.properties => archiva-cache.properties} | 0
.../src/test/resources/log4j2-test.xml | 36 +++++
...s-cache.properties => archiva-cache.properties} | 0
...s-cache.properties => archiva-cache.properties} | 0
9 files changed, 194 insertions(+), 61 deletions(-)
diff --git a/pom.xml b/pom.xml
index e202877..a0c52ba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -115,6 +115,18 @@
<version>${log4j.version}</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-slf4j-impl</artifactId>
+ <version>${log4j.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <version>${log4j.version}</version>
+ <scope>test</scope>
+ </dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
diff --git
a/spring-cache/spring-cache-api/src/main/java/org/apache/archiva/components/cache/factory/CacheFactory.java
b/spring-cache/spring-cache-api/src/main/java/org/apache/archiva/components/cache/factory/CacheFactory.java
index a79de17..dbca1a0 100644
---
a/spring-cache/spring-cache-api/src/main/java/org/apache/archiva/components/cache/factory/CacheFactory.java
+++
b/spring-cache/spring-cache-api/src/main/java/org/apache/archiva/components/cache/factory/CacheFactory.java
@@ -40,6 +40,8 @@ import java.util.Properties;
*/
public class CacheFactory
{
+ public static final String PROP_FILE = "META-INF/archiva-cache.properties";
+
static class CacheFactoryHolder
{
static CacheFactory instance = new CacheFactory( );
@@ -64,7 +66,7 @@ public class CacheFactory
classLoader = ClassLoader.getSystemClassLoader( );
}
- Enumeration cachePropResources = classLoader.getResources(
"META-INF/plexus-cache.properties" );
+ Enumeration cachePropResources = classLoader.getResources(
PROP_FILE );
if ( cachePropResources.hasMoreElements( ) )
{
diff --git a/spring-cache/spring-cache-providers/spring-cache-ehcache/pom.xml
b/spring-cache/spring-cache-providers/spring-cache-ehcache/pom.xml
index 620c2c2..9b14020 100644
--- a/spring-cache/spring-cache-providers/spring-cache-ehcache/pom.xml
+++ b/spring-cache/spring-cache-providers/spring-cache-ehcache/pom.xml
@@ -42,7 +42,7 @@
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
- <version>2.7.5</version>
+ <version>2.10.6</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
@@ -67,6 +67,21 @@
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-jcl</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-slf4j-impl</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git
a/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/java/org/apache/archiva/components/cache/ehcache/EhcacheCache.java
b/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/java/org/apache/archiva/components/cache/ehcache/EhcacheCache.java
index 4abeb44..7f3d74c 100644
---
a/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/java/org/apache/archiva/components/cache/ehcache/EhcacheCache.java
+++
b/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/java/org/apache/archiva/components/cache/ehcache/EhcacheCache.java
@@ -25,8 +25,11 @@ import net.sf.ehcache.Element;
import net.sf.ehcache.Status;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
+import net.sf.ehcache.config.ConfigurationFactory;
import net.sf.ehcache.config.DiskStoreConfiguration;
import net.sf.ehcache.config.MemoryUnit;
+import net.sf.ehcache.config.PersistenceConfiguration;
+import net.sf.ehcache.statistics.StatisticsGateway;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
import org.apache.archiva.components.cache.CacheStatistics;
import org.slf4j.Logger;
@@ -34,34 +37,53 @@ import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
+import java.nio.file.Files;
+import java.nio.file.Path;
/**
* EhcacheCache
* configuration document available <a
href="http://www.ehcache.org/documentation/configuration/index">EhcacheUserGuide</a>
*
+ * You can use the system property
<code>org.apache.archiva.ehcache.diskStore</code> to set the default disk store
path.
+ *
* @author <a href="mailto:[email protected]">Joakim Erdfelt</a>
*/
public class EhcacheCache<V, T>
implements org.apache.archiva.components.cache.Cache<V, T>
{
- private Logger log = LoggerFactory.getLogger( getClass( ) );
+ private static final String EHCACHE_DISK_STORE_PROPERTY =
"org.apache.archiva.ehcache.diskStore";
+
+ private Logger log = LoggerFactory.getLogger( EhcacheCache.class );
class Stats
implements CacheStatistics
{
+ private boolean useBaseLine = false;
+ private long hitCountBL = 0;
+ private long missCountBL = 0;
+ private long sizeBL = 0;
+ private long localHeapSizeInBytesBL = 0;
+
+ // No API for cache clear since 2.10. We use a baseline, if the cache
is cleared.
+ @Override
public void clear( )
{
- // TODO not supported anymore
- //ehcache.getStatistics().clearStatistics();
+ useBaseLine = true;
+ final StatisticsGateway cStats = ehcache.getStatistics( );
+ hitCountBL = cStats.cacheHitCount( );
+ missCountBL = cStats.cacheMissCount( );
+ sizeBL = cStats.getSize( );
+ localHeapSizeInBytesBL = cStats.getLocalHeapSizeInBytes( );
}
+ @Override
public double getCacheHitRate( )
{
- double hits = getCacheHits( );
- double miss = getCacheMiss( );
+ final double hits = getCacheHits( );
+ final double miss = getCacheMiss( );
- if ( ( hits == 0 ) && ( hits == 0 ) )
+ if ( ( hits < 0.1 ) && ( miss < 0.1 ) )
{
return 0.0;
}
@@ -69,26 +91,28 @@ public class EhcacheCache<V, T>
return hits / ( hits + miss );
}
+ @Override
public long getCacheHits( )
{
- return ehcache.getStatistics( ).cacheHitCount( );//.getCacheHits();
+ return useBaseLine ? ehcache.getStatistics( ).cacheHitCount( ) -
hitCountBL : ehcache.getStatistics( ).cacheHitCount( );
}
+ @Override
public long getCacheMiss( )
{
- return ehcache.getStatistics( ).cacheMissCount( );//
.getCacheMisses();
+ return useBaseLine ? ehcache.getStatistics( ).cacheMissCount( ) -
missCountBL : ehcache.getStatistics( ).cacheMissCount( );
}
+ @Override
public long getSize( )
{
- //
- return ehcache.getStatistics( ).getSize( );
+ return useBaseLine ? ehcache.getStatistics( ).getSize( ) - sizeBL
: ehcache.getStatistics( ).getSize( );
}
+ @Override
public long getInMemorySize( )
{
- return ehcache.getStatistics( ).getLocalHeapSizeInBytes( );
- //return ehcache.calculateInMemorySize();
+ return useBaseLine ? ehcache.getStatistics(
).getLocalHeapSizeInBytes( ) - localHeapSizeInBytesBL : ehcache.getStatistics(
).getLocalHeapSizeInBytes( );
}
}
@@ -105,7 +129,9 @@ public class EhcacheCache<V, T>
/**
* Location on disk for the ehcache store.
*/
- private String diskStorePath = System.getProperty( "java.io.tmpdir" ) +
"/ehcache";
+ private String diskStorePath = System.getProperties().containsKey(
EHCACHE_DISK_STORE_PROPERTY ) ?
+ System.getProperty( EHCACHE_DISK_STORE_PROPERTY ) :
+ System.getProperty( "java.io.tmpdir" ) + "/ehcache-archiva";
private boolean eternal = false;
@@ -146,17 +172,9 @@ public class EhcacheCache<V, T>
*/
private int maxElementsOnDisk;
- /**
- * @since 2.1
- */
- //private String persistenceStrategy =
PersistenceConfiguration.Strategy.LOCALTEMPSWAP.name();
+ private boolean statisticsEnabled = true;
- /**
- * @since 2.1
- */
- //private boolean synchronousWrites = false;
-
- private boolean statisticsEnabled = true;
+ private Path configurationFile = null;
private CacheManager cacheManager = null;//CacheManager.getInstance();
@@ -164,6 +182,7 @@ public class EhcacheCache<V, T>
private Stats stats;
+ @Override
public void clear( )
{
ehcache.removeAll( );
@@ -173,6 +192,12 @@ public class EhcacheCache<V, T>
@PostConstruct
public void initialize( )
{
+ // We are skipping the update check if not set explicitly
+ if ( !System.getProperties( ).containsKey(
"net.sf.ehcache.skipUpdateCheck" ) )
+ {
+ System.setProperty( "net.sf.ehcache.skipUpdateCheck", "true" );
+ }
+
stats = new Stats( );
boolean cacheManagerExists = CacheManager.getCacheManager( getName( )
) != null;
@@ -191,7 +216,14 @@ public class EhcacheCache<V, T>
}
else
{
- this.cacheManager = new CacheManager( new Configuration( ).name(
getName( ) ).diskStore(
+ Configuration configuration;
+ if (configurationFile != null && Files.exists( configurationFile)
&& Files.isReadable( configurationFile )) {
+ configuration = ConfigurationFactory.parseConfiguration(
configurationFile.toFile( ) );
+ } else
+ {
+ configuration = new Configuration( );
+ }
+ this.cacheManager = new CacheManager( configuration.name( getName(
) ).diskStore(
new DiskStoreConfiguration( ).path( getDiskStorePath( ) ) ) );
}
@@ -209,8 +241,7 @@ public class EhcacheCache<V, T>
ehcache = cacheManager.getCache( getName( ) );
}
}
-
- if ( !cacheExists )
+ else
{
CacheConfiguration cacheConfiguration =
new CacheConfiguration( ).name( getName( )
).memoryStoreEvictionPolicy(
@@ -218,8 +249,15 @@ public class EhcacheCache<V, T>
getTimeToLiveSeconds( ) ).timeToIdleSeconds(
getTimeToIdleSeconds( ) ).diskExpiryThreadIntervalSeconds(
getDiskExpiryThreadIntervalSeconds( ) ).overflowToOffHeap(
- isOverflowToOffHeap( ) ).maxEntriesLocalDisk(
getMaxElementsOnDisk( ) ).diskPersistent(
- isDiskPersistent( ) ).overflowToDisk( overflowToDisk );
+ isOverflowToOffHeap( ) ).maxEntriesLocalDisk(
getMaxElementsOnDisk( ) );
+
+ cacheConfiguration.addPersistence( new PersistenceConfiguration()
);
+ if (isDiskPersistent())
+ {
+ cacheConfiguration.getPersistenceConfiguration( ).setStrategy(
PersistenceConfiguration.Strategy.LOCALTEMPSWAP.name( ) );
+ } else {
+ cacheConfiguration.getPersistenceConfiguration( ).setStrategy(
PersistenceConfiguration.Strategy.NONE.name( ) );
+ }
if ( getMaxElementsInMemory( ) > 0 )
{
@@ -237,23 +275,20 @@ public class EhcacheCache<V, T>
}
ehcache = new Cache( cacheConfiguration );
-
cacheManager.addCache( ehcache );
- // TODO not supported anymore?
- //ehcache.setStatisticsEnabled( statisticsEnabled );
}
}
@PreDestroy
public void dispose( )
{
- if ( cacheManager.getStatus( ).equals( Status.STATUS_ALIVE ) )
+ if ( this.cacheManager.getStatus( ).equals( Status.STATUS_ALIVE ) )
{
log.info( "Disposing cache: {}", ehcache );
if ( this.ehcache != null )
{
- cacheManager.removeCache( this.ehcache.getName( ) );
- ehcache = null;
+ this.cacheManager.removeCache( this.ehcache.getName( ) );
+ this.ehcache = null;
}
}
else
@@ -262,6 +297,8 @@ public class EhcacheCache<V, T>
}
}
+ @SuppressWarnings( "unchecked" )
+ @Override
public T get( V key )
{
if ( key == null )
@@ -287,6 +324,7 @@ public class EhcacheCache<V, T>
return diskStorePath;
}
+ @Override
public int getMaxElementsInMemory( )
{
return maxElementsInMemory;
@@ -307,21 +345,25 @@ public class EhcacheCache<V, T>
return name;
}
+ @Override
public CacheStatistics getStatistics( )
{
return stats;
}
+ @Override
public int getTimeToIdleSeconds( )
{
return timeToIdleSeconds;
}
+ @Override
public int getTimeToLiveSeconds( )
{
return timeToLiveSeconds;
}
+ @Override
public boolean hasKey( V key )
{
return ehcache.isKeyInCache( key );
@@ -337,20 +379,27 @@ public class EhcacheCache<V, T>
return eternal;
}
+ /**
+ * @deprecated This flag is ignored. The persistence strategy is always
overflow to disk, if on.
+ * @return true, or false
+ */
public boolean isOverflowToDisk( )
{
return overflowToDisk;
}
+ @Override
public void register( V key, T value )
{
ehcache.put( new Element( key, value ) );
}
+ @Override
+ @SuppressWarnings( "unchecked" )
public T put( V key, T value )
{
// Multiple steps done to satisfy Cache API requirement for Previous
object return.
- Element elem = null;
+ Element elem;
Object previous = null;
elem = ehcache.get( key );
if ( elem != null )
@@ -362,9 +411,11 @@ public class EhcacheCache<V, T>
return (T) previous;
}
+ @Override
+ @SuppressWarnings( "unchecked" )
public T remove( V key )
{
- Element elem = null;
+ Element elem;
Object previous = null;
elem = ehcache.get( key );
if ( elem != null )
@@ -397,12 +448,12 @@ public class EhcacheCache<V, T>
}
+ @Override
public void setMaxElementsInMemory( int maxElementsInMemory )
{
this.maxElementsInMemory = maxElementsInMemory;
if ( this.ehcache != null )
{
- this.ehcache.getCacheConfiguration( ).setMaxElementsInMemory(
this.maxElementsInMemory );
this.ehcache.getCacheConfiguration( ).setMaxEntriesLocalHeap(
this.maxElementsInMemory );
}
@@ -418,11 +469,16 @@ public class EhcacheCache<V, T>
this.name = name;
}
+ /**
+ * @deprecated This flag is ignored. The persistence strategy is always
overflow to disk, if on.
+ * @param overflowToDisk true, or false
+ */
public void setOverflowToDisk( boolean overflowToDisk )
{
this.overflowToDisk = overflowToDisk;
}
+ @Override
public void setTimeToIdleSeconds( int timeToIdleSeconds )
{
if ( this.ehcache != null )
@@ -432,6 +488,7 @@ public class EhcacheCache<V, T>
this.timeToIdleSeconds = timeToIdleSeconds;
}
+ @Override
public void setTimeToLiveSeconds( int timeToLiveSeconds )
{
if ( this.ehcache != null )
@@ -491,38 +548,41 @@ public class EhcacheCache<V, T>
this.maxBytesLocalOffHeap = maxBytesLocalOffHeap;
}
+ @Override
public int getMaxElementsOnDisk( )
{
return maxElementsOnDisk;
}
+ @Override
public void setMaxElementsOnDisk( int maxElementsOnDisk )
{
this.maxElementsOnDisk = maxElementsOnDisk;
if ( this.ehcache != null )
{
- this.ehcache.getCacheConfiguration( ).setMaxElementsOnDisk(
this.maxElementsOnDisk );
+ this.ehcache.getCacheConfiguration( ).setMaxEntriesInCache(
this.maxElementsOnDisk );
this.ehcache.getCacheConfiguration( ).maxEntriesLocalDisk(
this.maxElementsOnDisk );
}
}
- /*public String getPersistenceStrategy()
- {
- return persistenceStrategy;
- }
-
- public void setPersistenceStrategy( String persistenceStrategy )
+ /**
+ * Sets the path to the configuration file. If this value is set to a
valid file path,
+ * the configuration will be loaded from the given file. The cache defined
in this file must
+ * match the cache name of this instance.
+ *
+ * @param configurationFile a valid path to a ehcache xml configuration
file
+ */
+ public void setConfigurationFile( Path configurationFile )
{
- this.persistenceStrategy = persistenceStrategy;
+ this.configurationFile = configurationFile;
}
- public boolean isSynchronousWrites()
+ /**
+ * Returns the path to the configuration file or <code>null</code>, if not
set.
+ * @return the path of the configuration file or <code>null</code>
+ */
+ public Path getConfigurationFile( )
{
- return synchronousWrites;
+ return configurationFile;
}
-
- public void setSynchronousWrites( boolean synchronousWrites )
- {
- this.synchronousWrites = synchronousWrites;
- }*/
}
diff --git
a/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/java/org/apache/archiva/components/cache/ehcache/EhcacheCreator.java
b/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/java/org/apache/archiva/components/cache/ehcache/EhcacheCreator.java
index b55e1ea..d0cd773 100644
---
a/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/java/org/apache/archiva/components/cache/ehcache/EhcacheCreator.java
+++
b/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/java/org/apache/archiva/components/cache/ehcache/EhcacheCreator.java
@@ -23,9 +23,10 @@ import org.apache.archiva.components.cache.Cache;
import org.apache.archiva.components.cache.CacheException;
import org.apache.archiva.components.cache.CacheHints;
import org.apache.archiva.components.cache.factory.CacheCreator;
-import org.apache.commons.lang3.SystemUtils;
-import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
/**
* EhcacheCreator - runtime creation of an ehcache.
@@ -36,6 +37,7 @@ public class EhcacheCreator
implements CacheCreator
{
+ @Override
public Cache createCache( CacheHints hints )
throws CacheException
{
@@ -46,19 +48,25 @@ public class EhcacheCreator
cache.setDiskPersistent( hints.isOverflowToDisk( ) );
if ( hints.isOverflowToDisk( ) )
{
- File overflowPath = null;
+ Path overflowPath;
if ( hints.getDiskOverflowPath( ) != null )
{
- overflowPath = hints.getDiskOverflowPath( );
+ overflowPath = hints.getDiskOverflowPath( ).toPath();
}
else
{
- File tmpDir = SystemUtils.getJavaIoTmpDir( );
- overflowPath = new File( tmpDir, "ehcache/" + hints.getName( )
);
+ try
+ {
+ overflowPath = Files.createTempDirectory(
"ehcache-archiva-"+hints.getName());
+ }
+ catch ( IOException e )
+ {
+ throw new CacheException( e );
+ }
}
- cache.setDiskStorePath( overflowPath.getAbsolutePath( ) );
+ cache.setDiskStorePath( overflowPath.toAbsolutePath( ).toString()
);
}
cache.setMaxElementsInMemory( hints.getMaxElements( ) );
diff --git
a/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/resources/META-INF/plexus-cache.properties
b/spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/resources/META-INF/archiva-cache.properties
similarity index 100%
rename from
spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/resources/META-INF/plexus-cache.properties
rename to
spring-cache/spring-cache-providers/spring-cache-ehcache/src/main/resources/META-INF/archiva-cache.properties
diff --git
a/spring-cache/spring-cache-providers/spring-cache-ehcache/src/test/resources/log4j2-test.xml
b/spring-cache/spring-cache-providers/spring-cache-ehcache/src/test/resources/log4j2-test.xml
new file mode 100644
index 0000000..a80d159
--- /dev/null
+++
b/spring-cache/spring-cache-providers/spring-cache-ehcache/src/test/resources/log4j2-test.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ -->
+<configuration>
+ <appenders>
+ <Console name="console" target="SYSTEM_OUT">
+ <PatternLayout pattern="[%t] %-5p %c %x - %m%n"/>
+ </Console>
+ </appenders>
+ <loggers>
+ <logger name="org.apache.archiva" level="warn"/>
+ <logger name="org.apache.archiva.components.cache" level="info"/>
+
+ <root level="error" includeLocation="true">
+ <appender-ref ref="console"/>
+ </root>
+ </loggers>
+</configuration>
+
+
diff --git
a/spring-cache/spring-cache-providers/spring-cache-hashmap/src/main/resources/META-INF/plexus-cache.properties
b/spring-cache/spring-cache-providers/spring-cache-hashmap/src/main/resources/META-INF/archiva-cache.properties
similarity index 100%
rename from
spring-cache/spring-cache-providers/spring-cache-hashmap/src/main/resources/META-INF/plexus-cache.properties
rename to
spring-cache/spring-cache-providers/spring-cache-hashmap/src/main/resources/META-INF/archiva-cache.properties
diff --git
a/spring-cache/spring-cache-providers/spring-cache-oscache/src/main/resources/META-INF/plexus-cache.properties
b/spring-cache/spring-cache-providers/spring-cache-oscache/src/main/resources/META-INF/archiva-cache.properties
similarity index 100%
rename from
spring-cache/spring-cache-providers/spring-cache-oscache/src/main/resources/META-INF/plexus-cache.properties
rename to
spring-cache/spring-cache-providers/spring-cache-oscache/src/main/resources/META-INF/archiva-cache.properties