1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.controller.impl;
17
18 import static org.seasar.cubby.CubbyConstants.INTERNAL_FORWARD_DIRECTORY;
19
20 import java.lang.reflect.Method;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import org.seasar.cubby.controller.ActionDef;
27 import org.seasar.cubby.controller.ActionDefBuilder;
28 import org.seasar.cubby.util.CubbyUtils;
29 import org.seasar.framework.container.ComponentDef;
30 import org.seasar.framework.container.S2Container;
31 import org.seasar.framework.exception.ClassNotFoundRuntimeException;
32 import org.seasar.framework.exception.NoSuchMethodRuntimeException;
33 import org.seasar.framework.util.ClassUtil;
34
35
36
37
38
39
40
41 public class ActionDefBuilderImpl implements ActionDefBuilder {
42
43
44 private static final Pattern INTERNAL_FORWARD_PATH_PATTERN = Pattern
45 .compile("^/" + INTERNAL_FORWARD_DIRECTORY + "/(.*)/(.*)$");
46
47
48 private S2Container container;
49
50
51
52
53
54
55
56 public void setContainer(final S2Container container) {
57 this.container = container;
58 }
59
60
61
62
63 public ActionDef build(final HttpServletRequest request) {
64 final String path = CubbyUtils.getPath(request);
65 final Matcher matcher = INTERNAL_FORWARD_PATH_PATTERN.matcher(path);
66 if (!matcher.matches()) {
67 return null;
68 }
69 final String actionClassName = matcher.group(1);
70 final Class<?> actionClass;
71 try {
72 actionClass = ClassUtil.forName(actionClassName);
73 } catch (final ClassNotFoundRuntimeException e) {
74 return null;
75 }
76 if (!container.getRoot().hasComponentDef(actionClass)) {
77 return null;
78 }
79 final ComponentDef componentDef = container.getRoot().getComponentDef(
80 actionClass);
81
82 final String methodName = matcher.group(2);
83 final Method method;
84 try {
85 method = ClassUtil.getMethod(componentDef.getComponentClass(),
86 methodName, new Class[0]);
87 } catch (final NoSuchMethodRuntimeException e) {
88 return null;
89 }
90
91 final ActionDef actionDef = new ActionDefImpl(componentDef, method);
92
93 return actionDef;
94 }
95
96 }