KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FeatureOAMPHandlerList.java
3  *
4  * Created on May 27, 2002, 6:53 PM
5  */

6
7 package com.quikj.application.web.oamp.plugin;
8
9 import com.quikj.server.web.*;
10
11 import java.util.*;
12
13
14 public class FeatureOAMPHandlerList
15 {
16     private FeatureOAMPHandlerList()
17     {
18         instance = this;
19     }
20     
21     public static FeatureOAMPHandlerList Instance()
22     {
23         if (instance == null)
24         {
25             new FeatureOAMPHandlerList();
26         }
27         
28         return instance;
29     }
30     
31     public synchronized void dispose()
32     {
33         Iterator i = featureList.values().iterator();
34         while (i.hasNext() == true)
35         {
36             ((FeatureHandlerInterface) i.next()).dispose();
37         }
38         
39         featureList.clear();
40     }
41     
42     public synchronized boolean add(String JavaDoc feature_name, String JavaDoc class_name, PluginParameters parms, ClientParms client_parms)
43     {
44         try
45         {
46             // check if the application already exists in the list
47
if (featureList.get(new String JavaDoc(feature_name)) != null)
48             {
49                 errorMessage = new String JavaDoc("Could not add the application "
50                 + class_name
51                 + " to the list because the application with name "
52                 + feature_name
53                 + " is duplicate");
54                 return false;
55             }
56             
57             Class JavaDoc app_class = Class.forName(class_name);
58             
59             // check if the class implements the FeatureHandlerInterface
60
Class JavaDoc[] interfaces = app_class.getInterfaces();
61             
62             boolean found = false;
63             for (int i = 0; i < interfaces.length; i++)
64             {
65                 if (interfaces[i].getName().equals("com.quikj.application.web.oamp.plugin.FeatureHandlerInterface") == true)
66                 {
67                     found = true;
68                     break;
69                 }
70             }
71             
72             if (found == false)
73             {
74                 errorMessage = new String JavaDoc("Class "
75                 + class_name
76                 + " does not implement the com.quikj.application.web.oamp.plugin.FeatureHandlerInterface");
77                 return false;
78             }
79             
80             // get a new instance of this class
81
com.quikj.application.web.oamp.plugin.FeatureHandlerInterface obj = null;
82             try
83             {
84                 obj = (FeatureHandlerInterface)app_class.newInstance();
85             }
86             catch (InstantiationException JavaDoc ex1)
87             {
88                 errorMessage = new String JavaDoc("InstantiationException : " + ex1.getMessage());
89                 return false;
90             }
91             catch (IllegalAccessException JavaDoc ex2)
92             {
93                 errorMessage = new String JavaDoc("IllegalAccessException : " + ex2.getMessage());
94                 return false;
95             }
96             
97             // next, call the init method to initialize the application
98
if (obj.init(feature_name, parms, client_parms) == false)
99             {
100                 // the application signalled that it has initialization problem(s)
101
return false;
102             }
103             
104             // start the application
105
obj.start();
106             
107             // finally, add the object to the list
108
featureList.put(new String JavaDoc(feature_name), obj);
109         }
110         catch (ClassNotFoundException JavaDoc ex)
111         {
112             errorMessage = new String JavaDoc("Class "
113             + class_name
114             + " not found");
115             return false;
116         }
117         
118         return true;
119     }
120     
121     public void addItemToList (FeatureHandlerInterface item)
122     {
123         featureList.put(new String JavaDoc(item.getFeatureName()), item);
124     }
125     
126     public synchronized boolean delete(String JavaDoc feature_name)
127     {
128         FeatureHandlerInterface obj = (FeatureHandlerInterface)featureList.get(new String JavaDoc(feature_name));
129         
130         if (obj == null)
131         {
132             errorMessage = new String JavaDoc("Application with name "
133             + feature_name
134             + " not found in list");
135             return false;
136         }
137         
138         if (featureList.remove(new String JavaDoc(feature_name)) == null)
139         {
140             errorMessage = new String JavaDoc("The application with name "
141             + feature_name
142             + " could not be removed from the list");
143         }
144         
145         // call dispose
146
obj.dispose();
147         
148         return true;
149     }
150     
151     public synchronized FeatureHandlerInterface getHandler (String JavaDoc feature_name)
152     {
153         return ((FeatureHandlerInterface) featureList.get(new String JavaDoc(feature_name)));
154     }
155     
156     public synchronized FeatureHandlerInterface[] getElements()
157     {
158         FeatureHandlerInterface[] temp = new FeatureHandlerInterface[featureList.size()];
159         return ((FeatureHandlerInterface[]) (featureList.values().toArray(temp)));
160     }
161     
162     public String JavaDoc getErrorMessage()
163     {
164         return errorMessage;
165     }
166     
167     private String JavaDoc errorMessage = "";
168     private Hashtable featureList = new Hashtable();
169     private static FeatureOAMPHandlerList instance = null;
170 }
Popular Tags