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.CallingServiceManager;
00052 import org.openmobileis.services.common.ServiceManager;
00053
00054 public class OpenMISServlet extends HttpServlet {
00055 static final long serialVersionUID = 5521257935120563452L;
00056
00057
00058 private static ApplicationContextManager applicationContextManager;
00059
00060
00061
00062
00063
00064
00065
00066 public OpenMISServlet() {
00067 applicationContextManager = ApplicationContextManager.getManager();
00068 }
00069
00070 public void init() throws ServletException {
00071
00072
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
00085
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
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
00121
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
00130
00131
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
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
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 {
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 }