Hi everyone,

When I was coding my implementation of CacheManager (see SHIRO-481), i made it implements Initializable.

I was disappointed :-/

Since it's a CacheManager, i defined getCache

protected CacheFactory cacheFactory;

Cache getCache(String name) {
    return cacheFactory.newCache(name);
}

Of course, i need to instantiate cacheFactory before getCache is called, so I defined Initializable#init to

init() {
    cacheFactory = new CacheFactory();
}

When I start my application, everything works fine (shiro.ini parsing...). But when getCache is called for the first time, i get an NPE. Since it's strange, i used by debugger to see what was happening.

Well, init is called, but it's called AFTER getCache() is called. so cacheFactory is null → NPE.

I had a look at others CacheManager implementation: they use a lazy initialization mechanism to solve the issue. See for example:

org.apache.shiro.cache.ehcache.EhCacheManager#ensureCacheManager

Can someone tell me what is the point of the init method if it's called too late, after your object may already have been used. That's counter-intuitive.

In my opinion, init should be called before any call to getCache().

Or, maybe i miss something ?


--
Brendan Le Ny, Code Lutin
[email protected]
(+33) 02 40 50 29 28

Reply via email to