org.nutz.ioc.impl.ScopeContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nutz Show documentation
Show all versions of nutz Show documentation
Nutz, which is a collections of lightweight frameworks, each of them can be used independently
package org.nutz.ioc.impl;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.nutz.ioc.IocContext;
import org.nutz.ioc.ObjectProxy;
import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
/**
* 自定义级别上下文对象
*
* @author zozoh([email protected])
*/
public class ScopeContext implements IocContext {
private static final Log log = Logs.get();
private String scope;
private Map objs;
public ScopeContext(String scope) {
this.scope = scope;
objs = new HashMap();
}
private void checkBuffer() {
if (null == objs)
throw Lang.makeThrow("Context '%s' had been deposed!", scope);
}
public Map getObjs() {
return objs;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public ObjectProxy fetch(String name) {
checkBuffer();
return objs.get(name);
}
public boolean save(String scope, String name, ObjectProxy obj) {
if (accept(scope)) {
checkBuffer();
synchronized (this) {
if (!objs.containsKey(name)) {
if (log.isDebugEnabled())
log.debugf("Save object '%s' to [%s] ", name, scope);
objs.put(name, obj);
return true;
}
}
}
return false;
}
protected boolean accept(String scope) {
return null != scope && this.scope.equals(scope);
}
public boolean remove(String scope, String name) {
if (accept(scope)) {
checkBuffer();
synchronized (this) {
if (objs.containsKey(name)) {
if (log.isDebugEnabled())
log.debugf("Remove object '%s' from [%s] ", name, scope);
return null != objs.remove(name);
}
}
}
return false;
}
public void clear() {
checkBuffer();
for (Entry en : objs.entrySet()) {
if (log.isDebugEnabled())
log.debugf("Depose object '%s' ...", en.getKey());
en.getValue().depose();
}
objs.clear();
}
public void depose() {
if (objs != null) {
clear();
objs = null;
} else {
if (log.isWarnEnabled())
log.warn("can't depose twice , skip");
}
}
public Set names() {
return objs.keySet();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy