ServiceManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
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   // key : service name, value : service instance
00062   private java.util.Hashtable services = new java.util.Hashtable(10);
00063   private String baseURI;// = "/services";
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   // init called by the web server at the initialization phase (new Serve())
00090   public void init (java.util.Properties props, String baseURI) throws ServiceException {
00091        // Init baseURI
00092         this.baseURI = baseURI;
00093 
00094                 // load services
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                                          // Check if we're allowed to make one of these.
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                                         } // end catch
00133                                 } //end while
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     // load services
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     // load listener
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   * Return the Service called serviceName. Return null if it is not registered
00179   * @param : String service name
00180   * @return: Service or null
00181   */
00182   private Service getService (String serviceName) {
00183     // try to get the service from URL of type /serviceName/otherPath/....
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    * Return the Service with its URI. Return null if it is not registered
00201    * @param : String service URI
00202    * @return: Service if found or null
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     if (!uri.startsWith(baseURI)) {
00220       uri=baseURI+uri;
00221     }
00222     //parse URL to get parameters
00223     int index = uri.indexOf("?");
00224     if (index != -1){
00225         String parameterstr = uri.substring(index+1, uri.length());
00226         index = parameterstr.indexOf("#");
00227         if (index != -1)        { //remove remainding anchor #
00228                 parameterstr = parameterstr.substring(0, index);
00229         }
00230         StringTokenizer token = new StringTokenizer(parameterstr, "&");
00231         Map parammap = req.getParameterMap();
00232         while (token.hasMoreTokens())   {
00233                 String paramval = token.nextToken();
00234                 index = paramval.indexOf("=");
00235                 if (index != -1)        {
00236                         String value = paramval.substring(0, index);
00237                         String param[] = new String[]{paramval.substring(index+1, paramval.length())};
00238                         parammap.put(value, param);
00239                 }
00240         }
00241     }
00242 
00243     if (req instanceof OpenmisHttpServletRequest)       {
00244             OpenmisHttpServletRequest request = (OpenmisHttpServletRequest)req;
00245             request.setRequestURI(uri);
00246     }
00247           service.runService(req, res);
00248   }
00249 
00250   public CallingServiceManager getCallingServiceManager()       {
00251                 CallingServiceManager callServManager = CallingServiceManager.getManager();
00252                 return callServManager;
00253   }
00254 
00255 }

Generated on Mon Jan 14 17:29:49 2008 for OpenMobileIS by  doxygen 1.5.4