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

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

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