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.controller;
17  
18  import java.beans.PropertyDescriptor;
19  import java.lang.reflect.InvocationTargetException;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.ResourceBundle;
28  
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.commons.beanutils.PropertyUtils;
34  import org.apache.struts.Globals;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  import org.apache.struts.action.DynaActionForm;
38  import org.apache.struts.config.ActionConfig;
39  import org.apache.struts.config.FormBeanConfig;
40  import org.apache.struts.config.FormPropertyConfig;
41  import org.apache.struts.config.ForwardConfig;
42  import org.apache.struts.config.ModuleConfig;
43  import org.seasar.tuigwaa.controller.config.CustomFormConfigGenerator;
44  import org.seasar.tuigwaa.controller.config.EntityConfigGenerator;
45  import org.seasar.tuigwaa.controller.config.ModuleConfigMetadata;
46  import org.seasar.tuigwaa.controller.config.SearchConfigGenerator;
47  import org.seasar.tuigwaa.controller.config.TableConfigGenerator;
48  import org.seasar.tuigwaa.database.function.SearchExeFunction;
49  import org.seasar.tuigwaa.database.function.UpdateExeFunction;
50  import org.seasar.tuigwaa.model.core.TgwEntity;
51  import org.seasar.tuigwaa.plugin.WebAppli;
52  import org.seasar.tuigwaa.security.Action;
53  import org.seasar.tuigwaa.system.SiteConfig;
54  import org.seasar.tuigwaa.system.TgwServiceException;
55  import org.seasar.tuigwaa.util.TgwNameUtils;
56  import org.seasar.tuigwaa.util.TgwUtils;
57  
58  import com.isenshi.util.TableMap;
59  import com.isenshi.util.converter.ConverterResource;
60  import com.isenshi.util.extlib.StrutsUtil;
61  
62  /***
63   * @author nishioka
64   */
65  public class ControllerServiceStrutsImpl implements ControllerService {
66  
67  	private static final String MODULE_CONFIG_NAME = "/WEB-INF/struts-config-site-prototype.xml";
68  
69  	private static final String SECURITY_PROPS = "config/action_security";
70  
71  	private static ResourceBundle actionSecurity = ResourceBundle
72  			.getBundle(SECURITY_PROPS);;
73  
74  	private ConverterResource converterResource_;
75  
76  	private Map formSetCacheMap_ = Collections.synchronizedMap(new HashMap());
77  
78  	private Map metadataMap = new HashMap();
79  
80  	private Map webAppliMap = new HashMap();
81  
82  	private List prototypeWebapplis = new ArrayList();
83  
84  	private TableMap methodPathMap = new TableMap();
85  
86  	private TableMap customFormPathMap = new TableMap();
87  
88  	public ControllerServiceStrutsImpl(ConverterResource converterResource) {
89  		this.converterResource_ = converterResource;
90  	}
91  
92  	private void registerMethodPath(String siteName, String path,
93  			String methodName) {
94  		methodPathMap.put(methodName, siteName, path);
95  	}
96  
97  	private void registerCustomFormNamePath(String siteName, String path,
98  			String customFormName) {
99  		customFormPathMap.put(customFormName, siteName, path);
100 	}
101 
102 	public String getMethodName(String siteName, String path) {
103 		return (String) methodPathMap.get(siteName, path);
104 	}
105 
106 	public void createSite(SiteConfig siteConfig) {
107 		String siteName = siteConfig.getName();
108 		String prefix = "/" + siteName;
109 		try {
110 			TuigwaaActionServlet servlet = TgwUtils.getTuigwaaActionServlet();
111 			ModuleConfig config = servlet.createModuleConfig(prefix,
112 					MODULE_CONFIG_NAME);
113 			ModuleConfigMetadata metadata = new ModuleConfigMetadata(siteName,
114 					config);
115 			putMetadata(siteName, metadata);
116 
117 			for (Iterator i = prototypeWebapplis.iterator(); i.hasNext();) {
118 				WebAppli appli = (WebAppli) i.next();
119 				addPluginConfig(siteName, appli);
120 			}
121 		} catch (ServletException e) {
122 			e.printStackTrace();
123 			throw new RuntimeException(e);
124 		}
125 		StrutsUtil.addPrefix(prefix);
126 	}
127 
128 	public void deleteSite(String siteName) {
129 		converterResource_.deregister(siteName);
130 		ModuleConfigMetadata metadata = (ModuleConfigMetadata) metadataMap
131 				.remove(siteName);
132 		ModuleConfig config = metadata.getModuleConfig();
133 		TgwUtils.getTuigwaaActionServlet().getServletContext().removeAttribute(
134 				Globals.MODULE_KEY + config.getPrefix());
135 		StrutsUtil.removePrefix(config.getPrefix());
136 	}
137 
138 	public String getSearchActionName(TgwEntity entity, SearchExeFunction exe) {
139 		String siteName = entity.getDomainName();
140 		ModuleConfigMetadata metadata = getMetadata(siteName);
141 		return metadata.getSearchActionName(entity.getName(), exe.getName());
142 	}
143 
144 	public String getSearchFormName(TgwEntity entity, SearchExeFunction exe) {
145 		String siteName = entity.getDomainName();
146 		ModuleConfigMetadata metadata = getMetadata(siteName);
147 		return metadata.getSearchFormName(entity.getName(), exe.getName());
148 	}
149 
150 	public String getCustomActionName(TgwEntity entity,
151 			UpdateExeFunction updateExe) {
152 		String siteName = entity.getDomainName();
153 		ModuleConfigMetadata metadata = getMetadata(siteName);
154 		return metadata.getCustomActionName(entity.getName(), updateExe
155 				.getName());
156 	}
157 
158 	public String getCustomFormName(String siteName, String path) {
159 		return (String) customFormPathMap.get(siteName, path);
160 	}
161 
162 	public String getCustomFormName(TgwEntity entity,
163 			UpdateExeFunction updateExe) {
164 		String siteName = entity.getDomainName();
165 		ModuleConfigMetadata metadata = getMetadata(siteName);
166 		return metadata
167 				.getCustomFormName(entity.getName(), updateExe.getName());
168 	}
169 
170 	public void setActionRoles(String siteName, Action action, String roles) {
171 		ModuleConfigMetadata metadata = getMetadata(siteName);
172 		ModuleConfig mconfig = metadata.getModuleConfig();
173 		String actions = actionSecurity.getString(action.getActionName());
174 		String[] actionArray = actions.split(",");
175 		for (int i = 0; i < actionArray.length; i++) {
176 			try {
177 				ActionConfig aconfig = mconfig.findActionConfig(actionArray[i]);
178 				aconfig.setRoles(roles);
179 			} catch (Exception e) {
180 				e.printStackTrace();
181 			}
182 		}
183 	}
184 
185 	public String addSearchConfig(TgwEntity entity,
186 			SearchExeFunction searchExeFunction) {
187 		String domain = entity.getDomainName();
188 		ModuleConfigMetadata metadata = getMetadata(domain);
189 		SearchConfigGenerator generator = new SearchConfigGenerator(
190 				converterResource_, entity, metadata);
191 		generator.generate(searchExeFunction);
192 		String searchPath = generator.getActionName();
193 		registerMethodPath(domain, searchPath, searchExeFunction.getName());
194 		return searchPath;
195 	}
196 
197 	public void removeSearchConfig(TgwEntity entity, String searchExeName) {
198 		ModuleConfigMetadata metadata = getMetadata(entity.getDomainName());
199 		metadata.removeSearchConfig(entity, searchExeName);
200 		converterResource_.deregister(entity.getDomainName(), searchExeName);
201 	}
202 
203 	public String addCustomFormConfig(TgwEntity entity,
204 			UpdateExeFunction updateExe) {
205 		String schema = entity.getDomainName();
206 		ModuleConfigMetadata metadata = getMetadata(schema);
207 		CustomFormConfigGenerator generator = new CustomFormConfigGenerator(
208 				converterResource_, entity, metadata, updateExe,
209 				formSetCacheMap_);
210 		generator.generate();
211 
212 		registerCustomFormNamePath(schema, generator.getActionName(), updateExe
213 				.getName());
214 
215 		TableConfigGenerator tgenerator = new TableConfigGenerator(generator,
216 				converterResource_, metadata, formSetCacheMap_);
217 		tgenerator.generate();
218 
219 		registerCustomFormNamePath(schema, tgenerator.getActionName(),
220 				getCustomFormName(entity, updateExe));
221 
222 		return generator.getActionName();
223 	}
224 
225 	public void removeCustomFormConfig(TgwEntity entity, String customFormName) {
226 		ModuleConfigMetadata metadata = getMetadata(entity.getDomainName());
227 		metadata.removeCustomFormConfig(entity, customFormName);
228 		converterResource_.deregister(entity.getDomainName(), customFormName);
229 	}
230 
231 	public void addEntityConfigs(Collection entities) {
232 		if (entities == null) {
233 			return;
234 		}
235 		for (Iterator i = entities.iterator(); i.hasNext();) {
236 			TgwEntity entity = (TgwEntity) i.next();
237 			addEntityConfig(entity);
238 		}
239 	}
240 
241 	public void addEntityConfig(TgwEntity entity) {
242 		String schema = entity.getDomainName();
243 		ModuleConfigMetadata metadata = getMetadata(schema);
244 
245 		EntityConfigGenerator generator = new EntityConfigGenerator(
246 				converterResource_, entity, metadata, formSetCacheMap_);
247 		generator.generate();
248 	}
249 
250 	public void alterEntityConfigs(Collection entities) {
251 		if (entities == null) {
252 			return;
253 		}
254 		for (Iterator i = entities.iterator(); i.hasNext();) {
255 			TgwEntity entity = (TgwEntity) i.next();
256 			alterEntityConfig(entity);
257 		}
258 	}
259 
260 	public void alterEntityConfig(TgwEntity entity) {
261 		removeEntityConfig(entity.getDomainName(), entity.getName());
262 		addEntityConfig(entity);
263 	}
264 
265 	public void removeEntityConfig(String siteName, String entityName) {
266 		ModuleConfigMetadata metadata = getMetadata(siteName);
267 		metadata.removeEntityConfig(siteName, entityName);
268 
269 		// Can't remove validatorResource
270 		// ValidatorResources resource = (ValidatorResources) StrutsUtil
271 		// .getValidatorResources(moduleConfig);
272 
273 		converterResource_.deregister(siteName, TgwNameUtils
274 				.toFormName(entityName));
275 	}
276 
277 	// plugin
278 
279 	public void addPluginConfig(String siteName, WebAppli appli) {
280 
281 		String path = "/plugin." + appli.getPath();
282 
283 		ActionMapping mapping = new ActionMapping();
284 		mapping.setPath(path);
285 		mapping.setParameter(appli.getDtoClass().getName());
286 		mapping.setType(PluginProxyAction.class.getName());
287 		mapping.setName("plugin." + appli.getPath());
288 
289 		FormBeanConfig fconfig = new FormBeanConfig();
290 		fconfig.setType(DynaActionForm.class.getName());
291 		fconfig.setName("plugin." + appli.getPath());
292 
293 		ModuleConfig moduleConfig = getMetadata(siteName).getModuleConfig();
294 		mapping.setModuleConfig(moduleConfig);
295 		moduleConfig.addActionConfig(mapping);
296 		moduleConfig.addFormBeanConfig(fconfig);
297 
298 		PropertyDescriptor[] descs = PropertyUtils.getPropertyDescriptors(appli
299 				.getDtoClass());
300 		for (int i = 0; i < descs.length; i++) {
301 			PropertyDescriptor desc = descs[i];
302 			if (desc.getReadMethod() != null && desc.getWriteMethod() != null) {
303 				FormPropertyConfig prop = new FormPropertyConfig();
304 				prop.setName(desc.getName());
305 				prop.setType(desc.getPropertyType().getName());
306 				fconfig.addFormPropertyConfig(prop);
307 			}
308 		}
309 		webAppliMap.put(path, appli);
310 	}
311 
312 	public String proxyAction(String path, Object dto, HttpServletRequest req,
313 			HttpServletResponse res) throws IllegalArgumentException,
314 			IllegalAccessException, InvocationTargetException {
315 		WebAppli appli = (WebAppli) webAppliMap.get(path);
316 		return appli.invoke(dto, req, res);
317 	}
318 
319 	public void addPrototypeWebAppli(WebAppli webAppli) {
320 		prototypeWebapplis.add(webAppli);
321 	}
322 	
323 	
324 	public void addForwardPath(String siteName, String path, String forwardName, String forwardPath) throws TgwServiceException{
325 		
326 		ActionConfig actionConfig = getActionConfig(siteName,path);
327 		
328 		// null-check of actionConfig
329 		if(actionConfig == null){
330 			throw new TgwServiceException("action config for " + path + " should not be null");
331 		}
332 		
333 		ForwardConfig forwardConfig = actionConfig.findForwardConfig(forwardName);
334 		if(forwardConfig == null){
335 			forwardConfig = new ActionForward(forwardName, forwardPath, false);
336 			actionConfig.addForwardConfig(forwardConfig);						
337 		}		
338 	}
339 	
340 	public void removeForwardPath(String siteName, String path, String forwardName, String forwardPath) throws TgwServiceException{		
341 		ActionConfig actionConfig = getActionConfig(siteName, path);		
342 		ForwardConfig forwardConfig = actionConfig.findForwardConfig(forwardName);
343 		
344 		if(actionConfig == null){
345 			throw new TgwServiceException("action config for " + path + " should not be null");
346 		}
347 		
348 		if(forwardConfig != null){
349 			actionConfig.removeForwardConfig(forwardConfig);
350 		}						
351 	}
352 	
353 	public String[] getForwardNames(String siteName, String path){
354 		List nameList = new ArrayList();		
355 		ActionConfig actionConfig = getActionConfig(siteName, path);
356 		ForwardConfig[] configs = actionConfig.findForwardConfigs();
357 		
358 		for(int i=0;i<configs.length;i++){
359 			ForwardConfig config = configs[i];
360 			String name = config.getName();
361 			nameList.add(name);
362 		}		
363 		return (String[]) nameList.toArray(new String[nameList.size()]);
364 	}			
365 
366 
367 	public ModuleConfigMetadata getMetadata(String siteName) {
368 		return (ModuleConfigMetadata) metadataMap.get(siteName);
369 	}
370 
371 	// [Start] ------ Private Method ------
372 
373 	private void putMetadata(String siteName, ModuleConfigMetadata metadata) {
374 		metadataMap.put(siteName, metadata);
375 	}
376 	
377 	private ActionConfig getActionConfig(String siteName, String path){
378 		ModuleConfigMetadata metadata = getMetadata(siteName);
379 		ModuleConfig moduleConfig = metadata.getModuleConfig();
380 		return moduleConfig.findActionConfig(path);		
381 	}	
382 }