1 5 6 package com.raptus.owxv3; 7 8 import java.util.*; 9 10 import javax.sql.DataSource ; 11 12 import org.apache.struts.action.ActionForward; 13 import org.apache.struts.config.*; 14 15 37 public class VModuleManager extends Object 38 { 39 42 public static final String GLOBALRESOURCES_DS = "_gresds"; 43 44 47 protected String [] vmodules = null; 48 49 52 protected Hashtable datasources = null; 53 54 55 59 protected Hashtable tablenames=null; 60 61 62 65 protected String directoryWEBINF; 66 67 70 protected static VModuleManager _instance = null; 71 72 75 public static VModuleManager getInstance() 76 { 77 if(_instance == null) 78 _instance = new VModuleManager(); 79 80 return _instance; 81 } 82 83 86 public boolean initialize(StrutsOWXPlugIn plugin) 87 { 88 datasources = new Hashtable(); 89 tablenames=new Hashtable(); 90 directoryWEBINF = plugin.getWEBINFDir(); 91 92 XMLConfigManager cm = XMLConfigManager.getInstance(); 93 Configuration cfg = cm.getConfiguration(); 94 95 LoggingManager.log("Initializing global resources", this); 97 LoggingManager.log(cm.getPropertyByTree("virtualhost/global_resource/property?name=datasource", "value"), this); 98 99 String gresDatasrc = cm.getPropertyByTree("virtualhost/global_resource/property?name=datasource", "value"); 100 DataSource ds = plugin.getServlet().findDataSource(gresDatasrc); 101 if(gresDatasrc == null && ds != null) 102 { 103 LoggingManager.log("Cannot retrieve valid datasource! Check struts-config.xml.", this); 104 return false; 105 } 106 107 datasources.put(GLOBALRESOURCES_DS, ds); 108 LoggingManager.log("Datasource="+cm.getPropertyByTree("virtualhost/global_resource/property?name=datasource", "value"), this); 109 110 LoggingManager.log("tables="+cm.getPropertyByTree("virtualhost/global_resource/property?name=tables", "value"), this); 111 SQLDataManager sqlMgr = new SQLDataManager(); 112 if(!sqlMgr.initialize(Constants.GLOBALRESOURCES_KEY, ds)) 113 { 114 LoggingManager.log("Failed to initialize SQLDataManager!", this); 115 return false; 116 } 117 118 if(!sqlMgr.prepareData()) 119 { 120 LoggingManager.log("Failed to prepare data for SQLDataManager!", this); 121 return false; 122 } 123 else 124 LoggingManager.log("Global resources sucessfully initialized",this); 125 127 String [] vmlist = cm.getStringArrayByTree("virtualhost/vmodules", "items"); 129 if(vmlist != null) 131 { 132 LoggingManager.log("Got "+vmlist.length+" vmodules", this); 133 } 134 if(vmlist != null) 135 { 136 vmodules = vmlist; 137 138 for(int i = 0; i < vmlist.length; i ++) 140 { 141 LoggingManager.log("Initializing virtual module " + vmlist[i], this); 142 143 146 String dsname=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vmlist[i]+"/properties/property?name=datasource", "value"); 147 LoggingManager.log("Data source of "+vmlist[i]+" is "+dsname, this); 148 149 if(dsname != null && dsname.length() > 0) 150 { 151 ds = plugin.getServlet().findDataSource(dsname); 152 datasources.put(vmlist[i], ds); 153 154 LoggingManager.log("Datasource for virtual module " + vmlist[i] + " is " + 155 dsname, this); 156 } 157 158 if(!bootVModule(vmlist[i], cfg, ds, plugin)) 159 LoggingManager.log("FAILED to boot virtual module " + vmlist[i], this); 160 161 LoggingManager.log("Virtual module " + vmlist[i] + " done.", this); 162 } } 164 165 166 167 return true; 168 } 169 170 173 public boolean bootVModule(String vmid, 174 Configuration cfg, 175 DataSource ds, 176 StrutsOWXPlugIn plugin) 177 { 178 VModule vmod = new VModule(); 179 if(!vmod.initialize(vmid, ds)) 180 { 181 LoggingManager.log("FAILED vmodule.initialise()", this); 182 return false; 183 } 184 185 LoggingManager.log("Prepare data needs for vmodule "+vmod.getIdentification(),this); 187 DataManager dm = vmod.getDatamanager(); 188 if(dm != null) 189 { 190 if(dm.initialize(vmod)) 191 { 192 if(!dm.prepareData()) 193 LoggingManager.log("FAILED to prepare data for virtual module " + vmid, this); 194 195 196 200 if(dm instanceof com.raptus.owxv3.SQLDataManager) 201 { 202 tablenames.putAll( ((com.raptus.owxv3.SQLDataManager)dm).getTableNames() ); 203 } 204 207 208 209 } 210 else 211 { 212 LoggingManager.log("FAILED to initialize the datamanager for virtual " + 213 "module " + vmid, this); 214 return false; 215 } 216 } 217 else 218 return false; 219 220 String pfx = Constants.VMODULE_PREFIX + vmid; 223 String [] sections = vmod.getSections(); 224 Hashtable forwards=null; 225 for(int i = 0; i < sections.length; i ++) 226 { 227 VModuleSection section = vmod.getSection(sections[i]); 228 String [] elements = section.getElements(); 229 if(elements == null) 230 continue; 231 232 for(int n = 0; n < elements.length; n ++) 233 { 234 VModuleSectionElement element = section.getElement(elements[n]); 235 if(element == null) 236 continue; 237 238 String reqPath = element.getController(); 239 String view = element.getView(); 240 241 String err = null; 242 if(reqPath == null || reqPath.length() == 0) 243 err = "controller"; 244 245 if(view == null || view.length() == 0) 246 err = "view"; 247 248 if(err != null) 249 { 250 LoggingManager.log("FAILED to find controller or view for element " + elements[n] + 251 " in section " + sections[i] + " for virtual module " + vmid, this); 252 continue; 253 } 254 255 String viewname = element.getViewName(); 256 updateActionMapping(plugin, viewname, reqPath, view); 257 258 forwards=element.getForwards(); 259 String forwardalias=null; 260 for(Enumeration e=forwards.keys();e.hasMoreElements();) 261 { 262 forwardalias=(String ) e.nextElement(); 263 updateActionMapping(plugin, forwardalias, reqPath, (String ) forwards.get(forwardalias) ); 264 } 265 } 266 } 267 268 return true; 269 } 270 271 274 protected void updateActionMapping(StrutsOWXPlugIn plugin, 275 String viewname, 276 String controller, 277 String view) 278 { 279 ModuleConfig mc = plugin.getConfig(); 280 if(mc == null) 281 { 282 LoggingManager.log("FAILED to get ModuleConfig instance from plugin.", this); 283 return; 284 } 285 286 ActionConfig ac = mc.findActionConfig(controller); 287 if(ac == null) 288 { 289 LoggingManager.log("FAILED to find an ActionConfig for " + controller, this); 290 return; 291 } 292 293 ActionForward fwd = new ActionForward(); 294 fwd.setName(viewname); 295 fwd.setPath(view); 296 ac.addForwardConfig(fwd); 297 298 LoggingManager.log("Added section view " + view + " as alias " + viewname + " to controller " + controller, this); 299 } 300 301 304 Hashtable hvmodules = new Hashtable(); 305 308 public VModule getVModule(String vmid) 309 { 310 if(vmid == null || vmid.length() == 0) 311 return null; 312 313 if(hvmodules.get(vmid)==null) 314 { 315 VModule vm = new VModule(); 316 if(vm.initialize(vmid, this.getDataSource(vmid))) 317 { 318 LoggingManager.log("Saved "+vmid+" in cache", this); 319 hvmodules.put(vmid, vm); 320 return vm; 321 } 322 else 323 { 324 LoggingManager.log("FAILED to initialize virtual module " + vmid, this); 325 return null; 326 } 327 } 328 else 329 { 330 VModule vm = (VModule)hvmodules.get(vmid); 331 return vm; 333 } 334 335 } 343 344 public String getWEBINFDir() { return directoryWEBINF; } 345 public String [] getVModules() { return vmodules; } 346 public DataSource getDataSource(String vmid) { return (DataSource ) datasources.get(vmid); } 347 public String getTableOwnerVModule(String tablename){ return (String ) tablenames.get(tablename); } 348 349 350 351 } 352 353 354 | Popular Tags |