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

Generated on Mon Jul 10 10:29:31 2006 for OpenMobileIS by  doxygen 1.4.4