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.util.HashSet;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import javax.servlet.ServletException;
23  
24  import org.apache.struts.Globals;
25  import org.apache.struts.action.ActionServlet;
26  import org.apache.struts.action.RequestProcessor;
27  import org.apache.struts.config.ActionConfig;
28  import org.apache.struts.config.FormBeanConfig;
29  import org.apache.struts.config.MessageResourcesConfig;
30  import org.apache.struts.config.ModuleConfig;
31  import org.apache.struts.config.PlugInConfig;
32  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
33  import org.seasar.framework.util.ArrayUtil;
34  import org.seasar.struts.servlet.S2ActionServlet;
35  import org.seasar.tuigwaa.security.DirectoryService;
36  import org.seasar.tuigwaa.system.Constants;
37  import org.seasar.tuigwaa.system.TgwSystemService;
38  import org.seasar.tuigwaa.util.TgwPathResolver;
39  import org.seasar.tuigwaa.util.TgwResource;
40  
41  import com.isenshi.util.CharUtil;
42  import com.isenshi.util.ResourceUtils;
43  
44  /***
45   * @author nishioka
46   */
47  public class TuigwaaActionServlet extends S2ActionServlet {
48  
49  	private static final long serialVersionUID = 8114849767872940562L;
50  
51  	private TgwSystemService system;
52  
53  	public TuigwaaActionServlet() {
54  		super();
55  	}
56  
57  	public void init() throws ServletException {
58  		super.init();
59  		getServletContext().setAttribute(Constants.CATTR_TUIGWAASERVLET, this);
60  
61  		// Start Up Directroy Service
62  		SingletonS2ContainerFactory.getContainer().getComponent(
63  				DirectoryService.class);
64  
65  		system = (TgwSystemService) SingletonS2ContainerFactory.getContainer()
66  				.getComponent(TgwSystemService.class);
67  
68  		system.init();
69  	}
70  
71  	public void destroy() {
72  		super.destroy();
73  		system.destroy();
74  	}
75  
76  	public ModuleConfig createModuleConfig(String prefix, String paths)
77  			throws ServletException {
78  		ModuleConfig moduleConfig = super.initModuleConfig(prefix, paths);
79  
80  		String extPath = TgwPathResolver.getExternalConfig(prefix.substring(1,
81  				prefix.length()));
82  		if (ResourceUtils.isExist(extPath)) {
83  			ModuleConfig extConfig = super.initModuleConfig("/tuigwaa-tmp",
84  					"/WEB-INF/classes/" + extPath);
85  			ActionConfig[] aconfigs = extConfig.findActionConfigs();
86  			FormBeanConfig[] fconfigs = extConfig.findFormBeanConfigs();
87  			Set tmpActions = new HashSet();
88  			for (int i = 0; i < aconfigs.length; i++) {
89  				moduleConfig.addActionConfig(aconfigs[i]);
90  				aconfigs[i].setModuleConfig(moduleConfig);
91  				try {
92  					Class actionClass = Class.forName(aconfigs[i].getType());
93  					if (!tmpActions.contains(actionClass)) {
94  						tmpActions.add(actionClass);
95  						SingletonS2ContainerFactory.getContainer().register(
96  								actionClass);
97  					}
98  				} catch (ClassNotFoundException e) {
99  					e.printStackTrace();
100 				}
101 			}
102 			for (int i = 0; i < fconfigs.length; i++) {
103 				moduleConfig.addFormBeanConfig(fconfigs[i]);
104 			}
105 			MessageResourcesConfig[] messageConfigs = extConfig
106 					.findMessageResourcesConfigs();
107 			if (messageConfigs != null) {
108 				for (int i = 0; i < messageConfigs.length; i++) {
109 					moduleConfig.addMessageResourcesConfig(messageConfigs[i]);
110 				}
111 			}
112 
113 			PlugInConfig[] pluginConfigs = extConfig.findPlugInConfigs();
114 			PlugInConfig[] modulePluginConfigs = moduleConfig
115 					.findPlugInConfigs();
116 			for (int i = 0; i < pluginConfigs.length; i++) {
117 				for (int j = 0; j < modulePluginConfigs.length; j++) {
118 					if (pluginConfigs[i].getClassName().equals(
119 							modulePluginConfigs[j].getClassName())) {
120 						Map orgProps = modulePluginConfigs[j].getProperties();
121 						orgProps.putAll(pluginConfigs[i].getProperties());
122 						break;
123 					}
124 				}
125 			}
126 
127 		}
128 
129 		super.initModulePlugIns(moduleConfig);
130 		super.initModuleMessageResources(moduleConfig);
131 
132 		// create request processor
133 		RequestProcessor processor = (RequestProcessor) SingletonS2ContainerFactory
134 				.getContainer().getComponent(RequestProcessor.class);
135 		processor.init((ActionServlet) this, moduleConfig);
136 		String key = Globals.REQUEST_PROCESSOR_KEY + moduleConfig.getPrefix();
137 		getServletContext().setAttribute(key, processor);
138 
139 		return moduleConfig;
140 	}
141 	
142 	protected ModuleConfig initModuleConfig(String prefix, String path) throws ServletException{
143 		
144 		ModuleConfig mc = super.initModuleConfig(prefix,path);
145 		
146 		if(prefix.equals("/tgw-manager")){
147 			ActionConfig[] acs= mc.findActionConfigs();			
148 			for(int i=0;i<acs.length;i++){
149 				ActionConfig ac = acs[i];				
150 				String[] roleNames = ac.getRoleNames();
151 				String tgwRolename = TgwResource.getProperty("ldap.admin.rolename");				
152 				if(roleNames == null){
153 					ac.setRoles(tgwRolename);
154 				}else if(!ArrayUtil.contains(roleNames,tgwRolename)){
155 					String roles = CharUtil.join(roleNames,",");
156 					roles = roles + "," + tgwRolename;
157 					ac.setRoles(roles);
158 				}
159 			}
160 		}		
161 		return mc;
162 	}	
163 	
164 }