Skip to content

Commit

Permalink
new ActionMapper interface
Browse files Browse the repository at this point in the history
action annotation bug fix

Signed-off-by: 黄小虎 <[email protected]>
  • Loading branch information
velna committed Oct 12, 2013
1 parent f2a77fd commit 99f3f9b
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 138 deletions.
17 changes: 6 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,30 @@
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.2.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.2.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.2.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.2.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
Expand Down Expand Up @@ -162,19 +163,16 @@
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>3.6.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>3.6.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
Expand All @@ -185,25 +183,21 @@
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.6.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.6.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
Expand All @@ -221,6 +215,7 @@
</dependencyManagement>
<modules>
<module>sothis-core</module>
<module>sothis-mvc</module>
<module>sothis-dal</module>
<module>sothis-web</module>
<module>sothis-example</module>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.sothis.core.beans;

public class BeanInstantiationException extends Exception {
public class BeanInstantiationException extends RuntimeException {

private static final long serialVersionUID = 2583283563268973241L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public <T> Class<? extends T> getClass(String key, Class<? extends T> defaultVal
return (Class<T>) Class.forName(value);
}

public Class<?> getClass(String key) throws ClassNotFoundException {
public <T> Class<? extends T> getClass(String key) throws ClassNotFoundException {
return getClass(key, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void stop() {
* 任务Runnable
*/
public void set(Object key, Cron cron, Runnable runnable) {
Date next = cron.next(this.nextDate);
Date next = cron.next(new Date());
lock.lock();
try {
jobs.put(key, new CronJob(cron, runnable));
Expand Down Expand Up @@ -106,7 +106,7 @@ private void next() {
nextKeys.clear();
Date next = null;
for (Map.Entry<Object, CronJob> entry : jobs.entrySet()) {
Date jobNext = entry.getValue().getCron().next(this.nextDate);
Date jobNext = entry.getValue().getCron().next(nextDate);
if (null == next || jobNext.equals(next)) {
nextKeys.add(entry.getKey());
next = jobNext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@ public class CrondTest {
public void test() throws InterruptedException {
Crond crond = new Crond();
crond.start();
crond.set(1, Cron.compile("*/5 * * * * *"), new Runnable() {
crond.set(1, Cron.compile("*/20 * * * * *"), new Runnable() {
@Override
public void run() {
LOGGER.info("running 1");
}
});
Thread.sleep(10000);
crond.set(2, Cron.compile("*/10 * * * * *"), new Runnable() {
@Override
public void run() {
LOGGER.info("running 2");
}
});
Thread.sleep(10000);
crond.set(3, Cron.compile("*/20 * * * * *"), new Runnable() {
crond.set(3, Cron.compile("*/5 * * * * *"), new Runnable() {
@Override
public void run() {
LOGGER.info("running 3");
}
});
Thread.sleep(25000);
crond.stop();
Thread.sleep(30000);
Thread.sleep(10000);
}
}
1 change: 1 addition & 0 deletions sothis-dal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
Expand Down
8 changes: 8 additions & 0 deletions sothis-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sothis-example/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>fangjia-coresite</display-name>
<display-name>sothis-example</display-name>

<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.fallbackLocale</param-name>
Expand Down
2 changes: 1 addition & 1 deletion sothis-example/src/main/webapp/index.ftl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Hello ${message!"yo !"}
<@d_link id="link" controller="test" params={"a":message!"yo!"}>abc</@d_link>
<@d_link id="link" controller="" params={"message":message!"haha !"}>abc</@d_link>
46 changes: 30 additions & 16 deletions sothis-web/src/main/java/org/sothis/web/mvc/AbstractActionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.util.Collections;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
Expand All @@ -12,32 +14,38 @@
public abstract class AbstractActionBase implements ActionBase {

private final InterceptorStack interceptorStack;
private final Map<Class<?>, Annotation> annotations;
private final Map<Class<?>, List<Annotation>> annotations;

public AbstractActionBase(AnnotatedElement... elements) {
if (null != elements) {
this.annotations = new HashMap<Class<?>, Annotation>(4);
for (AnnotatedElement ae : elements) {
public AbstractActionBase(AnnotatedElement... parents) {
this.annotations = new HashMap<Class<?>, List<Annotation>>(4);
if (null != parents) {
for (AnnotatedElement ae : parents) {
if (null != ae) {
for (Annotation a : ae.getAnnotations()) {
this.annotations.put(a.annotationType(), a);
List<Annotation> as = this.annotations.get(a.annotationType());
if (null == as) {
as = new ArrayList<Annotation>();
this.annotations.put(a.annotationType(), as);
}
as.add(a);
}
}
}
} else {
this.annotations = Collections.emptyMap();
}

Sothis sothis = getAnnotation(Sothis.class);
Sothis[] ss = getAnnotation(Sothis.class);
SothisConfig config = SothisConfig.getConfig();
if (null == config) {
throw new NullPointerException("sothis config not initialized !");
}
if (null != sothis && StringUtils.isNotBlank(sothis.stack())) {
this.interceptorStack = config.getInterceptorStack(sothis.stack());
} else {
this.interceptorStack = config.getDefaultInterceptorStack();
InterceptorStack stack = null;
for (Sothis s : ss) {
if (null != s && StringUtils.isNotBlank(s.stack())) {
stack = config.getInterceptorStack(s.stack());
break;
}
}
this.interceptorStack = stack == null ? config.getDefaultInterceptorStack() : stack;
}

@Override
Expand All @@ -47,8 +55,14 @@ public InterceptorStack getInterceptorStack() {

@SuppressWarnings("unchecked")
@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return (T) annotations.get(annotationClass);
public <T extends Annotation> T[] getAnnotation(Class<T> annotationClass) {
List<Annotation> as = this.annotations.get(annotationClass);
if (null == as) {
return (T[]) Array.newInstance(annotationClass, 0);
}
Annotation[] ret = new Annotation[as.size()];
this.annotations.values().toArray(ret);
return (T[]) ret;
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions sothis-web/src/main/java/org/sothis/web/mvc/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ public interface Action extends ActionBase {
boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);

/**
* 得到在action method或controller class或controller package中出现的注解<br> {@inheritDoc}
* 得到在action method或controller class或controller package中出现的注解<br>
* {@inheritDoc}
*/
<T extends Annotation> T getAnnotation(Class<T> annotationClass);
<T extends Annotation> T[] getAnnotation(Class<T> annotationClass);

/**
* 得到所有在action method、controller class和controller package中声明的注解<br>
Expand All @@ -58,7 +59,8 @@ public interface Action extends ActionBase {
Annotation[] getAnnotations();

/**
* 同 {@link Action#getAnnotations()}<br> {@inheritDoc}
* 同 {@link Action#getAnnotations()}<br>
* {@inheritDoc}
*/
Annotation[] getDeclaredAnnotations();
}
11 changes: 9 additions & 2 deletions sothis-web/src/main/java/org/sothis/web/mvc/ActionBase.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.sothis.web.mvc;

import java.lang.reflect.AnnotatedElement;
import java.lang.annotation.Annotation;

public interface ActionBase extends AnnotatedElement {
public interface ActionBase {

/**
* 名称
Expand All @@ -20,4 +20,11 @@ public interface ActionBase extends AnnotatedElement {

InterceptorStack getInterceptorStack();

boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);

<T extends Annotation> T[] getAnnotation(Class<T> annotationClass);

Annotation[] getAnnotations();

Annotation[] getDeclaredAnnotations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class ActionContext {
public final static String SOTHIS_CONFIG = "org.sothis.web.mvc.SOTHIS_CONFIG";
public final static String MODEL_AND_VIEW_RESOLVER = "org.sothis.web.mvc.MODEL_AND_VIEW_RESOLVER";
public final static String ACTION_MAPPER = "org.sothis.web.mvc.ACTION_MAPPER";
public final static String ACTIONS = "org.sothis.web.mvc.ACTIONS";
public final static String ACTION_STORE = "org.sothis.web.mvc.ACTION_STORE";
public final static String ACTION_URI = "org.sothis.web.mvc.ActionUri";
public final static String EXCEPTION_HANDLER = "org.sothis.web.mvc.ExceptionHandler";
public final static String FLASH = "org.sothis.web.mvc.FLAH";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static void actionContextCheck(ActionContext context) {
checkContextKeyValue(context, ActionContext.SOTHIS_CONFIG, SothisConfig.class);
checkContextKeyValue(context, ActionContext.MODEL_AND_VIEW_RESOLVER, ModelAndViewResolver.class);
checkContextKeyValue(context, ActionContext.ACTION_MAPPER, ActionMapper.class);
checkContextKeyValue(context, ActionContext.ACTIONS, Map.class);
checkContextKeyValue(context, ActionContext.ACTION_STORE, ActionStore.class);
}

private static void checkContextKeyValue(ActionContext context, String key, Class<?> valueClass) {
Expand All @@ -76,12 +76,11 @@ private static void checkContextKeyValue(ActionContext context, String key, Clas
private static ActionInvocation prepareActionInvocation(ActionContext context, HttpServletRequest request, HttpServletResponse response, SothisConfig config)
throws IOException, BeanInstantiationException, ServletException {
ActionMapper actionMapper = (ActionMapper) context.get(ActionContext.ACTION_MAPPER);
Map<String, Action> actions = (Map<String, Action>) context.get(ActionContext.ACTIONS);
ActionStore actions = (ActionStore) context.get(ActionContext.ACTION_STORE);

context.setParameters(new HashMap<String, Object[]>(request.getParameterMap()));

String actionKey = actionMapper.resolve(request, response);
Action action = actions.get(actionKey);
Action action = actionMapper.resolve(request, response, actions);
if (null == action) {
return null;
}
Expand Down
19 changes: 9 additions & 10 deletions sothis-web/src/main/java/org/sothis/web/mvc/ActionMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@
*
*/
public interface ActionMapper {

/**
* 根据package名称、controller名称和action名称生成action的唯一ID
* 根据action生成action的唯一ID
*
* @param action
* 需要映射的action
*
* @param packageName
* 并不是完整的包名,是去掉包前缀后剩余的部分
* @param controllerName
* 去掉后缀Controller后的部分
* @param actionName
* 去掉Action后缀后的部分
* @return 返回action的唯一ID,返回null将导致NullPointerException
* @throws IllegalArgumentException
* 参数为null或空字符串时,抛出该异常
*/
String map(String controllerPackageName, Class<?> controllerClass, String actionName);
Object map(Action action);

/**
* 根据请求生成action的唯一ID
* 根据请求从actionStore中找到对应的action
*
* @param request
* @param response
* @param store
* @return 返回null则表示无法解析,将导致404,除非response已经被commit
* @throws NullPointerException
* request或response为null时,抛出该异常
*/
String resolve(HttpServletRequest request, HttpServletResponse response);
Action resolve(HttpServletRequest request, HttpServletResponse response, ActionStore store);
}
Loading

0 comments on commit 99f3f9b

Please sign in to comment.