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 import java.util.Map;
00033 import java.util.StringTokenizer;
00034
00035 import javax.servlet.ServletException;
00036 import javax.servlet.http.HttpServletRequest;
00037 import javax.servlet.http.HttpServletResponse;
00038
00039 import org.openmobileis.common.context.ApplicationContextManager;
00040 import org.openmobileis.common.util.collection.Array;
00041 import org.openmobileis.common.util.exception.ServiceException;
00042 import org.openmobileis.common.util.log.LogManager;
00043 import org.openmobileis.common.util.log.LogServices;
00044 import org.openmobileis.modules.profiles.terminal.RubricLoader;
00045 import org.openmobileis.services.Service;
00046 import org.openmobileis.services.servlet.OpenmisHttpServletRequest;
00047
00058 public class ServiceManager implements ServiceManagerService {
00059
00060 private static ServiceManager manager;
00061
00062 private java.util.Hashtable services = new java.util.Hashtable(10);
00063 private String baseURI;
00064
00065
00066 protected ServiceManager() {
00067 }
00068
00069 public static ServiceManager getManager() {
00070 if (manager == null) {
00071 synchronized(ServiceManager.class) {
00072 if (manager == null) {
00073 manager = new ServiceManager();
00074 }
00075 }
00076 }
00077 return manager;
00078 }
00079
00080 public static void registerManager(ServiceManager servicemanager) {
00081 if (manager == null) {
00082 manager = servicemanager;
00083 ApplicationContextManager.getManager().addManager(manager);
00084 } else {
00085 LogManager.traceDebug(0, "ServiceManager registerManager already done no use");
00086 }
00087 }
00088
00089
00090 public void init (java.util.Properties props, String baseURI) throws ServiceException {
00091
00092 this.baseURI = baseURI;
00093
00094
00095
00096 ReturnLinkManagerService returnlinkservice = new ReturnLinkManagerService();
00097 String returnlinkname = returnlinkservice.getServiceUri().substring(this.baseURI.length(), returnlinkservice.getServiceUri().length());
00098 services.put(returnlinkname, returnlinkservice);
00099
00100 if (props != null) {
00101 java.util.Enumeration enume = props.propertyNames();
00102 while (enume.hasMoreElements()) {
00103 String serviceFileName = (String)enume.nextElement();
00104 String className = props.getProperty(serviceFileName);
00105
00106 try {
00107 Object toload = Class.forName( className ).newInstance();
00108 if (toload instanceof Service) {
00109 Service service = (Service)toload;
00110 this.loadService(service);
00111 } else if (toload instanceof RubricLoader){
00112 RubricLoader module = (RubricLoader)toload;
00113 this.loadRubricLoader(module);
00114 } else {
00115 LogManager.traceError(LogServices.WEBSERVICE, "Try to load a class which is neither a service, neither a module loader : "+className);
00116 }
00117 } catch ( ClassNotFoundException e ) {
00118 LogManager.traceError(LogServices.WEBSERVICE, "Class not found: " + className );
00119 throw new ServiceException(e);
00120 } catch ( ClassCastException e ) {
00121 LogManager.traceError(LogServices.WEBSERVICE, "Class cast problem: " + e.getMessage());
00122 throw new ServiceException(e);
00123 } catch ( InstantiationException e ) {
00124 LogManager.traceError(LogServices.WEBSERVICE, "Instantiation problem: " + e.getMessage() );
00125 throw new ServiceException(e);
00126 } catch ( IllegalAccessException e ) {
00127 LogManager.traceError(LogServices.WEBSERVICE, "Illegal class access: " + e.getMessage());
00128 throw new ServiceException(e);
00129 } catch ( Exception e ) {
00130 LogManager.traceError(LogServices.WEBSERVICE, "Unexpected problem creating servlet: " + e );
00131 throw new ServiceException(e);
00132 }
00133 }
00134 }
00135 }
00136
00137 public void loadService(Service service) {
00138 String serviceURI = service.getServiceUri();
00139 if (serviceURI.startsWith(ServiceManager.getManager().getServiceBaseURI())) {
00140 serviceURI = serviceURI.substring(ServiceManager.getManager().getServiceBaseURI().length(), serviceURI.length());
00141 }
00142 services.put(serviceURI, service );
00143 }
00144
00145 public void unloadService(String serviceURI) {
00146 if (serviceURI.startsWith(ServiceManager.getManager().getServiceBaseURI())) {
00147 serviceURI = serviceURI.substring(ServiceManager.getManager().getServiceBaseURI().length(), serviceURI.length());
00148 }
00149 Object ok = services.remove(serviceURI);
00150 if (ok == null) LogManager.traceDebug(0, "erreur service non unregistrer "+serviceURI);
00151 }
00152
00153 public Array loadRubricLoader(RubricLoader rubricLoader) throws ServiceException {
00154 rubricLoader.preLoadingInit();
00155
00156
00157 Array serviceList = rubricLoader.loadService();
00158 for (int i=0; i<serviceList.size(); i++) {
00159 Service service = (Service)serviceList.get(i);
00160 this.loadService(service);
00161 }
00162
00163
00164 rubricLoader.postLoadingInit();
00165 return serviceList;
00166 }
00167
00168 public String getServiceBaseURI() {
00169 return this.baseURI;
00170 }
00171
00172 public void setServiceBaseURI(String uri) {
00173 this.baseURI = uri;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 private Service getService (String serviceName) {
00183
00184 Service service = (Service)services.get(serviceName);
00185 if (service == null) {
00186 int index = serviceName.lastIndexOf('/');
00187 while (index != -1) {
00188 serviceName = serviceName.substring(0, index);
00189 service = (Service)services.get(serviceName);
00190 if (service != null) {
00191 break;
00192 }
00193 index = serviceName.lastIndexOf('/');
00194 }
00195 }
00196 return service;
00197 }
00198
00199
00200
00201
00202
00203
00204 public Service getServiceByURI(String serviceURI) {
00205 if (serviceURI.startsWith(baseURI)) {
00206 String serviceName = serviceURI.substring(ServiceManager.getManager().getServiceBaseURI().length(), serviceURI.length());
00207 int index = serviceName.indexOf("?");
00208 if (index != -1) {
00209 serviceName = serviceName.substring(0, index);
00210 }
00211 return this.getService(serviceName);
00212 } else {
00213 return this.getService(serviceURI);
00214 }
00215 }
00216
00217 public void redirectToServiceURI(String uri, HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
00218 Service service = this.getServiceByURI(uri);
00219 OpenmisHttpServletRequest request = (OpenmisHttpServletRequest)req;
00220 if (!uri.startsWith(baseURI)) {
00221 uri=baseURI+uri;
00222 }
00223
00224 int index = uri.indexOf("?");
00225 if (index != -1){
00226 String parameterstr = uri.substring(index+1, uri.length());
00227 index = parameterstr.indexOf("#");
00228 if (index != -1) {
00229 parameterstr = parameterstr.substring(0, index);
00230 }
00231 StringTokenizer token = new StringTokenizer(parameterstr, "&");
00232 Map parammap = req.getParameterMap();
00233 while (token.hasMoreTokens()) {
00234 String paramval = token.nextToken();
00235 index = paramval.indexOf("=");
00236 if (index != -1) {
00237 String value = paramval.substring(0, index);
00238 String param[] = new String[]{paramval.substring(index+1, paramval.length())};
00239 parammap.put(value, param);
00240 }
00241 }
00242 }
00243
00244 request.setRequestURI(uri);
00245 service.runService(req, res);
00246 }
00247
00248 public CallingServiceManager getCallingServiceManager() {
00249 CallingServiceManager callServManager = CallingServiceManager.getManager();
00250 return callServManager;
00251 }
00252
00253 }