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