📄 httpsessionproxyvisitor.java
字号:
/**
* Copyright 2003-2005 the original author or authors.
* 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.container.visitor;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import com.jdon.container.ContainerWrapper;
import com.jdon.container.access.TargetMetaRequest;
import com.jdon.container.finder.ComponentKeys;
import com.jdon.container.visitor.data.SessionContext;
import com.jdon.util.Debug;
/**
* using HttpSession as those components that need be cached
*
* now there are three kinds type: ComponentKeys.PROXYINSTANCE_FACTORY
* ComponentKeys.TARGETSERVICE_FACTORY; ComponentKeys.SESSIONCONTEXT_FACTORY;
*
* PROXYINSTANCE_FACTORY and TARGETSERVICE_FACTORY are the factorys that create
* components that need be optimized, if every time create these components, it
* will cost performance.
*
* ComponentKeys.SESSIONCONTEXT_FACTORY is the factory of the state data from the
* web container.
*
* Decorator patterns. Decoratee is ComponentOriginVisitor Decorator is this
* class;
*
* @see ComponentKeys container.xml
* @author banq
*/
public class HttpSessionProxyVisitor implements ComponentVisitor, HttpSessionBindingListener {
private final static String module = HttpSessionProxyVisitor.class.getName();
/**
* in this box there are the result objects of the components running that
* need be optimized. note" not the components itself.
* the key = components name + VisitableName(such as :ComponentKeys.PROXYINSTANCE_FACTORY);
* the value is the result of components factory creating.
*
* samples:
* key: NewsManagerproxyInstanceFactoryVisitable
* value: the dynamic proxy instance for NewsManager object
*
* key:NewsManagertargetServiceFactoryVisitable
* value: the NewsManager object
*
*/
private Map componentsboxs = new HashMap();
private ComponentVisitor componentVisitor;
public HttpSessionProxyVisitor(ComponentVisitor componentVisitor) {
this.componentVisitor = componentVisitor;
}
public ContainerWrapper getContainerWrapper() {
return componentVisitor.getContainerWrapper();
}
public void valueBound(HttpSessionBindingEvent event) {
Debug.logVerbose(" valueBound active, sessionId :" + event.getSession().getId(), module);
componentsboxs.clear();
}
/**
* session destroyed. remove all ejb references;
*/
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();
componentsboxs.clear();
}
/**
* the object type saved in componentsboxs is decided by the method"
* visitableFactory.createVisitable. only ejb service need cached, pojo
* service not need.
*
* @param targetMetaDef
* TargetMetaDef
* @return Object
*/
public Object visit(TargetMetaRequest targetMetaRequest) {
Object o = null;
try {
StringBuffer sb = new StringBuffer(targetMetaRequest.getTargetMetaDef().getCacheKey());
sb.append(targetMetaRequest.getVisitableName());
Debug.logVerbose(" get the optimized instance for the key " + sb.toString(), module);
o = componentsboxs.get(sb.toString());
if (o == null) {
Debug.logVerbose(" first time visit: " + targetMetaRequest.getTargetMetaDef().getClassName(), module);
o = componentVisitor.visit(targetMetaRequest);
componentsboxs.put(sb.toString(), o);
}
} catch (Exception e) {
Debug.logError("visit error: " + e);
}
return o;
}
public SessionContext visitSessionContext(TargetMetaRequest targetMetaRequest){
Object sessionContext = componentsboxs.get(SessionContext.NAME);
if (sessionContext == null) {
Debug.logVerbose(" first time visit sessionContext: " + targetMetaRequest.getVisitableName(), module);
sessionContext = componentVisitor.visit(targetMetaRequest);
componentsboxs.put(SessionContext.NAME, sessionContext);
}
return (SessionContext)sessionContext;
}
/**
* remove all ejb references
*/
public void removeObjects() {
/**
*
* try { Iterator iter = componentsboxs.keySet().iterator(); while
* (iter.hasNext()) { String key = (String) iter.next(); Object ccEjb =
* (Object) componentsboxs.get(key); removeEJBObject(ccEjb); } } catch
* (Exception ex) { Debug.logWarning(ex, module); } finally {
* componentsboxs.clear(); }
*/
}
/**
* destroy the EJB.
*
* private void removeEJBObject(Object ccEjb) { if (ccEjb == null) return;
* try { //这个EJB的remove方法需要被任何角色访问,因为此时principle为空 if (ccEjb instanceof
* EJBLocalObject) { EJBLocalObject eo = ( (EJBLocalObject) ccEjb);
* eo.remove(); } else if (ccEjb instanceof EJBObject) { EJBObject eo = (
* (EJBObject) ccEjb); eo.remove(); } } catch (Exception re) {
* } }
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -