00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 package org.openmobileis.services.common;
00031
00032 import java.io.IOException;
00033
00034 import javax.servlet.ServletException;
00035 import javax.servlet.http.HttpServletRequest;
00036 import javax.servlet.http.HttpServletResponse;
00037
00038 import org.openmobileis.common.util.collection.Array;
00039 import org.openmobileis.common.util.log.LogManager;
00040 import org.openmobileis.common.util.log.LogServices;
00041 import org.openmobileis.embedded.modules.ModuleLoader;
00042 import org.openmobileis.services.Service;
00043 import org.openmobileis.services.servlet.OpenmisHttpServletRequest;
00044
00055 public class ServiceManager {
00056
00057 private static ServiceManager manager;
00058
00059 private java.util.Hashtable services = new java.util.Hashtable(10);
00060 private Array modulesList;
00061 private String baseURI;
00062
00063
00064
00065 public void init (java.util.Properties props, String baseURI) {
00066
00067 this.baseURI = baseURI;
00068
00069
00070 modulesList = new Array(5);
00071
00072 ReturnLinkManagerService returnlinkservice = new ReturnLinkManagerService();
00073 String returnlinkname = returnlinkservice.getServiceUri().substring(this.baseURI.length(), returnlinkservice.getServiceUri().length());
00074 services.put(returnlinkname, returnlinkservice);
00075
00076 java.util.Enumeration enume = props.propertyNames();
00077 while (enume.hasMoreElements()) {
00078 String serviceName = (String)enume.nextElement();
00079 String className = props.getProperty(serviceName);
00080
00081 try {
00082
00083 Object toload = Class.forName( className ).newInstance();
00084 if (toload instanceof Service) {
00085 Service service = (Service)toload;
00086 service.setServiceURI(this.baseURI + serviceName);
00087 services.put( serviceName, service );
00088 } else if (toload instanceof ModuleLoader){
00089 ModuleLoader modules = (ModuleLoader)toload;
00090 modules.loadService(services);
00091 modulesList.add(modules);
00092 } else {
00093 LogManager.traceError(LogServices.WEBSERVICE, "Try to load a class which is neither a service, neither a module loader : "+className);
00094 }
00095 } catch ( ClassNotFoundException e ) {
00096 LogManager.traceError(LogServices.WEBSERVICE, "Class not found: " + className );
00097 LogManager.traceError(LogServices.WEBSERVICE, e );
00098 } catch ( ClassCastException e ) {
00099 LogManager.traceError(LogServices.WEBSERVICE, "Class cast problem: " + e.getMessage());
00100 LogManager.traceError(LogServices.WEBSERVICE, e );
00101 } catch ( InstantiationException e ) {
00102 LogManager.traceError(LogServices.WEBSERVICE, "Instantiation problem: " + e.getMessage() );
00103 LogManager.traceError(LogServices.WEBSERVICE, e );
00104 } catch ( IllegalAccessException e ) {
00105 LogManager.traceError(LogServices.WEBSERVICE, "Illegal class access: " + e.getMessage());
00106 LogManager.traceError(LogServices.WEBSERVICE, e );
00107 } catch ( Exception e ) {
00108 LogManager.traceError(LogServices.WEBSERVICE, "Unexpected problem creating servlet: " + e );
00109 LogManager.traceError(LogServices.WEBSERVICE, e );
00110 }
00111 }
00112 }
00113
00114 public void addModuleLoader(ModuleLoader moduleLoader) {
00115 moduleLoader.loadService(services);
00116 modulesList.add(moduleLoader);
00117 }
00118
00119 public void removeModuleLoader(ModuleLoader moduleLoader) {
00120 moduleLoader.unloadService(services);
00121 modulesList.remove(moduleLoader);
00122 }
00123
00124 public String getServiceBaseURI() {
00125 return baseURI;
00126 }
00127
00128 public void setServiceBaseURI(String uri) {
00129 baseURI = uri;
00130 }
00131
00132
00133 public static ServiceManager getManager() {
00134 if (manager == null) {
00135 synchronized(ServiceManager.class) {
00136 if (manager == null) {
00137 manager = new ServiceManager();
00138 }
00139 }
00140 }
00141 return manager;
00142 }
00143
00144 private ServiceManager() {
00145 }
00146
00147
00148
00149
00150
00151
00152 public Service getService (String serviceName) {
00153
00154 Service service = (Service)services.get(serviceName);
00155 if (service == null) {
00156 int index = serviceName.lastIndexOf('/');
00157 while (index != -1) {
00158 serviceName = serviceName.substring(0, index);
00159 service = (Service)services.get(serviceName);
00160 if (service != null) {
00161 break;
00162 }
00163 index = serviceName.lastIndexOf('/');
00164 }
00165 }
00166 return service;
00167 }
00168
00169 public Service getServiceByURI(String serviceURI) {
00170 if (serviceURI.startsWith(baseURI)) {
00171 return this.getService(serviceURI.substring(baseURI.length()));
00172 } else {
00173 return this.getService(serviceURI);
00174 }
00175 }
00176
00177 public boolean exists (String serviceName) {
00178 return services.containsKey(serviceName);
00179 }
00180
00181 public void redirectToServiceURI(String uri, HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
00182 Service service = this.getServiceByURI(uri);
00183 OpenmisHttpServletRequest request = (OpenmisHttpServletRequest)req;
00184 if (!uri.startsWith(baseURI)) {
00185 uri=baseURI+uri;
00186 }
00187 request.setRequestURI(uri);
00188 service.runService(req, res);
00189 }
00190
00191 }