applicationcontextmigratorutil.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 402 行 · 第 1/2 页
JAVA
402 行
put((String)entry.getKey(), entry.getValue());
}
}
@Override
public boolean containsKey(Object key) {
return userMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return userMap.containsValue(value);
}
@Override
public Set entrySet() {
return new ApplicationPropertyMapEntrySet(userMap.entrySet(), mepCtx);
}
@Override
public Object get(Object key) {
// WARNING: there's no real guarantee that the reason a migrator is getting
// a property is due to it being put on the MessageContext.
// We would therefore be setting scope for a property that never actually makes
// its way into the messageContext.
Object obj = userMap.get(key);
if (obj != null) {
mepCtx.setScope((String)key, Scope.APPLICATION);
}
return obj;
}
@Override
public boolean isEmpty() {
return userMap.isEmpty();
}
@Override
public Set keySet() {
return userMap.keySet();
}
@Override
public Object remove(Object key) {
return userMap.remove(key);
}
@Override
public int size() {
return userMap.size();
}
@Override
public Collection values() {
return userMap.values();
}
private class ApplicationPropertyMapEntrySet extends AbstractSet {
Set containedSet;
MEPContext mepCtx;
public ApplicationPropertyMapEntrySet(Set set, MEPContext mepCtx) {
containedSet = set;
this.mepCtx = mepCtx;
}
@Override
public EntrySetIterator iterator() {
return new EntrySetIterator(containedSet.iterator(), mepCtx);
}
@Override
public int size() {
return containedSet.size();
}
}
private class EntrySetIterator implements Iterator {
private Iterator containedIterator;
private MEPContext mepCtx;
private EntrySetIterator(Iterator containedIterator, MEPContext mepCtx) {
this.containedIterator = containedIterator;
this.mepCtx = mepCtx;
}
// override remove() to make this Iterator class read-only
public void remove() {
throw new UnsupportedOperationException();
}
public boolean hasNext() {
return containedIterator.hasNext();
}
public Object next() {
// WARNING: there's no real guarantee that the reason a migrator is iterating
// over the properties is due to this being the source object for a putAll(source)
// We would therefore be setting scope for a property that never actually makes
// its way into the messageContext
Entry entry = (Entry)containedIterator.next();
mepCtx.setScope((String)entry.getKey(), Scope.APPLICATION);
return entry;
}
}
}
/**
* ApplicationPropertyMapWriter is similar to the ApplicationPropertyMapReader in that it
* observes scope to determine what can be returned to a property migrator. Individual
* property migrators should only be allowed to retrieve APPLICATION-scoped properties.
*
* TODO: There's quite a bit of expensive logic that would need to go into this to be
* fully correct. For example, if a migrator calls size, we cannot simply return
* userMap.size(). Rather, we would have to count only the APPLICATION scoped properties
* and return those.
*
* @author rott
*
*/
private static class ApplicationPropertyMapWriter extends HashMap<String, Object> {
private Map<String, Object> userMap;
private MEPContext mepCtx;
public ApplicationPropertyMapWriter(Map<String, Object> userMap, MEPContext mepCtx) {
this.userMap = userMap;
this.mepCtx = mepCtx;
}
@Override
public Object put(String key, Object value) {
// notice the logic here! We won't put a property on the userMap that is not APPLICATION scoped
if (mepCtx.getScope(key) == Scope.APPLICATION) {
return userMap.put(key, value);
}
return null;
}
@Override
public void putAll(Map<? extends String, ? extends Object> m) {
// we need to take advantage of the smarter put(String, Object)
for (Iterator it = m.entrySet().iterator(); it.hasNext();) {
Entry entry = (Entry)it.next();
put((String)entry.getKey(), entry.getValue());
}
}
@Override
public boolean containsKey(Object key) {
if (mepCtx.getScope((String)key) == Scope.APPLICATION) {
return userMap.containsKey(key);
}
return false;
}
@Override
public boolean containsValue(Object value) {
return userMap.containsValue(value);
}
@Override
public Set entrySet() {
return userMap.entrySet();
}
@Override
public Object get(Object key) {
return userMap.get(key);
}
@Override
public boolean isEmpty() {
return userMap.isEmpty();
}
@Override
public Set keySet() {
return userMap.keySet();
}
@Override
public Object remove(Object key) {
return userMap.remove(key);
}
@Override
public int size() {
return userMap.size();
}
@Override
public Collection values() {
return userMap.values();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?