
com.openthinks.libs.utilities.pools.object.SharedContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utilities Show documentation
Show all versions of utilities Show documentation
A common utilities for other projects under domain com.openthinks.
The newest version!
package com.openthinks.libs.utilities.pools.object;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.openthinks.libs.utilities.InstanceUtilities;
import com.openthinks.libs.utilities.InstanceUtilities.InstanceWrapper;
/**
* Shared web context, used {@link ObjectPool} as object shared pool
* @author [email protected]
*
*/
public abstract class SharedContext {
private static final SharedContext defaultContext = new SharedContext() {
};
public static final SharedContext get() {
return defaultContext;
}
// ///////////////////////////////////////////////////////////////////////////////////////////////
private final ObjectPool objectPool = new ObjectPool();
private final Lock lock = new ReentrantLock();
// ////////////////////////////////////////////////////////////////////////////////////////////////
/**
* lookup a object by its type, if not find firstly, try to instance by
* instanceType and constructor parameters args
*
* @param lookup object type
* @param type
* Class lookup key type
* @param args
* Object[] instance constructor parameters
* @return T lookup object
*/
public T lookup(Class type, Object... args) {
return lookup(type, InstanceWrapper.build(type), args);
}
void cleanUp() {
objectPool.cleanUp();
}
/**
* lookup a object by its type, if not find firstly, try to instance by
* instanceType and constructor parameters args
*
* @param lookup object type
* @param lookup object type
* @param searchType
* Class lookup key type
* @param instancewrapper
* Class instance type when not lookup the key
* @param args
* Object[] instance constructor parameters
* @return T lookup object
*/
public T lookup(final Class searchType, InstanceWrapper instancewrapper, Object... args) {
T object = objectPool.get(searchType);
lock.lock();
try {
object = objectPool.get(searchType);
if (object == null) {
object = InstanceUtilities.create(searchType, instancewrapper, args);
register(object);
}
} finally {
lock.unlock();
}
return object;
}
public void register(T object) {
if (object != null) {
objectPool.put(object.getClass(), object);
}
}
public void register(Class classType, T object) {
if (object != null) {
objectPool.put(classType, object);
}
}
/**
* look up object by its bean name
* @param lookup object type
* @param beanName String lookup object mapping name
* @return T lookup object
*/
public T lookup(String beanName) {
return objectPool.get(beanName);
}
/**
* register object and mapping it to given bean name
* @param register object type
* @param beanName String bean name
* @param object register object
*/
public void register(String beanName, T object) {
if (object != null) {
objectPool.put(beanName, object);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy