001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice;
021:
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026:
027: import de.schlund.pfixcore.webservice.config.Configuration;
028: import de.schlund.pfixcore.webservice.config.ServiceConfig;
029:
030: /**
031: * @author mleidig@schlund.de
032: */
033: public class ServiceRegistry {
034:
035: enum RegistryType {
036: APPLICATION, SESSION
037: };
038:
039: Configuration configuration;
040: Map<String, ServiceConfig> runtimeServices;
041:
042: RegistryType registryType;
043: Map<String, ServiceDescriptor> serviceDescriptors;
044: Map<String, Object> serviceObjects;
045:
046: public ServiceRegistry(Configuration configuration,
047: RegistryType registryType) {
048: this .configuration = configuration;
049: this .registryType = registryType;
050: serviceDescriptors = new HashMap<String, ServiceDescriptor>();
051: serviceObjects = new HashMap<String, Object>();
052: runtimeServices = new HashMap<String, ServiceConfig>();
053: }
054:
055: public void deregisterService(String serviceName) {
056: //remove serviceconfig, servicedescriptor and serviceobject
057: throw new RuntimeException("Not yet implemented!");
058: }
059:
060: public boolean isRegistered(String serviceName) {
061: return getService(serviceName) != null;
062: }
063:
064: public ServiceConfig getService(String serviceName) {
065: ServiceConfig srvConf = configuration
066: .getServiceConfig(serviceName);
067: if (srvConf == null) {
068: synchronized (runtimeServices) {
069: srvConf = runtimeServices.get(serviceName);
070: }
071: }
072: if (srvConf != null) {
073: String scope = srvConf.getScopeType();
074: if ((scope.equals(Constants.SERVICE_SCOPE_APPLICATION) && registryType == RegistryType.APPLICATION)
075: || (scope.equals(Constants.SERVICE_SCOPE_SESSION) && registryType == RegistryType.SESSION)
076: || (scope.equals(Constants.SERVICE_SCOPE_REQUEST)))
077: return srvConf;
078: }
079: return null;
080: }
081:
082: public List<ServiceConfig> getServices() {
083: List<ServiceConfig> list = new ArrayList<ServiceConfig>();
084: for (ServiceConfig srvConf : configuration.getServiceConfig()) {
085: String scope = srvConf.getScopeType();
086: if ((scope.equals(Constants.SERVICE_SCOPE_APPLICATION) && registryType == RegistryType.APPLICATION)
087: || (scope.equals(Constants.SERVICE_SCOPE_SESSION) && registryType == RegistryType.SESSION))
088: list.add(srvConf);
089: }
090: return list;
091: }
092:
093: public ServiceDescriptor getServiceDescriptor(String serviceName)
094: throws ServiceException {
095: ServiceDescriptor srvDesc = null;
096: synchronized (serviceDescriptors) {
097: srvDesc = serviceDescriptors.get(serviceName);
098: }
099: if (srvDesc == null) {
100: ServiceConfig srvConf = getService(serviceName);
101: if (srvConf != null) {
102: synchronized (serviceDescriptors) {
103: srvDesc = new ServiceDescriptor(srvConf);
104: serviceDescriptors.put(serviceName, srvDesc);
105: }
106: }
107: }
108: return srvDesc;
109: }
110:
111: public Object getServiceObject(String serviceName)
112: throws ServiceException {
113: Object serviceObject = null;
114: synchronized (serviceObjects) {
115: serviceObject = serviceObjects.get(serviceName);
116: }
117: if (serviceObject == null) {
118: ServiceConfig srvConf = getService(serviceName);
119: if (srvConf != null) {
120: String scope = srvConf.getScopeType();
121: if (scope.equals(Constants.SERVICE_SCOPE_REQUEST))
122: return createServiceObject(srvConf);
123: synchronized (serviceObjects) {
124: serviceObject = createServiceObject(srvConf);
125: serviceObjects.put(serviceName, serviceObject);
126: }
127: }
128: }
129: return serviceObject;
130: }
131:
132: private Object createServiceObject(ServiceConfig srvConf)
133: throws ServiceException {
134: try {
135: Class<?> clazz = Class.forName(srvConf
136: .getImplementationName());
137: Object serviceObject = clazz.newInstance();
138: return serviceObject;
139: } catch (Exception x) {
140: throw new ServiceException(
141: "Can't create instance of service '"
142: + srvConf.getName() + "'.", x);
143: }
144: }
145:
146: }
|