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.intl.IntlResourceManager;
00044 import org.openmobileis.common.util.PropertiesManager;
00045 import org.openmobileis.common.util.log.FileLogManager;
00046 import org.openmobileis.common.util.log.LogManager;
00047 import org.openmobileis.common.util.log.LogServices;
00048 import org.openmobileis.services.Service;
00049 import org.openmobileis.services.ServiceManager;
00050
00051 public class OpenMISServlet extends HttpServlet {
00052
00053 private ApplicationContextManager applicationContextManager;
00054
00055
00056
00057
00058
00059
00060
00061 public OpenMISServlet() {
00062 }
00063
00064 public void init() throws ServletException {
00065
00066
00067 String installpath = this.getInitParameter("org.openmis.services.installpath");
00068 if (installpath==null) {
00069 installpath="";
00070 }
00071 String userdir = this.getServletContext().getRealPath(installpath);
00072 File file = new File(userdir);
00073 if (!file.isDirectory()) {
00074 throw new ServletException("Invalid installation path : "+userdir+". Can't Initialize application !!!");
00075 }
00076 System.setProperty("user.dir", userdir);
00077
00078
00079
00080 String propsFile = this.getInitParameter("org.openmis.services.conffile");
00081 if (propsFile == null || propsFile.trim().length() == 0) {
00082 throw new ServletException("Servlet didn't find 'org.openmis.services.conffile' init parameter. Can't Initialize application !!!");
00083 }
00084
00085 initOpenMIS(propsFile);
00086 }
00087
00088 private void initOpenMIS(String propertiesFileName) throws ServletException {
00089 OpenMISInit openmisInit = null;
00090
00091
00092
00093
00094 String absolutePath = System.getProperty("user.dir") + java.io.File.separator + propertiesFileName;
00095 File confFile = new File(absolutePath);
00096
00097 if ((!confFile.exists()) || (!confFile.isFile())) {
00098
00099 confFile = new File(propertiesFileName);
00100
00101 if ((!confFile.exists()) || (!confFile.isFile())) {
00102 throw new ServletException("Servlet didn't find property file. Didn't find "+absolutePath+" neither "+propertiesFileName+". Can't Initialize application !!!");
00103 }
00104 absolutePath = propertiesFileName;
00105 }
00106
00107 try {
00108 System.out.println("Webserver start property file : " + absolutePath);
00109
00110 PropertiesManager.getManager().addPropertiesFileFromFilePath(absolutePath);
00111
00112
00113 String logsFile = PropertiesManager.getManager().getProperty("LOGFILE");
00114
00115
00116 if (logsFile == null)
00117 LogManager.getInstance(null);
00118 else {
00119 logsFile = org.openmobileis.common.util.file.FileUtilities.convertFileNameToSystem(logsFile);
00120 FileLogManager.getInstance(PropertiesManager.getManager().getProperties());
00121 }
00122
00123
00124
00125
00126 String openmisInitClass = PropertiesManager.getManager().getProperty("INITCLASS");
00127 if (openmisInitClass != null && openmisInitClass.length() > 0) {
00128 Object init = Class.forName( openmisInitClass ).newInstance();
00129 if (init instanceof OpenMISInit) {
00130 openmisInit = (OpenMISInit)init;
00131 openmisInit.preLoadingInit();
00132 } else {
00133 LogManager.traceError(LogServices.WEBSERVICE, openmisInitClass+" isn't an instance of OpenMISInit !!!");
00134 }
00135 }
00136
00137 } catch (Exception ex) {
00138 throw new ServletException("error during init server properties and log", ex);
00139 }
00140
00141
00142 applicationContextManager = ApplicationContextManager.getManager();
00143
00144 String listServices = PropertiesManager.getManager().getProperty("listServicesFile");
00145 if (listServices==null) {
00146 throw new ServletException("Servlet didn't find property listServicesFile. Can not load services.");
00147 }
00148 this.loadServices(listServices);
00149
00150 if (openmisInit != null) {
00151 try {
00152 openmisInit.postLoadingInit();
00153 } catch (Exception ex) {
00154 throw new ServletException("error during init server properties and log", ex);
00155 }
00156 }
00157
00158
00159 try {
00160 String plateform = PropertiesManager.getManager().getProperty("serve.plateform");
00161 if ((plateform != null) && (plateform.equals("PPC2002"))) {
00162
00163
00164 System.loadLibrary("ExecDLL2002");
00165 } else if ((plateform != null) && (plateform.equals("PPC2003"))) {
00166
00167 LogManager.traceWarning(0, "WEBSERVER loading 2003 libraries");
00168 System.loadLibrary("ExecDLL2002");
00169
00170 }
00171 } catch (Throwable ex) {
00172 LogManager.traceError(LogServices.WEBSERVICE,
00173 "error during loading system lib WebServer");
00174 LogManager.traceError(LogServices.WEBSERVICE, ex);
00175 ex.getMessage();
00176 System.exit(1);
00177 }
00178
00179 }
00180
00181 private void loadServices(String listServicesFile) {
00182 java.io.File file = new java.io.File(listServicesFile);
00183 if (!file.exists())
00184 LogManager.traceAlert(LogServices.WEBSERVICE,
00185 "Can not load service properties " + listServicesFile);
00186 else {
00187 Properties props = new Properties();
00188 FileInputStream fileservice = null;
00189 try {
00190 fileservice = new java.io.FileInputStream(file);
00191 props.load(fileservice);
00192 ServiceManager.getManager().init(props, "/"+this.getServletName());
00193 } catch (Exception ex) {
00194 LogManager.traceAlert(LogServices.WEBSERVICE, "Error loading services");
00195 LogManager.traceAlert(LogServices.WEBSERVICE, ex);
00196 } finally {
00197 if (fileservice != null) {
00198 try {
00199 fileservice.close();
00200 } catch (Exception ex) {
00201 LogManager.traceAlert(LogServices.WEBSERVICE, "Unable to close file : "+listServicesFile);
00202 }
00203 }
00204 }
00205 }
00206 }
00207
00208 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00209 this.processRequest(req, res);
00210 }
00211
00222 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00223 this.processRequest(req, res);
00224 }
00225
00226 private void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00227 String serviceName = req.getPathInfo();
00228 if (serviceName == null) {
00229 res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00230 }
00231 Service service = ServiceManager.getManager().getService(serviceName);
00232 if (service == null) {
00233 LogManager.traceNotice(0, "ServiceError Service not found " + serviceName);
00234 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00235 String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.UnknownService");
00236 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + serviceName);
00237 } else {
00238 service.runService(new OpenmisHttpServletRequest(req), res);
00239 }
00240 }
00241
00242 }