/*
* JBoss, Home of Professional Open Source.
* Copyright 2010, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ha.ispn;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.infinispan.manager.CacheContainer;
import org.jboss.ha.ispn.config.Alias;
import org.jboss.ha.ispn.config.CacheContainerRegistryConfiguration;
import org.jboss.ha.ispn.config.CacheContainerRegistryConfigurationEntry;
import org.jboss.ha.ispn.config.CacheContainerRegistryConfigurationSource;
import org.jboss.logging.Logger;
/**
* Cache container registry that populates itself using a specified factory from configuration from a specified source.
* All cache containers in the registry are bound to jndi.
* @author Paul Ferraro
*/
public class DefaultCacheContainerRegistry implements CacheContainerRegistry
{
private static Logger logger = Logger.getLogger(DefaultCacheContainerRegistry.class);
private static final AtomicReference<CacheContainerRegistry> singleton = new AtomicReference<CacheContainerRegistry>();
public static CacheContainerRegistry getInstance()
{
return singleton.get();
}
private final CacheContainerFactory factory;
private final CacheContainerRegistryConfigurationSource source;
private final Context context;
private final Map<String, Map.Entry<String, CacheContainer>> containers = new ConcurrentHashMap<String, Map.Entry<String, CacheContainer>>();
private CacheContainer defaultContainer;
/**
* Creates a new cache container registry using the specified factory and source.
* @param factory used to create cache container instances from configuration
* @param source source of cache container configurations.
* @throws NamingException if inital context could not be created
*/
public DefaultCacheContainerRegistry(CacheContainerFactory factory, CacheContainerRegistryConfigurationSource source) throws NamingException
{
this(factory, source, new InitialContext());
}
/**
* Creates a new cache container registry using the specified factory and source.
* @param factory used to create cache container instances from configuration
* @param source source of cache container configurations.
* @param context jndi context to which to bind cache containers.
*/
public DefaultCacheContainerRegistry(CacheContainerFactory factory, CacheContainerRegistryConfigurationSource source, Context context)
{
this.factory = factory;
this.source = source;
this.context = context;
}
public void start() throws Exception
{
CacheContainerRegistryConfiguration registry = this.source.getRegistryConfiguration();
for (CacheContainerRegistryConfigurationEntry entry: registry.getEntries())
{
if (entry.getJndiName() == null)
{
entry.setJndiName(this.context.composeName(entry.getId(), registry.getBaseJndiName()));
}
this.add(entry);
}
CacheContainerRegistryConfigurationEntry defaultEntry = registry.getDefaultEntry();
if (defaultEntry == null)
{
defaultEntry = registry.getEntries().get(0);
}
this.defaultContainer = this.containers.get(defaultEntry.getId()).getValue();
singleton.compareAndSet(null, this);
}
public void stop() throws Exception
{
singleton.set(null);
for (Map.Entry<String, CacheContainer> entry: this.containers.values())
{
try
{
this.context.unbind(entry.getKey());
}
catch (NamingException e)
{
logger.warn(e.getMessage(), e);
}
entry.getValue().stop();
}
this.containers.clear();
this.defaultContainer = null;
}
/**
* {@inheritDoc}
* @see org.jboss.ha.ispn.CacheContainerRegistry#getCacheContainers()
*/
@Override
public Set<String> getCacheContainers()
{
return Collections.unmodifiableSet(this.containers.keySet());
}
/**
* {@inheritDoc}
* @see org.jboss.ha.ispn.CacheContainerRegistry#getCacheContainer()
*/
@Override
public CacheContainer getCacheContainer()
{
return this.defaultContainer;
}
/**
* {@inheritDoc}
* @see org.jboss.ha.ispn.CacheContainerRegistry#getCacheContainer(java.lang.String)
*/
@Override
public CacheContainer getCacheContainer(String name)
{
Map.Entry<String, CacheContainer> entry = (name != null) ? this.containers.get(name) : null;
// Return default cache manager, if name was not found or if it was null
return (entry != null) ? entry.getValue() : this.defaultContainer;
}
public void add(CacheContainerRegistryConfigurationEntry entry) throws NamingException
{
CacheContainerConfiguration configuration = new CacheContainerConfigurationAdapter(entry.getConfiguration());
CacheContainer container = this.factory.createCacheContainer(configuration, this.mapAliases(entry));
String jndiName = entry.getJndiName();
// Store cache containers with jndi name, so they can be unbound during stop()
this.containers.put(entry.getId(), new AbstractMap.SimpleImmutableEntry<String, CacheContainer>(jndiName, container));
// Bind cache container to jndi
this.context.bind(jndiName, container);
}
private Map<String, String> mapAliases(CacheContainerRegistryConfigurationEntry entry)
{
List<Alias> aliases = entry.getAliases();
if ((aliases == null) || aliases.isEmpty()) return Collections.emptyMap();
if (aliases.size() == 1)
{
Alias alias = aliases.get(0);
return Collections.singletonMap(alias.getName(), alias.getTarget());
}
Map<String, String> map = new HashMap<String, String>();
for (Alias alias: aliases)
{
map.put(alias.getName(), alias.getTarget());
}
return map;
}
public void remove(CacheContainerRegistryConfigurationEntry config) throws NamingException
{
Map.Entry<String, CacheContainer> entry = this.containers.remove(config.getId());
if (entry != null)
{
entry.getValue().stop();
this.context.unbind(entry.getKey());
}
}
}