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.services.Service;
00051 import org.openmobileis.services.common.ServiceManager;
00052 
00053 public class OpenMISServlet extends HttpServlet {
00054         static final long serialVersionUID = 5521257935120563452L;
00055 
00056         //inited to avoid class garbage collection.
00057         private static ApplicationContextManager applicationContextManager;
00058 
00059         /*
00060          * Start service from GET method, same behavior as POST @param req the
00061          * servlet request @param req the servlet response @exception
00062          * ServletException when an exception has occurred
00063          */
00064 
00065         public OpenMISServlet() {
00066                 applicationContextManager = ApplicationContextManager.getManager();
00067                 applicationContextManager.getClass(); // to be read.
00068         }
00069 
00070         public void init() throws ServletException {
00071 
00072                 //init log 
00073                 Properties props = new Properties();
00074                 LogManager.registerLogManager(props); //init LogManager to the console
00075 
00076                 // This property gives OpenMIS' install path.
00077                 String installpath = this.getInitParameter("org.openmis.services.installpath");
00078                 if (installpath == null) {
00079                         installpath = "";
00080                 }
00081                 String userdir = this.getServletContext().getRealPath(installpath);
00082                 File file = new File(userdir);
00083                 if (!file.isDirectory()) {
00084                         throw new ServletException("Invalid installation path : " + userdir + ". Can't Initialize application !!!");
00085                 }
00086                 try {
00087                         userdir = file.getCanonicalPath(); //get full path
00088                 } catch (Throwable ex) {
00089                         ex.printStackTrace();
00090                 }
00091 
00092                 System.setProperty("user.dir", userdir);
00093                 // This property gives OpenMIS' property file.
00094                 // It can be relative from the install path defined before, or absolute.
00095                 String propsFile = this.getInitParameter("org.openmis.services.conffile");
00096                 if (propsFile == null || propsFile.trim().length() == 0) {
00097                         throw new ServletException("Servlet didn't find 'org.openmis.services.conffile' init parameter. Can't Initialize application !!!");
00098                 }
00099 
00100                 LogManager.traceInfo(0, "OpenMISServlet install path :" + installpath);
00101                 LogManager.traceInfo(0, "OpenMISServlet userdir :" + userdir);
00102                 LogManager.traceInfo(0, "OpenMISServlet propsFile :" + propsFile);
00103 
00104                 initOpenMIS(propsFile);
00105         }
00106 
00107         private void initOpenMIS(String propertiesFileName) throws ServletException {
00108                 OpenMISInit openmisInit = null;
00109 
00110                 // The property filename can be relative to user.dir or absolute. We test both.
00111                 String absolutePath = System.getProperty("user.dir") + propertiesFileName;
00112                 File confFile = new File(absolutePath);
00113 
00114                 if ((!confFile.exists()) || (!confFile.isFile())) {
00115                         absolutePath = propertiesFileName;
00116                 }
00117                 try {
00118                         try {
00119                                 PropertiesManager.getManager().addPropertiesFileFromFilePath(absolutePath);
00120                         } catch (Throwable ex) {
00121                                 throw new ServletException("Servlet didn't find property file. Didn't find " + absolutePath + " neither " + propertiesFileName
00122                                                 + ". Can't Initialize application !!!");
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                         this.loadServices(listServices);
00160                 } else {
00161                         try {
00162                                 ServiceManager.getManager().init(null, "/" + this.getServletName());
00163                         } catch (ServiceException ex) {
00164                                 LogManager.traceError(0, ex);
00165                         }
00166                 }
00167 
00168                 if (openmisInit != null) {
00169                         try {
00170                                 openmisInit.postLoadingInit();
00171                         } catch (Exception ex) {
00172                                 throw new ServletException("error during init server properties and log", ex);
00173                         }
00174                 }
00175         }
00176 
00177         private void loadServices(String listServicesFile) {
00178                 java.io.File file = new java.io.File(listServicesFile);
00179                 if (!file.exists())
00180                         LogManager.traceAlert(LogServices.WEBSERVICE, "OpenMIS service list file not found. " + listServicesFile);
00181                 else {
00182                         Properties props = new Properties();
00183                         FileInputStream fileservice = null;
00184                         try {
00185                                 fileservice = new java.io.FileInputStream(file);
00186                                 props.load(fileservice);
00187                                 ServiceManager.getManager().init(props, "/" + this.getServletName());
00188                         } catch (Exception ex) {
00189                                 LogManager.traceAlert(LogServices.WEBSERVICE, "Error loading services");
00190                                 LogManager.traceAlert(LogServices.WEBSERVICE, ex);
00191                         } finally {
00192                                 if (fileservice != null) {
00193                                         try {
00194                                                 fileservice.close();
00195                                         } catch (Exception ex) {
00196                                                 LogManager.traceAlert(LogServices.WEBSERVICE, "Unable to close file : " + listServicesFile);
00197                                         }
00198                                 }
00199                         }
00200                 }
00201         }
00202 
00203         public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00204                 this.processRequest(req, res);
00205         }
00206 
00217         public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00218                 this.processRequest(req, res);
00219         }
00220 
00221         private void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00222                 String serviceName = req.getPathInfo();
00223                 if (serviceName == null) {
00224                         res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00225                 }
00226                 Service service = ServiceManager.getManager().getServiceByURI(serviceName);
00227                 if (service == null) {
00228 
00229                         LogManager.traceNotice(0, "ServiceError Service not found " + serviceName);
00230                         IntlResourceManager resourceManager = IntlResourceManager.getManager();
00231                         String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.UnknownService");
00232                         res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + serviceName);
00233 
00234                 } else {
00235                         if (this.isAuthorized(service)) {
00236                                 // manage session context
00237                                 this.getValideSessionContext(req.getSession(true).getId());
00238 
00239                                 try {
00240                                         service.runService(new OpenmisHttpServletRequest(req), res);
00241                                 } finally {
00242                                         SessionContextManager.getManager().leaveSessionContext();
00243                                 }
00244 
00245                         } else {// Not allow to execute the service
00246                                 LogManager.traceNotice(0, "ServiceError unauthorized service error " + serviceName);
00247                                 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00248                                 String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.AccessDeny");
00249                                 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + service.getServiceUri());
00250                         }
00251                 }
00252         }
00253 
00258         private boolean isAuthorized(Service service) {
00259                 return true;
00260         }
00261 
00266         protected SessionContext getValideSessionContext(String contextID) {
00267                 SessionContext session = SessionContextManager.getManager().getSessionContext(contextID);
00268                 if (session == null) {
00269                         synchronized (SessionContextManager.getManager()) {
00270                                 session = SessionContextManager.getManager().getSessionContext(contextID);
00271                                 if (session == null) {
00272                                         session = SessionContextManager.getManager().createSessionContext(contextID);
00273                                 }
00274                         }
00275                 }
00276                 SessionContextManager.getManager().joinSessionContext(session.getId());
00277                 return session;
00278         }
00279 
00280 }

Generated on Tue May 22 23:01:11 2007 for OpenMobileIS by  doxygen 1.5.1-p1