KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > plugin > FeatureFactory


1 /*
2  * FeatureFactory.java
3  *
4  * Created on April 25, 2002, 2:52 AM
5  */

6
7 package com.quikj.application.web.talk.plugin;
8
9 import com.quikj.server.framework.*;
10 import com.quikj.server.web.*;
11 import com.quikj.client.raccess.*;
12
13 import java.util.*;
14 import java.sql.*;
15
16 /**
17  *
18  * @author amit
19  */

20 public class FeatureFactory implements RemoteServiceInterface
21 {
22     private static FeatureFactory instance = null;
23     
24     private String JavaDoc errorMessage = "";
25     
26     private HashMap featureList = new HashMap(); // key = feature name, value = FeatureElement object
27

28     private static HashMap instantiatedClassList = new HashMap();
29     
30     
31     /** Creates a new instance of FeatureFactory */
32     public FeatureFactory()
33     {
34         setInstance(this);
35     }
36     
37     public boolean init()
38     {
39         // Read all feature info from the database, build the featureList
40

41         Connection connection = JDBCConnection.getInstance().getNewConnection(TalkPluginApp.Instance().getDatabaseName());
42         if (connection == null)
43         {
44             errorMessage = "FeatureFactory connection to the database server failed. " + JDBCConnection.getErrorMessage();
45             return false;
46         }
47         
48         FeatureTable tbl = new FeatureTable();
49         tbl.setConnection(connection);
50         
51         ArrayList list = tbl.queryAll();
52         if (list == null)
53         {
54             errorMessage = "Error reading feature information from the database : " + tbl.getErrorMessage();
55             try
56             {
57                 connection.close();
58             }
59             catch (SQLException ex)
60             {
61             }
62             
63             return false;
64         }
65         
66         for (Iterator i = list.iterator(); i.hasNext();)
67         {
68             FeatureTableElement data = (FeatureTableElement) i.next();
69             
70             if (data.isActive() == true)
71             {
72                 FeatureElement feature = new FeatureElement();
73                 feature.setName(data.getName());
74                 feature.setClassName(data.getClassName());
75                 feature.setMap(data.getParams());
76                 
77                 featureList.put(new String JavaDoc(feature.getName()), feature);
78             }
79         }
80         
81         try
82         {
83             connection.close();
84         }
85         catch (SQLException ex)
86         {
87             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
88             "FeatureFactory.init() -- Error closing database connection : " + ex.getMessage());
89         }
90         
91         return true;
92     }
93     
94     public void dispose()
95     {
96         AceRMIImpl rs = AceRMIImpl.getInstance();
97         if (rs != null) // if remote service has been started
98
{
99             rs.unregisterService("com.quikj.application.web.talk.plugin.FeatureFactory");
100         }
101         
102         setInstance(null);
103     }
104     
105     public static FeatureFactory getInstance()
106     {
107         return instance;
108     }
109     
110     private void setInstance(FeatureFactory instance)
111     {
112         FeatureFactory.instance = instance;
113     }
114     
115     public int numFeatures()
116     {
117         return featureList.size();
118     }
119     
120     public String JavaDoc[] getFeatureNames()
121     {
122         String JavaDoc[] names = new String JavaDoc[featureList.size()];
123         return ((String JavaDoc[]) (featureList.keySet().toArray(names)));
124     }
125     
126     private boolean startFeature(FeatureElement element)
127     {
128         // check if the class exists in the instantiated class list
129
Class JavaDoc app_class = (Class JavaDoc)instantiatedClassList.get(element.getClassName());
130         
131         if (app_class == null) // does not exist
132
{
133             try
134             {
135                 app_class = Class.forName(element.getClassName());
136             }
137             catch (ClassNotFoundException JavaDoc ex)
138             {
139                 errorMessage = "ClassNotFoundException : " + ex.getMessage();
140                 return false;
141             }
142             
143             // check if the class implements the PluginAppInterface
144
Class JavaDoc[] interfaces = app_class.getInterfaces();
145             
146             int found = 0;
147             for (int i = 0; i < interfaces.length; i++)
148             {
149                 if (interfaces[i].getName().equals("com.quikj.application.web.talk.plugin.FeatureInterface")
150                 == true)
151                 {
152                     found++;
153                 }
154                 else if (interfaces[i].getName().equals("com.quikj.server.web.EndPointInterface")
155                 == true)
156                 {
157                     found++;
158                 }
159             }
160             
161             if (found < 2)
162             {
163                 errorMessage = "Class " + element.getClassName()
164                 + " does not implement the necessary interfaces";
165                 return false;
166             }
167             
168             instantiatedClassList.put(element.getClassName(), app_class);
169             
170         }
171         // get a new instance of this class
172
FeatureInterface obj = null;
173         try
174         {
175             obj = (FeatureInterface)app_class.newInstance();
176         }
177         catch (InstantiationException JavaDoc ex1)
178         {
179             errorMessage = "InstantiationException : " + ex1.getMessage();
180             return false;
181         }
182         catch (IllegalAccessException JavaDoc ex2)
183         {
184             errorMessage = "IllegalAccessException : " + ex2.getMessage();
185             return false;
186         }
187         
188         if (obj.init(element.getName(), element.getMap()) == false)
189         {
190             errorMessage = "Class " + element.getClassName()
191             + " returned error during init";
192             return false;
193         }
194         
195         // start the feature
196
obj.start();
197         element.setObj(obj);
198         
199         return true;
200     }
201     
202     public String JavaDoc getErrorMessage()
203     {
204         return errorMessage;
205     }
206     
207     public boolean isFeature(String JavaDoc name)
208     {
209         return featureList.containsKey(name);
210     }
211     
212     public void startUp()
213     {
214         // register for RMI
215

216         AceRMIImpl rs = AceRMIImpl.getInstance();
217         if (rs != null) // if remote service has been started
218
{
219             rs.registerService("com.quikj.application.web.talk.plugin.FeatureFactory",
220             getInstance());
221         }
222         
223         // start all features
224

225         Collection features = featureList.values();
226         for (Iterator i = features.iterator(); i.hasNext();)
227         {
228             FeatureElement feature = (FeatureElement) i.next();
229             try
230             {
231                 if (startFeature(feature) == false)
232                 {
233                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
234                     "FeatureFactory.startUp() -- Feature "
235                     + feature.getName()
236                     + " not started - "
237                     + getErrorMessage());
238                 }
239                 else
240                 {
241                     AceLogger.Instance().log(AceLogger.INFORMATIONAL,
242                     AceLogger.SYSTEM_LOG,
243                     "Feature "
244                     + feature.getName()
245                     + " started");
246                 }
247             }
248             catch (Exception JavaDoc ex)
249             {
250                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
251                 "FeatureFactory.startUp() -- "
252                 + ex.getClass().getName()
253                 + " occurred while creating feature: "
254                 + feature.getName());
255             }
256         }
257     }
258     
259     public String JavaDoc getRMIParam(String JavaDoc param)
260     {
261         return null;
262     }
263     
264     public boolean setRMIParam(String JavaDoc param, String JavaDoc value)
265     {
266         // param = "feature:xxxx" where xxxx is the feature name
267
// value = "activate", "deactivate", or "synch"
268

269         String JavaDoc str = "feature:";
270         if (param.startsWith(str) == true)
271         {
272             String JavaDoc name = param.substring(str.length());
273             if (name.length() > 0)
274             {
275                 if (value.equals("activate") == true)
276                 {
277                     return activateFeature(name);
278                 }
279                 else if (value.equals("deactivate") == true)
280                 {
281                     return deactivateFeature(name);
282                 }
283                 else if (value.equals("synch") == true)
284                 {
285                     return synchFeature(name);
286                 }
287             }
288         }
289         
290         AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
291             "FeatureFactory.setRMIParam() -- Invalid param and/or value received. Param = "
292             + param
293             + ", value = "
294             + value);
295         
296         return false;
297     }
298     
299     public synchronized boolean activateFeature(String JavaDoc name)
300     {
301         FeatureElement feature = getActiveFeatureInfo(name);
302         
303         if (feature == null)
304         {
305             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
306             "FeatureFactory.activateFeature() -- Feature "
307             + name
308             + " not started - "
309             + getErrorMessage());
310             
311             return false;
312         }
313         
314         try
315         {
316             // check if it is already running, for whatever reason...
317

318             FeatureElement old = (FeatureElement) featureList.get(name);
319             if (old != null)
320             {
321                 featureList.remove(name);
322                 
323                 FeatureInterface obj = old.getObj();
324                 if (obj != null)
325                 {
326                     obj.dispose();
327                     
328                     AceLogger.Instance().log(AceLogger.WARNING,
329                     AceLogger.SYSTEM_LOG,
330                     "Feature "
331                     + name
332                     + " being activated, but the feature was already running. Restarting the feature.");
333                 }
334             }
335             
336             // start the feature
337

338             featureList.put(new String JavaDoc(feature.getName()), feature);
339             
340             if (startFeature(feature) == false)
341             {
342                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
343                 "FeatureFactory.activateFeature() -- Feature "
344                 + feature.getName()
345                 + " not started - "
346                 + getErrorMessage());
347                 
348                 featureList.remove(feature.getName());
349             }
350             else
351             {
352                 AceLogger.Instance().log(AceLogger.INFORMATIONAL,
353                 AceLogger.SYSTEM_LOG,
354                 "Feature "
355                 + feature.getName()
356                 + " started");
357                 
358                 return true;
359             }
360         }
361         catch (Exception JavaDoc ex)
362         {
363             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
364             "FeatureFactory.activateFeature() -- "
365             + ex.getClass().getName()
366             + " occurred while creating feature: "
367             + feature.getName());
368             
369             featureList.remove(feature.getName());
370         }
371         
372         return false;
373     }
374     
375     public synchronized boolean deactivateFeature(String JavaDoc name)
376     {
377         FeatureElement feature = (FeatureElement) featureList.get(name);
378         if (feature != null)
379         {
380             featureList.remove(name);
381             
382             FeatureInterface obj = feature.getObj();
383             if (obj != null)
384             {
385                 obj.dispose();
386                 
387                 AceLogger.Instance().log(AceLogger.INFORMATIONAL,
388                 AceLogger.SYSTEM_LOG,
389                 "Feature "
390                 + feature.getName()
391                 + " stopped");
392                 
393                 return true;
394             }
395         }
396         
397         return true;
398     }
399     
400     public synchronized boolean synchFeature(String JavaDoc name)
401     {
402         FeatureElement current_feature = (FeatureElement) featureList.get(name);
403         if (current_feature != null)
404         {
405             FeatureInterface obj = current_feature.getObj();
406             if (obj != null)
407             {
408                 // the feature is currently running
409
FeatureElement new_feature_info = getActiveFeatureInfo(name);
410                 
411                 if (new_feature_info == null)
412                 {
413                     AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
414                     "FeatureFactory.synchFeature() -- Feature "
415                     + name
416                     + " not updated - "
417                     + getErrorMessage());
418                     
419                     return false;
420                 }
421                 
422                 obj.resynchParam(new_feature_info.getMap());
423                 current_feature.setMap(new_feature_info.getMap());
424                 
425                 AceLogger.Instance().log(AceLogger.INFORMATIONAL,
426                 AceLogger.SYSTEM_LOG,
427                 "Feature "
428                 + name
429                 + " parameters re-initialized");
430                 
431                 return true;
432             }
433         }
434         
435         AceLogger.Instance().log(AceLogger.WARNING,
436         AceLogger.SYSTEM_LOG,
437         "Feature "
438         + name
439         + " being updated (synched), but the feature isn't running. Attempting to start the feature with the new data.");
440         
441         return activateFeature(name);
442     }
443     
444     private FeatureElement getActiveFeatureInfo(String JavaDoc name)
445     {
446         FeatureElement feature = null;
447         
448         Connection connection = JDBCConnection.getInstance().getNewConnection(TalkPluginApp.Instance().getDatabaseName());
449         if (connection == null)
450         {
451             errorMessage = "FeatureFactory connection to the database server failed. " + JDBCConnection.getErrorMessage();
452             return null;
453         }
454         
455         FeatureTable tbl = new FeatureTable();
456         tbl.setConnection(connection);
457         
458         FeatureTableElement data = tbl.query(name);
459         
460         if (data == null)
461         {
462             if (tbl.getErrorMessage() == null)
463             {
464                 errorMessage = "Feature name not found in database";
465             }
466             else
467             {
468                 errorMessage = tbl.getErrorMessage();
469             }
470         }
471         else
472         {
473             if (data.isActive() == true)
474             {
475                 feature = new FeatureElement();
476                 feature.setName(data.getName());
477                 feature.setClassName(data.getClassName());
478                 feature.setMap(data.getParams());
479             }
480             else
481             {
482                 errorMessage = "Feature information in database indicates feature not active";
483             }
484         }
485         
486         try
487         {
488             connection.close();
489         }
490         catch (SQLException ex)
491         {
492             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
493             "FeatureFactory.getActiveFeatureInfo() -- Error closing database connection : " + ex.getMessage());
494         }
495         
496         return feature;
497     }
498     
499     
500 }
501
Popular Tags