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

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4