KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > oamp > plugin > FeatureOAMP


1
2 package com.quikj.application.web.oamp.plugin;
3
4 import com.quikj.server.framework.*;
5 import com.quikj.server.web.*;
6 import com.quikj.application.web.oamp.messaging.*;
7
8 import java.io.*;
9 import java.util.*;
10
11 // JAXP packages
12
import javax.xml.parsers.*;
13 import org.xml.sax.*;
14 import org.w3c.dom.*;
15
16
17 public class FeatureOAMP implements PluginAppInterface
18 {
19     
20     public static final String JavaDoc ROOT_NODE_NAME = "application-list";
21     public static final String JavaDoc EL_APPLICATION = "plugin-oamp";
22     public static final String JavaDoc ATT_FEATURE_NAME = "name";
23     public static final String JavaDoc EL_CLIENT = "client";
24     public static final String JavaDoc ATT_CLIENT_CLASS = "class";
25     public static final String JavaDoc ATT_CLIENT_MENUNAME = "menu-name";
26     public static final String JavaDoc ATT_CLIENT_DISPLAY_TO_SUBSCRIBER = "display-to-subscriber";
27     public static final String JavaDoc EL_PLUGIN_PARAM = "param";
28     public static final String JavaDoc ATT_PLUGIN_NAME = "name";
29     public static final String JavaDoc ATT_PLUGIN_VALUE = "value";
30     public static final String JavaDoc EL_SERVER = "server";
31     public static final String JavaDoc ATT_SERVER_CLASS = "class";
32     
33     
34     public FeatureOAMP()
35     {
36         instance = this;
37     }
38     
39     public static FeatureOAMP Instance()
40     {
41         return instance;
42     }
43     
44     public boolean applicationInit(PluginParameters params)
45     {
46         sqlDatabase = params.findKey("sql-database");
47         if (sqlDatabase == null) // not found
48
{
49             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
50             "FeatureOAMP.applicationInit() -- database name not specified");
51             return false;
52         }
53         
54         featureConfigDir = params.findKey("feature-dir");
55         featureConfigFile = params.findKey("feature-file");
56         
57         if ((featureConfigDir == null) || (featureConfigFile == null))
58         {
59             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
60             "FeatureOAMP.applicationInit() -- feature oamp config directory and/or filename not specified");
61             return false;
62         }
63         
64         try
65         {
66             absPath = AceConfigTableFileParser.getAcePath(AceConfigTableFileParser.LOCAL_DATA,
67             featureConfigDir, featureConfigFile);
68             loadConfigurationFile();
69         }
70         catch (Exception JavaDoc ex)
71         {
72             //System.out.println("Logger = " + AceLogger.Instance() + " FIX THIS");
73
// TEMP WORKAROUND
74
//System.out.println("FeatureOAMP.applicationInit() -- "
75
//+ ex.getClass().getName()
76
//+ " occurred : "
77
//+ ex.getMessage());
78

79             // END OF WORKAROUND
80

81             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
82             "FeatureOAMP.applicationInit() -- "
83             + ex.getClass().getName()
84             + " occurred : "
85             + ex.getMessage());
86             
87             return false;
88         }
89         
90         try
91         {
92             SystemOAMP sys = new SystemOAMP();
93             
94             if (sys.init(OAMPSystemMessageParser.OAMP_SYSTEM_FEATURE_NAME, null, null) == false)
95             {
96                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
97                 "FeatureOAMP.applicationInit() -- couldn't initialize system OAMP");
98                 
99                 return false;
100             }
101             
102             sys.start();
103             FeatureOAMPHandlerList.Instance().addItemToList(sys);
104             
105             logListener = new LogListener();
106             
107             new CommunicatorClientList();
108             
109         }
110         catch (IOException ex)
111         {
112             // print error message
113
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
114             "FeatureOAMP.applicationInit() -- An IO error occured while trying to create the System OAMP thread - "
115             + ex.getMessage());
116             
117             return false;
118         }
119         
120         
121         // finally, start the log message monitor thread
122
try
123         {
124             logMonitor = new LogMessageMonitor();
125             logMonitor.start();
126         }
127         catch (Exception JavaDoc ex)
128         {
129             // print error message
130
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
131             "FeatureOAMP.applicationInit() -- An error "
132             + ex.getClass().getName()
133             + " occured while trying to create the Log message monitor thread - "
134             + ex.getMessage());
135             
136             // and ignore
137
}
138         
139         // if there is an access list, initialize it
140
int plugin_size = params.numParameters();
141         
142         for (int i = 0; i < plugin_size; i++)
143         {
144             if (params.keyAt(i).equals("user-access") == true)
145             {
146                 String JavaDoc value = params.valueAt(i);
147                 
148                 StringTokenizer tokens = new StringTokenizer(value);
149                 if (tokens.countTokens() < 2)
150                 {
151                     AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
152                     "FeatureOAMP.applicationInit() -- Invalid value for user-access: "
153                     + value + ", ignorning entry");
154                     continue;
155                 }
156                 
157                 boolean created = false;
158                 if (accessInfo == null) // one does not exist
159
{
160                     accessInfo = new AceNetworkAccess();
161                     created = true;
162                 }
163                 
164                 if (accessInfo.add(tokens.nextToken(), tokens.nextToken()) == false)
165                 {
166                    AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
167                     "FeatureOAMP.applicationInit() -- Invalid IP addresses specified for user-access: "
168                     + value + ", ignorning entry");
169                     
170                     if (created == true) // just created
171
{
172                         accessInfo = null;
173                     }
174                 }
175             }
176         }
177         
178         
179         return true;
180     }
181     
182     
183     public void dispose()
184     {
185         FeatureOAMPHandlerList.Instance().dispose();
186         
187         if (logMonitor != null)
188         {
189             logMonitor.dispose();
190         }
191         
192         if (CommunicatorClientList.getInstance() != null)
193         {
194             CommunicatorClientList.getInstance().dispose();
195         }
196         
197         instance = null;
198     }
199     
200     public String JavaDoc getFullPathName()
201     {
202         return absPath;
203     }
204     
205     public void loadConfigurationFile()
206     throws FileNotFoundException, IOException, SAXException, AceException, ParserConfigurationException
207     {
208         File file = new File(absPath);
209         lastModTime = file.lastModified();
210         
211         FileInputStream fis = new FileInputStream(file);
212         
213         DocumentBuilder doc_builder = XMLBuilder.Instance().getDocumentBuilder();
214         Document doc = doc_builder.parse(fis);
215         
216         processDoc(doc);
217         
218         fis.close();
219     }
220     
221     public boolean isModified()
222     {
223         File file = new File(absPath);
224         long mod_time = file.lastModified();
225         
226         if (mod_time > lastModTime)
227         {
228             return true;
229         }
230         else
231         {
232             return false;
233         }
234     }
235     
236     private void processDoc(Document doc)
237     throws AceException
238     {
239         if (doc.getNodeType() != Node.DOCUMENT_NODE)
240         {
241             // the root node must be of the type document
242

243             throw new AceException("Document does not begin with an XML node");
244         }
245         
246         Node root_element;
247         boolean root_found = false;
248         
249         for (root_element = doc.getFirstChild();
250         root_element != null;
251         root_element = root_element.getNextSibling())
252         {
253             if (root_element.getNodeType() == Node.ELEMENT_NODE)
254             {
255                 if (root_element.getNodeName().equals(ROOT_NODE_NAME) == false)
256                 {
257                     throw new AceException("Root node name "
258                     + root_element.getNodeName()
259                     + " must be "
260                     + ROOT_NODE_NAME);
261                 }
262                 root_found = true;
263                 break;
264                 
265             }
266             // else discard
267
}
268         
269         if (root_found == false)
270         {
271             throw new AceException("Root node "
272             + ROOT_NODE_NAME
273             + " not found");
274         }
275         
276         // parse all the child elements
277
Node ele_node;
278         for (ele_node = root_element.getFirstChild();
279         ele_node != null;
280         ele_node = ele_node.getNextSibling())
281         {
282             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
283             {
284                 String JavaDoc node_name = ele_node.getNodeName();
285                 
286                 if (node_name.equals(EL_APPLICATION) == true)
287                 {
288                     processApplication(ele_node);
289                 }
290                 else
291                 {
292                     throw new AceException("Unknown element "
293                     + node_name);
294                 }
295             }
296             // ignore all types other than the ELEMENT_NODE
297
}
298     }
299     
300     private void processApplication(Node node)
301     throws AceException
302     {
303         String JavaDoc[] attributes =
304         {ATT_FEATURE_NAME};
305         
306         String JavaDoc[] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes);
307         
308         if (attrib_values[0] == null)
309         {
310             throw new AceException("Attribute "
311             + ATT_FEATURE_NAME
312             + " is not specified in "
313             + EL_APPLICATION);
314         }
315         
316         String JavaDoc feature_name = attrib_values[0];
317         
318         // process client and server elements
319

