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.embedded.webserver;
00031
00032 import java.io.DataInputStream;
00033 import java.io.File;
00034 import java.io.FileInputStream;
00035 import java.io.IOException;
00036 import java.io.InputStream;
00037 import java.net.MalformedURLException;
00038 import java.net.ServerSocket;
00039 import java.net.Socket;
00040 import java.net.URL;
00041 import java.util.Enumeration;
00042 import java.util.Hashtable;
00043 import java.util.Properties;
00044 import java.util.StringTokenizer;
00045 import java.util.Vector;
00046
00047 import javax.servlet.RequestDispatcher;
00048 import javax.servlet.Servlet;
00049 import javax.servlet.ServletContext;
00050 import javax.servlet.ServletException;
00051 import javax.servlet.ServletRequest;
00052 import javax.servlet.ServletResponse;
00053
00054 import org.openmobileis.common.context.ApplicationContextManager;
00055 import org.openmobileis.common.context.Plateform;
00056 import org.openmobileis.common.context.SessionContextManager;
00057 import org.openmobileis.common.util.PropertiesManager;
00058 import org.openmobileis.common.util.file.FileUtilities;
00059 import org.openmobileis.common.util.log.LogManager;
00060 import org.openmobileis.common.util.log.LogServices;
00061 import org.openmobileis.embedded.util.SystemAPI;
00062
00063 public class WebServer implements ServletContext, RequestDispatcher {
00064
00065 private final static int DEFAULT_PORT = 8080;
00066
00067 private ServerThreadPool connexionPool;
00068
00069 protected PathTreeDictionary registry;
00070
00071 private Hashtable servlets = new Hashtable();
00072
00073 private Hashtable attributes;
00074
00075 boolean running = true;
00076 private ServerSocket serverSocket;
00077 protected String hostName;
00078 protected int port = DEFAULT_PORT;
00079
00080 private static Properties webserverProps;
00081
00082
00083 public static String servletFile = "";
00084 public static String installPath = "";
00085
00086
00088
00089 public WebServer() {
00090 this(System.getProperty("user.dir"));
00091 }
00092
00093 public WebServer(String pathToConfFile) {
00094 System.setProperty("user.dir", pathToConfFile);
00095 try {
00096 if (pathToConfFile != null) {
00097 webserverProps = PropertiesManager.getManager().getPropertiesFromPath(pathToConfFile+File.separator+"WEB-INF"+File.separator+"conf"+java.io.File.separator+"properties"+java.io.File.separator+"webserver.properties");
00098 } else {
00099 webserverProps = PropertiesManager.getManager().getPropertiesFromPath("properties"+java.io.File.separator+"webserver.properties");
00100 }
00101
00102 } catch (Exception ex) {
00103 System.out.println("Unable to find webserver.properties !!\nexit(1)");
00104 System.exit(1);
00105 }
00106 String strPort = webserverProps.getProperty("port");
00107 if (strPort != null && strPort.length() > 0) {
00108 try {
00109 this.port = Integer.parseInt(strPort);
00110 } catch (NumberFormatException ex) {
00111 this.port = DEFAULT_PORT;
00112 }
00113 }
00114
00115 WebServer.installPath = webserverProps.getProperty("installPath");
00116 servletFile = webserverProps.getProperty("servletFile");
00117 registry = new PathTreeDictionary();
00118 try {
00119 connexionPool = new ServerThreadPool();
00120 } catch (Exception ex) {
00121 LogManager.traceError(LogServices.WEBSERVICE,
00122 "can not initialise server connexion pool");
00123 LogManager.traceError(0, ex);
00124 }
00125 attributes = new Hashtable();
00126
00127
00128
00129 SessionContextManager.getManager().createSessionContext("OPENMOBILEISSESSION");
00130 SessionContextManager.getManager().joinSessionContext("OPENMOBILEISSESSION");
00131 }
00132
00134
00135
00136
00137 public void addServlet(String urlPat, String className) {
00138 addServlet(urlPat, className, (Hashtable) null);
00139 }
00140
00141 public void addServlet(String urlPat, String className, Hashtable initParams) {
00142
00143 SecurityManager security = System.getSecurityManager();
00144 if (security != null) {
00145 int i = className.lastIndexOf('.');
00146 if (i != -1) {
00147 security.checkPackageAccess(className.substring(0, i));
00148 security.checkPackageDefinition(className.substring(0, i));
00149 }
00150 }
00151
00152
00153 try {
00154 addServlet(urlPat, (Servlet) Class.forName(className).newInstance(), initParams);
00155 return;
00156 } catch (ClassNotFoundException e) {
00157 LogManager.traceError(0, e);
00158 LogManager.traceError(LogServices.WEBSERVICE, "Class not found: " + className);
00159 } catch (ClassCastException e) {
00160 LogManager.traceError(0, e);
00161 LogManager.traceError(LogServices.WEBSERVICE, "Class cast problem: " + e.getMessage());
00162 } catch (InstantiationException e) {
00163 LogManager.traceError(0, e);
00164 LogManager.traceError(LogServices.WEBSERVICE, "Instantiation problem: " + e.getMessage());
00165 } catch (IllegalAccessException e) {
00166 LogManager.traceError(0, e);
00167 LogManager.traceError(LogServices.WEBSERVICE, "Illegal class access: " + e.getMessage());
00168 } catch (Exception e) {
00169 LogManager.traceError(0, e);
00170 LogManager.traceError(LogServices.WEBSERVICE, "Unexpected problem creating servlet: " + e);
00171 LogManager.traceError(LogServices.WEBSERVICE, e);
00172 }
00173 }
00174
00176
00177
00178
00179 public void addServlet(String urlPat, Servlet servlet) {
00180 addServlet(urlPat, servlet, (Hashtable) null);
00181 }
00182
00183 public void addServlet(String urlPat, Servlet servlet, Hashtable initParams) {
00184 try {
00185 servlet.init(new ServeConfig((ServletContext) this, initParams, urlPat));
00186 registry.put(urlPat, servlet);
00187 servlets.put(servlet.getClass().getName(), urlPat);
00188 } catch (ServletException e) {
00189 LogManager.traceError(0, e);
00190 LogManager.traceError(LogServices.WEBSERVICE,
00191 "Problem initializing servlet: " + e);
00192 }
00193 }
00194
00195 public Object[] getServletByURLorClassName(String name) {
00196 Object[] servlet = new Object[2];
00197 servlet = registry.get(name);
00198 if (servlet[0] == null) {
00199 String servletUri = (String)servlets.get(name);
00200 if (servletUri != null && servletUri.trim().length() > 0) {
00201 servlet = registry.get(servletUri);
00202 }
00203 }
00204 if (servlet[0] == null) {
00205 String newName = name;
00206 int index = newName.lastIndexOf('/');
00207 while (index != -1) {
00208 newName = newName.substring(0, index);
00209 servlet = registry.get(newName);
00210 if (servlet[0] != null) {
00211 break;
00212 }
00213 index = newName.lastIndexOf('/');
00214 }
00215 }
00216 return servlet;
00217 }
00218
00219
00220 public void serve() throws IOException{
00221
00222
00223
00224
00225 if (LogManager.getInstance() == null) {
00226 LogManager.registerLogManager(null);
00227 }
00228
00229 this.readServlets(servletFile);
00230 serverSocket = new ServerSocket(port, 1000);
00231 hostName = serverSocket.getInetAddress().getHostName();
00232
00233
00234
00235 try {
00236 Plateform plateform = ApplicationContextManager.getManager().getApplicationContext().getPlateform();
00237 if (plateform.getOS().equals(Plateform.POCKETPCOS)) {
00238
00239 if ((PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser")!=null) && (PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser.args")!=null)) {
00240 System.loadLibrary("systemAPI");
00241 LogManager.traceInfo(0,"Start Pocket IE");
00242 SystemAPI.execProgram(PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser"), PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser.args")+"/"+Long.toString(System.currentTimeMillis()));
00243 } else {
00244
00245 System.loadLibrary("systemAPI");
00246 String path = "\\Windows\\iexplore.exe";
00247 if(plateform.getOSVersion().substring(0,1).equals("4")){
00248 LogManager.traceInfo(0,"Start Pocket IE 2003, OSVersion : "+plateform.getOSVersion());
00249 SystemAPI.execProgram(path, "http://127.0.0.1:9090/services/index"+"/"+Long.toString(System.currentTimeMillis()));
00250
00251 }else if(plateform.getOSVersion().substring(0,1).equals("5")){
00252 LogManager.traceInfo(0,"Start Pocket IE 2005, OSVersion : "+plateform.getOSVersion());
00253 SystemAPI.execProgram(path, "http://localhost:9090/services/index"+"/"+Long.toString(System.currentTimeMillis()));
00254 }else{
00255 LogManager.traceInfo(0,"Pocket IE not started on PocketPC no properties defined. OS Version : "+plateform.getOSVersion());
00256 }
00257
00258 }
00259 }
00260 } catch (Throwable e) {
00261 LogManager.traceError(0, e);
00262 LogManager.traceError(LogServices.WEBSERVICE, "Can not launch ie with parameters : " + PropertiesManager.getManager().getProperty("execprog.browser")
00263 + " args : " + PropertiesManager.getManager().getProperty("execprog.browser.args"));
00264 }
00265
00266 try {
00267 while (true) {
00268 Socket socket = serverSocket.accept();
00269 connexionPool.runServerConnexion(this, socket);
00270 }
00271 } catch (IOException e) {
00272 LogManager.traceError(0, e);
00273 LogManager.traceError(LogServices.WEBSERVICE,
00274 "IOEXception during servlet process Accept: " + e);
00275 } finally {
00276 try {
00277 serverSocket.close();
00278 } catch (Exception e) {
00279 LogManager.traceError(LogServices.WEBSERVICE, e);
00280 }
00281 }
00282 }
00283
00285 public void destroyAllServlets() {
00286 Enumeration en = registry.elements();
00287 while (en.hasMoreElements()) {
00288 Servlet servlet = (Servlet) en.nextElement();
00289 servlet.destroy();
00290 }
00291 registry = new PathTreeDictionary();
00292
00293 }
00294
00295
00296
00297
00298 public void freeResources() throws Exception {
00299 this.destroyAllServlets();
00300 connexionPool.freeResources();
00301 serverSocket.close();
00302 LogManager.traceInfo(LogServices.WEBSERVICE, "Web server is stopped");
00303
00304 }
00305
00306 private void readServlets(String cfgfile) {
00311 Hashtable servletstbl, parameterstbl;
00312 servletstbl = new Hashtable();
00313 parameterstbl = new Hashtable();
00314 if (cfgfile != null) {
00315 File file = new File(cfgfile);
00316 if (file.exists() && file.canRead()) {
00317 try {
00318 DataInputStream in = new DataInputStream(new FileInputStream(file));
00322 do {
00323 String servletdsc = in.readLine();
00324 if (servletdsc == null)
00325 break;
00326 StringTokenizer dsctokenzr = new StringTokenizer(servletdsc, ".=,", false);
00327 if (dsctokenzr.hasMoreTokens()) {
00328 if (!dsctokenzr.nextToken().equalsIgnoreCase("servlet")) {
00329 System.err.println("No leading 'servlet' keyword, the sentence is skipped");
00330 break;
00331 }
00332 if (dsctokenzr.hasMoreTokens()) {
00333 String servletname = dsctokenzr.nextToken();
00334
00335 if (dsctokenzr.hasMoreTokens()) {
00336 String lt = dsctokenzr.nextToken();
00337 if (lt.equalsIgnoreCase("code")) {
00338 if (dsctokenzr.hasMoreTokens())
00339 servletstbl.put(servletname, dsctokenzr.nextToken("="));
00340 } else if (lt.equalsIgnoreCase("initArgs")) {
00341 Hashtable initparams = new Hashtable();
00342 while (dsctokenzr.hasMoreTokens()) {
00343 String key = dsctokenzr.nextToken("=,");
00344 if (dsctokenzr.hasMoreTokens())
00345 initparams.put(key, dsctokenzr.nextToken(",="));
00346 }
00347 parameterstbl.put(servletname, initparams);
00348 } else
00349 System.err.println("Unrecognized token " + lt + " in " + servletdsc + ", the line's skipped");
00350 }
00351 }
00352 }
00353 }
00354 while (true);
00355 } catch (IOException e) {
00356 System.err.println("Problem reading cfg file: " + e);
00357 }
00358 Enumeration se = servletstbl.keys();
00359 String servletname;
00360 while (se.hasMoreElements()) {
00361 servletname = (String) se.nextElement();
00362 addServlet(servletname, (String) servletstbl.get(servletname), (Hashtable) parameterstbl.get(servletname));
00363 }
00364 }
00365 }
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00399
00400
00401
00402 public Object getAttribute(String name) {
00403
00404 return attributes.get(name);
00405 }
00406
00407 public Enumeration getAttributeNames() {
00408 return attributes.keys();
00409 }
00410
00411 public ServletContext getContext(String uripath) {
00412 return this;
00413 }
00414
00415
00416 public String getInitParameter(String param) {
00417 return null;
00418 }
00419
00420 public Enumeration getInitParameterNames() {
00421 return null;
00422 }
00423
00424 public int getMajorVersion() {
00425 return 2;
00426 }
00427
00429
00430 public String getMimeType(String file) {
00431 file = file.toUpperCase();
00432
00433 if (file.endsWith(".HTML") || file.endsWith(".HTM"))
00434 return "text/html";
00435 if (file.endsWith(".TXT"))
00436 return "text/plain";
00437 if (file.endsWith(".XML"))
00438 return "text/xml";
00439 if (file.endsWith(".CSS"))
00440 return "text/css";
00441 if (file.endsWith(".SGML") || file.endsWith(".SGM"))
00442 return "text/x-sgml";
00443
00444 if (file.endsWith(".GIF"))
00445 return "image/gif";
00446 if (file.endsWith(".JPG") || file.endsWith(".JPEG") || file.endsWith(".JPE"))
00447 return "image/jpeg";
00448 if (file.endsWith(".PNG"))
00449 return "image/png";
00450 if (file.endsWith(".TIF") || file.endsWith(".TIFF"))
00451 return "image/tiff";
00452 if (file.endsWith(".RGB"))
00453 return "image/x-rgb";
00454 if (file.endsWith(".XPM"))
00455 return "image/x-xpixmap";
00456 if (file.endsWith(".XBM"))
00457 return "image/x-xbitmap";
00458 if (file.endsWith(".SVG"))
00459 return "image/svg-xml ";
00460 if (file.endsWith(".SVGZ"))
00461 return "image/svg-xml ";
00462
00463 if (file.endsWith(".AU") || file.endsWith(".SND"))
00464 return "audio/basic";
00465 if (file.endsWith(".MID") || file.endsWith(".MIDI") || file.endsWith(".RMI") || file.endsWith(".KAR"))
00466 return "audio/mid";
00467 if (file.endsWith(".MPGA") || file.endsWith(".MP2") || file.endsWith(".MP3"))
00468 return "audio/mpeg";
00469 if (file.endsWith(".WAV"))
00470 return "audio/wav";
00471 if (file.endsWith(".AIFF") || file.endsWith(".AIFC"))
00472 return "audio/aiff";
00473 if (file.endsWith(".AIF"))
00474 return "audio/x-aiff";
00475 if (file.endsWith(".RA"))
00476 return "audio/x-realaudio";
00477 if (file.endsWith(".RPM"))
00478 return "audio/x-pn-realaudio-plugin";
00479 if (file.endsWith(".RAM"))
00480 return "audio/x-pn-realaudio";
00481 if (file.endsWith(".SD2"))
00482 return "audio/x-sd2";
00483
00484 if (file.endsWith(".BIN") || file.endsWith(".DMS") || file.endsWith(".LHA") || file.endsWith(".LZH") || file.endsWith(".EXE") || file.endsWith(".CLASS"))
00485 return "application/octet-stream";
00486 if (file.endsWith(".HQX"))
00487 return "application/mac-binhex40";
00488 if (file.endsWith(".PS") || file.endsWith(".AI") || file.endsWith(".EPS"))
00489 return "application/postscript";
00490 if (file.endsWith(".PDF"))
00491 return "application/pdf";
00492 if (file.endsWith(".RTF"))
00493 return "application/rtf";
00494 if (file.endsWith(".DOC"))
00495 return "application/msword";
00496 if (file.endsWith(".PPT"))
00497 return "application/powerpoint";
00498 if (file.endsWith(".FIF"))
00499 return "application/fractals";
00500 if (file.endsWith(".P7C"))
00501 return "application/pkcs7-mime";
00502
00503 if (file.endsWith(".JS"))
00504 return "application/x-javascript";
00505 if (file.endsWith(".Z"))
00506 return "application/x-compress";
00507 if (file.endsWith(".GZ"))
00508 return "application/x-gzip";
00509 if (file.endsWith(".TAR"))
00510 return "application/x-tar";
00511 if (file.endsWith(".TGZ"))
00512 return "application/x-compressed";
00513 if (file.endsWith(".ZIP"))
00514 return "application/x-zip-compressed";
00515 if (file.endsWith(".DIR") || file.endsWith(".DCR") || file.endsWith(".DXR"))
00516 return "application/x-director";
00517 if (file.endsWith(".DVI"))
00518 return "application/x-dvi";
00519 if (file.endsWith(".TEX"))
00520 return "application/x-tex";
00521 if (file.endsWith(".LATEX"))
00522 return "application/x-latex";
00523 if (file.endsWith(".TCL"))
00524 return "application/x-tcl";
00525 if (file.endsWith(".CER") || file.endsWith(".CRT") || file.endsWith(".DER"))
00526 return "application/x-x509-ca-cert";
00527
00528 if (file.endsWith(".MPG") || file.endsWith(".MPE") || file.endsWith(".MPEG"))
00529 return "video/mpeg";
00530 if (file.endsWith(".QT") || file.endsWith(".MOV"))
00531 return "video/quicktime";
00532 if (file.endsWith(".AVI"))
00533 return "video/x-msvideo";
00534 if (file.endsWith(".MOVIE"))
00535 return "video/x-sgi-movie";
00536
00537 if (file.endsWith(".PDB") || file.endsWith(".XYZ"))
00538 return "chemical/x-pdb";
00539
00540 if (file.endsWith(".ICE"))
00541 return "x-conference/x-cooltalk";
00542 if (file.endsWith(".WRL") || file.endsWith(".VRML"))
00543 return "x-world/x-vrml";
00544 if (file.endsWith(".WML"))
00545 return "text/vnd.wap.wml";
00546 if (file.endsWith(".WMLC"))
00547 return "application/vnd.wap.wmlc";
00548 if (file.endsWith(".WMLS"))
00549 return "text/vnd.wap.wmlscript";
00550 if (file.endsWith(".WMLSC"))
00551 return "application/vnd.wap.wmlscriptc";
00552 if (file.endsWith(".WBMP"))
00553 return "image/vnd.wap.wbmp";
00554
00555 return null;
00556 }
00557
00558 public int getMinorVersion() {
00559 return 3;
00560 }
00561
00562 public RequestDispatcher getNamedDispatcher(String name) {
00563 return this;
00564 }
00565
00566 public String getRealPath(String path) {
00567 return installPath + FileUtilities.convertFileNameToSystem(path);
00568
00569 }
00570
00571 public RequestDispatcher getRequestDispatcher(String path) {
00572 return this;
00573 }
00574
00575
00576 public URL getResource(String path) throws MalformedURLException {
00577 return new URL("http", hostName, port, path);
00578 }
00579
00580 public InputStream getResourceAsStream(String path) {
00581 return null;
00582 }
00583
00584 public java.util.Set getResourcePaths(java.lang.String path) {
00585
00586 return null;
00587 }
00588
00590
00591
00592 public String getServerInfo() {
00593 return Identification.serverName + " " + Identification.serverVersion + " (" + Identification.serverUrl + ")";
00594 }
00595
00596 public Servlet getServlet(String name) {
00597
00598
00599
00600
00601
00602
00603
00604 return null;
00605 }
00606
00613 public java.lang.String getServletContextName() {
00614 return null;
00615 }
00616
00618
00619
00620 public Enumeration getServletNames() {
00621
00622
00623
00624
00625 return new Vector().elements();
00626 }
00627
00628 public Enumeration getServlets() {
00629
00630
00631
00632
00633 return new Vector().elements();
00634 }
00635
00636 public void log(Exception exception, String msg) {
00637 LogManager.traceWarning(LogServices.WEBSERVICE, msg);
00638 LogManager.traceWarning(LogServices.WEBSERVICE, exception);
00639 }
00640
00641 public void log(String msg) {
00642 LogManager.traceWarning(LogServices.WEBSERVICE, msg);
00643 }
00644
00645 public void log(String message, Throwable throwable) {
00646 LogManager.traceWarning(LogServices.WEBSERVICE, message);
00647 LogManager.traceWarning(LogServices.WEBSERVICE, throwable);
00648 }
00649
00650 public void removeAttribute(String name) {
00651 attributes.remove(name);
00652 }
00653
00654 public void setAttribute(String name, Object object) {
00655 attributes.put(name, object);
00656 }
00657
00658
00659
00660
00661
00662
00663 public void forward(ServletRequest request, ServletResponse response) {
00664 }
00665 public void include(ServletRequest request, ServletResponse response) {
00666 }
00667 }