![JAR search and dependency download from the Maven repository](/logo.png)
org.brandao.concurrent.DisposableSingletonContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of named-lock Show documentation
Show all versions of named-lock Show documentation
The named-lock is a utility for acquiring named locks.
The newest version!
/*
* Named Lock http://namedlock.brandao.org/
* Copyright (C) 2006-2016 Afonso Brandao. ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.brandao.concurrent;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
class DisposableSingletonContext {
protected ConcurrentMap factoryList =
new ConcurrentHashMap();
protected ConcurrentMap beans =
new ConcurrentHashMap();
protected ConcurrentMap> references =
new ConcurrentHashMap>();
private long prefixIndex = 0;
private Object getUniqueThreadReference(Thread thread){
return Long.toString(thread.getId(), Character.MAX_RADIX) + ":" +
Long.toString(prefixIndex++, Character.MAX_RADIX);
}
public synchronized void registryBeanDefinition(String name, ObjectFactory factory){
if(factoryList.containsKey(name))
throw new IllegalArgumentException(name);
this.factoryList.put(name, factory);
}
public Object getBean(T alias, String beanName){
Thread currentThread = Thread.currentThread();
Object reference = getUniqueThreadReference(currentThread);
ObjectFactory factory = factoryList.get(beanName);
synchronized(factory){
Object bean = this.create(alias, beanName);
return this.createProxy(alias, beanName, bean, currentThread, reference);
}
}
private Object createProxy(T alias, String beanName, Object bean,
Thread thread, Object reference){
Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy