package com.simplegame.core.action.manager;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.simplegame.core.SpringApplicationContext;
import com.simplegame.core.action.annotation.ActionMapping;
import com.simplegame.core.action.annotation.ActionWorker;
import com.simplegame.core.action.resolver.DefaultActionResolver;
import com.simplegame.core.action.resolver.IActionResolver;

/**
 * 
 * @Author zeusgooogle@gmail.com
 * @sine 2015年4月26日 下午6:33:00
 * 
 */
@Component
public class DefaultActionManager implements IActionManager {

	private final static Logger LOG = LoggerFactory.getLogger(DefaultActionResolver.class);

	private Map<String, IActionResolver> resolvers = new HashMap<String, IActionResolver>();

	private ApplicationContext ctx;
	
	@PostConstruct
	public void init() {
		ctx = SpringApplicationContext.getApplicationContext();

		Map<String, Object> workers = ctx.getBeansWithAnnotation(ActionWorker.class);
		for (Object clazz : workers.values()) {
			analyzeClass( clazz.getClass() );
		}
	}

	private void analyzeClass(Class<?> clazz) {
		ActionWorker actionWorker = clazz.getAnnotation(ActionWorker.class);
		if (null != actionWorker) {
			try {
				
				Method[] methods = clazz.getDeclaredMethods();

				for (Method m : methods) {
					ActionMapping commandMapping = m.getAnnotation(ActionMapping.class);
					if (null != commandMapping) {
						resolvers.put(commandMapping.mapping(), new DefaultActionResolver(m, ctx.getBean(clazz)));
					}
				}
			} catch (Exception e) {
				LOG.error("analye class error: ", e);
				throw new RuntimeException("error in analyzeClass", e);
			}
		}

	}

	@Override
	public IActionResolver getResolver(String command) {
		return resolvers.get(command);
	}


}

@@ -268,24 +268,84 @@
 ionContext;%0A
+import org.springframework.core.annotation.AnnotationUtils;%0A
 import org.s
@@ -1389,25 +1389,36 @@
 orker = 
-clazz.get
+AnnotationUtils.find
 Annotati
@@ -1412,32 +1412,39 @@
 .findAnnotation(
+clazz, 
 ActionWorker.cla
@@ -1615,13 +1615,28 @@
 g = 
-m.get
+AnnotationUtils.find
 Anno
@@ -1638,24 +1638,27 @@
 dAnnotation(
+m, 
 ActionMappin

change annotation get