00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00060
00061
00062
00063
00064 public OpenMISServlet() {
00065 applicationContextManager = ApplicationContextManager.getManager();
00066 }
00067
00068 public void init() throws ServletException {
00069
00070
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
00083
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
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
00119
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
00128
00129
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
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
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 {
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 }