View Javadoc

1   /*
2    * Copyright 2004-2006 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.tuigwaa.plugin;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.lang.reflect.Method;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javassist.CannotCompileException;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.seasar.tuigwaa.controller.ControllerService;
31  import org.seasar.tuigwaa.model.DAOService;
32  import org.seasar.tuigwaa.model.ModelService;
33  import org.seasar.tuigwaa.model.common.EntityDAO;
34  import org.seasar.tuigwaa.model.core.TgwEntity;
35  import org.seasar.tuigwaa.system.Constants;
36  
37  public class WebAppli {
38  
39  	public static final String THIS_PAGE = "/**THIS PAGE*/";
40  
41  	public static final String HIDDEN_PAGE_PARAM = Constants.PARAM_PAGENAME;
42  
43  	private String prefix;
44  
45  	private String name;
46  
47  	private ActionPlugin action;
48  
49  	private Method actionMethod;
50  
51  	private Plugin view;
52  
53  	private Class actionClass;
54  
55  	private Class viewClass;
56  
57  	private Class dtoClass;
58  
59  	private Class pojoClass;
60  
61  	private Map daoMap = new HashMap();
62  
63  	// private ThreadLocal message = new ThreadLocal();
64  
65  	private ModelService model;
66  
67  	private DAOService daoManager;
68  
69  	private ControllerService controller;
70  	
71  	private Log log = LogFactory.getLog(getClass());
72  		
73  	public WebAppli(ModelService model, DAOService daoManager, ControllerService controller) {
74  		this.model = model;
75  		this.daoManager = daoManager;
76  		this.controller = controller;
77  	}
78  
79  	public void autoRegisterByAction(ActionPlugin action)
80  			throws ClassNotFoundException, SecurityException,
81  			NoSuchMethodException {
82  		setAction(action);
83  
84  		this.name = createNameByAction(action);
85  
86  		this.actionClass = action.getClass();
87  		this.viewClass = getReplacedClass(actionClass, "Action", "View");
88  		this.actionMethod = findExecuteMethod();
89  		this.dtoClass = findDtoClass();
90  	}
91  
92  	public void setPrefix(String prefix) {
93  		this.prefix = prefix;
94  	}
95  
96  	public void setView(Plugin view) {
97  		this.view = view;
98  		view.setWebAppli(this);
99  	}
100 
101 	public void setAction(ActionPlugin action) {
102 		this.action = action;
103 		action.setWebAppli(this);
104 	}
105 
106 	public Plugin getView() {
107 		return view;
108 	}
109 
110 	public void setPojoClass(Class pojoClass) {
111 		this.pojoClass = pojoClass;
112 	}
113 
114 	public DaoPlugin getDao(String siteName) {
115 		DaoPlugin dao = (DaoPlugin) daoMap.get(siteName);
116 		if (dao == null) {
117 			dao = createDao(siteName);
118 			daoMap.put(siteName, dao);
119 		}
120 		return dao;
121 	}
122 
123 	public String getPath() {
124 		if (prefix != null) {
125 			return prefix + "." + name;
126 		}
127 		return name;
128 	}
129 
130 	public Class getDtoClass() {
131 		return dtoClass;
132 	}
133 
134 	public Class getViewClass() {
135 		return viewClass;
136 	}
137 
138 	public String invoke(Object dto, HttpServletRequest req,
139 			HttpServletResponse res) throws IllegalArgumentException,
140 			IllegalAccessException, InvocationTargetException {
141 		
142 		Object[] args = new Object[] { dto, req, res };
143 		String retPageName = (String) actionMethod.invoke(action, args);
144 		if (THIS_PAGE.equals(retPageName)) {
145 			
146 			String thisPageName = req.getParameter(HIDDEN_PAGE_PARAM);
147 			if(thisPageName == null || "".equals(thisPageName)){// for multipart request
148 				thisPageName = (String)req.getAttribute(Constants.RATTR_PAGENAME);
149 			}
150 			
151 			return thisPageName;
152 		}
153 		return retPageName;
154 	}
155 	
156 	public TgwEntity getEntity(String siteName, String entityName){		
157 		return model.getEntity(siteName,entityName);		
158 	}	
159 
160 	// [Start] ------ Private Method -------
161 
162 	private String createNameByAction(ActionPlugin action) {
163 		String packageName = action.getClass().getPackage().getName();
164 		String className = action.getClass().getName();
165 		className = removeInnerClassName(className);
166 		returng> className.substring(packageName.length() + 1,
167 				className.length() - "Action".length()).toLowerCase();
168 	}
169 
170 	private Class getReplacedClass(Class clazz, String from, String to)
171 			throws ClassNotFoundException {
172 		String newClassName = clazz.getName().replaceAll(from, to);
173 		newClassName = removeInnerClassName(newClassName);
174 		return Class.forName(newClassName);
175 	}
176 
177 	private DaoPlugin createDao(String siteName) {
178 		DaoPlugin dao = null;
179 		try {
180 			if (pojoClass == null) {
181 				pojoClass = getReplacedClass(actionClass, "Action", "");
182 			}
183 			String entityName = "tgw_plugin_" + name;
184 			TgwEntity entity = model.getEntity(siteName, entityName);
185 			if (entity == null) {
186 				entity = model.readJavaClass(siteName, entityName, pojoClass);
187 				model.createEntity(entity);
188 				controller.addEntityConfig(entity);
189 			}			
190 			EntityDAO entityDao = daoManager.getDAO(entity);			
191 			try{
192 				Class daoClass = getReplacedClass(actionClass, "Action", "Dao");
193 				dao = (DaoPlugin) daoClass.getConstructor(
194 						new Class[] { EntityDAO.class }).newInstance(
195 						new Object[] { entityDao });				
196 			}catch(ClassNotFoundException cnfe){ // standard DaoPlugin class
197 				dao = new DaoPlugin(entityDao);				
198 			}			
199 
200 		}catch(ClassNotFoundException cnfe){
201 			log.error("failed to loading class : " + cnfe.getMessage());
202 		}catch (CannotCompileException e) {
203 			//
204 		} catch (IllegalArgumentException e) {
205 			//
206 		} catch (SecurityException e) {
207 			//
208 		} catch (InstantiationException e) {
209 			//
210 		} catch (IllegalAccessException e) {
211 			//
212 		} catch (InvocationTargetException e) {
213 			//
214 		} catch (NoSuchMethodException e) {
215 			//
216 		}
217 		return dao;
218 	}
219 	
220 	private String removeInnerClassName(String className){
221 		int index = className.indexOf("$$");
222 		if(index > 0){
223 			return className.substring(0, index);
224 		}
225 		return className;
226 	}
227 	
228 	private Method findExecuteMethod(){
229 		Method[] methods = actionClass.getMethods();
230 		for(int i=0; i<methods.length;i++){
231 			if(methods[i].getName().equals("execute")){
232 				return methods[i];
233 			}
234 		}
235 		return null;
236 	}
237 	
238 	private Class findDtoClass(){
239 		if(actionMethod == null){
240 			return null;
241 		}
242 		Class[] classes = actionMethod.getParameterTypes();
243 		if(classes.length>2){
244 			return classes[0];
245 		}
246 		return null;
247 	}
248 }