com.tangosol.util.SimpleResourceRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coherence Show documentation
Show all versions of coherence Show documentation
Oracle Coherence Community Edition
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
package com.tangosol.util;
import com.oracle.coherence.common.base.Disposable;
import com.tangosol.net.security.LocalPermission;
import static com.tangosol.util.BuilderHelper.using;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* A {@link SimpleResourceRegistry} is a basic implementation of a {@link ResourceRegistry}.
*
* @author bo 2011.06.22
* @author pp 2012.02.29
* @since Coherence 12.1.2
*/
public class SimpleResourceRegistry
implements ResourceRegistry
{
// ----- constructors ---------------------------------------------------
/**
* Construct a {@link SimpleResourceRegistry}.
*/
public SimpleResourceRegistry()
{
m_mapResource = new ConcurrentHashMap<>();
}
// ----- SimpleResourceRegistry methods ---------------------------------
/**
* Determine if the {@link ResourceRegistry} is empty (contains no resource registrations).
*
* @return true if the registry contains no resource registrations
*/
public boolean isEmpty()
{
return m_mapResource.isEmpty();
}
/**
* Register all the resources contained in this registry with the target registry.
*
* @param registry the target {@link ResourceRegistry} to register resources with
*/
public void registerResources(ResourceRegistry registry)
{
registerResources(registry, RegistrationBehavior.FAIL);
}
/**
* Register all the resources contained in this registry with the target registry.
*
* @param registry the target {@link ResourceRegistry} to register resources with
* @param behavior the {@link RegistrationBehavior} to use
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public void registerResources(ResourceRegistry registry, RegistrationBehavior behavior)
{
for (Map.Entry entry : m_mapResource.entrySet())
{
RegistryKey key = entry.getKey();
RegistryValue value = entry.getValue();
Class clz = key.getResourceClass();
registry.registerResource(clz, key.getName(), using(value.getResource()), behavior, value.getObserver());
}
}
// ----- ResourceRegistry interface -------------------------------------
/**
* {@inheritDoc}
*/
@Override
public synchronized void dispose()
{
Map mapResource = m_mapResource;
for (Map.Entry entry : mapResource.entrySet())
{
RegistryValue value = entry.getValue();
try
{
value.dispose();
}
catch (RuntimeException e)
{
Base.log("Exception while disposing the " + entry.getKey().getName() + " resource: " + e);
Base.log(e);
}
}
mapResource.clear();
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public R getResource(Class clzResource)
{
RegistryValue value = m_mapResource.get(new RegistryKey(clzResource));
return value == null ? null : (R) value.getResource();
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public R getResource(Class clzResource, String sResourceName)
{
RegistryValue value = m_mapResource.get(new RegistryKey(clzResource, sResourceName));
return value == null ? null : (R) value.getResource();
}
/**
* {@inheritDoc}
*/
@Override
public String registerResource(Class clzResource, R resource)
{
return registerResource(clzResource, clzResource.getName(), using(resource), RegistrationBehavior.FAIL, null);
}
/**
* {@inheritDoc}
*/
@Override
public String registerResource(Class clzResource, String sResourceName, R resource)
{
return registerResource(clzResource, sResourceName, using(resource), RegistrationBehavior.FAIL, null);
}
/**
* {@inheritDoc}
*/
@Override
public String registerResource(Class clzResource, Builder extends R> bldrResource,
RegistrationBehavior behavior, ResourceLifecycleObserver observer)
{
return registerResource(clzResource, clzResource.getName(), bldrResource, behavior, observer);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public String registerResource(Class clzResource, String sResourceName, Builder extends R> bldrResource,
RegistrationBehavior behavior, ResourceLifecycleObserver observer)
{
SecurityManager security = System.getSecurityManager();
if (security != null)
{
security.checkPermission(
new LocalPermission("Service.registerResource"));
}
synchronized (clzResource)
{
// attempt to get an existing resource registration for the key
RegistryKey key = new RegistryKey(clzResource, sResourceName);
RegistryValue value = m_mapResource.get(key);
if (value == null)
{
// register the resource as it's not in the registry
value = new RegistryValue(bldrResource.realize(), (ResourceLifecycleObserver