WEB开发网
开发学院软件开发Java 通过 HTTP 加载 Java 资源包 阅读

通过 HTTP 加载 Java 资源包

 2009-12-24 00:00:00 来源:WEB开发网   
核心提示: ResourceBundle 使用 getFormats() 方法来决定资源包应该使用什么格式,在示例应用程序中,通过 HTTP 加载 Java 资源包(5),您只需要加载类型属性包;例如,autoparts.properties,用于基本的资源包名称、位置和格式,(要了解资源包加载流程的完整描

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; 
}

上一页  1 2 3 4 5 6 7 8  下一页

Tags:通过 HTTP 加载

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接