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

OpenMISServlet.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.servlet;
00031 
00032 import java.io.File;
00033 import java.io.FileInputStream;
00034 import java.io.IOException;
00035 import java.util.Properties;
00036 
00037 import javax.servlet.ServletException;
00038 import javax.servlet.http.HttpServlet;
00039 import javax.servlet.http.HttpServletRequest;
00040 import javax.servlet.http.HttpServletResponse;
00041 
00042 import org.openmobileis.common.context.ApplicationContextManager;
00043 import org.openmobileis.common.intl.IntlResourceManager;
00044 import org.openmobileis.common.util.PropertiesManager;
00045 import org.openmobileis.common.util.log.FileLogManager;
00046 import org.openmobileis.common.util.log.LogManager;
00047 import org.openmobileis.common.util.log.LogServices;
00048 import org.openmobileis.services.Service;
00049 import org.openmobileis.services.ServiceManager;
00050 
00051 public class OpenMISServlet extends HttpServlet {
00052     
00053     private ApplicationContextManager applicationContextManager;
00054 
00055     /*
00056      * Start service from GET method, same behavior as POST @param req the
00057      * servlet request @param req the servlet response @exception
00058      * ServletException when an exception has occurred
00059      */
00060     
00061     public OpenMISServlet() {
00062     }
00063     
00064     public void init() throws ServletException {
00065         
00066         // This property gives OpenMIS' install path.
00067         String installpath = this.getInitParameter("org.openmis.services.installpath");
00068         if (installpath==null) {
00069             installpath="";
00070         }
00071         String userdir = this.getServletContext().getRealPath(installpath);
00072         File file = new File(userdir);
00073         if (!file.isDirectory()) {
00074             throw new ServletException("Invalid installation path : "+userdir+". Can't Initialize application !!!");
00075         }
00076                 System.setProperty("user.dir", userdir);
00077         
00078         // This property gives OpenMIS' property file.
00079         // It can be relative from the install path defined before, or absolute.
00080         String propsFile = this.getInitParameter("org.openmis.services.conffile");
00081         if (propsFile == null || propsFile.trim().length() == 0) {
00082             throw new ServletException("Servlet didn't find 'org.openmis.services.conffile' init parameter. Can't Initialize application !!!");
00083         }
00084         
00085         initOpenMIS(propsFile);
00086     }
00087     
00088     private void initOpenMIS(String propertiesFileName) throws ServletException {
00089         OpenMISInit openmisInit = null;
00090 
00091 
00092 
00093         // The property filename can be relative to user.dir or absolute. We test both.
00094         String absolutePath = System.getProperty("user.dir") + java.io.File.separator + propertiesFileName;
00095         File confFile = new File(absolutePath);
00096         
00097         if ((!confFile.exists()) || (!confFile.isFile())) {
00098             
00099             confFile = new File(propertiesFileName);
00100             
00101             if ((!confFile.exists()) || (!confFile.isFile())) {
00102                 throw new ServletException("Servlet didn't find property file. Didn't find "+absolutePath+" neither "+propertiesFileName+". Can't Initialize application !!!");
00103             }
00104             absolutePath = propertiesFileName;
00105         }
00106 
00107         try {
00108             System.out.println("Webserver start property file : " + absolutePath);
00109             
00110             PropertiesManager.getManager().addPropertiesFileFromFilePath(absolutePath);
00111             
00112 
00113             String logsFile = PropertiesManager.getManager().getProperty("LOGFILE");
00114             // log either on console or in a log file defined in the property
00115             // file
00116             if (logsFile == null)
00117                 LogManager.getInstance(null);
00118             else {
00119                 logsFile = org.openmobileis.common.util.file.FileUtilities.convertFileNameToSystem(logsFile);
00120                 FileLogManager.getInstance(PropertiesManager.getManager().getProperties());
00121             }
00122             
00123             // First of all, we try to identify if an OpenMISInit class has been declared.
00124             // If yes, we run its preLoadingInit method first.
00125             // Then, its postLoadingInit when all initialization done.
00126             String openmisInitClass = PropertiesManager.getManager().getProperty("INITCLASS");
00127             if (openmisInitClass != null && openmisInitClass.length() > 0) {
00128                 Object init = Class.forName( openmisInitClass ).newInstance();
00129                 if (init instanceof OpenMISInit) {
00130                     openmisInit = (OpenMISInit)init;
00131                     openmisInit.preLoadingInit();
00132                 } else {
00133                     LogManager.traceError(LogServices.WEBSERVICE, openmisInitClass+" isn't an instance of OpenMISInit !!!");
00134                 }
00135             }
00136 
00137         } catch (Exception ex) {
00138             throw new ServletException("error during init server properties and log", ex);
00139         }
00140 
00141         //init application context
00142         applicationContextManager = ApplicationContextManager.getManager();
00143         
00144         String listServices = PropertiesManager.getManager().getProperty("listServicesFile");
00145         if (listServices==null) {
00146             throw new ServletException("Servlet didn't find property listServicesFile. Can not load services.");
00147         }
00148         this.loadServices(listServices);
00149         
00150         if (openmisInit != null) {
00151             try {
00152                 openmisInit.postLoadingInit();
00153             } catch (Exception ex) {
00154                 throw new ServletException("error during init server properties and log", ex);
00155             }
00156         }
00157 
00158         //init system lib
00159         try {
00160             String plateform = PropertiesManager.getManager().getProperty("serve.plateform");
00161             if ((plateform != null) && (plateform.equals("PPC2002"))) {
00162                 //    System.loadLibrary("pimconnector2002");
00163                 //  System.loadLibrary("openmiscnnx2002");
00164                 System.loadLibrary("ExecDLL2002");
00165             } else if ((plateform != null) && (plateform.equals("PPC2003"))) {
00166                 //    System.loadLibrary("pimconnector2002");
00167                 LogManager.traceWarning(0, "WEBSERVER loading 2003 libraries");
00168                 System.loadLibrary("ExecDLL2002");
00169                 //                      System.load("/windows/openmiscnnx2003.dll");
00170             }
00171         } catch (Throwable ex) {
00172             LogManager.traceError(LogServices.WEBSERVICE,
00173                     "error during loading system lib WebServer");
00174             LogManager.traceError(LogServices.WEBSERVICE, ex);
00175             ex.getMessage();
00176             System.exit(1);
00177         }
00178 
00179     }
00180     
00181     private void loadServices(String listServicesFile) {
00182         java.io.File file = new java.io.File(listServicesFile);
00183         if (!file.exists())
00184             LogManager.traceAlert(LogServices.WEBSERVICE,
00185                       "Can not load service properties " + listServicesFile);
00186         else {
00187             Properties props = new Properties();
00188             FileInputStream fileservice = null;
00189             try {
00190                 fileservice = new java.io.FileInputStream(file);
00191                 props.load(fileservice);
00192                 ServiceManager.getManager().init(props, "/"+this.getServletName());
00193             } catch (Exception ex) {
00194                 LogManager.traceAlert(LogServices.WEBSERVICE, "Error loading services");
00195                 LogManager.traceAlert(LogServices.WEBSERVICE, ex);
00196             } finally {
00197                 if (fileservice != null) {
00198                     try {
00199                         fileservice.close();
00200                     } catch (Exception ex) {
00201                         LogManager.traceAlert(LogServices.WEBSERVICE, "Unable to close file : "+listServicesFile);
00202                     }
00203                 }
00204             }
00205         }
00206     }
00207     
00208     public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00209         this.processRequest(req, res);
00210     }
00211 
00222     public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00223         this.processRequest(req, res);
00224     }
00225 
00226     private void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00227         String serviceName = req.getPathInfo();
00228         if (serviceName == null) {
00229             res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00230         }
00231         Service service = ServiceManager.getManager().getService(serviceName);
00232         if (service == null) {
00233             LogManager.traceNotice(0, "ServiceError Service not found " + serviceName);
00234             IntlResourceManager resourceManager = IntlResourceManager.getManager();
00235             String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.UnknownService");
00236             res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + serviceName);
00237         } else {
00238             service.runService(new OpenmisHttpServletRequest(req), res);
00239         }
00240     }
00241 
00242 }

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