KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * AdminConfig.java
3  *
4  * Created on April 13, 2003, 10:20 AM
5  */

6
7 package com.quikj.application.communicator.admin.controller;
8
9 import java.util.*;
10 import java.io.*;
11
12 // JAXP packages
13
import javax.xml.parsers.*;
14 import org.xml.sax.*;
15 import org.w3c.dom.*;
16
17 import com.quikj.server.framework.*;
18
19 /**
20  *
21  * @author bhm
22  */

23 public class AdminConfig
24 {
25     
26     /** Holds value of property dBParams. */
27     private DBParams dBParams;
28     
29     /** Holds value of property params. */
30     private PluginParameters params;
31     
32     /** Holds value of property applications. */
33     private ArrayList applications;
34     
35     /** Holds value of property errorMessage. */
36     private String JavaDoc errorMessage;
37     
38     private String JavaDoc configFilePath;
39     private long lastModTime;
40     private DocumentBuilderFactory docBuilderFactory;
41     private static AdminConfig instance = null;
42     
43     private static final String JavaDoc ROOT_NODE_NAME = "communicator-configuration";
44     
45     private static final String JavaDoc EL_DB_PARAMS = "db-params";
46     private static final String JavaDoc ATT_DB_CLASS = "class";
47     private static final String JavaDoc ATT_DB_URL = "url";
48     private static final String JavaDoc ATT_DB_HOST = "host";
49     private static final String JavaDoc ATT_DB_USER = "user";
50     private static final String JavaDoc ATT_DB_PASSWORD = "password";
51     private static final String JavaDoc ATT_DB_DATABASE = "admin-db";
52     
53     private static final String JavaDoc EL_APPLICATION = "application";
54     private static final String JavaDoc ATT_APP_NAME = "name";
55     private static final String JavaDoc ATT_APP_CLASS = "class";
56     private static final String JavaDoc ATT_APP_DISPLAY = "display-name";
57     private static final String JavaDoc ATT_APP_FORWARD = "forward-name";
58     
59     private static final String JavaDoc EL_PARAM = "param";
60     private static final String JavaDoc ATT_PARAM_NAME = "name";
61     private static final String JavaDoc ATT_PARAM_VALUE = "value";
62     
63     private static final String JavaDoc EL_RACCESS = "remote-access";
64     private static final String JavaDoc ATT_RURL = "url";
65     private static final String JavaDoc ATT_RHOST = "host";
66     private static final String JavaDoc ATT_HTTPS_RSERVICE = "https-service-name";
67     
68     private static final String JavaDoc EL_LOG_CONFIG = "log-parms";
69     
70     private static final String JavaDoc EL_MAIL_PARAMS = "mail-params";
71     private static final String JavaDoc ATT_MAIL_PATH = "path";
72     
73     private static final String JavaDoc EL_LICENSE_PARAMS = "license-params";
74     private static final String JavaDoc ATT_LICENSE_PATH = "path";
75     
76     private static final String JavaDoc EL_DISPLAY_PARAMS = "display";
77     private static final String JavaDoc ATT_IMAGE = "image";
78     private static final String JavaDoc ATT_COMPANY = "company";
79     private static final String JavaDoc ATT_COMPANY_URL = "url";
80     
81     private static final String JavaDoc EL_LINK_PARAMS = "link";
82     private static final String JavaDoc ATT_LINK_URL = "url";
83     private static final String JavaDoc ATT_LINK_CAPTION = "caption";
84     
85     /** Holds value of property remoteAccessParams. */
86     private RemoteAccessParams remoteAccessParams;
87     
88     /** Holds value of property logParams. */
89     private AceConfigLogParams logParams;
90     
91     /** Holds value of property mailConfigPath. */
92     private String JavaDoc mailConfigPath;
93     
94     /** Holds value of property licenseConfigPath. */
95     private String JavaDoc licenseConfigPath;
96     
97     /** Holds value of property links. */
98     private List links;
99     
100     /** Holds value of property menuProperties. */
101     private MenuLinks menuProperties;
102     
103     /** Creates a new instance of AdminConfig */
104     public AdminConfig(String JavaDoc path) // call loadConfigurationFile() after this
105
{
106         configFilePath = path;
107         
108         docBuilderFactory = DocumentBuilderFactory.newInstance();
109         docBuilderFactory.setValidating(false);
110         docBuilderFactory.setIgnoringComments(true);
111         docBuilderFactory.setIgnoringElementContentWhitespace(true);
112         docBuilderFactory.setCoalescing(true);
113         
114         instance = this;
115     }
116     
117     public static AdminConfig getInstance()
118     {
119         return instance;
120     }
121     
122     public boolean loadConfigurationFile() // if returns false, call getErrorMessage()
123
throws FileNotFoundException, IOException, SAXException, ParserConfigurationException
124     {
125         File file = new File(configFilePath);
126         lastModTime = file.lastModified();
127         
128         FileInputStream fis = new FileInputStream(file);
129         
130         DocumentBuilder doc_builder = docBuilderFactory.newDocumentBuilder();
131         Document doc = doc_builder.parse(fis);
132         
133         boolean ret = processDoc(doc);
134         
135         fis.close();
136         
137         return ret;
138     }
139     
140     public boolean isModified()
141     {
142         File file = new File(configFilePath);
143         long mod_time = file.lastModified();
144         
145         if (mod_time > lastModTime)
146         {
147             return true;
148         }
149         else
150         {
151             return false;
152         }
153     }
154     
155     private boolean processDoc(Document doc)
156     {
157         DBParams db_params = new DBParams();
158         PluginParameters param_list = new PluginParameters();
159         ArrayList app_list = new ArrayList();
160         RemoteAccessParams raccess = new RemoteAccessParams();
161         AceConfigLogParams lparms = null;
162         String JavaDoc mail_path = null;
163         String JavaDoc license_path = null;
164         MenuLinks ml = null;
165         
166         if (doc.getNodeType() != Node.DOCUMENT_NODE)
167         {
168             // the root node must be of the type document
169
errorMessage = "Document does not begin with an XML node, file = " + configFilePath;
170             return false;
171         }
172         
173         Node root_element;
174         for (root_element = doc.getFirstChild();
175         root_element != null;
176         root_element = root_element.getNextSibling())
177         {
178             if (root_element.getNodeType() == Node.ELEMENT_NODE)
179             {
180                 if (root_element.getNodeName().equals(ROOT_NODE_NAME) == false)
181                 {
182                     errorMessage = "Root node name "
183                     + root_element.getNodeName()
184                     + " must be "
185                     + ROOT_NODE_NAME
186                     + ", file = " + configFilePath;
187                     
188                     return false;
189                 }
190                 break;
191             }
192             // else discard
193
}
194         
195         if (root_element == null)
196         {
197             errorMessage = "Root node "
198             + ROOT_NODE_NAME
199             + " not found, file = " + configFilePath;
200             
201             return false;
202         }
203         
204         // parse all the child elements
205
Node ele_node;
206         errorMessage = null;
207         
208         for (ele_node = root_element.getFirstChild();
209         ele_node != null && errorMessage == null;
210         ele_node = ele_node.getNextSibling())
211         {
212             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
213             {
214                 String JavaDoc node_name = ele_node.getNodeName();
215                 
216                 if (node_name.equals(EL_DB_PARAMS) == true)
217                 {
218                     processDBParams(ele_node, db_params);
219                 }
220                 else if (node_name.equals(EL_PARAM) == true)
221                 {
222                     processParam(ele_node, param_list);
223                 }
224                 else if (node_name.equals(EL_APPLICATION) == true)
225                 {
226                     processApplication(ele_node, app_list);
227                 }
228                 else if (node_name.equals(EL_RACCESS) == true)
229                 {
230                     processRemoteAccess(ele_node, raccess);
231                 }
232                 else if (node_name.equals(EL_MAIL_PARAMS) == true)
233                 {
234                     mail_path = processMailParam(ele_node);
235                 }
236                 else if (node_name.equals(EL_LICENSE_PARAMS) == true)
237                 {
238                     license_path = processLicenseParam(ele_node);
239                 }
240                 else if (node_name.equals(EL_DISPLAY_PARAMS) == true)
241                 {
242                     ml = processDisplayParam(ele_node);
243                 }
244                 else if (node_name.equals(EL_LOG_CONFIG) == true)
245                 {
246                     try
247                     {
248                         lparms = new AceConfigLogParams(ele_node);
249                     }
250                     catch (AceException ex)
251                     {
252                         errorMessage = ex.getMessage();
253                     }
254                 }
255                 else
256                 {
257                     errorMessage = "Unknown element "
258                     + node_name + " in file " + configFilePath;
259                     return false;
260                 }
261             }
262             // ignore all types other than the ELEMENT_NODE
263

264             if (errorMessage != null)
265             {
266                 break;
267             }
268         }
269         
270         if (errorMessage != null)
271         {
272             return false;
273         }
274         
275         // check if mandatory parameters have been received
276
if (db_params.getUser() == null) // if one is set, all are OK
277
{
278             errorMessage = "Mandatory database parameter(s) missing in file "
279             + configFilePath;
280             return false;
281         }
282         
283         if (lparms == null)
284         {
285             errorMessage = "Mandatory log parameter(s) missing in file "
286             + configFilePath;
287             return false;
288         }
289         
290         if (license_path == null)
291         {
292             errorMessage = "Mandatory license parameter missing in file "
293             + configFilePath;
294             return false;
295         }
296         
297         dBParams = db_params;
298         params = param_list;
299         applications = app_list;
300         remoteAccessParams = raccess;
301         logParams = lparms;
302         mailConfigPath = mail_path;
303         licenseConfigPath = license_path;
304         menuProperties = ml;
305         return true;
306     }
307     
308     private MenuLinks processDisplayParam(Node node)
309     {
310         MenuLinks ml = new MenuLinks();
311         String JavaDoc[] attributes =
312         {
313             ATT_IMAGE,
314             ATT_COMPANY,
315             ATT_COMPANY_URL
316         };
317                 
318         String JavaDoc[] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes);
319         ml.setImage(attrib_values[0]);
320         ml.setCompany(attrib_values[1]);
321         ml.setUrl(attrib_values[2]);
322         
323         // next go through the child elements
324
for (Node ele_node = node.getFirstChild();
325         ele_node != null && errorMessage == null;
326         ele_node = ele_node.getNextSibling())
327         {
328             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
329             {
330                 String JavaDoc node_name = ele_node.getNodeName();
331                 
332                 if (node_name.equals(EL_LINK_PARAMS) == true)
333                 {
334                     Link l = processLink(ele_node);
335                     if (l == null)
336                     {
337                         return null;
338                     }
339                     
340                     ml.addLink(l);
341                 }
342             }
343         }
344         return ml;
345     }
346     
347     private Link processLink (Node node)
348     {
349         String JavaDoc[] attributes =
350         {
351             ATT_LINK_URL,
352             ATT_LINK_CAPTION
353         };
354         
355         String JavaDoc[] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes);
356         
357         if ((attrib_values[0] == null) || (attrib_values[1] == null))
358         {
359             errorMessage = "Attribute(s) missing from the link node";
360             return null;
361         }
362         
363         Link l = new Link();
364         l.setSource(attrib_values[0]);
365         l.setCaption(attrib_values[1]);
366         return l;
367     }
368     
369     private String JavaDoc processMailParam(Node node)
370     {
371         try
372         {
373             return AceXMLHelper.getXMLAttribute(node, ATT_MAIL_PATH);
374         }
375         catch (AceException ex)
376         {
377             errorMessage = ex.getMessage();
378             return null;
379         }
380     }
381     
382     private String JavaDoc processLicenseParam(Node node)
383     {
384         try
385         {
386             return AceXMLHelper.getXMLAttribute(node, ATT_LICENSE_PATH);
387         }
388         catch (AceException ex)
389         {
390             errorMessage = ex.getMessage();
391             return null;
392         }
393     }
394     
395     private void processRemoteAccess(Node node, RemoteAccessParams params)
396     {
397         String JavaDoc[] attributes =
398         {
399             ATT_RHOST,
400             ATT_RURL,
401             ATT_HTTPS_RSERVICE
402         };
403         
404         String JavaDoc[] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes);
405         if (attrib_values[0] != null)
406         {
407             params.setHost(attrib_values[0]);
408         }
409         
410         if (attrib_values[1] != null)
411         {
412             params.setUrl(attrib_values[1]);
413         }
414         
415         if (attrib_values[2] != null)
416         {
417             params.setHttpsServiceName(attrib_values[2]);
418         }
419     }
420     
421     private void processDBParams(Node node, DBParams db_params)
422     {
423         String JavaDoc[] attributes =
424         {
425             ATT_DB_CLASS,
426             ATT_DB_URL,
427             ATT_DB_HOST,
428             ATT_DB_USER,
429             ATT_DB_PASSWORD,
430             ATT_DB_DATABASE
431         };
432         
433         String JavaDoc[] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes);
434         
435         if ((attrib_values[0] == null) ||
436         (attrib_values[1] == null) ||
437         (attrib_values[2] == null) ||
438         (attrib_values[3] == null) ||
439         (attrib_values[4] == null) ||
440         (attrib_values[5] == null))
441         {
442             errorMessage = "One or more options from "
443             + EL_DB_PARAMS
444             + " is not specified, file = " + configFilePath;
445             
446             return;
447         }
448         
449         db_params.setDriverClass(attrib_values[0]);
450         db_params.setUrl(attrib_values[1]);
451         
452         if (attrib_values[2].equals("localhost") == true)
453         {
454             attrib_values[2] = "127.0.0.1";
455         }
456         
457         db_params.setHost(attrib_values[2]);
458         db_params.setUser(attrib_values[3]);
459         db_params.setPassword(attrib_values[4]);
460         db_params.setAdminDb(attrib_values[5]);
461         
462     }
463     
464     private void processParam(Node ele_node, PluginParameters param_list)
465     {
466         String JavaDoc[] sattributes =
467         { ATT_PARAM_NAME, ATT_PARAM_VALUE};
468         String JavaDoc[] sattrib_values = AceXMLHelper.getXMLAttributes(ele_node, sattributes);
469         
470         if ((sattrib_values[0] == null) || (sattrib_values[1] == null))
471         {
472             errorMessage = "One or more options from "
473             + ATT_PARAM_NAME
474             + ", "
475             + ATT_PARAM_VALUE
476             + " is not specified for top-level "
477             + EL_PARAM
478             + " in file " + configFilePath;
479             
480             return;
481         }
482         
483         param_list.addParameter(sattrib_values[0], sattrib_values[1]);
484     }
485     
486     private void processApplication(Node node, ArrayList app_list)
487     {
488         String JavaDoc[] attributes =
489         { ATT_APP_NAME,
490           ATT_APP_CLASS,
491           ATT_APP_DISPLAY,
492           ATT_APP_FORWARD };
493           
494           String JavaDoc[] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes);
495           
496           if ((attrib_values[0] == null) ||
497           (attrib_values[2] == null) ||
498           (attrib_values[3] == null))
499           {
500               errorMessage = "One or more attributes from "
501               + EL_APPLICATION
502               + " is not specified, file = " + configFilePath;
503               
504               return;
505           }
506           
507           ApplicationElement app_element = new ApplicationElement();
508           
509           app_element.setName(attrib_values[0]);
510           app_element.setInitClass(attrib_values[1]);
511           app_element.setDisplayName(attrib_values[2]);
512           app_element.setForwardName(attrib_values[3]);
513           
514           
515           // check if there are any parameters
516
PluginParameters params = new PluginParameters();
517           
518           Node ele_node;
519           for (ele_node = node.getFirstChild();
520           ele_node != null;
521           ele_node = ele_node.getNextSibling())
522           {
523               if (ele_node.getNodeType() == Node.ELEMENT_NODE)
524               {
525                   if (ele_node.getNodeName().equals(EL_PARAM) == true)
526                   {
527                       String JavaDoc[] sattributes =
528                       { ATT_PARAM_NAME, ATT_PARAM_VALUE};
529                       String JavaDoc[] sattrib_values = AceXMLHelper.getXMLAttributes(ele_node, sattributes);
530                       
531                       if ((sattrib_values[0] == null) || (sattrib_values[1] == null))
532                       {
533                           errorMessage = "One or more options from "
534                           + ATT_PARAM_NAME
535                           + ", "
536                           + ATT_PARAM_VALUE
537                           + " is not specified for "
538                           + EL_PARAM
539                           + " for application "
540                           + app_element.getName()
541                           + " in file " + configFilePath;
542                           
543                           return;
544                       }
545                       
546                       params.addParameter(sattrib_values[0], sattrib_values[1]);
547                   }
548                   else
549                   {
550                       errorMessage = "invalid node name - "
551                       + ele_node.getNodeName()
552                       + " for application "
553                       + app_element.getName()
554                       + " in file " + configFilePath;
555                       
556                       return;
557                   }
558               }
559               // else ignore other stuff
560
}
561           
562           app_element.setParams(params);
563           
564           app_list.add(app_element);
565     }
566     
567     /** Getter for property dBParams.
568      * @return Value of property dBParams.
569      *
570      */

571     public DBParams getDBParams()
572     {
573         return this.dBParams;
574     }
575     
576     /** Getter for property applications.
577      * @return Value of property applications.
578      *
579      */

580     public ArrayList getApplications()
581     {
582         return this.applications;
583     }
584     
585     public int getApplicationsSize()
586     {
587         return applications.size();
588     }
589     
590     /** Getter for property errorMessage.
591      * @return Value of property errorMessage.
592      *
593      */

594     public String JavaDoc getErrorMessage()
595     {
596         return this.errorMessage;
597     }
598     
599     /** Getter for property params.
600      * @return Value of property params.
601      *
602      */

603     public PluginParameters getParams()
604     {
605         return this.params;
606     }
607     
608     public int getParamsSize()
609     {
610         return this.params.numParameters();
611     }
612     
613     /** Getter for property remoteAccessParams.
614      * @return Value of property remoteAccessParams.
615      *
616      */

617     public RemoteAccessParams getRemoteAccessParams()
618     {
619         return this.remoteAccessParams;
620     }
621     
622     /** Setter for property remoteAccessParams.
623      * @param remoteAccessParams New value of property remoteAccessParams.
624      *
625      */

626     public void setRemoteAccessParams(RemoteAccessParams remoteAccessParams)
627     {
628         this.remoteAccessParams = remoteAccessParams;
629     }
630     
631     /** Getter for property logParams.
632      * @return Value of property logParams.
633      *
634      */

635     public AceConfigLogParams getLogParams()
636     {
637         return this.logParams;
638     }
639     
640     /** Setter for property logParams.
641      * @param logParams New value of property logParams.
642      *
643      */

644     public void setLogParams(AceConfigLogParams logParams)
645     {
646         this.logParams = logParams;
647     }
648     
649     /** Getter for property mailConfigPath.
650      * @return Value of property mailConfigPath.
651      *
652      */

653     public String JavaDoc getMailConfigPath()
654     {
655         return this.mailConfigPath;
656     }
657     
658     /** Getter for property licenseConfigPath.
659      * @return Value of property licenseConfigPath.
660      *
661      */

662     public String JavaDoc getLicenseConfigPath()
663     {
664         return this.licenseConfigPath;
665     }
666     
667     /** Getter for property links.
668      * @return Value of property links.
669      *
670      */

671     public List getLinks()
672     {
673         return this.links;
674     }
675     
676     /** Getter for property menuProperties.
677      * @return Value of property menuProperties.
678      *
679      */

680     public MenuLinks getMenuProperties()
681     {
682         return this.menuProperties;
683     }
684     
685 }
686
Popular Tags