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

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;
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   // key : service name, value : service instance
00057   private java.util.Hashtable services = new java.util.Hashtable(10);
00058   private Array modulesList; //keep module list to avoid class garbage
00059   private String baseURI;// = "/services";
00060 
00061 
00062   // init called by the web server at the initialization phase (new Serve())
00063   public void init (java.util.Properties props, String baseURI) {
00064         // Init baseURI
00065         this.baseURI = baseURI;
00066       
00067                 // load services
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                                  // Check if we're allowed to make one of these.
00074                          try {
00075 //      LogManager.traceWarning(0, "SERVICE_MANAGER loading service "+serviceName+" class :"+className);
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                                 } // end catch
00105                         } //end while
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   * Return the Service called serviceName. Return null if it is not registered
00133   * @param : String service name
00134   * @return: Service or null
00135   */
00136   public Service getService (String serviceName) {
00137     // try to get the service from URL of type /serviceName/otherPath/....
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 }

Generated on Thu Oct 6 10:06:33 2005 for OpenMobileIS by  doxygen 1.4.3