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;
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
00053 public class ServiceManager {
00054
00055 private static ServiceManager manager;
00056
00057 private java.util.Hashtable services = new java.util.Hashtable(10);
00058 private Array modulesList;
00059 private String baseURI;
00060
00061
00062
00063 public void init (java.util.Properties props, String baseURI) {
00064
00065 this.baseURI = baseURI;
00066
00067
00068 modulesList = new Array(5);
00069 java.util.Enumeration enume = props.propertyNames();
00070 while (enume.hasMoreElements()) {
00071 String serviceName = (String)enume.nextElement();
00072 String className = props.getProperty(serviceName);
00073
00074 try {
00075
00076 Object toload = Class.forName( className ).newInstance();
00077 if (toload instanceof Service) {
00078 Service service = (Service)toload;
00079 service.init(serviceName);
00080 service.setServiceURI(this.baseURI + serviceName);
00081 services.put( serviceName, service );
00082 } else if (toload instanceof ModuleLoader){
00083 ModuleLoader modules = (ModuleLoader)toload;
00084 modules.loadService(services);
00085 modulesList.add(modules);
00086 } else {
00087 LogManager.traceError(LogServices.WEBSERVICE, "Try to load a class which is neither a service, neither a module loader : "+className);
00088 }
00089 } catch ( ClassNotFoundException e ) {
00090 LogManager.traceError(LogServices.WEBSERVICE, "Class not found: " + className );
00091 LogManager.traceError(LogServices.WEBSERVICE, e );
00092 } catch ( ClassCastException e ) {
00093 LogManager.traceError(LogServices.WEBSERVICE, "Class cast problem: " + e.getMessage());
00094 LogManager.traceError(LogServices.WEBSERVICE, e );
00095 } catch ( InstantiationException e ) {
00096 LogManager.traceError(LogServices.WEBSERVICE, "Instantiation problem: " + e.getMessage() );
00097 LogManager.traceError(LogServices.WEBSERVICE, e );
00098 } catch ( IllegalAccessException e ) {
00099 LogManager.traceError(LogServices.WEBSERVICE, "Illegal class access: " + e.getMessage());
00100 LogManager.traceError(LogServices.WEBSERVICE, e );
00101 } catch ( Exception e ) {
00102 LogManager.traceError(LogServices.WEBSERVICE, "Unexpected problem creating servlet: " + e );
00103 LogManager.traceError(LogServices.WEBSERVICE, e );
00104 }
00105 }
00106 }
00107
00108 public String getServiceBaseURI() {
00109 return baseURI;
00110 }
00111
00112 public void setServiceBaseURI(String uri) {
00113 baseURI = uri;
00114 }
00115
00116
00117 public static ServiceManager getManager() {
00118 if (manager == null) {
00119 synchronized(ServiceManager.class) {
00120 if (manager == null) {
00121 manager = new ServiceManager();
00122 }
00123 }
00124 }
00125 return manager;
00126 }
00127
00128 private ServiceManager() {
00129 }
00130
00131
00132
00133
00134
00135
00136 public Service getService (String serviceName) {
00137
00138 Service service = (Service)services.get(serviceName);
00139 if (service == null) {
00140 int index = serviceName.lastIndexOf('/');
00141 while (index != -1) {
00142 serviceName = serviceName.substring(0, index);
00143 service = (Service)services.get(serviceName);
00144 if (service != null) {
00145 break;
00146 }
00147 index = serviceName.lastIndexOf('/');
00148 }
00149 }
00150 return service;
00151 }
00152
00153 public Service getServiceByURI(String serviceURI) {
00154 if (serviceURI.startsWith(baseURI)) {
00155 return this.getService(serviceURI.substring(baseURI.length()));
00156 } else {
00157 return this.getService(serviceURI);
00158 }
00159 }
00160
00161 public boolean exists (String serviceName) {
00162 return services.containsKey(serviceName);
00163 }
00164
00165 public void redirectToServiceURI(String uri, HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
00166 Service service = this.getServiceByURI(uri);
00167 service.runService(req, res);
00168 }
00169
00170 }