⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serve.java

📁 用java开发的
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
						if (as != null && as.isValid())
							try {
								as.invalidate();
							} catch (IllegalStateException ise) {

							}
					}
				}
			}
		}
		// destroy servlets
		final Enumeration en = registry.elements();
		Runnable servletDestroyer = new Runnable() {
			public void run() {
				((Servlet) en.nextElement()).destroy();
			}
		};
		while (en.hasMoreElements()) {
			Thread destroyThread = new Thread(servletDestroyer, "Destroy");
			destroyThread.start();
			try {
				destroyThread.join(10 * 1000);
			} catch (InterruptedException e) {
			}
			if (destroyThread.isAlive())
				destroyThread.stop();
		}
		// clean access tree
		registry = new PathTreeDictionary();
	}

	protected void setMappingTable(PathTreeDictionary mappingtable) {
		this.mappingtable = mappingtable;
	}

	protected void setRealms(PathTreeDictionary realms) {
		this.realms = realms;
	}

	AcmeSession getSession(String id) {
		return (AcmeSession) sessions.get(id);
	}

	HttpSession createSession() {
		Integer ms = (Integer) this.arguments.get(ARG_MAX_ACTIVE_SESSIONS);
		if (ms != null && ms.intValue() < sessions.size())
			return null;
		HttpSession result = new AcmeSession(generateSessionId(), Math.abs(expiredIn) * 60, this, sessions);
		sessions.put(result.getId(), result);
		return result;
	}

	void removeSession(String id) {
		sessions.remove(id);
	}

	// / Write information to the servlet log.
	// @param message the message to log
	public void log(String message) {
		Date date = new Date(System.currentTimeMillis());
		logStream.println("[" + date.toString() + "] " + message);
	}

	public void log(String message, Throwable throwable) {
		if (throwable != null) {
			StringWriter sw;
			PrintWriter pw = new PrintWriter(sw = new StringWriter());
			throwable.printStackTrace(pw);
			// printCauses(throwable, pw);
			message = message + '\n' + sw;
		}
		log(message);
	}

	// protected void printCauses(Throwable throwable, PrintWriter printWriter) {
	// try {
	// throwable = throwable instanceof ServletException ? ((ServletException) throwable).getRootCause()
	// : (Throwable) throwable.getClass().getMethod("getCause", new Class[] {}).invoke(throwable,
	// new Object[] {});
	// if (throwable != null) {
	// printWriter.write("Caused by:\n");
	// throwable.printStackTrace(printWriter);
	// printCauses(throwable, printWriter);
	// }
	// } catch (Exception e) {
	// }
	// }

	// / Write a stack trace to the servlet log.
	// @param exception where to get the stack trace
	// @param message the message to log
	public void log(Exception exception, String message) {
		log(message, exception);
	}

	// / Applies alias rules to the specified virtual path and returns the
	// corresponding real path. It returns null if the translation
	// cannot be performed.
	// @param path the path to be translated
	public String getRealPath(String path) {
		// try {
		// path = new String(path.getBytes("ISO-8859-1"), UTF8);
		// } catch (Exception ee) { // no encoding
		// }
		// System.err.print("[" + path + "]->[");
		if (mappingtable != null) {
			// try find first sub-path
			Object[] os = mappingtable.get(path);
			// System.err.println("Searching for path: "+path+" found: "+os[0]);
			if (os[0] == null)
				return null;
			int slpos = ((Integer) os[1]).intValue();
			int pl = path.length();
			if (slpos > 0) {
				if (path.length() > slpos)
					path = path.substring(slpos + 1);
				else
					path = "";
			} else if (pl > 0) {
				for (int i = 0; i < pl; i++) {
					char s = path.charAt(i);
					if (s == '/' || s == '\\')
						continue;
					else {
						if (i > 0)
							path = path.substring(i);
						break;
					}
				}
			}
			// System.err.println("Path after processing :"+path+" slash was at
			// "+slpos);
			return new File((File) os[0], path).getPath();
		}
		return path;
	}

	// / Returns the MIME type of the specified file.
	// @param file file name whose MIME type is required
	public String getMimeType(String file) {
		file = file.toUpperCase();
		// faster coulde extract extension and then linear search in a string of extension
		// and use found index as a key to type
		if (file.endsWith(".HTML") || file.endsWith(".HTM"))
			return "text/html";
		if (file.endsWith(".TXT"))
			return "text/plain";
		if (file.endsWith(".XML"))
			return "text/xml";
		if (file.endsWith(".CSS"))
			return "text/css";
		if (file.endsWith(".SGML") || file.endsWith(".SGM"))
			return "text/x-sgml";
		// Image
		if (file.endsWith(".GIF"))
			return "image/gif";
		if (file.endsWith(".JPG") || file.endsWith(".JPEG") || file.endsWith(".JPE"))
			return "image/jpeg";
		if (file.endsWith(".PNG"))
			return "image/png";
		if (file.endsWith(".BMP"))
			return "image/bmp";
		if (file.endsWith(".TIF") || file.endsWith(".TIFF"))
			return "image/tiff";
		if (file.endsWith(".RGB"))
			return "image/x-rgb";
		if (file.endsWith(".XPM"))
			return "image/x-xpixmap";
		if (file.endsWith(".XBM"))
			return "image/x-xbitmap";
		if (file.endsWith(".SVG"))
			return "image/svg-xml ";
		if (file.endsWith(".SVGZ"))
			return "image/svg-xml ";
		// Audio
		if (file.endsWith(".AU") || file.endsWith(".SND"))
			return "audio/basic";
		if (file.endsWith(".MID") || file.endsWith(".MIDI") || file.endsWith(".RMI") || file.endsWith(".KAR"))
			return "audio/mid";
		if (file.endsWith(".MPGA") || file.endsWith(".MP2") || file.endsWith(".MP3"))
			return "audio/mpeg";
		if (file.endsWith(".WAV"))
			return "audio/wav";
		if (file.endsWith(".AIFF") || file.endsWith(".AIFC"))
			return "audio/aiff";
		if (file.endsWith(".AIF"))
			return "audio/x-aiff";
		if (file.endsWith(".RA"))
			return "audio/x-realaudio";
		if (file.endsWith(".RPM"))
			return "audio/x-pn-realaudio-plugin";
		if (file.endsWith(".RAM"))
			return "audio/x-pn-realaudio";
		if (file.endsWith(".SD2"))
			return "audio/x-sd2";
		// Application
		if (file.endsWith(".BIN") || file.endsWith(".DMS") || file.endsWith(".LHA") || file.endsWith(".LZH")
				|| file.endsWith(".EXE") || file.endsWith(".DLL") || file.endsWith(".CLASS"))
			return "application/octet-stream";
		if (file.endsWith(".HQX"))
			return "application/mac-binhex40";
		if (file.endsWith(".PS") || file.endsWith(".AI") || file.endsWith(".EPS"))
			return "application/postscript";
		if (file.endsWith(".PDF"))
			return "application/pdf";
		if (file.endsWith(".RTF"))
			return "application/rtf";
		if (file.endsWith(".DOC"))
			return "application/msword";
		if (file.endsWith(".PPT"))
			return "application/powerpoint";
		if (file.endsWith(".FIF"))
			return "application/fractals";
		if (file.endsWith(".P7C"))
			return "application/pkcs7-mime";
		// Application/x
		if (file.endsWith(".JS"))
			return "application/x-javascript";
		if (file.endsWith(".Z"))
			return "application/x-compress";
		if (file.endsWith(".GZ"))
			return "application/x-gzip";
		if (file.endsWith(".TAR"))
			return "application/x-tar";
		if (file.endsWith(".TGZ"))
			return "application/x-compressed";
		if (file.endsWith(".ZIP"))
			return "application/x-zip-compressed";
		if (file.endsWith(".DIR") || file.endsWith(".DCR") || file.endsWith(".DXR"))
			return "application/x-director";
		if (file.endsWith(".DVI"))
			return "application/x-dvi";
		if (file.endsWith(".TEX"))
			return "application/x-tex";
		if (file.endsWith(".LATEX"))
			return "application/x-latex";
		if (file.endsWith(".TCL"))
			return "application/x-tcl";
		if (file.endsWith(".CER") || file.endsWith(".CRT") || file.endsWith(".DER"))
			return "application/x-x509-ca-cert";
		// Video
		if (file.endsWith(".MPG") || file.endsWith(".MPE") || file.endsWith(".MPEG"))
			return "video/mpeg";
		if (file.endsWith(".QT") || file.endsWith(".MOV"))
			return "video/quicktime";
		if (file.endsWith(".AVI"))
			return "video/x-msvideo";
		if (file.endsWith(".MOVIE"))
			return "video/x-sgi-movie";
		// Chemical
		if (file.endsWith(".PDB") || file.endsWith(".XYZ"))
			return "chemical/x-pdb";
		// X-
		if (file.endsWith(".ICE"))
			return "x-conference/x-cooltalk";
		if (file.endsWith(".JNLP"))
			return "application/x-java-jnlp-file";
		if (file.endsWith(".WRL") || file.endsWith(".VRML"))
			return "x-world/x-vrml";
		if (file.endsWith(".WML"))
			return "text/vnd.wap.wml";
		if (file.endsWith(".WMLC"))
			return "application/vnd.wap.wmlc";
		if (file.endsWith(".WMLS"))
			return "text/vnd.wap.wmlscript";
		if (file.endsWith(".WMLSC"))
			return "application/vnd.wap.wmlscriptc";
		if (file.endsWith(".WBMP"))
			return "image/vnd.wap.wbmp";

		return null;
	}

	// / Returns the name and version of the web server under which the servlet
	// is running.
	// Same as the CGI variable SERVER_SOFTWARE.
	public String getServerInfo() {
		return Serve.Identification.serverName + " " + Serve.Identification.serverVersion + " ("
				+ Serve.Identification.serverUrl + ")";
	}

	// / Returns the value of the named attribute of the network service, or
	// null if the attribute does not exist. This method allows access to
	// additional information about the service, not already provided by
	// the other methods in this interface.
	public Object getAttribute(String name) {
		return attributes.get(name);
	}

	// ///////////////// JSDK 2.1 extensions //////////////////////////
	public void removeAttribute(String name) {
		attributes.remove(name);
	}

	public void setAttribute(String name, Object object) {
		if (object != null)
			attributes.put(name, object);
		else
			attributes.remove(name);
	}

	public Enumeration getAttributeNames() {
		return attributes.keys();
	}

	public ServletContext getContext(String uripath) {
		// TODO check webapp servlets to find out conexts for uri
		return this; // only root context supported
	}

	public int getMajorVersion() {
		return 2; // support 2.x
	}

	public int getMinorVersion() {
		return 5; // support 2.5
	}

	// 2.3

	/**
	 * Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument.
	 * Paths indicating subdirectory paths end with a '/'. The returned paths are all relative to the root of the web application and have a leading '/'. For
	 * example, for a web application containing
	 * <p>
	 * /welcome.html <br>
	 * /catalog/index.html <br>
	 * /catalog/products.html <br>
	 * /catalog/offers/books.html <br>
	 * /catalog/offers/music.html <br>
	 * /customer/login.jsp <br>
	 * /WEB-INF/web.xml <br>
	 * /WEB-INF/classes/com.acme.OrderServlet.class,
	 * <p>
	 * getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"} <br>
	 * getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.
	 * <p>
	 * 
	 * @param the -
	 *            partial path used to match the resources, which must start with a /
	 * @return a Set containing the directory listing, or null if there are no resources in the web application whose path begins with the supplied path.
	 * @since Servlet 2.3
	 * 
	 */
	public java.util.Set getResourcePaths(java.lang.String path) {
		String realPath = getRealPath(path);
		if (realPath != null) {

			String[] dir = new File(realPath).list();
			if (dir.length > 0) {
				HashSet set = new HashSet(dir.length);
				for (int i = 0; i < dir.length; i++)
					set.add(dir[i]);
				return set;
			}
		}
		return null;
	}

	/**
	 * Returns the name of this web application correponding to this ServletContext as specified in the deployment descriptor for this web application by the
	 * display-name element.
	 * 
	 * @return The name of the web application or null if no name has been declared in the deployment descriptor.
	 * 
	 * @since Servlet 2.3
	 */
	public java.lang.String getServletContextName() {
		return null;
	}

	/**
	 * Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context
	 * root.
	 * 
	 * <p>
	 * This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file
	 * system, in a database, or in a <code>.war</code> file.
	 * 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -