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.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   * @author baba
39   * @since 1.0.0
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  	 * @param container
54  	 *            コンテナ
55  	 */
56  	public void setContainer(final S2Container container) {
57  		this.container = container;
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
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  }