KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > communicator > admin > controller > AdminApplication


1 /*
2  * AdminApplication.java
3  *
4  * Created on April 13, 2003, 3:58 PM
5  */

6
7 package com.quikj.application.communicator.admin.controller;
8
9 import java.util.*;
10 import java.sql.*;
11 import javax.servlet.*;
12 import org.apache.struts.action.*;
13 import org.apache.struts.config.*;
14
15 import com.quikj.application.communicator.admin.model.*;
16 import com.quikj.server.framework.*;
17 import com.quikj.client.raccess.*;
18
19 /**
20  *
21  * @author bhm
22  */

23 public class AdminApplication implements PlugIn
24 {
25     
26     /** Holds value of property configFile. */
27     private String JavaDoc configFile;
28     
29     private AdminConfig configuration;
30     private String JavaDoc errMessage;
31     
32     /** Creates a new instance of AdminApplication */
33     public AdminApplication()
34     {
35     }
36     
37     public void destroy()
38     {
39         if (AceMailService.getInstance() != null)
40         {
41             AceMailService.getInstance().dispose();
42         }
43         
44         if (AceLogger.Instance() != null)
45         {
46             AceLogger.Instance().dispose();
47         }
48     }
49     
50     public void init(ActionServlet actionServlet, ModuleConfig moduleConfig)
51     throws javax.servlet.ServletException JavaDoc
52     {
53         // create AdminConfig
54

55         String JavaDoc path = actionServlet.getServletContext().getRealPath(configFile);
56         configuration = new AdminConfig(path);
57         try
58         {
59             if (configuration.loadConfigurationFile() == false)
60             {
61                 throw new ServletException("Error processing admin config : " +
62                 configuration.getErrorMessage());
63             }
64         }
65         catch (Exception JavaDoc ex)
66         {
67             throw new ServletException("Exception processing admin config: "
68             + ex.getClass().getName() + " : " + ex.getMessage());
69         }
70         
71         // check for license
72
try
73         {
74             new AceLicenseManager(actionServlet.getServletContext().getRealPath(configuration.getLicenseConfigPath()));
75         }
76         catch (Exception JavaDoc ex)
77         {
78             throw new ServletException ("Exception processing license: "
79             + ex.getClass().getName() + " : " + ex.getMessage());
80         }
81         
82         if (AceLicenseManager.getInstance().licenseFeature("ace-communicator") == false)
83         {
84             throw new ServletException(AceLicenseManager.getInstance().getErrorMessage());
85         }
86         
87         // save AdminConfig in the application scope
88
actionServlet.getServletContext().setAttribute("adminConfig",
89         configuration);
90         
91         
92         if (configuration.getMenuProperties() != null)
93         {
94             // save MenuProperties in the application scope
95
actionServlet.getServletContext().setAttribute("menuProperties",
96             configuration.getMenuProperties());
97         }
98         
99         // Start Ace Logger
100
try
101         {
102             // start the time thread, required by logging
103
AceTimer.Instance().start();
104             
105             AceConfigLogParams log_parms = configuration.getLogParams();
106             // start the log server
107
new AceLogger(log_parms.getTxHost(),
108             log_parms.getTxPort(),
109             log_parms.getProcessName(),
110             log_parms.getProcessInstance(),
111             log_parms.getLogGroup());
112             AceLogger.Instance().start();
113             
114             // store log severity level strings in application scope
115
actionServlet.getServletContext().setAttribute("logSeverityLevelStrings",
116             AceLogger.SEVERITY_S);
117             
118         }
119         catch (Exception JavaDoc ex)
120         {
121             throw new ServletException("System logger could not be started: "
122             + ex.getClass().getName() + ": "
123             + ex.getMessage());
124         }
125         
126         // load application classes and init them
127
int n = configuration.getApplicationsSize();
128         ArrayList apps = configuration.getApplications();
129         
130         CommunicatorApplicationInterface application = null;
131         for (int i = 0; i < n; i++)
132         {
133             ApplicationElement app = (ApplicationElement)apps.get(i);
134             
135             if (app.getInitClass() != null)
136             {
137                 application = loadClass(app.getInitClass());
138                 
139                 if (application == null)
140                 {
141                     throw new ServletException("Application class: "
142                     + app.getInitClass()
143                     + " could not be loaded - "
144                     + errMessage);
145                 }
146                 
147                 PluginParameters params = app.getParams();
148                 int num_params = params.numParameters();
149                 
150                 for (int j = 0; j < num_params; j++)
151                 {
152                     String JavaDoc key = params.keyAt(j);
153                     String JavaDoc value = params.valueAt(j);
154                     
155                     application.setParam(key, value);
156                 }
157                 
158                 application.init();
159             }
160             
161         }
162         
163         // create DBConnection
164

165         DBParams dbparams = configuration.getDBParams();
166         DBConnection conn_class = new DBConnection();
167         conn_class.setJdbcDriver(dbparams.getDriverClass());
168         conn_class.setUser(dbparams.getUser());
169         conn_class.setPassword(dbparams.getPassword());
170         
171         String JavaDoc url_str = dbparams.getUrl() + "://" + dbparams.getHost() + "/"
172         + dbparams.getAdminDb();
173         
174         conn_class.setUrl(url_str);
175         
176         // save it in the application scope
177
actionServlet.getServletContext().setAttribute("connection",
178         conn_class);
179         
180         // create user access restriction list, if any
181
AceNetworkAccess access_info = initUserAccessList();
182         
183         // save it in the application scope
184
actionServlet.getServletContext().setAttribute("accessInfo",
185         access_info);
186         
187         // create the remote access connection to the Ace application server
188
RemoteAccessClient com = new RemoteAccessClient(configuration.getRemoteAccessParams().getUrl(),
189         configuration.getRemoteAccessParams().getHttpsServiceName(),
190         configuration.getRemoteAccessParams().getHost());
191         
192         // and save this to the application scope
193
actionServlet.getServletContext().setAttribute("remoteAccess",
194         com);
195         
196         // start Mail service
197
if (configuration.getMailConfigPath() != null)
198         {
199             try
200             {
201                 AceMailService service = new AceMailService(actionServlet.getServletContext().getRealPath(configuration.getMailConfigPath()),
202                 AceLogger.Instance());
203                 service.start();
204             }
205             catch (Exception JavaDoc ex)
206             {
207                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
208                 "AdminApplication.init() -- Failed to start Ace Mail Service: "
209                 + ex.getMessage());
210             }
211         }
212         
213         AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.SYSTEM_LOG,
214         "Ace Communicator application started");
215     }
216     
217     private CommunicatorApplicationInterface loadClass(String JavaDoc class_name)
218     {
219         Class JavaDoc app_class;
220         
221         try
222         {
223             app_class = Class.forName(class_name);
224         }
225         catch (ClassNotFoundException JavaDoc ex)
226         {
227             errMessage = "Class " + class_name + " not found";
228             return null;
229         }
230         
231         // check if the class implements the CommunicatorApplicationInterface
232
Class JavaDoc[] interfaces = app_class.getInterfaces();
233         
234         boolean found = false;
235         for (int i = 0; i < interfaces.length; i++)
236         {
237             if
238             (interfaces[i].getName().equals("com.quikj.application.communicator.admin.controller.CommunicatorApplicationInterface")
239             == true)
240             {
241                 found = true;
242                 break;
243             }
244         }
245         
246         if (found == false)
247         {
248             errMessage = "Class "
249             + class_name
250             + " does not implement "
251             + "com.quikj.application.communicator.admin.controller.CommunicatorApplicationInterface";
252             return null;
253         }
254         
255         // get a new instance of this class
256
CommunicatorApplicationInterface obj = null;
257         try
258         {
259             obj = (CommunicatorApplicationInterface)app_class.newInstance();
260         }
261         catch (InstantiationException JavaDoc ex1)
262         {
263             errMessage = "InstantiationException : " + ex1.getMessage();
264             return null;
265         }
266         catch (IllegalAccessException JavaDoc ex2)
267         {
268             errMessage = "IllegalAccessException : " + ex2.getMessage();
269             return null;
270         }
271         
272         return obj;
273     }
274     
275     private AceNetworkAccess initUserAccessList()
276     {
277         AceNetworkAccess access_info = null;
278         
279         // if there is an access list, initialize it
280
PluginParameters params = configuration.getParams();
281         int num_params = params.numParameters();
282         
283         for (int i = 0; i < num_params; i++)
284         {
285             if (params.keyAt(i).equals("user-access") == true)
286             {
287                 String JavaDoc value = params.valueAt(i);
288                 
289                 StringTokenizer tokens = new StringTokenizer(value);
290                 if (tokens.countTokens() < 2)
291                 {
292                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
293                     "AdminApplication.initUserAccessList() -- Invalid value for user-access: "
294                     + value + ", ignoring entry");
295                     continue;
296                 }
297                 
298                 boolean created = false;
299                 if (access_info == null) // one does not exist
300
{
301                     access_info = new AceNetworkAccess();
302                     created = true;
303                 }
304                 
305                 if (access_info.add(tokens.nextToken(), tokens.nextToken()) == false)
306                 {
307                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
308                     "AdminApplication.initUserAccessList() -- Invalid IP addresses specified for user-access: "
309                     + value + ", ignoring entry");
310                     
311                     if (created == true) // just created
312
{
313                         access_info = null;
314                     }
315                 }
316             }
317         }
318         
319         return access_info;
320     }
321     
322     
323     public static void main(String JavaDoc[] args)
324     {
325         // quick test to check config, authentication
326
if (args.length > 0)
327         {
328             AdminConfig cfg = new AdminConfig(args[0]);
329             try
330             {
331                 if (cfg.loadConfigurationFile() == false)
332                 {
333                     System.out.println("Error processing config - " +
334                     cfg.getErrorMessage());
335                     System.exit(1);
336                 }
337                 
338                 System.out.println("Config file loaded OK\n");
339                 
340                 DBParams dbparams = cfg.getDBParams();
341                 System.out.println("DB DriverClass = " +
342                 dbparams.getDriverClass());
343                 System.out.println("DB Host = " + dbparams.getHost());
344                 System.out.println("DB Url = " + dbparams.getUrl());
345                 System.out.println("DB User = " + dbparams.getUser());
346                 System.out.println("DB Password = " + dbparams.getPassword());
347                 System.out.println("DB AdminDb = " + dbparams.getAdminDb());
348                 
349                 PluginParameters params = cfg.getParams();
350                 int num_params = params.numParameters();
351                 System.out.println("\nNum Top-Level Params = " + num_params);
352                 
353                 for (int j = 0; j < num_params; j++)
354                 {
355                     String JavaDoc key = params.keyAt(j);
356                     String JavaDoc value = params.valueAt(j);
357                     
358                     System.out.print(" Key = " + key);
359                     System.out.println(" Value = " + value);
360                 }
361                 
362                 int n = cfg.getApplicationsSize();
363                 System.out.println("\nNum applications = " + n);
364                 ArrayList apps = cfg.getApplications();
365                 
366                 for (int i = 0; i < n; i++)
367                 {
368                     ApplicationElement app = (ApplicationElement)apps.get(i);
369                     System.out.println("App Name = " + app.getName());
370                     System.out.println("App InitClass = " + app.getInitClass());
371                     System.out.println("App DisplayName = " +
372                     app.getDisplayName());
373                     System.out.println("App ForwardName = " +
374                     app.getForwardName());
375                     
376                     params = app.getParams();
377                     num_params = params.numParameters();
378                     System.out.println("App Num Params = " + num_params);
379                     
380                     for (int j = 0; j < num_params; j++)
381                     {
382                         String JavaDoc key = params.keyAt(j);
383                         String JavaDoc value = params.valueAt(j);
384                         
385                         System.out.print(" Key = " + key);
386                         System.out.println(" Value = " + value);
387                     }
388                 }
389                 
390                 // create & initialize the DBConnection class
391

392                 DBConnection conn_class = new DBConnection();
393                 conn_class.setJdbcDriver(dbparams.getDriverClass());
394                 conn_class.setUser(dbparams.getUser());
395                 conn_class.setPassword(dbparams.getPassword());
396                 
397                 String JavaDoc url_str = dbparams.getUrl() + "://" + dbparams.getHost()
398                 + "/"
399                 + dbparams.getAdminDb();
400                 
401                 conn_class.setUrl(url_str);
402                 
403                 
404                 // try an authentication failure
405

406                 Connection conn = DBConnection.getInstance().getConnection();
407                 if (conn == null)
408                 {
409                     System.out.println("Error getting DB connection: "
410                     + DBConnection.getInstance().getErrorMessage());
411                     System.exit(1);
412                 }
413                 
414                 AccountsTable accounts = new AccountsTable(dbparams.getAdminDb());
415                 accounts.setConnection(conn);
416                 AccountElement userinfo = accounts.authenticate("ace",
417                 "aaaa1111");
418                 if (userinfo == null)
419                 {
420                     System.out.println("TEST PASSED - " +
421                     accounts.getErrorMessage());
422                 }
423                 else
424                 {
425                     System.out.println("TEST FAILED miserably ");
426                 }
427                 
428                 try
429                 {
430                     conn.close();
431                 }
432                 catch (SQLException ex)
433                 {
434                     ;
435                 }
436                 
437                 // try an authentication success
438

439                 conn = DBConnection.getInstance().getConnection();
440                 if (conn == null)
441                 {
442                     System.out.println("Error2 getting DB connection: "
443                     + DBConnection.getInstance().getErrorMessage());
444                     System.exit(1);
445                 }
446                 
447                 accounts = new AccountsTable(dbparams.getAdminDb());
448                 accounts.setConnection(conn);
449                 userinfo = accounts.authenticate("ace", "a1b2c3d4");
450                 if (userinfo != null)
451                 {
452                     System.out.println("TEST PASSED");
453                 }
454                 else
455                 {
456                     System.out.println("TEST FAILED - " +
457                     accounts.getErrorMessage());
458                 }
459                 
460                 try
461                 {
462                     conn.close();
463                 }
464                 catch (SQLException ex)
465                 {
466                     ;
467                 }
468             }
469             catch (Exception JavaDoc ex)
470             {
471                 System.out.println("Exception: " + ex.getClass().getName() + " :" + ex.getMessage());
472                 System.exit(1);
473             }
474             
475             System.exit(0);
476         }
477         
478         System.out.println("Please give as an argument the full path/filename ofthe communicator config file");
479         System.exit(1);
480     }
481     
482     /** Getter for property configFile.
483      * @return Value of property configFile.
484      *
485      */

486     public String JavaDoc getConfigFile()
487     {
488         return this.configFile;
489     }
490     
491     /** Setter for property configFile.
492      * @param configFile New value of property configFile.
493      *
494      */

495     public void setConfigFile(String JavaDoc configFile)
496     {
497         this.configFile = configFile;
498     }
499     
500 }
501
502
Popular Tags