View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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  public class CubbyConventionImpl implements CubbyConvention {
38  
39  	private S2Container container;
40  
41  	private NamingConvention namingConvention;
42  
43  	public void setContainer(final S2Container container) {
44  		this.container = container;
45  	}
46  
47  	public void setNamingConvention(final NamingConvention namingConvention) {
48  		this.namingConvention = namingConvention;
49  	}
50  
51  	public ActionDef fromPathToActionDef(final HttpServletRequest request) {
52  		final String path = CubbyUtils.getPath(request);
53  		final List<String> tokens = new ArrayList<String>();
54  		for (final StringTokenizer tokenizer = new StringTokenizer(path, "/"); tokenizer
55  				.hasMoreTokens();) {
56  			final String token = tokenizer.nextToken();
57  			tokens.add(token);
58  		}
59  		if (tokens.isEmpty()) {
60  			return null;
61  		}
62  
63  		final String methodName = tokens.remove(tokens.size() - 1);
64  
65  		final String actionName = CubbyUtils.join(tokens.iterator(), '_')
66  				+ namingConvention.getActionSuffix();
67  		final ComponentDef componentDef = fromRequestOrActionNameToComponentDef(
68  				request, actionName);
69  		if (componentDef == null) {
70  			return null;
71  		}
72  
73  		final Method method;
74  		try {
75  			method = ClassUtil.getMethod(componentDef.getComponentClass(),
76  					methodName, new Class[0]);
77  		} catch (final NoSuchMethodRuntimeException e) {
78  			return null;
79  		}
80  
81  		final ActionDef actionDef = new ActionDefImpl(componentDef, method);
82  
83  		return actionDef;
84  	}
85  
86  	private ComponentDef fromRequestOrActionNameToComponentDef(
87  			final HttpServletRequest request, final String actionName) {
88  		ComponentDef componentDef = fromRequestToComponentDef(request);
89  		if (componentDef == null) {
90  			componentDef = fromActionNameToComponentDef(actionName);
91  		}
92  		return componentDef;
93  	}
94  
95  	private ComponentDef fromRequestToComponentDef(
96  			final HttpServletRequest request) {
97  		final String actionClassName = (String) request
98  				.getAttribute(ATTR_ACTION_CLASS_NAME);
99  		if (actionClassName == null) {
100 			return null;
101 		}
102 		final Class<?> actionClass = ClassUtil.forName(actionClassName);
103 		if (!container.getRoot().hasComponentDef(actionClass)) {
104 			return null;
105 		}
106 		final ComponentDef componentDef = container.getRoot().getComponentDef(
107 				actionClass);
108 		return componentDef;
109 	}
110 
111 	private ComponentDef fromActionNameToComponentDef(final String actionName) {
112 		if (!container.getRoot().hasComponentDef(actionName)) {
113 			return null;
114 		}
115 		final ComponentDef componentDef = container.getRoot().getComponentDef(
116 				actionName);
117 		return componentDef;
118 	}
119 
120 }