1 package org.apache.turbine.modules; 2 3 56 57 import java.util.Iterator ; 58 import java.util.List ; 59 import java.util.Map ; 60 61 import org.apache.commons.collections.FastArrayList; 62 import org.apache.commons.collections.FastHashMap; 63 import org.apache.commons.configuration.Configuration; 64 import org.apache.commons.logging.Log; 65 import org.apache.commons.logging.LogFactory; 66 import org.apache.turbine.Turbine; 67 import org.apache.turbine.TurbineException; 68 import org.apache.turbine.pipeline.PipelineUtil; 69 70 77 public class ModuleLoader 78 { 79 private static final Log log = LogFactory.getLog( ModuleLoader.class ); 80 83 protected List modulePackages; 84 85 89 protected StringBuffer modulePackagesNames; 90 91 95 protected String defaultModule; 96 97 100 protected Map defaultModules; 101 102 106 protected boolean scriptingEnabled; 107 108 protected Configuration configuration; 109 110 113 public ModuleLoader() 114 { 115 modulePackages = new FastArrayList(); 116 modulePackagesNames = new StringBuffer (); 117 defaultModules = new FastHashMap(); 118 } 119 120 125 public void addModulePackage(String modulePackage) 126 { 127 modulePackages.add(modulePackage); 128 modulePackagesNames.append(modulePackage).append("\n"); 129 } 130 131 132 135 public void setConfiguration(Configuration configuration) 136 { 137 this.configuration = configuration; 138 } 139 140 143 public void init() 144 throws TurbineException 145 { 146 Configuration moduleTypes = configuration.subset("module.default"); 147 148 if (moduleTypes == null) 149 { 150 throw new TurbineException( 151 "module.default subset is missing from TR.props"); 152 } 153 154 Iterator j = moduleTypes.getKeys(); 155 156 while (j.hasNext()) 157 { 158 String moduleType = (String ) j.next(); 159 String defaultModule = moduleTypes.getString(moduleType); 160 161 setDefaultModule(moduleType, defaultModule); 164 } 165 166 List modulePackages = configuration.getList(Turbine.MODULE_PACKAGES); 169 170 Iterator i = modulePackages.iterator(); 171 172 while (i.hasNext()) 173 { 174 addModulePackage((String ) i.next()); 175 } 176 177 addModulePackage(Turbine.DEFAULT_MODULE_PACKAGE); 181 } 182 183 195 public Module getModule(String type, String name) 196 throws Exception 197 { 198 Module module = null; 199 200 Iterator k; 201 if (type.equals("actions")) 202 { 203 k = getAllPossibleActions(name); 204 } 205 else 206 { 207 StringBuffer sb = new StringBuffer (); 208 PipelineUtil.parseTemplatePath(name, sb); 209 List names = getPossibleModules(sb.toString()); 210 k = getAllPossibleModules(names, type); 211 } 212 213 while (k.hasNext()) 214 { 215 String moduleClass = (String ) k.next(); 216 217 try 218 { 219 log.debug("Looking for " + moduleClass); 220 module = (Module) Class.forName(moduleClass).newInstance(); 221 log.debug("Mapped " + name + " to " + moduleClass); 222 break; 223 } 224 catch (Exception ignored) 225 { 226 } 228 } 229 230 if (module == null) 231 { 232 throw new Exception ( 233 "Can't find module for " + name + " in " + modulePackagesNames); 234 } 235 236 return module; 237 } 238 239 246 private Iterator getAllPossibleModules(List names, String type) 247 { 248 FastArrayList modules = new FastArrayList(); 249 FastArrayList defaultModules = new FastArrayList(); 250 251 Iterator i = modulePackages.iterator(); 252 while (i.hasNext()) 253 { 254 String modulePackage = (String ) i.next(); 255 Iterator m = (Iterator ) names.iterator(); 256 while (m.hasNext()) 257 { 258 String module = modulePackage + "." + type + "." + m.next(); 259 modules.add(module); 260 261 } 262 263 defaultModules.add(modulePackage + "." + getDefaultModule(type)); 265 } 266 267 modules.addAll(defaultModules); 268 269 return modules.iterator(); 270 } 271 272 276 private Iterator getAllPossibleActions(String name) 277 { 278 FastArrayList actions = new FastArrayList(); 279 280 Iterator i = modulePackages.iterator(); 281 while (i.hasNext()) 282 { 283 String action = (String ) i.next() + ".actions." + name; 284 actions.add(action); 285 } 286 287 return actions.iterator(); 288 } 289 290 294 public void setDefaultModule(String defaultModule) 295 { 296 this.defaultModule = defaultModule; 297 } 298 299 302 public void setDefaultModule(String type, String defaultModule) 303 { 304 defaultModules.put(type, defaultModule); 305 } 306 307 310 public String getDefaultModule(String moduleType) 311 { 312 return (String ) defaultModules.get(moduleType); 313 } 314 315 320 public void setScriptingEnabled(boolean state) 321 { 322 scriptingEnabled = state; 323 } 324 325 331 protected List getPossibleModules(String template) 332 throws Exception 333 { 334 List packages = new FastArrayList(); 335 336 StringBuffer pckage = new StringBuffer (); 338 int i = PipelineUtil.parseTemplatePath(template,pckage); 339 340 if (pckage.charAt(0) == '/') 341 { 342 pckage.deleteCharAt(0); 343 i--; 344 } 345 346 if (i >= 0) 347 { 348 for (int j = 0; j <= i; j++) 349 { 350 if (pckage.charAt(j) == '/') 351 { 352 pckage.setCharAt(j, '.'); 353 } 354 } 355 } 356 357 for (int j = i + 1; j < pckage.length(); j++) 359 { 360 if (pckage.charAt(j) == '.') 361 { 362 pckage.delete(j,pckage.length()); 363 break; 364 } 365 } 366 367 int j = 9999; 371 String module; 372 while (j-- > 0) 373 { 374 module = pckage.toString(); 375 376 packages.add(module); 377 378 pckage.setLength(i + 1); 379 if (i > 0) 380 { 381 for (i = pckage.length() - 2; i >= 0; i--) 383 { 384 if (pckage.charAt(i) == '.') 385 { 386 break; 387 } 388 } 389 } 390 else if (j > 0) 391 { 392 j = 1; 394 } 395 pckage.append("Default"); 396 } 397 398 return packages; 400 } 401 } 402 | Popular Tags |