OpenMISServlet.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  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 
00030 package org.openmobileis.services.servlet;
00031 
00032 import java.io.File;
00033 import java.io.FileInputStream;
00034 import java.io.IOException;
00035 import java.util.Hashtable;
00036 import java.util.Properties;
00037 
00038 import javax.servlet.ServletContext;
00039 import javax.servlet.ServletException;
00040 import javax.servlet.http.HttpServlet;
00041 import javax.servlet.http.HttpServletRequest;
00042 import javax.servlet.http.HttpServletResponse;
00043 
00044 import org.openmobileis.common.context.ApplicationContextManager;
00045 import org.openmobileis.common.context.SessionContext;
00046 import org.openmobileis.common.context.SessionContextManager;
00047 import org.openmobileis.common.intl.IntlResourceManager;
00048 import org.openmobileis.common.util.PropertiesManager;
00049 import org.openmobileis.common.util.log.FileLogManager;
00050 import org.openmobileis.common.util.log.FileOpenCloseLogManager;
00051 import org.openmobileis.common.util.log.LogManager;
00052 import org.openmobileis.common.util.log.LogServices;
00053 import org.openmobileis.embedded.webserver.ServeConfig;
00054 import org.openmobileis.embedded.webserver.WebServerConnection;
00055 import org.openmobileis.services.Service;
00056 import org.openmobileis.services.common.CallingServiceManager;
00057 import org.openmobileis.services.common.ServiceManager;
00058 
00059 public class OpenMISServlet extends HttpServlet {
00060   static final long serialVersionUID = 5521257935120563452L;
00061     
00062     //inited to avoid class garbage collection.
00063     private static ApplicationContextManager applicationContextManager;
00064  
00065     /*
00066      * Start service from GET method, same behavior as POST @param req the
00067      * servlet request @param req the servlet response @exception
00068      * ServletException when an exception has occurred
00069      */
00070     
00071     public OpenMISServlet() {
00072       applicationContextManager = ApplicationContextManager.getManager();
00073       applicationContextManager.getClass(); // to be read.
00074     }
00075     
00076     public void init() throws ServletException {
00077         
00078         // This property gives OpenMIS' install path.
00079         String installpath = this.getInitParameter("org.openmis.services.installpath");
00080         if (installpath==null) {
00081             installpath="";
00082         }
00083         String userdir = this.getServletContext().getRealPath(installpath);
00084         File file = new File(userdir);
00085         if (!file.isDirectory()) {
00086             throw new ServletException("Invalid installation path : "+userdir+". Can't Initialize application !!!");
00087         }
00088                 System.setProperty("user.dir", userdir);
00089         
00090         // This property gives OpenMIS' property file.
00091         // It can be relative from the install path defined before, or absolute.
00092         String propsFile = this.getInitParameter("org.openmis.services.conffile");
00093         if (propsFile == null || propsFile.trim().length() == 0) {
00094             throw new ServletException("Servlet didn't find 'org.openmis.services.conffile' init parameter. Can't Initialize application !!!");
00095         }
00096         
00097         initOpenMIS(propsFile);
00098     }
00099     
00100     private void initOpenMIS(String propertiesFileName) throws ServletException {
00101         OpenMISInit openmisInit = null;
00102 
00103 
00104 
00105         // The property filename can be relative to user.dir or absolute. We test both.
00106         String absolutePath = System.getProperty("user.dir") + propertiesFileName;
00107         File confFile = new File(absolutePath);
00108         
00109         if ((!confFile.exists()) || (!confFile.isFile())) {
00110             
00111             confFile = new File(propertiesFileName);
00112             
00113             if ((!confFile.exists()) || (!confFile.isFile())) {
00114                 throw new ServletException("Servlet didn't find property file. Didn't find "+absolutePath+" neither "+propertiesFileName+". Can't Initialize application !!!");
00115             }
00116             absolutePath = propertiesFileName;
00117         }
00118 
00119         try {
00120             System.out.println("Webserver start property file : " + absolutePath);
00121             
00122             PropertiesManager.getManager().addPropertiesFileFromFilePath(absolutePath);
00123             
00124 
00125  /*         String logsFile = PropertiesManager.getManager().getProperty("org.openmobileis.common.log.file");
00126             // log either on console or in a log file defined in the property
00127             // file
00128             if (logsFile == null)
00129                 LogManager.registerLogManager(null);
00130             else {
00131                 logsFile = org.openmobileis.common.util.file.FileUtilities.convertFileNameToSystem(logsFile);
00132                 FileOpenCloseLogManager.registerLogManager(PropertiesManager.getManager().getProperties());
00133             }  */
00134             
00135             // First of all, we try to identify if an OpenMISInit class has been declared.
00136             // If yes, we run its preLoadingInit method first.
00137             // Then, its postLoadingInit when all initialization done.
00138             String openmisInitClass = PropertiesManager.getManager().getProperty("org.openmis.services.initclass");
00139             if (openmisInitClass != null && openmisInitClass.length() > 0) {
00140                 Object init = Class.forName( openmisInitClass ).newInstance();
00141                 if (init instanceof OpenMISInit) {
00142                     openmisInit = (OpenMISInit)init;
00143                     openmisInit.preLoadingInit();
00144                 } else {
00145                     LogManager.traceError(LogServices.WEBSERVICE, openmisInitClass+" isn't an instance of OpenMISInit !!!");
00146                 }
00147             }
00148 
00149         } catch (Exception ex) {
00150             LogManager.traceError(0, ex);
00151             throw new ServletException(ex);
00152         }
00153 
00154         //init application context
00155         applicationContextManager = ApplicationContextManager.getManager();
00156         
00157         String listServices = PropertiesManager.getManager().getProperty("listServicesFile");
00158         if (listServices==null) {
00159             throw new ServletException("Servlet didn't find property listServicesFile. Can not load services.");
00160         }
00161         this.loadServices(listServices);
00162         
00163         if (openmisInit != null) {
00164             try {
00165                 openmisInit.postLoadingInit();
00166             } catch (Exception ex) {
00167                 throw new ServletException("error during init server properties and log", ex);
00168             }
00169         }
00170     }
00171     
00172     private void loadServices(String listServicesFile) {
00173         java.io.File file = new java.io.File(listServicesFile);
00174         if (!file.exists())
00175             LogManager.traceAlert(LogServices.WEBSERVICE,
00176                       "OpenMIS service list file not found. " + listServicesFile);
00177         else {
00178             Properties props = new Properties();
00179             FileInputStream fileservice = null;
00180             try {
00181                 fileservice = new java.io.FileInputStream(file);
00182                 props.load(fileservice);
00183                 ServiceManager.getManager().init(props, "/"+this.getServletName());
00184             } catch (Exception ex) {
00185                 LogManager.traceAlert(LogServices.WEBSERVICE, "Error loading services");
00186                 LogManager.traceAlert(LogServices.WEBSERVICE, ex);
00187             } finally {
00188                 if (fileservice != null) {
00189                     try {
00190                         fileservice.close();
00191                     } catch (Exception ex) {
00192                         LogManager.traceAlert(LogServices.WEBSERVICE, "Unable to close file : "+listServicesFile);
00193                     }
00194                 }
00195             }
00196         }
00197     }
00198     
00199     public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00200         this.processRequest(req, res);
00201     }
00202 
00213     public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00214         this.processRequest(req, res);
00215     }
00216 
00217     private void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00218         String serviceName = req.getPathInfo();
00219         if (serviceName == null) {
00220             res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00221         }
00222          Service service = ServiceManager.getManager().getServiceByURI(serviceName);
00223         if (service == null) {
00224         
00225             LogManager.traceNotice(0, "ServiceError Service not found " + serviceName);
00226             IntlResourceManager resourceManager = IntlResourceManager.getManager();
00227             String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.UnknownService");
00228             res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + serviceName);
00229          
00230         } else {
00231             if (this.isAuthorized(service)) {
00232                           // manage session context
00233                   this.getValideSessionContext(req.getSession(true).getId());
00234             
00235             try {
00236                       service.runService(new OpenmisHttpServletRequest(req), res);
00237               } finally {
00238                 SessionContextManager.getManager().leaveSessionContext();
00239               }
00240 
00241           } else    {// Not allow to execute the service
00242                          LogManager.traceNotice(0, "ServiceError unauthorized service error "+serviceName);
00243            IntlResourceManager resourceManager = IntlResourceManager.getManager();
00244             String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.AccessDeny");
00245             res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + service.getServiceUri());
00246           }
00247         }
00248     }
00253      private boolean isAuthorized (Service service) {
00254       return true;
00255       }
00256 
00261       protected SessionContext getValideSessionContext(String contextID) {
00262         SessionContext session = SessionContextManager.getManager().getSessionContext(contextID);
00263         if (session == null)  {
00264           synchronized (SessionContextManager.getManager()) {
00265             session = SessionContextManager.getManager().getSessionContext(contextID);
00266             if (session == null)  {
00267               session = SessionContextManager.getManager().createSessionContext(contextID);
00268             }
00269           }
00270         }
00271         SessionContextManager.getManager().joinSessionContext(session.getId());
00272         return session;
00273       }
00274 
00275 }

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