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.exception.ServiceException;
00048 import org.openmobileis.common.util.log.LogManager;
00049 import org.openmobileis.common.util.log.LogServices;
00050 import org.openmobileis.embedded.webserver.StartWebServer;
00051 import org.openmobileis.services.Service;
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                 applicationContextManager.getClass(); // to be read.
00069         }
00070 
00071         public void init() throws ServletException {
00072 
00073                 // init log
00074                 Properties props = new Properties();
00075                 LogManager.registerLogManager(props); // init LogManager to the
00076                                                                                                 // console
00077 
00078                 // This property gives OpenMIS' install path.
00079                 String installpath = this
00080                                 .getInitParameter("org.openmis.services.installpath");
00081                 if (installpath == null) {
00082                         installpath = "";
00083                 }
00084                 String userdir = this.getServletContext().getRealPath(installpath);
00085                 File file = new File(userdir);
00086                 if (!file.isDirectory()) {
00087                         throw new ServletException("Invalid installation path : " + userdir
00088                                         + ". Can't Initialize application !!!");
00089                 }
00090                 try {
00091                         userdir = file.getCanonicalPath(); // get full path
00092                 } catch (Throwable ex) {
00093                         ex.printStackTrace();
00094                 }
00095 
00096                 System.setProperty("user.dir", userdir);
00097                 // This property gives OpenMIS' property file.
00098                 // It can be relative from the install path defined before, or absolute.
00099                 String propsFile = this
00100                                 .getInitParameter("org.openmis.services.conffile");
00101                 if (propsFile == null || propsFile.trim().length() == 0) {
00102                         throw new ServletException(
00103                                         "Servlet didn't find 'org.openmis.services.conffile' init parameter. Can't Initialize application !!!");
00104                 }
00105 
00106                 LogManager.traceInfo(0, "OpenMISServlet install path :" + installpath);
00107                 LogManager.traceInfo(0, "OpenMISServlet userdir :" + userdir);
00108                 LogManager.traceInfo(0, "OpenMISServlet propsFile :" + propsFile);
00109 
00110                 initOpenMIS(propsFile);
00111         }
00112 
00113         private void initOpenMIS(String propertiesFileName) throws ServletException {
00114                 OpenMISInit openmisInit = null;
00115 
00116                 // The property filename can be relative to user.dir or absolute. We
00117                 // test both.
00118                 String absolutePath = System.getProperty("user.dir")
00119                                 + propertiesFileName;
00120                 File confFile = new File(absolutePath);
00121 
00122                 if ((!confFile.exists()) || (!confFile.isFile())) {
00123                         absolutePath = propertiesFileName;
00124                 }
00125                 try {
00126                         try {
00127                                 PropertiesManager.getManager().addPropertiesFileFromFilePath(
00128                                                 absolutePath);
00129                         } catch (Throwable ex) {
00130                                 throw new ServletException(
00131                                                 "Servlet didn't find property file. Didn't find "
00132                                                                 + absolutePath + " neither "
00133                                                                 + propertiesFileName
00134                                                                 + ". Can't Initialize application !!!");
00135                         }
00136 
00137                         /*
00138                          * String logsFile =
00139                          * PropertiesManager.getManager().getProperty("org.openmobileis.common.log.file"); //
00140                          * log either on console or in a log file defined in the property //
00141                          * file if (logsFile == null) LogManager.registerLogManager(null);
00142                          * else { logsFile =
00143                          * org.openmobileis.common.util.file.FileUtilities.convertFileNameToSystem(logsFile);
00144                          * FileOpenCloseLogManager.registerLogManager(PropertiesManager.getManager().getProperties()); }
00145                          */
00146 
00147                         // First of all, we try to identify if an OpenMISInit class has been
00148                         // declared.
00149                         // If yes, we run its preLoadingInit method first.
00150                         // Then, its postLoadingInit when all initialization done.
00151                         String openmisInitClass = PropertiesManager.getManager().getProperty("org.openmis.services.initclass");
00152                         if (openmisInitClass != null && openmisInitClass.length() > 0) {
00153                                 Object init = Class.forName(openmisInitClass).newInstance();
00154                                 if (init instanceof OpenMISInit) {
00155                                         openmisInit = (OpenMISInit) init;
00156                                         openmisInit.preLoadingInit();
00157                                 } else {
00158                                         LogManager.traceError(LogServices.WEBSERVICE,
00159                                                         openmisInitClass
00160                                                                         + " isn't an instance of OpenMISInit !!!");
00161                                 }
00162                         }
00163 
00164                         //LOG open mobile IS Buils version :                    
00165                     LogManager.traceInfo(0, "Open Mobile IS Build Version  : " + StartWebServer.class.getPackage().getImplementationVersion() );
00166 
00167                 } catch (Exception ex) {
00168                         LogManager.traceError(0, ex);
00169                         throw new ServletException(ex);
00170                 }
00171 
00172                 // init application context
00173                 applicationContextManager = ApplicationContextManager.getManager();
00174 
00175                 String listServices = PropertiesManager.getManager().getProperty(
00176                                 "listServicesFile");
00177                 if (listServices != null) {
00178                         this.loadServices(listServices);
00179                 } else {
00180                         try {
00181                                 ServiceManager.getManager().init(null,
00182                                                 "/" + this.getServletName());
00183                         } catch (ServiceException ex) {
00184                                 LogManager.traceError(0, ex);
00185                         }
00186                 }
00187 
00188                 if (openmisInit != null) {
00189                         try {
00190                                 openmisInit.postLoadingInit();
00191                         } catch (Exception ex) {
00192                                 throw new ServletException(
00193                                                 "error during init server properties and log", ex);
00194                         }
00195                 }
00196         }
00197 
00198         private void loadServices(String listServicesFile) {
00199                 java.io.File file = new java.io.File(listServicesFile);
00200                 if (!file.exists())
00201                         LogManager.traceAlert(LogServices.WEBSERVICE,
00202                                         "OpenMIS service list file not found. " + listServicesFile);
00203                 else {
00204                         Properties props = new Properties();
00205                         FileInputStream fileservice = null;
00206                         try {
00207                                 fileservice = new java.io.FileInputStream(file);
00208                                 props.load(fileservice);
00209                                 ServiceManager.getManager().init(props,
00210                                                 "/" + this.getServletName());
00211                         } catch (Exception ex) {
00212                                 LogManager.traceAlert(LogServices.WEBSERVICE,
00213                                                 "Error loading services");
00214                                 LogManager.traceAlert(LogServices.WEBSERVICE, ex);
00215                         } finally {
00216                                 if (fileservice != null) {
00217                                         try {
00218                                                 fileservice.close();
00219                                         } catch (Exception ex) {
00220                                                 LogManager.traceAlert(LogServices.WEBSERVICE,
00221                                                                 "Unable to close file : " + listServicesFile);
00222                                         }
00223                                 }
00224                         }
00225                 }
00226         }
00227 
00228         public void doGet(HttpServletRequest req, HttpServletResponse res)
00229                         throws ServletException, IOException {
00230                 this.processRequest(req, res);
00231         }
00232 
00243         public void doPost(HttpServletRequest req, HttpServletResponse res)
00244                         throws ServletException, IOException {
00245                 this.processRequest(req, res);
00246         }
00247 
00248         private void processRequest(HttpServletRequest req, HttpServletResponse res)
00249                         throws ServletException, IOException {
00250                 String serviceName = req.getPathInfo();
00251                 if (serviceName == null) {
00252                         res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00253                 }
00254                 Service service = ServiceManager.getManager().getServiceByURI(
00255                                 serviceName);
00256                 if (service == null) {
00257 
00258                         LogManager.traceNotice(0, "ServiceError Service not found "
00259                                         + serviceName);
00260                         IntlResourceManager resourceManager = IntlResourceManager
00261                                         .getManager();
00262                         String title = resourceManager
00263                                         .getLocalizedProperty("ServiceManagerServlet.UnknownService");
00264                         res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title
00265                                         + serviceName);
00266 
00267                 } else {
00268                         if (this.isAuthorized(service)) {
00269                                 // manage session context
00270                                 this.getValideSessionContext(req.getSession(true).getId());
00271 
00272                                 try {
00273                                         service.runService(new OpenmisHttpServletRequest(req), res);
00274                                 } catch (java.net.SocketException ex) {
00275                                         LogManager.traceNotice(0, ex);
00276                                         res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
00277                                                         "Network error");
00278                                 } catch (Throwable ex) {
00279                                         LogManager.traceNotice(0, ex);
00280                                         res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
00281                                                         "See terminal log");
00282                                 } finally {
00283                                         SessionContextManager.getManager().leaveSessionContext();
00284                                 }
00285 
00286                         } else {// Not allow to execute the service
00287                                 LogManager.traceNotice(0,
00288                                                 "ServiceError unauthorized service error "
00289                                                                 + serviceName);
00290                                 IntlResourceManager resourceManager = IntlResourceManager
00291                                                 .getManager();
00292                                 String title = resourceManager
00293                                                 .getLocalizedProperty("ServiceManagerServlet.AccessDeny");
00294                                 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
00295                                                 title + service.getServiceUri());
00296                         }
00297                 }
00298         }
00299 
00304         private boolean isAuthorized(Service service) {
00305                 return true;
00306         }
00307 
00312         protected SessionContext getValideSessionContext(String contextID) {
00313                 SessionContext session = SessionContextManager.getManager()
00314                                 .getSessionContext(contextID);
00315                 if (session == null) {
00316                         synchronized (SessionContextManager.getManager()) {
00317                                 session = SessionContextManager.getManager().getSessionContext(
00318                                                 contextID);
00319                                 if (session == null) {
00320                                         session = SessionContextManager.getManager()
00321                                                         .createSessionContext(contextID);
00322                                 }
00323                         }
00324                 }
00325                 SessionContextManager.getManager().joinSessionContext(session.getId());
00326                 return session;
00327         }
00328 
00329 }

Generated on Mon Jan 14 17:29:49 2008 for OpenMobileIS by  doxygen 1.5.4