All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jdon.aop.interceptor.StatefulInterceptor Maven / Gradle / Ivy

Go to download

JdonFramework is a java framework that you can use to build your Domain Driven Design + CQRS + EventSource applications with asynchronous concurrency and higher throughput.

There is a newer version: 6.9
Show newest version
/*
 * Copyright 2003-2006 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.aop.interceptor;

import java.util.ArrayList;
import java.util.List;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import com.jdon.aop.reflection.ProxyMethodInvocation;
import com.jdon.bussinessproxy.TargetMetaDef;
import com.jdon.container.ContainerWrapper;
import com.jdon.container.access.TargetMetaRequest;
import com.jdon.container.access.TargetMetaRequestsHolder;
import com.jdon.container.finder.ComponentKeys;
import com.jdon.container.finder.ContainerCallback;
import com.jdon.container.pico.Startable;
import com.jdon.container.visitor.ComponentVisitor;
import com.jdon.controller.service.Stateful;
import com.jdon.util.Debug;

/**
 * StatefulInterceptor is a Interceptor of creating target object. must be the
 * last in Interceptors.
 * 
 * it active for all pojoServices that implements
 * com.jdon.controller.service.StatefulPOJOService
 * 
 * this class stateful function is using ComponentVisitor's cache, such as
 * HttpSession.
 * 
 * @author banq
 * 
 */
public class StatefulInterceptor implements MethodInterceptor, Startable {
	private final static String module = StatefulInterceptor.class.getName();

	private final List isStatefulCache = new ArrayList();

	private final List unStatefulCache = new ArrayList();

	private final ContainerCallback containerCallback;

	private final TargetMetaRequestsHolder targetMetaRequestsHolder;

	/**
	 * @param containerCallback
	 */
	public StatefulInterceptor(ContainerCallback containerCallback, TargetMetaRequestsHolder targetMetaRequestsHolder) {
		super();
		this.containerCallback = containerCallback;
		this.targetMetaRequestsHolder = targetMetaRequestsHolder;
	}

	/*
	 * check the pojoService if implements StatefulPOJOService using
	 * ComponentVisitor, we can get targetObjRef from httpsession.
	 * 
	 * @see
	 * org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept
	 * .MethodInvocation)
	 */
	public Object invoke(MethodInvocation invocation) throws Throwable {
		ProxyMethodInvocation pmi = (ProxyMethodInvocation) invocation;
		TargetMetaRequest targetMetaRequest = targetMetaRequestsHolder.getTargetMetaRequest();
		TargetMetaDef targetMetaDef = targetMetaRequest.getTargetMetaDef();
		if (targetMetaDef.isEJB())
			return invocation.proceed();

		if (!isStateful(targetMetaDef)) {
			// Debug.logVerbose("[JdonFramework] target service is not Stateful: "
			// + targetMetaDef.getClassName() +
			// " StatefulInterceptor unactiive", module);
			return invocation.proceed();
		}
		Debug.logVerbose("[JdonFramework] enter StatefulInterceptor", module);
		Object result = null;
		try {
			ComponentVisitor cm = targetMetaRequest.getComponentVisitor();
			targetMetaRequest.setVisitableName(ComponentKeys.TARGETSERVICE_FACTORY);
			Debug.logVerbose(ComponentKeys.TARGETSERVICE_FACTORY + " in action (Stateful)", module);
			Object targetObjRef = cm.visit();
			pmi.setThis(targetObjRef);
			result = invocation.proceed();
		} catch (Exception ex) {
			Debug.logError("[JdonFramework]StatefulInterceptor error: " + ex, module);
		}
		return result;
	}

	public boolean isStateful(TargetMetaDef targetMetaDef) {
		boolean found = false;
		if (isStatefulCache.contains(targetMetaDef.getName())) {
			found = true;
		} else if (!unStatefulCache.contains(targetMetaDef.getName())) {
			Debug.logVerbose("[JdonFramework] check if it is a isStateful", module);
			ContainerWrapper containerWrapper = containerCallback.getContainerWrapper();
			Class thisCLass = containerWrapper.getComponentClass(targetMetaDef.getName());
			if (Stateful.class.isAssignableFrom(thisCLass) || thisCLass.isAnnotationPresent(com.jdon.annotation.intercept.Stateful.class)) {
				found = true;
				isStatefulCache.add(targetMetaDef.getName());
			} else {
				unStatefulCache.add(targetMetaDef.getName());
			}
		}
		return found;
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub

	}

	@Override
	public void stop() {
		this.isStatefulCache.clear();
		this.unStatefulCache.clear();

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy