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 if ((PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser")!=null) &&
00239 (PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser.args")!=null)) {
00240 System.loadLibrary("ExecDLL2002");
00241 LogManager.traceInfo(0,"Start Pocket IE");
00242 SystemAPI.execProgram(PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser")
00243 , PropertiesManager.getManager().getProperty("org.openmobileis.embedded.execprog.browser.args")+"/"+Long.toString(System.currentTimeMillis()));
00244 } else {
00245 LogManager.traceInfo(0,"Pocket IE not started on PocketPC no properties defined.");
00246 }
00247 }
00248 } catch (Throwable e) {
00249 LogManager.traceError(0, e);
00250 LogManager.traceError(LogServices.WEBSERVICE, "Can not launch ie with parameters : " + PropertiesManager.getManager().getProperty("execprog.browser")
00251 + " args : " + PropertiesManager.getManager().getProperty("execprog.browser.args"));
00252 }
00253
00254 try {
00255 while (true) {
00256 Socket socket = serverSocket.accept();
00257 connexionPool.runServerConnexion(this, socket);
00258 }
00259 } catch (IOException e) {
00260 LogManager.traceError(0, e);
00261 LogManager.traceError(LogServices.WEBSERVICE,
00262 "IOEXception during servlet process Accept: " + e);
00263 } finally {
00264 try {
00265 serverSocket.close();
00266 } catch (Exception e) {
00267 LogManager.traceError(LogServices.WEBSERVICE, e);
00268 }
00269 }
00270 }
00271
00273 public void destroyAllServlets() {
00274 Enumeration en = registry.elements();
00275 while (en.hasMoreElements()) {
00276 Servlet servlet = (Servlet) en.nextElement();
00277 servlet.destroy();
00278 }
00279 registry = new PathTreeDictionary();
00280
00281 }
00282
00283
00284
00285
00286 public void freeResources() throws Exception {
00287 this.destroyAllServlets();
00288 connexionPool.freeResources();
00289 serverSocket.close();
00290 LogManager.traceInfo(LogServices.WEBSERVICE, "Web server is stopped");
00291
00292 }
00293
00294 private void readServlets(String cfgfile) {
00299 Hashtable servletstbl, parameterstbl;
00300 servletstbl = new Hashtable();
00301 parameterstbl = new Hashtable();
00302 if (cfgfile != null) {
00303 File file = new File(cfgfile);
00304 if (file.exists() && file.canRead()) {
00305 try {
00306 DataInputStream in = new DataInputStream(new FileInputStream(file));
00310 do {
00311 String servletdsc = in.readLine();
00312 if (servletdsc == null)
00313 break;
00314 StringTokenizer dsctokenzr = new StringTokenizer(servletdsc, ".=,", false);
00315 if (dsctokenzr.hasMoreTokens()) {
00316 if (!dsctokenzr.nextToken().equalsIgnoreCase("servlet")) {
00317 System.err.println("No leading 'servlet' keyword, the sentence is skipped");
00318 break;
00319 }
00320 if (dsctokenzr.hasMoreTokens()) {
00321 String servletname = dsctokenzr.nextToken();
00322
00323 if (dsctokenzr.hasMoreTokens()) {
00324 String lt = dsctokenzr.nextToken();
00325 if (lt.equalsIgnoreCase("code")) {
00326 if (dsctokenzr.hasMoreTokens())
00327 servletstbl.put(servletname, dsctokenzr.nextToken("="));
00328 } else if (lt.equalsIgnoreCase("initArgs")) {
00329 Hashtable initparams = new Hashtable();
00330 while (dsctokenzr.hasMoreTokens()) {
00331 String key = dsctokenzr.nextToken("=,");
00332 if (dsctokenzr.hasMoreTokens())
00333 initparams.put(key, dsctokenzr.nextToken(",="));
00334 }
00335 parameterstbl.put(servletname, initparams);
00336 } else
00337 System.err.println("Unrecognized token " + lt + " in " + servletdsc + ", the line's skipped");
00338 }
00339 }
00340 }
00341 }
00342 while (true);
00343 } catch (IOException e) {
00344 System.err.println("Problem reading cfg file: " + e);
00345 }
00346 Enumeration se = servletstbl.keys();
00347 String servletname;
00348 while (se.hasMoreElements()) {
00349 servletname = (String) se.nextElement();
00350 addServlet(servletname, (String) servletstbl.get(servletname), (Hashtable) parameterstbl.get(servletname));
00351 }
00352 }
00353 }
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00387
00388
00389
00390 public Object getAttribute(String name) {
00391
00392 return attributes.get(name);
00393 }
00394
00395 public Enumeration getAttributeNames() {
00396 return attributes.keys();
00397 }
00398
00399 public ServletContext getContext(String uripath) {
00400 return this;
00401 }
00402
00403
00404 public String getInitParameter(String param) {
00405 return null;
00406 }
00407
00408 public Enumeration getInitParameterNames() {
00409 return null;
00410 }
00411
00412 public int getMajorVersion() {
00413 return 2;
00414 }
00415
00417
00418 public String getMimeType(String file) {
00419 file = file.toUpperCase();
00420
00421 if (file.endsWith(".HTML") || file.endsWith(".HTM"))
00422 return "text/html";
00423 if (file.endsWith(".TXT"))
00424 return "text/plain";
00425 if (file.endsWith(".XML"))
00426 return "text/xml";
00427 if (file.endsWith(".CSS"))
00428 return "text/css";
00429 if (file.endsWith(".SGML") || file.endsWith(".SGM"))
00430 return "text/x-sgml";
00431
00432 if (file.endsWith(".GIF"))
00433 return "image/gif";
00434 if (file.endsWith(".JPG") || file.endsWith(".JPEG") || file.endsWith(".JPE"))
00435 return "image/jpeg";
00436 if (file.endsWith(".PNG"))
00437 return "image/png";
00438 if (file.endsWith(".TIF") || file.endsWith(".TIFF"))
00439 return "image/tiff";
00440 if (file.endsWith(".RGB"))
00441 return "image/x-rgb";
00442 if (file.endsWith(".XPM"))
00443 return "image/x-xpixmap";
00444 if (file.endsWith(".XBM"))
00445 return "image/x-xbitmap";
00446 if (file.endsWith(".SVG"))
00447 return "image/svg-xml ";
00448 if (file.endsWith(".SVGZ"))
00449 return "image/svg-xml ";
00450
00451 if (file.endsWith(".AU") || file.endsWith(".SND"))
00452 return "audio/basic";
00453 if (file.endsWith(".MID") || file.endsWith(".MIDI") || file.endsWith(".RMI") || file.endsWith(".KAR"))
00454 return "audio/mid";
00455 if (file.endsWith(".MPGA") || file.endsWith(".MP2") || file.endsWith(".MP3"))
00456 return "audio/mpeg";
00457 if (file.endsWith(".WAV"))
00458 return "audio/wav";
00459 if (file.endsWith(".AIFF") || file.endsWith(".AIFC"))
00460 return "audio/aiff";
00461 if (file.endsWith(".AIF"))
00462 return "audio/x-aiff";
00463 if (file.endsWith(".RA"))
00464 return "audio/x-realaudio";
00465 if (file.endsWith(".RPM"))
00466 return "audio/x-pn-realaudio-plugin";
00467 if (file.endsWith(".RAM"))
00468 return "audio/x-pn-realaudio";
00469 if (file.endsWith(".SD2"))
00470 return "audio/x-sd2";
00471
00472 if (file.endsWith(".BIN") || file.endsWith(".DMS") || file.endsWith(".LHA") || file.endsWith(".LZH") || file.endsWith(".EXE") || file.endsWith(".CLASS"))
00473 return "application/octet-stream";
00474 if (file.endsWith(".HQX"))
00475 return "application/mac-binhex40";
00476 if (file.endsWith(".PS") || file.endsWith(".AI") || file.endsWith(".EPS"))
00477 return "application/postscript";
00478 if (file.endsWith(".PDF"))
00479 return "application/pdf";
00480 if (file.endsWith(".RTF"))
00481 return "application/rtf";
00482 if (file.endsWith(".DOC"))
00483 return "application/msword";
00484 if (file.endsWith(".PPT"))
00485 return "application/powerpoint";
00486 if (file.endsWith(".FIF"))
00487 return "application/fractals";
00488 if (file.endsWith(".P7C"))
00489 return "application/pkcs7-mime";
00490
00491 if (file.endsWith(".JS"))
00492 return "application/x-javascript";
00493 if (file.endsWith(".Z"))
00494 return "application/x-compress";
00495 if (file.endsWith(".GZ"))
00496 return "application/x-gzip";
00497 if (file.endsWith(".TAR"))
00498 return "application/x-tar";
00499 if (file.endsWith(".TGZ"))
00500 return "application/x-compressed";
00501 if (file.endsWith(".ZIP"))
00502 return "application/x-zip-compressed";
00503 if (file.endsWith(".DIR") || file.endsWith(".DCR") || file.endsWith(".DXR"))
00504 return "application/x-director";
00505 if (file.endsWith(".DVI"))
00506 return "application/x-dvi";
00507 if (file.endsWith(".TEX"))
00508 return "application/x-tex";
00509 if (file.endsWith(".LATEX"))
00510 return "application/x-latex";
00511 if (file.endsWith(".TCL"))
00512 return "application/x-tcl";
00513 if (file.endsWith(".CER") || file.endsWith(".CRT") || file.endsWith(".DER"))
00514 return "application/x-x509-ca-cert";
00515
00516 if (file.endsWith(".MPG") || file.endsWith(".MPE") || file.endsWith(".MPEG"))
00517 return "video/mpeg";
00518 if (file.endsWith(".QT") || file.endsWith(".MOV"))
00519 return "video/quicktime";
00520 if (file.endsWith(".AVI"))
00521 return "video/x-msvideo";
00522 if (file.endsWith(".MOVIE"))
00523 return "video/x-sgi-movie";
00524
00525 if (file.endsWith(".PDB") || file.endsWith(".XYZ"))
00526 return "chemical/x-pdb";
00527
00528 if (file.endsWith(".ICE"))
00529 return "x-conference/x-cooltalk";
00530 if (file.endsWith(".WRL") || file.endsWith(".VRML"))
00531 return "x-world/x-vrml";
00532 if (file.endsWith(".WML"))
00533 return "text/vnd.wap.wml";
00534 if (file.endsWith(".WMLC"))
00535 return "application/vnd.wap.wmlc";
00536 if (file.endsWith(".WMLS"))
00537 return "text/vnd.wap.wmlscript";
00538 if (file.endsWith(".WMLSC"))
00539 return "application/vnd.wap.wmlscriptc";
00540 if (file.endsWith(".WBMP"))
00541 return "image/vnd.wap.wbmp";
00542
00543 return null;
00544 }
00545
00546 public int getMinorVersion() {
00547 return 3;
00548 }
00549
00550 public RequestDispatcher getNamedDispatcher(String name) {
00551 return this;
00552 }
00553
00554 public String getRealPath(String path) {
00555 return installPath + FileUtilities.convertFileNameToSystem(path);
00556
00557 }
00558
00559 public RequestDispatcher getRequestDispatcher(String path) {
00560 return this;
00561 }
00562
00563
00564 public URL getResource(String path) throws MalformedURLException {
00565 return new URL("http", hostName, port, path);
00566 }
00567
00568 public InputStream getResourceAsStream(String path) {
00569 return null;
00570 }
00571
00572 public java.util.Set getResourcePaths(java.lang.String path) {
00573
00574 return null;
00575 }
00576
00578
00579
00580 public String getServerInfo() {
00581 return Identification.serverName + " " + Identification.serverVersion + " (" + Identification.serverUrl + ")";
00582 }
00583
00584 public Servlet getServlet(String name) {
00585
00586
00587
00588
00589
00590
00591
00592 return null;
00593 }
00594
00601 public java.lang.String getServletContextName() {
00602 return null;
00603 }
00604
00606
00607
00608 public Enumeration getServletNames() {
00609
00610
00611
00612
00613 return new Vector().elements();
00614 }
00615
00616 public Enumeration getServlets() {
00617
00618
00619
00620
00621 return new Vector().elements();
00622 }
00623
00624 public void log(Exception exception, String msg) {
00625 LogManager.traceWarning(LogServices.WEBSERVICE, msg);
00626 LogManager.traceWarning(LogServices.WEBSERVICE, exception);
00627 }
00628
00629 public void log(String msg) {
00630 LogManager.traceWarning(LogServices.WEBSERVICE, msg);
00631 }
00632
00633 public void log(String message, Throwable throwable) {
00634 LogManager.traceWarning(LogServices.WEBSERVICE, message);
00635 LogManager.traceWarning(LogServices.WEBSERVICE, throwable);
00636 }
00637
00638 public void removeAttribute(String name) {
00639 attributes.remove(name);
00640 }
00641
00642 public void setAttribute(String name, Object object) {
00643 attributes.put(name, object);
00644 }
00645
00646
00647
00648
00649
00650
00651 public void forward(ServletRequest request, ServletResponse response) {
00652 }
00653 public void include(ServletRequest request, ServletResponse response) {
00654 }
00655 }