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 
00033 import javax.servlet.ServletException;
00034 import javax.servlet.http.HttpServletRequest;
00035 import javax.servlet.http.HttpServletResponse;
00036 
00037 import org.openmobileis.common.context.SessionContext;
00038 import org.openmobileis.common.context.SessionContextManager;
00039 import org.openmobileis.common.util.collection.Array;
00040 import org.openmobileis.common.util.exception.ServiceException;
00041 import org.openmobileis.common.util.log.LogManager;
00042 import org.openmobileis.common.util.log.LogServices;
00043 import org.openmobileis.modules.profiles.terminal.RubricLoader;
00044 import org.openmobileis.services.Service;
00045 import org.openmobileis.services.servlet.OpenmisHttpServletRequest;
00046 
00057 public class ServiceManager {
00058 
00059   private static ServiceManager manager;
00060   // key : service name, value : service instance
00061   private java.util.Hashtable services = new java.util.Hashtable(10);
00062   private String baseURI;// = "/services";
00063 
00064 
00065   // init called by the web server at the initialization phase (new Serve())
00066   public void init (java.util.Properties props, String baseURI) throws ServiceException {
00067         // Init baseURI
00068         this.baseURI = baseURI;
00069       
00070                 // load services
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 serviceFileName = (String)enume.nextElement();
00079                          String className = props.getProperty(serviceFileName);
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              this.loadService(service);
00087                                          } else if (toload instanceof RubricLoader){
00088              RubricLoader module = (RubricLoader)toload;
00089                                                  this.loadRubricLoader(module);
00090                                          } else {
00091                                              LogManager.traceError(LogServices.WEBSERVICE, "Try to load a class which is neither a service, neither a module loader : "+className);
00092                                          }
00093                                  } catch ( ClassNotFoundException e )  {
00094                                                 LogManager.traceError(LogServices.WEBSERVICE, "Class not found: " + className );
00095                                                 throw new ServiceException(e);
00096                                  }      catch ( ClassCastException e )  {
00097                                          LogManager.traceError(LogServices.WEBSERVICE, "Class cast problem: " + e.getMessage());
00098            throw new ServiceException(e);
00099                                  }      catch ( InstantiationException e ) {
00100                                          LogManager.traceError(LogServices.WEBSERVICE, "Instantiation problem: " + e.getMessage() );
00101            throw new ServiceException(e);
00102                                  }      catch ( IllegalAccessException e )  {
00103                                          LogManager.traceError(LogServices.WEBSERVICE, "Illegal class access: " + e.getMessage());
00104            throw new ServiceException(e);
00105                                  }      catch ( Exception e )  {
00106                                          LogManager.traceError(LogServices.WEBSERVICE, "Unexpected problem creating servlet: " + e );
00107            throw new ServiceException(e);
00108                                 } // end catch
00109                         } //end while
00110   }
00111   
00112   public void loadService(Service service) {
00113     String serviceURI = service.getServiceUri();
00114     if (serviceURI.startsWith(ServiceManager.getManager().getServiceBaseURI())) {
00115       serviceURI = serviceURI.substring(ServiceManager.getManager().getServiceBaseURI().length(), serviceURI.length());
00116     }
00117      services.put(serviceURI, service );    
00118   }
00119   
00120   public void loadRubricLoader(RubricLoader rubricLoader) throws ServiceException  {
00121     rubricLoader.preLoadingInit();
00122     
00123     // load services
00124     Array serviceList = rubricLoader.loadService();
00125     for (int i=0; i<serviceList.size(); i++)  {
00126       Service service = (Service)serviceList.get(i);
00127       this.loadService(service);
00128     }
00129    
00130     // load listener
00131     rubricLoader.postLoadingInit();
00132   }
00133   
00134   public String getServiceBaseURI()     {
00135         return baseURI;
00136   }
00137   
00138         public void setServiceBaseURI(String uri)       {
00139                 baseURI = uri;
00140         }
00141 
00142 
00143   public static ServiceManager getManager() {
00144         if (manager == null)    {
00145                 synchronized(ServiceManager.class)      {
00146                                 if (manager == null)    {
00147                                         manager = new ServiceManager();
00148                                 }
00149                 }
00150         }
00151     return manager;
00152   }
00153 
00154   private ServiceManager() {
00155   }
00156 
00157   /*
00158   * Return the Service called serviceName. Return null if it is not registered
00159   * @param : String service name
00160   * @return: Service or null
00161   */
00162   private Service getService (String serviceName) {
00163     // try to get the service from URL of type /serviceName/otherPath/....
00164     Service service =  (Service)services.get(serviceName);
00165     if (service == null)    {
00166         int index  = serviceName.lastIndexOf('/');
00167         while (index != -1) {
00168            serviceName = serviceName.substring(0, index); 
00169            service =  (Service)services.get(serviceName);
00170            if (service != null) {
00171             break;
00172            }
00173            index  = serviceName.lastIndexOf('/');
00174         }
00175     }
00176     return service;
00177   }
00178   
00179   /*
00180    * Return the Service with its URI. Return null if it is not registered
00181    * @param : String service URI
00182    * @return: Service if found or null
00183    */
00184   public Service getServiceByURI(String serviceURI) {
00185           if (serviceURI.startsWith(baseURI)) {
00186       String serviceName = serviceURI.substring(ServiceManager.getManager().getServiceBaseURI().length(), serviceURI.length());
00187                   return this.getService(serviceName);
00188           } else {
00189                   return this.getService(serviceURI);
00190           }
00191   }
00192   
00193   public void redirectToServiceURI(String uri, HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
00194           Service service = this.getServiceByURI(uri);
00195     OpenmisHttpServletRequest request = (OpenmisHttpServletRequest)req;
00196     if (!uri.startsWith(baseURI)) {
00197       uri=baseURI+uri;
00198     } 
00199     request.setRequestURI(uri);
00200           service.runService(req, res);
00201   }
00202   
00203   public CallingServiceManager getCallingServiceManager()       {
00204     SessionContext session = SessionContextManager.getManager().getSessionContext();
00205                 CallingServiceManager callServManager = CallingServiceManager.getManager();
00206                 return callServManager;   
00207   }
00208 
00209 }

Generated on Mon Dec 4 11:03:30 2006 for OpenMobileIS by  doxygen 1.5.1-p1