CacheManagerEventListener の実装を作ってみる

今回は、CacheManagerEventListener のサンプルとして DefaultCacheRepressor を作成してみます。

DefaultCacheRepressor

Ehcacheの上位のライブラリからEhcacheを使用する場合、Ehcache側でキャッシュを設定していなかった場合に、自動的にキャッシュを作成してくれる機能がある場合があります。しかし、キャッシュ名を間違って設定した場合など、意図せずデフォルトキャッシュが使用されてしまう場合があります。そのため、デフォルトキャッシュを使用しようとすると例外を返すリスナーを作成します。

CacheManagerEventAdapter

CacheManagerEventListener には5つのメソッドが定義されています。しかし、DefaultCacheRepressor では notifyCacheAdded しか使用しないため、5つのメソッドをすべて実装するのは面倒です。そのため、CacheEventListener の空実装として、CacheEventAdapter を作成します。

/**
 * Adapter for CacheManagerEventListener.
 */
public class CacheManagerEventAdapter implements CacheManagerEventListener {
	public void dispose() throws CacheException {}
	public Status getStatus() { return null; }
	public void init() throws CacheException {}
	public void notifyCacheAdded(String cacheName) {}
	public void notifyCacheRemoved(String cacheName) {}
}

ehcache-amountOverflowAlermSample.xml

このサンプル用の、Ehcacheの定義ファイルです。

キャッシュ名として、testCache1 と testCache2 のみ許可しています。

<ehcache>
	<cacheManagerEventListenerFactory
		class="DefaultCacheRepressorFactory"
		properties="allowedCacheNames=testCache1:testCache2"
	/>
	<defaultCache />
	<cache name="testCache1" />
	<cache name="testCache2" />
	<cache name="testCache3" />
</ehcache>

DefaultCacheRepressor

CacheManagerEventListener の実装クラスです。

notifyCacheAdded メソッドでキャッシュの追加を監視し、許可されていないキャッシュ名の場合には例外を返します。

/**
 * CacheManagerEventListener to prohibit using default cache.
 */
public class DefaultCacheRepressor extends CacheManagerEventAdapter {
	/**
	 * Allowed cache names.
	 */
	List<String> allowedCacheNames;
	
	/**
	 * Set allowedCacheNames.
	 * @param allowedCacheNames allowedCacheNames
	 */
	public DefaultCacheRepressor(List<String> allowedCacheNames) {
		this.allowedCacheNames = allowedCacheNames;
	}
	
	/**
	 * Throws IllegalStateException if added cache is not allowed.
	 * @throws IllegalStateException added cache is not allowed
	 */
	@Override
	public void notifyCacheAdded(String cacheName) {
		if (!this.allowedCacheNames.contains(cacheName))
			throw new IllegalStateException("Specified cache name is not allowed : ["+cacheName+"]");
	}
}

DefaultCacheRepressorFactory

定義ファイル上でキャッシュに CacheManagerEventListener を設定するためにはファクトリが必須となるため、DefaultCacheRepressorFactory を作成します。

/**
 * Factory for DefaultCacheRepressor.
 */
public class DefaultCacheRepressorFactory extends CacheManagerEventListenerFactory {
	/**
	 * @param properties the key is "allowedCacheNames", the value is cache names separated with ':'. 
	 */
	@Override
	public CacheManagerEventListener createCacheManagerEventListener(Properties properties) {
		String[] allowedCacheNames = properties.getProperty("allowedCacheNames").split(":");
		return new DefaultCacheRepressor(Arrays.asList(allowedCacheNames));
	}
}

DefaultCacheRepressorSample

DefaultCacheRepressor のサンプルプログラムです。定義ファイル ehcache-DefaultCacheRepressorSample.xml で CacheManager を設定します。

/**
 * Instantiate CacheManager with ehcache-DefaultCacheRepressorSample.xml
 */
public class DefaultCacheRepressorSample {

	public static void main(String[] args) {
		URL url = ClassLoader.getSystemResource("ehcache-DefaultCacheRepressorSample.xml");
		new CacheManager(url);
	}

}

結果。

思惑どおり、許可していない名前(testCache3)のキャッシュを作成しようとしたら、例外が発生しました。

Exception in thread "main" java.lang.IllegalStateException: Specified cache name is not allowed : [testCache3]