MVC框架实现8---Smart框架*Mapper类实现

本文详细探讨了智能映射领域的关键组件——ActionDefineMapper和DefaultActionDefineMapper,以及它们如何通过映射机制将行动名称转换为具体定义。同时,介绍了DefaultActionMapper在获取和实例化相应类方面的作用,确保了系统的灵活性与高效性。
package com.smart.mapping;

import com.smart.config.ActionDefine;

public interface ActionDefineMapper {

	public ActionDefine mapping( String actionName );
}


package com.smart.mapping;

import java.util.Map;
import java.util.Set;

import com.smart.config.ActionDefine;
import com.smart.context.SmartContext;

public class DefaultActionDefineMapper implements ActionDefineMapper {

	private static final DefaultActionDefineMapper mapper = new DefaultActionDefineMapper();
	
	private DefaultActionDefineMapper(){}
	
	@Override
	public ActionDefine mapping(String actionName) {
		Map<String,ActionDefine> actions = SmartContext.getContext().getConfigContext()
				.getActions();
		Set<String> keys = actions.keySet();
		for( String key : keys){
			ActionDefine ad = (ActionDefine)actions.get(key);
			if( actionName.equals(ad.getName())){
				return ad;
			}
		}
		return null;
	}

	public static DefaultActionDefineMapper getMapper() {
		return mapper;
	}
	
	

}

package com.smart.mapping;


public interface  ActionMapper {

	public Object mapping( String actionName );

}

package com.smart.mapping;

import com.smart.config.ActionDefine;
import com.smart.exception.NoSuchActionDefineException;
import com.smart.exception.ReflectionClassNotFoundException;
import com.smart.exception.ReflectionIllegalAccessException;
import com.smart.exception.ReflectionInstantiationException;

public class DefaultActionMapper implements ActionMapper {
	
	private final static DefaultActionMapper actionMapper = new DefaultActionMapper();

	private DefaultActionMapper(){}
	
	@Override
	public Object mapping(String actionName) {
		
		ActionDefine actionDefine = DefaultActionDefineMapper.getMapper().mapping(actionName);
		if( actionDefine == null )
			throw new NoSuchActionDefineException(" no such ActionDefine exits ");
		
		Object obj = null;
		try {
			obj = Class.forName(actionDefine.getClazz()).newInstance();
		} catch (InstantiationException e) {
			throw new ReflectionInstantiationException(e);
		} catch (IllegalAccessException e) {
			throw new ReflectionIllegalAccessException(e);
		} catch (ClassNotFoundException e) {
			throw new ReflectionClassNotFoundException(e);
		}
		
		return obj;
	}

	public static DefaultActionMapper getActionmapper() {
		return actionMapper;
	}
}

package com.smart.mapping;

import com.smart.forwarder.Forwarder;

public interface ForwarderMapper {
	public Forwarder mapping( String clazz,String name );
}

package com.smart.mapping;

import java.util.List;
import java.util.Map;

import com.smart.config.ActionDefine;
import com.smart.config.ForwarderDefine;
import com.smart.context.SmartContext;
import com.smart.forwarder.DefaultForwarder;
import com.smart.forwarder.Forwarder;

public class DefaultForwarderMapper implements ForwarderMapper {

	private final static DefaultForwarderMapper mapper = new DefaultForwarderMapper();
	
	private DefaultForwarderMapper(){}
	
	@Override
	public Forwarder mapping(String clazz,String name) {
		Map<String,ActionDefine> actions = SmartContext.getContext()
				.getConfigContext().getActions();
		ActionDefine ad = (ActionDefine)actions.get(clazz);
		List<ForwarderDefine> fds = ad.getFdList();
		for( ForwarderDefine fd : fds){
			if( fd.getName().equals(name)){
				return new DefaultForwarder(fd.getPath());
			}
		}
		
		return null;
	}

	public static DefaultForwarderMapper getMapper() {
		return mapper;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值