001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * WebServiceManager.java
020: *
021: * The web service manager. Responsible for controlling the loading and
022: * unloading of web services.
023: */
024:
025: // package paths
026: package com.rift.coad.lib.deployment.webservice;
027:
028: // java imports
029: import java.util.Map;
030: import java.util.HashMap;
031: import java.util.Set;
032: import java.util.Iterator;
033:
034: // logging import
035: import org.apache.log4j.Logger;
036:
037: // coadunation imports
038: import com.rift.coad.lib.deployment.DeploymentLoader;
039:
040: /**
041: * The web service manager. Responsible for controlling the loading and
042: * unloading of web services.
043: *
044: * @author Brett Chaldecott
045: */
046: public class WebServiceManager {
047:
048: /**
049: * This class wrapps the web service list and supplies thread safe access
050: * to the references held within.
051: */
052: public class WebServiceList {
053: // classes private member variables
054: private Map services = null;
055:
056: /**
057: * The constructor of the web service list object.
058: */
059: public WebServiceList() {
060: services = new HashMap();
061: }
062:
063: /**
064: * This method return a list of all the keys that identifying all the
065: * services loaded by the web service manager.
066: *
067: * @return The set containing the web service information.
068: */
069: public synchronized Set getServices() {
070: return services.keySet();
071: }
072:
073: /**
074: * This method adds the web service to the list of services.
075: *
076: * @param path The path of the object to add.
077: * @param obj The object reference to add.
078: */
079: public synchronized void addService(String path, Object obj) {
080: services.put(path, obj);
081: }
082:
083: /**
084: * This method returns the reference to the web service wrapper object.
085: *
086: * @return The reference to the object.
087: * @param The path to retrieve.
088: */
089: public synchronized Object getService(String path) {
090: return services.get(path);
091: }
092:
093: /**
094: * This method removes the service from the list of services.
095: *
096: * @param path The path to remove.
097: */
098: public synchronized void removeService(String path) {
099: services.remove(path);
100: }
101:
102: /**
103: * This method return TRUE if the object is found FALSE if not.
104: *
105: * @param path The path to this object.
106: */
107: public synchronized boolean contains(String path) {
108: return services.containsKey(path);
109: }
110: }
111:
112: // the class log variable
113: protected Logger log = Logger.getLogger(WebServiceManager.class
114: .getName());
115:
116: // the private member variables
117: private Map loaders = null;
118: private WebServiceList serviceList = null;
119:
120: /**
121: * Creates a new instance of WebServiceManager.
122: */
123: public WebServiceManager() {
124: loaders = new HashMap();
125: serviceList = new WebServiceList();
126: }
127:
128: /**
129: * This method will load the specified web service using the deployment
130: * loader.
131: *
132: * @param loader The reference to the loader object.
133: * @exception WebServiceException
134: */
135: public void load(DeploymentLoader loader)
136: throws WebServiceException {
137: if (loaders.containsKey(loader)) {
138: throw new WebServiceException(
139: "This entries has been loaded before.");
140: }
141: // load the web service using the a new web service loader
142: WebServiceLoader serviceLoader = new WebServiceLoader(loader);
143: Map services = serviceLoader.getServices();
144: serviceClash(services);
145:
146: // add the services to the service list
147: for (Iterator iter = services.keySet().iterator(); iter
148: .hasNext();) {
149: String path = (String) iter.next();
150: log.info("Load the web service [" + path + "]");
151: serviceList.addService(path, services.get(path));
152: }
153:
154: // add the entry to the loaders list
155: loaders.put(loader, serviceLoader);
156:
157: }
158:
159: /**
160: * This method unloads the web services from the this object.
161: *
162: * @param loader The reference to the loader responsible for accessing this
163: * object.
164: * @exception WebServiceException
165: */
166: public void unLoad(DeploymentLoader loader)
167: throws WebServiceException {
168: if (false == loaders.containsKey(loader)) {
169: // do nothing there is nothing known about this entry
170: return;
171: }
172:
173: // retrieve a reference to the web service loader
174: WebServiceLoader serviceLoader = (WebServiceLoader) loaders
175: .get(loader);
176:
177: // remove the services
178: Map services = serviceLoader.getServices();
179: for (Iterator iter = services.keySet().iterator(); iter
180: .hasNext();) {
181: String path = (String) iter.next();
182: log.info("Un-load the web service [" + path + "]");
183: serviceList.removeService(path);
184: }
185:
186: // remove the loader
187: loaders.remove(loader);
188: }
189:
190: /**
191: * Retrieve the list of web servies.
192: *
193: * @return The list of web services managed by this object.
194: */
195: public Set getServices() {
196: return serviceList.getServices();
197: }
198:
199: /**
200: * This method retrieve the service identified by the path
201: *
202: * @return The service identified by the path.
203: * @param path The path identifying the path.
204: */
205: public Object getService(String path) {
206: return serviceList.getService(path);
207: }
208:
209: /**
210: * This method checks to see if there is a clash with one of the classes
211: * web services.
212: *
213: * @param services The list of services to make the check on.
214: * @exception WebServiceException
215: */
216: private void serviceClash(Map services) throws WebServiceException {
217: Set keySet = services.keySet();
218: for (Iterator iter = keySet.iterator(); iter.hasNext();) {
219: String path = (String) iter.next();
220: if (serviceList.contains(path)) {
221: throw new WebServiceException(
222: "The service with the path [" + path
223: + "] is already bound");
224: }
225: }
226: }
227:
228: }
|