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 | 42 | public class ActionDefBuilderImpl implements ActionDefBuilder { |
42 | |
|
43 | |
|
44 | 1 | 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 | 41 | this.container = container; |
58 | 41 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public ActionDef build(final HttpServletRequest request) { |
64 | 5 | final String path = CubbyUtils.getPath(request); |
65 | 5 | final Matcher matcher = INTERNAL_FORWARD_PATH_PATTERN.matcher(path); |
66 | 5 | if (!matcher.matches()) { |
67 | 1 | return null; |
68 | |
} |
69 | 4 | final String actionClassName = matcher.group(1); |
70 | |
final Class<?> actionClass; |
71 | |
try { |
72 | 4 | actionClass = ClassUtil.forName(actionClassName); |
73 | 1 | } catch (final ClassNotFoundRuntimeException e) { |
74 | 1 | return null; |
75 | 3 | } |
76 | 3 | if (!container.getRoot().hasComponentDef(actionClass)) { |
77 | 0 | return null; |
78 | |
} |
79 | 3 | final ComponentDef componentDef = container.getRoot().getComponentDef( |
80 | |
actionClass); |
81 | |
|
82 | 3 | final String methodName = matcher.group(2); |
83 | |
final Method method; |
84 | |
try { |
85 | 3 | method = ClassUtil.getMethod(componentDef.getComponentClass(), |
86 | |
methodName, new Class[0]); |
87 | 2 | } catch (final NoSuchMethodRuntimeException e) { |
88 | 2 | return null; |
89 | 1 | } |
90 | |
|
91 | 1 | final ActionDef actionDef = new ActionDefImpl(componentDef, method); |
92 | |
|
93 | 1 | return actionDef; |
94 | |
} |
95 | |
|
96 | |
} |