DirectJNgine与Coop整合

将来可能需要处理DirectJNgine与Coop的整合,看了一下DirectJNgine的源码,初步找到以下扩展点可以利用。代码如下:

1. Java类
package bobbyyu.djn;

import java.util.HashMap;
import java.util.Map;

import com.softwarementors.extjs.djn.api.RegisteredMethod;
import com.softwarementors.extjs.djn.router.dispatcher.Dispatcher;

import coop.bootstrap.Service;
import coop.container.Component;
import coop.container.Container;
import coop.container.context.Context;
import coop.el.EL;

@Component(id = "coopDispatcher", context = Context.Application)
public class CoopDispatcher implements Dispatcher {
	private CoopDispatcher me;
	private Map<Class<?>, String> idMap = new HashMap<Class<?>, String>();
	
	public Object dispatch(RegisteredMethod method, Object[] args ) {
		Class<?> actionCls = method.getActionClass();
		String compId = idMap.get(actionCls);
		if(compId == null) {
			if(me == null)
				me = Service.adapt(EL.eval("#{coopDispatcher}"), CoopDispatcher.class);
			compId = me.getComponentId(actionCls);
			if(compId != null)
				idMap.put(actionCls, compId);
		}
		if(compId == null)
			return null;
		return EL.invoke("#{" + compId + "." + method.getName() + "}", args);
	}
	
	public String getComponentId(Class<?> type) {
		Context[] contexts = Container.getInstance().findRegisteredContexts(type);
		if(contexts.length == 0)
			return null;
		return contexts[0].getDescriptor(type).getComponentId();
	}
}


2. web.xml
<servlet>
	<servlet-name>DjnServlet</servlet-name>
	<servlet-class>com.softwarementors.extjs.djn.servlet.DirectJNgineServlet</servlet-class>
	...
	<init-param>
		<param-name>dispatcherClass</param-name>
		<param-value>bobbyyu.djn.CoopDispatcher</param-value>
	</init-param>
	...
</servlet>

你可能感兴趣的:(java,xml,Web,servlet)