All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atomikos.util.IntraVmObjectRegistry Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
/**
 * Copyright (C) 2000-2019 Atomikos 
 *
 * LICENSE CONDITIONS
 *
 * See http://www.atomikos.com/Main/WhichLicenseApplies for details.
 */

package com.atomikos.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.naming.NameAlreadyBoundException;
import javax.naming.NameNotFoundException;

/**
 * An intra-VM object registry for reconstructing Objects from references.
 *
 */
public class IntraVmObjectRegistry {

	private final static Map resourcesMap = new HashMap();

	public synchronized static void addResource(String resourceName, Object resource) throws NameAlreadyBoundException
	{
		if (resourcesMap.containsKey(resourceName))
			throw new NameAlreadyBoundException("resource with name '" + resourceName + "' already registered");

		resourcesMap.put(resourceName, resource);
	}

	public synchronized static Object getResource(String resourceName) throws NameNotFoundException
	{
		if (!resourcesMap.containsKey(resourceName))
			throw new NameNotFoundException("no resource with name '" + resourceName + "' has been registered yet");

		return resourcesMap.get(resourceName);
	}

	public synchronized static void removeResource(String resourceName) throws NameNotFoundException
	{
		if (!resourcesMap.containsKey(resourceName))
			throw new NameNotFoundException("no resource with name '" + resourceName + "' has been registered yet");

		resourcesMap.remove(resourceName);
	}
	
	public synchronized static List findAllResourcesOfType(Class clazz) {
	    List ret = new ArrayList();
	    for (Object r : resourcesMap.values()) {
	        if (clazz.isInstance(r)) {
	            ret.add(r);
	        }
	    }
	    return ret;
	}

}