KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > deployment > webservice > WebServiceManager


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * WebServiceManager.java
20  *
21  * The web service manager. Responsible for controlling the loading and
22  * unloading of web services.
23  */

24
25 // package paths
26
package com.rift.coad.lib.deployment.webservice;
27
28 // java imports
29
import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 // logging import
35
import org.apache.log4j.Logger;
36
37 // coadunation imports
38
import com.rift.coad.lib.deployment.DeploymentLoader;
39
40
41 /**
42  * The web service manager. Responsible for controlling the loading and
43  * unloading of web services.
44  *
45  * @author Brett Chaldecott
46  */

47 public class WebServiceManager {
48     
49     /**
50      * This class wrapps the web service list and supplies thread safe access
51      * to the references held within.
52      */

53     public class WebServiceList {
54         // classes private member variables
55
private Map JavaDoc services = null;
56         
57         /**
58          * The constructor of the web service list object.
59          */

60         public WebServiceList() {
61             services = new HashMap JavaDoc();
62         }
63         
64         
65         /**
66          * This method return a list of all the keys that identifying all the
67          * services loaded by the web service manager.
68          *
69          * @return The set containing the web service information.
70          */

71         public synchronized Set JavaDoc getServices() {
72             return services.keySet();
73         }
74         
75         
76         /**
77          * This method adds the web service to the list of services.
78          *
79          * @param path The path of the object to add.
80          * @param obj The object reference to add.
81          */

82         public synchronized void addService(String JavaDoc path, Object JavaDoc obj) {
83             services.put(path,obj);
84         }
85         
86         
87         /**
88          * This method returns the reference to the web service wrapper object.
89          *
90          * @return The reference to the object.
91          * @param The path to retrieve.
92          */

93         public synchronized Object JavaDoc getService(String JavaDoc path) {
94             return services.get(path);
95         }
96         
97         
98         /**
99          * This method removes the service from the list of services.
100          *
101          * @param path The path to remove.
102          */

103         public synchronized void removeService(String JavaDoc path) {
104             services.remove(path);
105         }
106         
107         
108         /**
109          * This method return TRUE if the object is found FALSE if not.
110          *
111          * @param path The path to this object.
112          */

113         public synchronized boolean contains(String JavaDoc path) {
114             return services.containsKey(path);
115         }
116     }
117     
118     // the class log variable
119
protected Logger log =
120         Logger.getLogger(WebServiceManager.class.getName());
121     
122     // the private member variables
123
private Map JavaDoc loaders = null;
124     private WebServiceList serviceList = null;
125     
126     
127     /**
128      * Creates a new instance of WebServiceManager.
129      */

130     public WebServiceManager() {
131         loaders = new HashMap JavaDoc();
132         serviceList = new WebServiceList();
133     }
134     
135     
136     /**
137      * This method will load the specified web service using the deployment
138      * loader.
139      *
140      * @param loader The reference to the loader object.
141      * @exception WebServiceException
142      */

143     public void load(DeploymentLoader loader) throws WebServiceException {
144         if (loaders.containsKey(loader)) {
145             throw new WebServiceException(
146                     "This entries has been loaded before.");
147         }
148         // load the web service using the a new web service loader
149
WebServiceLoader serviceLoader = new WebServiceLoader(loader);
150         Map JavaDoc services = serviceLoader.getServices();
151         serviceClash(services);
152         
153         // add the services to the service list
154
for (Iterator JavaDoc iter = services.keySet().iterator(); iter.hasNext();) {
155             String JavaDoc path = (String JavaDoc)iter.next();
156             log.info("Load the web service [" + path + "]");
157             serviceList.addService(path, services.get(path));
158         }
159         
160         // add the entry to the loaders list
161
loaders.put(loader,serviceLoader);
162         
163     }
164     
165     
166     /**
167      * This method unloads the web services from the this object.
168      *
169      * @param loader The reference to the loader responsible for accessing this
170      * object.
171      * @exception WebServiceException
172      */

173     public void unLoad(DeploymentLoader loader) throws WebServiceException {
174         if (false == loaders.containsKey(loader)) {
175             // do nothing there is nothing known about this entry
176
return;
177         }
178         
179         // retrieve a reference to the web service loader
180
WebServiceLoader serviceLoader = (WebServiceLoader)loaders.get(loader);
181         
182         // remove the services
183
Map JavaDoc services = serviceLoader.getServices();
184         for (Iterator JavaDoc iter = services.keySet().iterator(); iter.hasNext();) {
185             String JavaDoc path = (String JavaDoc)iter.next();
186             log.info("Un-load the web service [" + path + "]");
187             serviceList.removeService(path);
188         }
189         
190         // remove the loader
191
loaders.remove(loader);
192     }
193     
194     /**
195      * Retrieve the list of web servies.
196      *
197      * @return The list of web services managed by this object.
198      */

199     public Set JavaDoc getServices() {
200         return serviceList.getServices();
201     }
202     
203     
204     /**
205      * This method retrieve the service identified by the path
206      *
207      * @return The service identified by the path.
208      * @param path The path identifying the path.
209      */

210     public Object JavaDoc getService(String JavaDoc path) {
211         return serviceList.getService(path);
212     }
213     
214     
215     /**
216      * This method checks to see if there is a clash with one of the classes
217      * web services.
218      *
219      * @param services The list of services to make the check on.
220      * @exception WebServiceException
221      */

222     private void serviceClash(Map JavaDoc services) throws WebServiceException {
223         Set JavaDoc keySet = services.keySet();
224         for (Iterator JavaDoc iter = keySet.iterator(); iter.hasNext();) {
225             String JavaDoc path = (String JavaDoc)iter.next();
226             if (serviceList.contains(path)) {
227                 throw new WebServiceException("The service with the path [" +
228                         path + "] is already bound");
229             }
230         }
231     }
232     
233 }
234
Popular Tags