1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.convention.impl; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.ATTR_ACTION_CLASS_NAME; |
19 | |
|
20 | |
import java.lang.reflect.Method; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.List; |
23 | |
import java.util.StringTokenizer; |
24 | |
|
25 | |
import javax.servlet.http.HttpServletRequest; |
26 | |
|
27 | |
import org.seasar.cubby.controller.ActionDef; |
28 | |
import org.seasar.cubby.controller.impl.ActionDefImpl; |
29 | |
import org.seasar.cubby.convention.CubbyConvention; |
30 | |
import org.seasar.cubby.util.CubbyUtils; |
31 | |
import org.seasar.framework.container.ComponentDef; |
32 | |
import org.seasar.framework.container.S2Container; |
33 | |
import org.seasar.framework.convention.NamingConvention; |
34 | |
import org.seasar.framework.exception.NoSuchMethodRuntimeException; |
35 | |
import org.seasar.framework.util.ClassUtil; |
36 | |
|
37 | 39 | public class CubbyConventionImpl implements CubbyConvention { |
38 | |
|
39 | |
private S2Container container; |
40 | |
|
41 | |
private NamingConvention namingConvention; |
42 | |
|
43 | |
public void setContainer(final S2Container container) { |
44 | 38 | this.container = container; |
45 | 38 | } |
46 | |
|
47 | |
public void setNamingConvention(final NamingConvention namingConvention) { |
48 | 38 | this.namingConvention = namingConvention; |
49 | 38 | } |
50 | |
|
51 | |
public ActionDef fromPathToActionDef(final HttpServletRequest request) { |
52 | 1 | final String path = CubbyUtils.getPath(request); |
53 | 1 | final List<String> tokens = new ArrayList<String>(); |
54 | 1 | for (final StringTokenizer tokenizer = new StringTokenizer(path, "/"); tokenizer |
55 | 3 | .hasMoreTokens();) { |
56 | 2 | final String token = tokenizer.nextToken(); |
57 | 2 | tokens.add(token); |
58 | 2 | } |
59 | 1 | if (tokens.isEmpty()) { |
60 | 0 | return null; |
61 | |
} |
62 | |
|
63 | 1 | final String methodName = tokens.remove(tokens.size() - 1); |
64 | |
|
65 | 1 | final String actionName = CubbyUtils.join(tokens.iterator(), '_') |
66 | |
+ namingConvention.getActionSuffix(); |
67 | 1 | final ComponentDef componentDef = fromRequestOrActionNameToComponentDef( |
68 | |
request, actionName); |
69 | 1 | if (componentDef == null) { |
70 | 1 | return null; |
71 | |
} |
72 | |
|
73 | |
final Method method; |
74 | |
try { |
75 | 0 | method = ClassUtil.getMethod(componentDef.getComponentClass(), |
76 | |
methodName, new Class[0]); |
77 | 0 | } catch (final NoSuchMethodRuntimeException e) { |
78 | 0 | return null; |
79 | 0 | } |
80 | |
|
81 | 0 | final ActionDef actionDef = new ActionDefImpl(componentDef, method); |
82 | |
|
83 | 0 | return actionDef; |
84 | |
} |
85 | |
|
86 | |
private ComponentDef fromRequestOrActionNameToComponentDef( |
87 | |
final HttpServletRequest request, final String actionName) { |
88 | 1 | ComponentDef componentDef = fromRequestToComponentDef(request); |
89 | 1 | if (componentDef == null) { |
90 | 1 | componentDef = fromActionNameToComponentDef(actionName); |
91 | |
} |
92 | 1 | return componentDef; |
93 | |
} |
94 | |
|
95 | |
private ComponentDef fromRequestToComponentDef( |
96 | |
final HttpServletRequest request) { |
97 | 1 | final String actionClassName = (String) request |
98 | |
.getAttribute(ATTR_ACTION_CLASS_NAME); |
99 | 1 | if (actionClassName == null) { |
100 | 1 | return null; |
101 | |
} |
102 | 0 | final Class<?> actionClass = ClassUtil.forName(actionClassName); |
103 | 0 | if (!container.getRoot().hasComponentDef(actionClass)) { |
104 | 0 | return null; |
105 | |
} |
106 | 0 | final ComponentDef componentDef = container.getRoot().getComponentDef( |
107 | |
actionClass); |
108 | 0 | return componentDef; |
109 | |
} |
110 | |
|
111 | |
private ComponentDef fromActionNameToComponentDef(final String actionName) { |
112 | 1 | if (!container.getRoot().hasComponentDef(actionName)) { |
113 | 1 | return null; |
114 | |
} |
115 | 0 | final ComponentDef componentDef = container.getRoot().getComponentDef( |
116 | |
actionName); |
117 | 0 | return componentDef; |
118 | |
} |
119 | |
|
120 | |
} |