320         String JavaDoc server_classname = null;
321         PluginParameters server_parms = new PluginParameters();
322         ClientParms client_parms = null;
323         
324         Node ele_node;
325         for (ele_node = node.getFirstChild();
326         ele_node != null;
327         ele_node = ele_node.getNextSibling())
328         {
329             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
330             {
331                 if (ele_node.getNodeName().equals(EL_CLIENT) == true)
332                 {
333                     client_parms = processClient(ele_node, feature_name);
334                 }
335                 else if (ele_node.getNodeName().equals(EL_SERVER) == true)
336                 {
337                     server_classname = processServer(ele_node, server_parms, feature_name);
338                 }
339                 else
340                 {
341                     throw new AceException("invalid node name - "
342                     + ele_node.getNodeName()
343                     + " for plugin oamp feature "
344                     + feature_name);
345                 }
346             }
347             // else ignore other stuff
348
}
349         
350         if ((client_parms == null) || (server_classname == null))
351         {
352             throw new AceException("missing client and/or server elements for plugin oamp feature "
353             + feature_name);
354         }
355         
356         // finally load the server class
357

358         if (FeatureOAMPHandlerList.Instance().add(feature_name,
359         server_classname,
360         server_parms,
361         client_parms) == false) // error plugging in application
362
{
363             throw new AceException("Plugin OAMP feature "
364             + feature_name
365             + " server class name: "
366             + server_classname
367             + " could not be loaded - "
368             + FeatureOAMPHandlerList.Instance().getErrorMessage());
369         }
370     }
371     
372     private ClientParms processClient(Node node, String JavaDoc featurename)
373     throws AceException
374     {
375         String JavaDoc[] attributes =
376         { ATT_CLIENT_CLASS,
377           ATT_CLIENT_MENUNAME,
378           ATT_CLIENT_DISPLAY_TO_SUBSCRIBER };
379           
380           String JavaDoc[] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes);
381           
382           if ((attrib_values[0] == null) || (attrib_values[1] == null) || (attrib_values[2] == null))
383           {
384               throw new AceException("One or more options from "
385               + ATT_CLIENT_CLASS
386               + ", "
387               + ATT_CLIENT_MENUNAME
388               + ", "
389               + ATT_CLIENT_DISPLAY_TO_SUBSCRIBER
390               + " is not specified in "
391               + EL_CLIENT
392               + " for feature "
393               + featurename);
394           }
395           
396           String JavaDoc display_s = attrib_values[2];
397           boolean display_to_subs;
398           
399           if (display_s.equals("yes") == true)
400           {
401               display_to_subs = true;
402           }
403           else if (display_s.equals("no") == true)
404           {
405               display_to_subs = false;
406           }
407           else
408           {
409               throw new AceException("Expecting yes or no for attribute "
410               + ATT_CLIENT_DISPLAY_TO_SUBSCRIBER
411               + " in "
412               + EL_CLIENT
413               + " for feature "
414               + featurename);
415           }
416           
417           PluginParameters params = new PluginParameters();
418           processPluginParameters(node, params, featurename);
419           
420           return new ClientParms(attrib_values[1], attrib_values[0], display_to_subs, params);
421     }
422     
423     private void processPluginParameters(Node node, PluginParameters params, String JavaDoc featurename)
424     throws AceException
425     {
426         Node ele_node;
427         for (ele_node = node.getFirstChild();
428         ele_node != null;
429         ele_node = ele_node.getNextSibling())
430         {
431             if (ele_node.getNodeType() == Node.ELEMENT_NODE)
432             {
433                 if (ele_node.getNodeName().equals(EL_PLUGIN_PARAM) == true)
434                 {
435                     String JavaDoc[] sattributes =
436                     { ATT_PLUGIN_NAME, ATT_PLUGIN_VALUE};
437                     String JavaDoc[] sattrib_values = AceXMLHelper.getXMLAttributes(ele_node, sattributes);
438                     
439                     if ((sattrib_values[0] == null) ||
440                     (sattrib_values[1] == null))
441                     {
442                         throw new AceException("One or more options from "
443                         + ATT_PLUGIN_NAME
444                         + ", "
445                         + ATT_PLUGIN_VALUE
446                         + " is not specified for "
447                         + EL_PLUGIN_PARAM
448                         + " for feature "
449                         + featurename);
450                     }
451                     
452                     params.addParameter(sattrib_values[0],
453                     sattrib_values[1]);
454                 }
455                 else
456                 {
457                     throw new AceException("invalid node name - "
458                     + ele_node.getNodeName()
459                     + " for feature "
460                     + featurename);
461                 }
462             }
463             // else ignore other stuff
464
}
465     }
466     
467     private String JavaDoc processServer(Node node, PluginParameters params, String JavaDoc featurename)
468     throws AceException
469     {
470         String JavaDoc[] attributes =
471         { ATT_SERVER_CLASS };
472         
473         String JavaDoc[] attrib_values = AceXMLHelper.getXMLAttributes(node, attributes);
474         
475         if (attrib_values[0] == null)
476         {
477             throw new AceException("server attribute "
478             + ATT_SERVER_CLASS
479             + " is not specified in "
480             + EL_SERVER
481             + " for feature "
482             + featurename);
483         }
484         
485         processPluginParameters(node, params, featurename);
486         
487         return new String JavaDoc(attrib_values[0]);
488     }
489     
490     public PluginAppClientInterface newInstance()
491     {
492         PluginAppClientInterface ret = null;
493         try
494         {
495             ret = new OAMPClient();
496         }
497         catch (AceException ex)
498         {
499             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
500             "FeatureOAMP.newInstance() -- An error occurred creating the OAMP client interface plugin: "
501             + ex.getMessage());
502             return null;
503         }
504         
505         return ret;
506     }
507     
508     
509     public String JavaDoc getApplicationDescription()
510     {
511         return "System and Feature OAMP Framework";
512     }
513     
514     public String JavaDoc getApplicationName()
515     {
516         return "OAMP";
517     }
518     
519     public String JavaDoc getDatabaseName()
520     {
521         return sqlDatabase;
522     }
523     
524     /** Getter for property accessInfo.
525      * @return Value of property accessInfo.
526      */

527     public com.quikj.server.framework.AceNetworkAccess getAccessInfo()
528     {
529         return accessInfo;
530     }
531         
532     private long lastModTime;
533     private String JavaDoc absPath;
534     private String JavaDoc sqlDatabase;
535     private String JavaDoc featureConfigDir;
536     private String JavaDoc featureConfigFile;
537     private LogListener logListener;
538     
539     private static FeatureOAMP instance = null;
540     private LogMessageMonitor logMonitor = null;
541     private AceNetworkAccess accessInfo = null;
542 }
543
Popular Tags