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 package org.openmobileis.services.common;
00030
00031 import java.io.IOException;
00032
00033 import javax.servlet.ServletException;
00034 import javax.servlet.http.HttpServletRequest;
00035 import javax.servlet.http.HttpServletResponse;
00036
00037 import org.openmobileis.common.context.SessionContext;
00038 import org.openmobileis.common.context.SessionContextManager;
00039 import org.openmobileis.common.util.collection.Array;
00040 import org.openmobileis.common.util.exception.ServiceException;
00041 import org.openmobileis.common.util.log.LogManager;
00042 import org.openmobileis.common.util.log.LogServices;
00043 import org.openmobileis.modules.profiles.terminal.RubricLoader;
00044 import org.openmobileis.services.Service;
00045 import org.openmobileis.services.servlet.OpenmisHttpServletRequest;
00046
00057 public class ServiceManager {
00058
00059 private static ServiceManager manager;
00060
00061 private java.util.Hashtable services = new java.util.Hashtable(10);
00062 private String baseURI;
00063
00064
00065
00066 public void init (java.util.Properties props, String baseURI) throws ServiceException {
00067
00068 this.baseURI = baseURI;
00069
00070
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 serviceFileName = (String)enume.nextElement();
00079 String className = props.getProperty(serviceFileName);
00080
00081 try {
00082
00083 Object toload = Class.forName( className ).newInstance();
00084 if (toload instanceof Service) {
00085 Service service = (Service)toload;
00086 this.loadService(service);
00087 } else if (toload instanceof RubricLoader){
00088 RubricLoader module = (RubricLoader)toload;
00089 this.loadRubricLoader(module);
00090 } else {
00091 LogManager.traceError(LogServices.WEBSERVICE, "Try to load a class which is neither a service, neither a module loader : "+className);
00092 }
00093 } catch ( ClassNotFoundException e ) {
00094 LogManager.traceError(LogServices.WEBSERVICE, "Class not found: " + className );
00095 throw new ServiceException(e);
00096 } catch ( ClassCastException e ) {
00097 LogManager.traceError(LogServices.WEBSERVICE, "Class cast problem: " + e.getMessage());
00098 throw new ServiceException(e);
00099 } catch ( InstantiationException e ) {
00100 LogManager.traceError(LogServices.WEBSERVICE, "Instantiation problem: " + e.getMessage() );
00101 throw new ServiceException(e);
00102 } catch ( IllegalAccessException e ) {
00103 LogManager.traceError(LogServices.WEBSERVICE, "Illegal class access: " + e.getMessage());
00104 throw new ServiceException(e);
00105 } catch ( Exception e ) {
00106 LogManager.traceError(LogServices.WEBSERVICE, "Unexpected problem creating servlet: " + e );
00107 throw new ServiceException(e);
00108 }
00109 }
00110 }
00111
00112 public void loadService(Service service) {
00113 String serviceURI = service.getServiceUri();
00114 if (serviceURI.startsWith(ServiceManager.getManager().getServiceBaseURI())) {
00115 serviceURI = serviceURI.substring(ServiceManager.getManager().getServiceBaseURI().length(), serviceURI.length());
00116 }
00117 services.put(serviceURI, service );
00118 }
00119
00120 public void loadRubricLoader(RubricLoader rubricLoader) throws ServiceException {
00121 rubricLoader.preLoadingInit();
00122
00123
00124 Array serviceList = rubricLoader.loadService();
00125 for (int i=0; i<serviceList.size(); i++) {
00126 Service service = (Service)serviceList.get(i);
00127 this.loadService(service);
00128 }
00129
00130
00131 rubricLoader.postLoadingInit();
00132 }
00133
00134 public String getServiceBaseURI() {
00135 return baseURI;
00136 }
00137
00138 public void setServiceBaseURI(String uri) {
00139 baseURI = uri;
00140 }
00141
00142
00143 public static ServiceManager getManager() {
00144 if (manager == null) {
00145 synchronized(ServiceManager.class) {
00146 if (manager == null) {
00147 manager = new ServiceManager();
00148 }
00149 }
00150 }
00151 return manager;
00152 }
00153
00154 private ServiceManager() {
00155 }
00156
00157
00158
00159
00160
00161
00162 private Service getService (String serviceName) {
00163
00164 Service service = (Service)services.get(serviceName);
00165 if (service == null) {
00166 int index = serviceName.lastIndexOf('/');
00167 while (index != -1) {
00168 serviceName = serviceName.substring(0, index);
00169 service = (Service)services.get(serviceName);
00170 if (service != null) {
00171 break;
00172 }
00173 index = serviceName.lastIndexOf('/');
00174 }
00175 }
00176 return service;
00177 }
00178
00179
00180
00181
00182
00183
00184 public Service getServiceByURI(String serviceURI) {
00185 if (serviceURI.startsWith(baseURI)) {
00186 String serviceName = serviceURI.substring(ServiceManager.getManager().getServiceBaseURI().length(), serviceURI.length());
00187 return this.getService(serviceName);
00188 } else {
00189 return this.getService(serviceURI);
00190 }
00191 }
00192
00193 public void redirectToServiceURI(String uri, HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
00194 Service service = this.getServiceByURI(uri);
00195 OpenmisHttpServletRequest request = (OpenmisHttpServletRequest)req;
00196 if (!uri.startsWith(baseURI)) {
00197 uri=baseURI+uri;
00198 }
00199 request.setRequestURI(uri);
00200 service.runService(req, res);
00201 }
00202
00203 public CallingServiceManager getCallingServiceManager() {
00204 SessionContext session = SessionContextManager.getManager().getSessionContext();
00205 CallingServiceManager callServManager = CallingServiceManager.getManager();
00206 return callServManager;
00207 }
00208
00209 }