📄 httpsessionproxyvisitor.java
字号:
/**
* Copyright 2005 Jdon.com
* 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 com.jdon.bussinessproxy.cache;
import com.jdon.bussinessproxy.TargetMetaDef;
import com.jdon.util.Debug;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import javax.ejb.EJBLocalObject;
import javax.ejb.EJBObject;
/**
* 管理和提供与HttSession相关组件的访问,缓存这些组件为了加速性能。
*
HttpSessionBindingListener:
每当往Session中存入一个对象(setAttribute)或从Session中删除一个对象的时候
(包括容器自动删除这个Session时),
如果这个对象实现了此监听器接口,应用服务器将会自动调用接口相应的方法:
valueBound和valueUnbound
HttpSessionBindingListener与HttpSessionListener区别是
前者无需web.xml设置
*
* <p>Copyright: Jdon.com Copyright (c) 2005</p>
* <p></p>
* @author banq
* @version JdonFramework 2005 v1.0
*/
public class HttpSessionProxyVisitor implements ComponentCacheVisitor, HttpSessionBindingListener{
private final static String module = HttpSessionProxyVisitor.class.getName();
private Map componentsboxs = new HashMap();
private VisitableFactory visitableFactory;
public HttpSessionProxyVisitor(VisitableFactory visitableFactory){
this.visitableFactory = visitableFactory;
}
public void valueBound(HttpSessionBindingEvent event) {
Debug.logVerbose(" valueBound active, sessionId :" + event.getSession().getId() , module);
Debug.logVerbose(" valueBound active, componentsboxs size:" + componentsboxs.size() , module);
}
/**
* session destroyed.
* 去除有状态Session Bean之类的有状态引用。
*/
public void valueUnbound(HttpSessionBindingEvent event) {
String sessionId = event.getSession().getId();
Debug.logVerbose(" unvalueBound active, sessionId :" + sessionId , module);
Debug.logVerbose(" unvalueUnbound active, componentsboxs size" + componentsboxs.size(), module);
removeObjects();
}
public Object visit(TargetMetaDef targetMetaDef){
StringBuffer sb = new StringBuffer(targetMetaDef.getCacheKey());
sb.append(targetMetaDef.getVisitableName());
Debug.logVerbose(" visit key is " + sb.toString(), module);
Object o = componentsboxs.get(sb.toString());
if (o == null){
Visitable vo = visitableFactory.createVisitable(targetMetaDef);
o = vo.accept(this);
componentsboxs.put(sb.toString(), o);
}else{
Debug.logVerbose(" got the component from session ", module);
}
return o;
}
/**
* 需要将一些对象引用去除
*/
public void removeObjects(){
try {
Iterator iter = componentsboxs.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
Object ccEjb = (Object) componentsboxs.get(key);
removeEJBObject(key);
}
} catch (Exception ex) {
Debug.logWarning(ex, module);
} finally {
componentsboxs.clear();
}
}
/**
* destroy the EJB.
*/
private void removeEJBObject(Object ccEjb) {
try {
//这个EJB的remove方法需要被任何角色访问,因为此时principle为空
if (ccEjb instanceof EJBLocalObject) {
if (ccEjb != null) {
EJBLocalObject eo = ( (EJBLocalObject) ccEjb);
eo.remove();
}
} else if (ccEjb instanceof EJBObject) {
if (ccEjb != null) {
EJBObject eo = ( (EJBObject) ccEjb);
eo.remove();
}
}
} catch (Exception re) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -