KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > jspbeans > ContactCenterCustomerProperty


1 /*
2  * AppletPropertyBean.java
3  *
4  * Created on January 11, 2003, 3:26 PM
5  */

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

22 public class ContactCenterCustomerProperty
23 {
24     public ContactCenterCustomerProperty(String JavaDoc path, String JavaDoc customer)
25     throws FileNotFoundException, IOException, SAXException, AceException, ParserConfigurationException
26     {
27         this.customer = customer;
28         
29         // read, from the config file and set the properties
30
file = new File(path + "/aceapp/data/customer",
31         customer + ".xml");
32         
33         if (file.exists() == false)
34         {
35             throw new AceException("File " + file.getAbsolutePath() + " does not exist");
36         }
37         
38         loadPropertyFromFile();
39     }
40     
41     private void setupDefaultValues()
42     {
43         validatorObject = null;
44                 
45         pagesParams = new HashMap();
46         
47         userParams = new HashMap();
48         
49         active = true;
50         
51         checkOperatorAvailability = false;
52         
53         oosEmail = null;
54         
55         accessType = "unrestricted";
56         
57         authenticatorClassName = null;
58         
59         authenticatorParams = new HashMap();
60         
61         operator = null;
62         
63         language = "English";
64         
65         clientType = "JAVA";
66         
67         locale = "en_US";
68     }
69     
70     private void loadPropertyFromFile()
71     throws FileNotFoundException, IOException, SAXParseException, SAXException, AceException, ParserConfigurationException
72     {
73         // setup the url of the file to parse and set up a parser
74
URL url = file.toURL();
75         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
76         
77         // set various configuration options
78
dbf.setValidating(true);
79         dbf.setIgnoringComments(true);
80         dbf.setIgnoringElementContentWhitespace(true);
81         dbf.setCoalescing(true);
82         dbf.setNamespaceAware(true);
83         
84         //Create the parser and set its error handler
85
DocumentBuilder doc_builder = dbf.newDocumentBuilder();
86         
87         doc_builder.setErrorHandler(new org.xml.sax.ErrorHandler JavaDoc()
88         {
89             // ignore fatal errors (an exception is guaranteed)
90
public void fatalError(SAXParseException e)
91             throws SAXException
92             {
93             }
94             
95             // treat validation errors as fatal
96
public void error(SAXParseException e)
97             throws SAXParseException
98             {
99                 throw e;
100             }
101             
102             // treat warnings as fatal too
103
public void warning(SAXParseException e)
104             throws SAXParseException
105             {
106                 throw e;
107             }
108             
109         }
110         
111         );
112         
113         //Parse the file into document doc.
114
Document doc = doc_builder.parse(new InputSource(url.toString()));
115         if (doc.getNodeType() != Node.DOCUMENT_NODE)
116         {
117             throw new AceException("Document does not begin with an XML node");
118         }
119         
120         setupDefaultValues();
121         
122         //Get the root node and verify that it is of type Element node
123
// and the node name is talk config. Set root found flag to true
124
Node root_element;
125         boolean root_found = false;
126         for (root_element = doc.getFirstChild();
127         root_element != null;
128         root_element = root_element.getNextSibling())
129         {
130             if (root_element.getNodeType() == Node.ELEMENT_NODE)
131             {
132                 if (root_element.getNodeName().equals("talk-config") == false)
133                 {
134                     throw new AceException("Root node name "
135                     + root_element.getNodeName()
136                     + " must be "
137                     + "talk-config");
138                     
139                 }
140                 
141                 root_found = true;
142                 break;
143                 
144             }
145             // else discard
146

147         }
148         
149         if (root_found == false)
150         {
151             throw new AceException("Root node "
152             + "talk-config"
153             + " not found");
154             
155         }
156         
157         // parse all the child elements
158
Node ele_node;
159         for (ele_node = root_element.getFirstChild();
160         ele_node != null;
161         ele_node = ele_node.getNextSibling())
162         {
163             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
164             {
165                 String JavaDoc node_name = ele_node.getNodeName();
166                 if (node_name.equals( "pages" ) == true)
167                 {
168                     processPagesParams( ele_node );
169                 }
170                 else if (node_name.equals( "service-params" ) == true)
171                 {
172                     processServiceParams( ele_node );
173                 }
174                 else if (node_name.equals( "user-params" ) == true)
175                 {
176                     processUserParams(ele_node);
177                 }
178                 else if (node_name.equals("access" ) == true)
179                 {
180                     processAccessParams( ele_node );
181                 }
182                 // else, ignore
183

184             }
185             
186         }
187         
188         lastLoaded = (new Date()).getTime();
189         
190     }
191     
192     private void processPagesParams(Node node)
193     throws AceException
194     {
195         NamedNodeMap attribs = node.getAttributes();
196         int length = attribs.getLength();
197         
198         for (int i = 0; i < length; i++)
199         {
200             Node attr = attribs.item(i);
201             if (attr.getNodeName().equals("language") == true)
202             {
203                 setLanguage(attr.getNodeValue());
204             }
205             // else, ignore
206
}
207         
208         Node ele_node;
209         for (ele_node = node.getFirstChild();
210         ele_node != null;
211         ele_node = ele_node.getNextSibling())
212         {
213             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
214             {
215                 if (ele_node.getNodeName().equals("page-param") == true)
216                 {
217                     attribs = ele_node.getAttributes();
218                     length = attribs.getLength();
219                     if (length < 2)
220                         
221                     {
222                         // not a single attribute was found
223
throw new AceException("Node does not have sufficient attributes");
224                         
225                     }// if
226

227                     String JavaDoc name = null;
228                     String JavaDoc value = null;
229                     
230                     for (int i = 0; i < length; i++)
231                     {
232                         Node attr = attribs.item(i);
233                         if (attr.getNodeName().equals("name") == true)
234                         {
235                             name = attr.getNodeValue();
236                         }
237                         else if (attr.getNodeName().equals("value") == true)
238                         {
239                             value = attr.getNodeValue();
240                         }
241                         // else, ignore
242
}// for
243

244                     if ((name == null) || (value == null))
245                     {
246                         throw new AceException("The name/value is missing for a page-param element");
247                     }
248                     
249                     pagesParams.put(name, value);
250                     
251                 }
252                 // else ignore
253
}
254             // else ignore
255
}
256     }
257     
258     private void processUserParams(Node node)
259     throws AceException
260     {
261         Node ele_node;
262         for (ele_node = node.getFirstChild();
263         ele_node != null;
264         ele_node = ele_node.getNextSibling())
265         {
266             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
267             {
268                 if (ele_node.getNodeName().equals("user-param") == true)
269                 {
270                     NamedNodeMap attribs = ele_node.getAttributes();
271                     int length = attribs.getLength();
272                     if (length < 2)
273                         
274                     {
275                         // not a single attribute was found
276
throw new AceException("Node does not have sufficient attributes");
277                         
278                     }// if
279

280                     String JavaDoc name = null;
281                     String JavaDoc value = null;
282                     
283                     for (int i = 0; i < length; i++)
284                     {
285                         Node attr = attribs.item(i);
286                         if (attr.getNodeName().equals("name") == true)
287                         {
288                             name = attr.getNodeValue();
289                         }
290                         else if (attr.getNodeName().equals("value") == true)
291                         {
292                             value = attr.getNodeValue();
293                         }
294                         // else, ignore
295
}// for
296

297                     if ((name == null) || (value == null))
298                     {
299                         throw new AceException("The name/value is missing for a user-param element");
300                     }
301                     
302                     userParams.put(name, value);
303                     
304                 }
305                 // else ignore
306
}
307             // else ignore
308
}
309     }
310     
311     private void processServiceParams(Node node)
312     throws AceException
313     {
314         // first process all the attributes
315
NamedNodeMap attribs = node.getAttributes();
316         int length = attribs.getLength();
317         
318         for (int i = 0; i < length; i++)
319         {
320             Node attr = attribs.item(i);
321             if (attr.getNodeName().equals("active") == true)
322             {
323                 String JavaDoc value = attr.getNodeValue();
324                 if (value.equalsIgnoreCase("yes") == true)
325                 {
326                     active = true;
327                 }
328                 else if (value.equalsIgnoreCase("no") == true)
329                 {
330                     active = false;
331                 }
332                 else
333                 {
334                     throw new AceException("The active attribute can only have values \"yes\" or \"no\"");
335                 }
336             }
337             else if (attr.getNodeName().equals("operator") == true)
338             {
339                 operator = attr.getNodeValue();
340             }
341             else if (attr.getNodeName().equals("checkOperatorAvailability") == true)
342             {
343                 String JavaDoc value = attr.getNodeValue();
344                 if (value.equalsIgnoreCase("yes") == true)
345                 {
346                     checkOperatorAvailability = true;
347                 }
348                 else if (value.equalsIgnoreCase("no") == true)
349                 {
350                     checkOperatorAvailability = false;
351                 }
352                 else
353                 {
354                     throw new AceException("The checkOperatorAvailability attribute can only have values \"yes\" or \"no\"");
355                 }
356             }
357             else if (attr.getNodeName().equals("email") == true)
358             {
359                 String JavaDoc val = attr.getNodeValue();
360                 if (val.length() > 0)
361                 {
362                     oosEmail = val;
363                 }
364             }
365             else if (attr.getNodeName().equals("clientType") == true)
366             {
367                 String JavaDoc val = attr.getNodeValue();
368                 if (val.length() > 0)
369                 {
370                     clientType = val;
371                 }
372             }
373         }
374     }
375     
376     
377     private void processAccessParams(Node node)
378     throws AceException
379     {
380         NamedNodeMap attribs = node.getAttributes();
381         int length = attribs.getLength();
382         
383         if (length < 1)
384         {
385             // not a single attribute was found
386
throw new AceException("access does not have sufficient attributes");
387         }
388         
389         for (int i = 0; i < length; i++)
390         {
391             Node attr = attribs.item(i);
392             if (attr.getNodeName().equals("type") == true)
393             {
394                 accessType = attr.getNodeValue();
395             }
396             
397         }
398         
399         if (accessType == null)
400         {
401             throw new AceException("The type attribute is missing for the \"access\" element");
402         }
403         
404         if (accessType.equals("unrestricted") == true)
405         {
406             return;
407         }
408         
409         // accessType = restricted
410
// iterate through the child node to get the database information
411
Node ele_node;
412         for (ele_node = node.getFirstChild();
413         ele_node != null;
414         ele_node = ele_node.getNextSibling())
415         {
416             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
417             {
418                 String JavaDoc node_name = ele_node.getNodeName();
419                 if (node_name.equals("authenticator") == true)
420                 {
421                     // go through the attributes
422
processAuthenticatorParams(ele_node);
423                     break;
424                 }
425             }
426         }
427         
428         // finally initialize the Authenticator class
429
try
430         {
431             Class JavaDoc cl = Class.forName(authenticatorClassName);
432             Class JavaDoc[] interfaces = cl.getInterfaces();
433             boolean iface_implemented = false;
434             for (int i = 0; i < interfaces.length; i++)
435             {
436                 if (interfaces[i].getName().equals("com.quikj.application.web.talk.jspbeans.ContactCenterAuthenticatorInterface")
437                 == true)
438                 {
439                     iface_implemented = true;
440                     break;
441                 }
442             }
443             
444             if (iface_implemented == false)
445             {
446                 throw new AceException("Class " + authenticatorClassName
447                 + " does not implement the proper interface");
448             }
449             
450             validatorObject = (ContactCenterAuthenticatorInterface)cl.newInstance();
451             
452             Iterator iter = authenticatorParams.keySet().iterator();
453             while (iter.hasNext() == true)
454             {
455                 String JavaDoc name = (String JavaDoc)iter.next();
456                 String JavaDoc value = (String JavaDoc)authenticatorParams.get(name);
457                 validatorObject.setParam(name, value);
458             }
459             
460             String JavaDoc error = validatorObject.init();
461             if (error != null)
462             {
463                 throw new AceException("Authenticator initialization failed: " + error);
464             }
465         }
466         catch (Exception JavaDoc ex)
467         {
468             throw new AceException(ex.getClass().getName() + ": " + ex.getMessage());
469         }
470         
471     }
472     
473     private void processAuthenticatorParams(Node node)
474     throws AceException
475     {
476         NamedNodeMap attribs = node.getAttributes();
477         int length = attribs.getLength();
478         
479         if (length < 1)
480         {
481             // not a single attribute was found
482
throw new AceException("Node db does not have sufficient attributes");
483         }
484         
485         for (int i = 0; i < length; i++)
486         {
487             Node attr = attribs.item(i);
488             if (attr.getNodeName().equals("className") == true)
489             {
490                 authenticatorClassName = attr.getNodeValue();
491             }
492             
493         }
494         
495         Node ele_node;
496         for (ele_node = node.getFirstChild();
497         ele_node != null;
498         ele_node = ele_node.getNextSibling())
499         {
500             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
501             {
502                 String JavaDoc node_name = ele_node.getNodeName();
503                 if (node_name.equals("authenticator-param") == true)
504                 {
505                     // go through the attributes
506
processAuthenticatorAttributes(ele_node);
507                 }
508             }
509         }
510         
511         if (authenticatorClassName == null)
512         {
513             throw new AceException("Authenticator className has not been specified");
514         }
515     }
516     
517     private void processAuthenticatorAttributes(Node node)
518     throws AceException
519     {
520         NamedNodeMap attribs = node.getAttributes();
521         int length = attribs.getLength();
522         
523         if (length < 2)
524         {
525             // not a single attribute was found
526
throw new AceException("Node param does not have sufficient attributes");
527         }// if
528

529         String JavaDoc name = null;
530         String JavaDoc value = null;
531         for (int i = 0; i < length; i++)
532         {
533             Node attr = attribs.item(i);
534             if (attr.getNodeName().equals("name") == true)
535             {
536                 name = attr.getNodeValue();
537             }
538             else if (attr.getNodeName().equals("value") == true)
539             {
540                 value = attr.getNodeValue();
541             }
542         }
543         
544         if ((name == null) || (value == null))
545         {
546             throw new AceException("node param does not contain either or both the name/value attributes");
547         }
548         
549         authenticatorParams.put(name, value);
550     }
551     
552     public void updateConfiguration()
553     throws FileNotFoundException, IOException, SAXParseException, SAXException, AceException, ParserConfigurationException
554     {
555         if (file == null)
556         {
557             return;
558         }
559         
560         long mod = file.lastModified();
561         if (mod > lastLoaded)
562         {
563             loadPropertyFromFile();
564         }
565     }
566     
567     
568     /** Getter for property validatorObject.
569      * @return Value of property validatorObject.
570      *
571      */

572     public ContactCenterAuthenticatorInterface getValidatorObject()
573     {
574         return this.validatorObject;
575     }
576     
577     /** Getter for property customer.
578      * @return Value of property customer.
579      *
580      */

581     public String JavaDoc getCustomer()
582     {
583         return this.customer;
584     }
585     
586     /** Setter for property customer.
587      * @param customer New value of property customer.
588      *
589      */

590     public void setCustomer(String JavaDoc customer)
591     {
592     }
593     
594     /** Getter for property pagesParams.
595      * @return Value of property pagesParams.
596      *
597      */

598     public HashMap getPagesParams()
599     {
600         return this.pagesParams;
601     }
602     
603     /** Setter for property pagesParams.
604      * @param pagesParams New value of property pagesParams.
605      *
606      */

607     public void setPagesParams(HashMap pagesParams)
608     {
609     }
610     
611     /** Getter for property userParam.
612      * @return Value of property userParam.
613      *
614      */

615     public HashMap getUserParams()
616     {
617         return this.userParams;
618     }
619     
620     /** Setter for property userParam.
621      * @param userParam New value of property userParam.
622      *
623      */

624     public void setUserParams(HashMap userParams)
625     {
626         this.userParams = userParams;
627     }
628     
629     /** Getter for property active.
630      * @return Value of property active.
631      *
632      */

633     public boolean isActive()
634     {
635         return this.active;
636     }
637     
638     /** Setter for property active.
639      * @param active New value of property active.
640      *
641      */

642     public void setActive(boolean active)
643     {
644         this.active = active;
645     }
646     
647     /** Getter for property checkOperatorAvailability.
648      * @return Value of property checkOperatorAvailability.
649      *
650      */

651     public boolean isCheckOperatorAvailability()
652     {
653         return this.checkOperatorAvailability;
654     }
655     
656     /** Setter for property checkOperatorAvailability.
657      * @param checkOperatorAvailability New value of property checkOperatorAvailability.
658      *
659      */

660     public void setCheckOperatorAvailability(boolean checkOperatorAvailability)
661     {
662         this.checkOperatorAvailability = checkOperatorAvailability;
663     }
664     
665     /** Getter for property oosEmail.
666      * @return Value of property oosEmail.
667      *
668      */

669     public String JavaDoc getOosEmail()
670     {
671         return this.oosEmail;
672     }
673     
674     /** Setter for property oosEmail.
675      * @param oosEmail New value of property oosEmail.
676      *
677      */

678     public void setOosEmail(String JavaDoc oosEmail)
679     {
680         this.oosEmail = oosEmail;
681     }
682     
683     /** Getter for property accessType.
684      * @return Value of property accessType.
685      *
686      */

687     public String JavaDoc getAccessType()
688     {
689         return this.accessType;
690     }
691     
692     /** Setter for property accessType.
693      * @param accessType New value of property accessType.
694      *
695      */

696     public void setAccessType(String JavaDoc accessType)
697     {
698         this.accessType = accessType;
699     }
700     
701     /** Getter for property authenticatorClassName.
702      * @return Value of property authenticatorClassName.
703      *
704      */

705     public String JavaDoc getAuthenticatorClassName()
706     {
707         return this.authenticatorClassName;
708     }
709     
710     /** Setter for property authenticatorClassName.
711      * @param authenticatorClassName New value of property authenticatorClassName.
712      *
713      */

714     public void setAuthenticatorClassName(String JavaDoc authenticatorClassName)
715     {
716         this.authenticatorClassName = authenticatorClassName;
717     }
718     
719     /** Getter for property authenticatorParams.
720      * @return Value of property authenticatorParams.
721      *
722      */

723     public HashMap getAuthenticatorParams()
724     {
725         return this.authenticatorParams;
726     }
727     
728     /** Setter for property authenticatorParams.
729      * @param authenticatorParams New value of property authenticatorParams.
730      *
731      */

732     public void setAuthenticatorParams(HashMap authenticatorParams)
733     {
734         this.authenticatorParams = authenticatorParams;
735     }
736     
737     /** Getter for property operator.
738      * @return Value of property operator.
739      *
740      */

741     public String JavaDoc getOperator()
742     {
743         return this.operator;
744     }
745     
746     /** Setter for property operator.
747      * @param operator New value of property operator.
748      *
749      */

750     public void setOperator(String JavaDoc operator)
751     {
752         this.operator = operator;
753     }
754     
755     /** Getter for property language.
756      * @return Value of property language.
757      *
758      */

759     public String JavaDoc getLanguage()
760     {
761         return this.language;
762     }
763     
764     /** Setter for property language.
765      * @param language New value of property language.
766      *
767      */

768     public void setLanguage(String JavaDoc language)
769     {
770         if (language != null)
771         {
772             if (language.equals("English") == true)
773             {
774                 setLocale("en_US");
775             }
776             else if (language.equals("French") == true)
777             {
778                 setLocale("fr_FR");
779             }
780             else if (language.equals("German") == true)
781             {
782                 setLocale("de_DE");
783             }
784             else if (language.equals("Spanish") == true)
785             {
786                 setLocale("es_ES");
787             }
788             else if (language.equals("Dutch") == true)
789             {
790                 setLocale("nl_BE");
791             }
792             else if (language.equals("Italian") == true)
793             {
794                 setLocale("it_IT");
795             }
796         }
797         
798         this.language = language;
799     }
800     
801     /** Getter for property clientType.
802      * @return Value of property clientType.
803      *
804      */

805     public String JavaDoc getClientType()
806     {
807         return this.clientType;
808     }
809     
810     /** Setter for property clientType.
811      * @param clientType New value of property clientType.
812      *
813      */

814     public void setClientType(String JavaDoc clientType)
815     {
816         this.clientType = clientType;
817     }
818     
819     /** Getter for property locale.
820      * @return Value of property locale.
821      *
822      */

823     public String JavaDoc getLocale()
824     {
825         return this.locale;
826     }
827     
828     /** Setter for property locale.
829      * @param locale New value of property locale.
830      *
831      */

832     public void setLocale(String JavaDoc locale)
833     {
834         this.locale = locale;
835     }
836     
837     private long lastLoaded = 0L;
838     private File file;
839     
840     /** Holds value of property validatorObject. */
841     private ContactCenterAuthenticatorInterface validatorObject;
842     
843     /** Holds value of property customer. */
844     private String JavaDoc customer;
845     
846     /** Holds value of property pagesParams. */
847     private HashMap pagesParams = new HashMap();
848     
849     /** Holds value of property userParams. */
850     private HashMap userParams = new HashMap();
851     
852     /** Holds value of property active. */
853     private boolean active = true;
854     
855     /** Holds value of property checkOperatorAvailability. */
856     private boolean checkOperatorAvailability = false;
857     
858     /** Holds value of property oosEmail. */
859     private String JavaDoc oosEmail;
860     
861     /** Holds value of property accessType. */
862     private String JavaDoc accessType;
863     
864     /** Holds value of property authenticatorClassName. */
865     private String JavaDoc authenticatorClassName;
866     
867     /** Holds value of property authenticatorParams. */
868     private HashMap authenticatorParams = new HashMap();
869     
870     /** Holds value of property operator. */
871     private String JavaDoc operator;
872     
873     /** Holds value of property language. */
874     private String JavaDoc language;
875     
876     /** Holds value of property clientType. */
877     private String JavaDoc clientType;
878     
879     /** Holds value of property locale. */
880     private String JavaDoc locale;
881     
882 }
Popular Tags