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.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
00058 private static ApplicationContextManager applicationContextManager;
00059
00060
00061
00062
00063
00064
00065
00066 public OpenMISServlet() {
00067 applicationContextManager = ApplicationContextManager.getManager();
00068 applicationContextManager.getClass();
00069 }
00070
00071 public void init() throws ServletException {
00072
00073
00074 Properties props = new Properties();
00075 LogManager.registerLogManager(props);
00076
00077
00078
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();
00092 } catch (Throwable ex) {
00093 ex.printStackTrace();
00094 }
00095
00096 System.setProperty("user.dir", userdir);
00097
00098
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
00117
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
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
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
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
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
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 {
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 }