KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > VModuleManager


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 package com.raptus.owxv3;
7
8 import java.util.*;
9
10 import javax.sql.DataSource JavaDoc;
11
12 import org.apache.struts.action.ActionForward;
13 import org.apache.struts.config.*;
14
15 /**
16  *
17  * <hr>
18  * <table width="100%" border="0">
19  * <tr>
20  * <td width="24%"><b>Filename</b></td><td width="76%">VModuleManager.java</td>
21  * </tr>
22  * <tr>
23  * <td width="24%"><b>Author</b></td><td width="76%">Guy Zürcher (gzuercher@raptus.com)</td>
24  * </tr>
25  * <tr>
26  * <td width="24%"><b>Date</b></td><td width="76%">15th of April 2001</td>
27  * </tr>
28  * </table>
29  * <hr>
30  * <table width="100%" border="0">
31  * <tr>
32  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
33  * </tr>
34  * </table>
35  * <hr>
36  */

37 public class VModuleManager extends Object JavaDoc
38 {
39     /**
40      *
41      */

42     public static final String JavaDoc GLOBALRESOURCES_DS = "_gresds";
43
44     /**
45      *
46      */

47     protected String JavaDoc[] vmodules = null;
48
49     /**
50      *
51      */

52     protected Hashtable datasources = null;
53
54
55     /**
56      *Added by REEA.<br>
57      *This hashtable holds the mapping between table names and vmodules
58      */

59     protected Hashtable tablenames=null;
60
61
62     /**
63      *
64      */

65     protected String JavaDoc directoryWEBINF;
66
67     /**
68      *
69      */

70     protected static VModuleManager _instance = null;
71
72     /**
73      * Singleton mechanism for the manager
74      */

75     public static VModuleManager getInstance()
76     {
77         if(_instance == null)
78             _instance = new VModuleManager();
79
80         return _instance;
81     }
82
83     /**
84      *
85      */

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         // initialize global resources
96
LoggingManager.log("Initializing global resources", this);
97         LoggingManager.log(cm.getPropertyByTree("virtualhost/global_resource/property?name=datasource", "value"), this);
98
99         String JavaDoc gresDatasrc = cm.getPropertyByTree("virtualhost/global_resource/property?name=datasource", "value");
100         DataSource JavaDoc 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         //LoggingManager.log("++++++++++++++==", this);
126

127         // initialize virtual modules
128
String JavaDoc[] vmlist = cm.getStringArrayByTree("virtualhost/vmodules", "items");
129         //LoggingManager.log("---------------------", this);
130
if(vmlist != null)
131         {
132             LoggingManager.log("Got "+vmlist.length+" vmodules", this);
133         }
134         if(vmlist != null)
135         {
136             vmodules = vmlist;
137
138             // loop through all virtual modules
139
for(int i = 0; i < vmlist.length; i ++)
140             {
141                 LoggingManager.log("Initializing virtual module " + vmlist[i], this);
142
143 // String pfx = Constants.VMODULE_PREFIX + vmlist[i];
144
// String dsname = cfg.getStringByKey(pfx + Constants.VMODULE_PROPERTY_DATA_SOURCE);
145

146                 String JavaDoc 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             } // loop through vmodules
163
}
164
165
166
167         return true;
168     }
169
170     /**
171      *
172      */

173     public boolean bootVModule(String JavaDoc vmid,
174                                Configuration cfg,
175                                DataSource JavaDoc 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         // prepare data needs for virtual module
186
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                 /**
197                  *Added by REEA
198                  *getting table names for this vmodule
199                  */

200                 if(dm instanceof com.raptus.owxv3.SQLDataManager)
201                 {
202                     tablenames.putAll( ((com.raptus.owxv3.SQLDataManager)dm).getTableNames() );
203                 }
204                 /**
205                  *end added by REEA
206                  */

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         // loop through all sections of module, in order to add the
221
// view's as struts forward objects.
222
String JavaDoc pfx = Constants.VMODULE_PREFIX + vmid;
223         String JavaDoc[] 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 JavaDoc[] 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 JavaDoc reqPath = element.getController();
239                 String JavaDoc view = element.getView();
240
241                 String JavaDoc 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 JavaDoc viewname = element.getViewName();
256                 updateActionMapping(plugin, viewname, reqPath, view);
257
258                 forwards=element.getForwards();
259                 String JavaDoc forwardalias=null;
260                 for(Enumeration e=forwards.keys();e.hasMoreElements();)
261                 {
262                     forwardalias=(String JavaDoc) e.nextElement();
263                     updateActionMapping(plugin, forwardalias, reqPath, (String JavaDoc) forwards.get(forwardalias) );
264                 }
265             }
266         }
267
268         return true;
269     }
270
271     /**
272      *
273      */

274     protected void updateActionMapping(StrutsOWXPlugIn plugin,
275                                        String JavaDoc viewname,
276                                        String JavaDoc controller,
277                                        String JavaDoc 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     /**
302      * The vmodule cache
303      */

304     Hashtable hvmodules = new Hashtable();
305     /**
306      *
307      */

308     public VModule getVModule(String JavaDoc 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 // LoggingManager.log("Returning "+vmid+" from cache", this);
332
return vm;
333         }
334         
335 // VModule vm = new VModule();
336
// if(vm.initialize(vmid, this.getDataSource(vmid)))
337
// return vm;
338
// else
339
// LoggingManager.log("Failed to initialize virtual module " + vmid, this);
340
//
341
//return null;
342
}
343
344     public String JavaDoc getWEBINFDir() { return directoryWEBINF; }
345     public String JavaDoc[] getVModules() { return vmodules; }
346     public DataSource JavaDoc getDataSource(String JavaDoc vmid) { return (DataSource JavaDoc) datasources.get(vmid); }
347     public String JavaDoc getTableOwnerVModule(String JavaDoc tablename){ return (String JavaDoc) tablenames.get(tablename); }
348
349
350
351 }
352
353 /* end class VModuleManager */
354
Popular Tags