Java Web Start为什么能加载pack.gz
我们每次给国外更新的时候,都只是更新了 .jar.pack.gz, 不用更新jar。只知道这样可以,不知道为什么可以,每次出问题我们也会考虑,会不会加载成老的jar包了。
今天到官网上找了找,发现了原因:
以JBOSS为例,在webstart.war\WEB-INF\lib下面有jnlp-servlet.jar,我们点击界面上jnlp的链接后,会与jnlp-servlet.jar中的JnlpDownloadServlet交互,在根据jnlp上的resource查找jar时,也会找以 .pack.gz或 .gz 结尾的同名文件,见代码:
// pack200 compression
if (encoding != null && _mimeType != null &&
(_mimeType.compareTo(JAR_MIME_TYPE) == 0 || _mimeType.compareTo(JAR_MIME_TYPE_NEW) == 0) &&
encoding.toLowerCase().indexOf(DownloadResponse.PACK200_GZIP_ENCODING) > -1){
search_path = orig_path + pack.gz";
_resource = context.getResource(search_path);
// gzip compression
if (found == false && encoding != null &&
encoding.toLowerCase().indexOf(DownloadResponse.GZIP_ENCODING) > -1){
search_path = orig_path +".gz";
_resource = context.getResource(search_path);
代码:public JnlpResource(ServletContext context,
String name,
String versionId,
String[] osList,
String[] archList,
String[] localeList,
String path,
String returnVersionId,
String encoding) {
// Matching arguments
_encoding = encoding;
_name = name;
_versionId = versionId;
_osList = osList;
_archList = archList;
_localeList = localeList;
_returnVersionId = returnVersionId;
/* Check for existance and get last modified timestamp */
try {
String orig_path = path.trim();
String search_path = orig_path;
_resource = context.getResource(orig_path);
_mimeType = getMimeType(context, orig_path);
if (_resource != null) {
boolean found = false;
// pack200 compression
if (encoding != null && _mimeType != null &&
(_mimeType.compareTo(JAR_MIME_TYPE) == 0 || _mimeType.compareTo(JAR_MIME_TYPE_NEW) == 0) &&
encoding.toLowerCase().indexOf(DownloadResponse.PACK200_GZIP_ENCODING) > -1){
search_path = orig_path + pack.gz";
_resource = context.getResource(search_path);
// Get last modified time
if (_resource != null) {
_lastModified = getLastModified(context, _resource, search_path);
if (_lastModified != 0) {
_path = search_path;
found = true;
} else {
_resource = null;
}
}
}
// gzip compression
if (found == false && encoding != null &&
encoding.toLowerCase().indexOf(DownloadResponse.GZIP_ENCODING) > -1){
search_path = orig_path +".gz";
_resource = context.getResource(search_path);
// Get last modified time
if (_resource != null) {
_lastModified = getLastModified(context, _resource, search_path);
if (_lastModified != 0) {
_path = search_path;
found = true;
} else {
_resource = null;
}
}
}
if (found == false) {
// no compression
search_path = orig_path;
_resource = context.getResource(search_path);
// Get last modified time
if (_resource != null) {
_lastModified = getLastModified(context, _resource, search_path);
if (_lastModified != 0) {
_path = search_path;
found = true;
} else {
_resource = null;
}
}
}
}
} catch(IOException ioe) {
_resource = null;
}
}
因为它还会判断.jar是否存在,所以不能说只有 .pack.gz没有 .jar也行
代码:JnlpResource jnlpres = new JnlpResource(getServletContext(), dreq.getPath());
if (!jnlpres.exists()) {
throw new ErrorResponseException(DownloadResponse.getNoContentResponse());
}
代码:public boolean exists() { return _resource != null; }
代码:_resource = context.getResource(orig_path);
orig_path就是以.jar结尾的文件路径。
而且jws是通过.jar的name来找 .pack.gz或 .gz的
最后说说为什么会用.pack.gz?
其实原因很简单,就是包会越来越大,不压缩的话,下载的速度会很慢,对用户和网费都是很大的考验。所以在java5.0后提供了pack 200, 能将包压缩到之前的10~15%。这样下载就会相对好多了
jnlp-servlet.jar的源码在JDK的。。。/sample/jnlp/servlet目录下面
更多详情,请查看
http://today.java.net/pub/a/today/2005/ ... start.html