通过 HTTP 加载 Java 资源包
2009-12-24 00:00:00 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鎯у⒔閹虫捇鈥旈崘顏佸亾閿濆簼绨绘い鎺嬪灪閵囧嫰骞囬姣挎捇鏌熸笟鍨妞ゎ偅绮撳畷鍗炍旈埀顒勭嵁婵犲嫮纾介柛灞捐壘閳ь剛鎳撻~婵嬪Ω閳轰胶鐤呯紓浣割儐椤戞瑩宕ョ€n喗鐓曟い鎰靛亝缁舵氨绱撻崘鈺傜婵﹤顭峰畷鎺戔枎閹搭厽袦婵犵數濮崑鎾绘⒑椤掆偓缁夌敻骞嗛悙鍝勭婵烇綆鍓欐俊鑲╃磼閹邦収娈滈柡灞糕偓鎰佸悑閹肩补鈧尙鏁栧┑鐐村灦閹稿摜绮旈悽绋课﹂柛鏇ㄥ灠閸愨偓濡炪倖鍔﹀鈧繛宀婁邯濮婅櫣绱掑Ο璇茶敿闂佺ǹ娴烽弫璇差嚕婵犳碍鏅插璺猴工瀹撳棝姊虹紒妯哄缂佷焦鎸冲畷鎴﹀箻鐠囧弶宓嶅銈嗘尰缁嬫垶绂嶉悙顒佸弿婵☆垳鍘ф禍楣冩倵濮樼偓瀚�

ResourceBundle 使用 getFormats() 方法来决定资源包应该使用什么格式。在示例应用程序中,您只需要加载类型属性包;例如,autoparts.properties。
清单 1. 为资源包指定格式private static String propertiesType = "properties";
// Only "properties" files are used (e.g., autoparts.properties)
public List<String> getFormats(String baseName) {
return Collections.singletonList(propertiesType);
RemoteResourceBundleLoader 中的 newBundle() 方法(清单 2)被工厂方法 ResourceBundle.getBundle() 调用,以实例化 ResourceBundle,用于基本的资源包名称、位置和格式。(要了解资源包加载流程的完整描述,请参见 ResourceBundle 的 Javadoc)。
清单 2. RemoteResourceBundleLoader 中的 newBundle() 方法public ResourceBundle newBundle(String baseName,
Locale locale,
String format,
ClassLoader loader,
boolean reload)
throws IllegalAccessException, InstantiationException,
IOException {
ResourceBundle bundle = null;
if ((baseName == null) ||
(locale == null) ||
(format == null) ||
(loader == null)) {
return null;
}
// format must be '.properties'
if (!format.equals(propertiesType)) {
return null;
}
// Create bundle name from baseName and locale (e.g., "autoparts"
// (no locale), autoparts_fr (French)
String bundleName = toBundleName(baseName, locale);
// Create resource name (e.g., "autoparts.properties",
// "autoparts_fr.properties"
String resourceName = toResourceName(bundleName, format);
// get product context roots, if not already obtained
Properties productContextRoots=ProductContextRoots.getProductContextRoots();
if (productContextRoots==null) {
ProductContextRoots pe = new ProductContextRoots();
pe.loadProductContextRoots();
productContextRoots = ProductContextRoots.getProductContextRoots();
if (productContextRoots==null) {
return null;
}
}
// The last segment of the baseName indicates product
// (i.e., 'autosales', 'autoparts').
// Use this string to find the product context root in productContextRoots.
int dotIndex = baseName.lastIndexOf('.');
if (dotIndex==-1) {
return null;
}
String productName= baseName.substring(dotIndex+1);
// Find product context root using productName
String productContextRoot = productContextRoots.getProperty(productName);
if (productContextRoot==null) {
return null;
}
// Create the full name for this resource
String fullResourceName;
if (!productContextRoot.startsWith("http")) {
if (this.refererHeader != null && this.refererHeader.length()>0) {
// Create full resource name using Referer,
// context root and resource name:
fullResourceName=this.refererHeader+
productContextRoot+resourceName;
} else {
// Create full resource name using scheme,
// host, port, context root and resource name:
fullResourceName=this.scheme+"://"+
this.host+":"+this.port+productContextRoot+resourceName;
}
} else {
// Create full resource name using context root and resource name:
fullResourceName=productContextRoot+resourceName;
}
// Create HttpURLConnection for the resource file
URL proxy=new URL(fullResourceName);
HttpURLConnection httpProxy = (HttpURLConnection)proxy.openConnection();
if (httpProxy == null) {
return null;
}
if (reload) {
httpProxy.setUseCaches(false);
}
// Instantiate the input stream
InputStream stream = httpProxy.getInputStream();
if (stream == null) {
return null;
}
BufferedInputStream bis = null;
// Instantiate the bundle with the stream.
try {
bis = new BufferedInputStream(stream);
bundle = new MessagesBundle(bis);
bis.close();
}finally{
if (bis != null) try { bis.close (); } catch (Throwable ignore) {}
}
return bundle;
}
更多精彩
赞助商链接