Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

ServiceManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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  *  2004 Modified by Romain Beaugrand
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   // key : service name, value : service instance
00059   private java.util.Hashtable services = new java.util.Hashtable(10);
00060   private Array modulesList; //keep module list to avoid class garbage
00061   private String baseURI;// = "/services";
00062 
00063 
00064   // init called by the web server at the initialization phase (new Serve())
00065   public void init (java.util.Properties props, String baseURI) {
00066         // Init baseURI
00067         this.baseURI = baseURI;
00068       
00069                 // load services
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                                  // Check if we're allowed to make one of these.
00081                          try {
00082 //      LogManager.traceWarning(0, "SERVICE_MANAGER loading service "+serviceName+" class :"+className);
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                                 } // end catch
00111                         } //end while
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   * Return the Service called serviceName. Return null if it is not registered
00149   * @param : String service name
00150   * @return: Service or null
00151   */
00152   public Service getService (String serviceName) {
00153     // try to get the service from URL of type /serviceName/otherPath/....
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 }

Generated on Wed Dec 14 21:05:35 2005 for OpenMobileIS by  doxygen 1.4.4