agapple

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            13 Posts :: 1 Stories :: 1 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(1)

          隨筆分類(12)

          隨筆檔案(14)

          文章分類

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          官方描述:http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html

          The META-INF directory

          The following files/directories in the META-INF directory are recognized and interpreted by the Java 2 Platform to configure applications, extensions, class loaders and services:
          • MANIFEST.MF
          The manifest file that is used to define extension and package related data.
          • INDEX.LIST
          This file is generated by the new "-i" option of the jar tool, which contains location information for packages defined in an application or extension.  It is part of the JarIndex implementation and used by class loaders to speed up their class loading process.
          • x.SF
          The signature file for the JAR file.  'x' stands for the base file name.
          • x.DSA
          The signature block file associated with the signature file with the same base file name. This file stores the digital signature of the corresponding signature file.
          • services/
          This directory stores all the service provider configuration files.

          這里指出了jar包的典型的目錄結構。簡單翻譯:

          META-INF目錄中的下列文件和目錄獲得Java 2平臺的認可與解釋,用來配置應用程序、擴展程序、類加載器和服務:
          • MANIFEST.MF:清單文件,用來定義與擴展和數據包相關的數據。
          • INDEX.LIST:這個文件由JAR工具的新“-i”選項生成,其中包含在一個應用程序或擴展中定義的數據包的地址信息。它是JarIndex的一部分,被類加載器用來加速類加載過程。
          • x.SF:JAR文件的簽名文件。x代表基礎文件名。
          • x.DSA:這個簽名塊文件與同名基礎簽名文件有關。此文件存儲對應簽名文件的數字簽名。
          • services/:這個目錄存儲所有服務提供程序配置文件。

          Service Provider

          Overview

          Files in the META-INF/services directory are service provider configuration files. A service is a well-known set of interfaces and (usually abstract) classes. A service provider is a specific implementation of a service. The classes in a provider typically implement the interfaces and subclass the classes defined in the service itself. Service providers may be installed in an implementation of the Java platform in the form of extensions, that is, jar files placed into any of the usual extension directories. Providers may also be made available by adding them to the applet or application class path or by some other platform-specific means.

          A service is represented by an abstract class. A provider of a given service contains one or more concrete classes that extend this service class with data and code specific to the provider. This provider class will typically not be the entire provider itself but rather a proxy that contains enough information to decide whether the provider is able to satisfy a particular request together with code that can create the actual provider on demand. The details of provider classes tend to be highly service-specific; no single class or interface could possibly unify them, so no such class has been defined. The only requirement enforced here is that provider classes must have a zero-argument constructor so that they may be instantiated during lookup.
           

          Provider-Configuration File

          A service provider identifies itself by placing a provider-configuration file in the resource directory META-INF/services. The file's name should consist of the fully-qualified name of the abstract service class. The file should contain a newline-separated list of unique concrete provider-class names. Space and tab characters, as well as blank lines, are ignored. The comment character is '#' (0x23); on each line all characters following the first comment character are ignored. The file must be encoded in UTF-8.
           

          Example

          Suppose we have a service class named java.io.spi.CharCodec. It has two abstract methods:

              public abstract CharEncoder getEncoder(String encodingName);
            public abstract CharDecoder getDecoder(String encodingName);

          Each method returns an appropriate object or null if it cannot translate the given encoding. Typical CharCodec providers will support more than one encoding.

          If sun.io.StandardCodec is a provider of the CharCodec service then its jar file would contain the file META-INF/services/java.io.spi.CharCodec. This file would contain the single line:

             sun.io.StandardCodec    # Standard codecs for the platform

          To locate an encoder for a given encoding name, the internal I/O code would do something like this:

             CharEncoder getEncoder(String encodingName) {
                 Iterator ps = Service.providers(CharCodec.class);
                 while (ps.hasNext()) {
                     CharCodec cc = (CharCodec)ps.next();
                     CharEncoder ce = cc.getEncoder(encodingName);
                     if (ce != null)
                         return ce;
                 }
                 return null;
             }
           

          The provider-lookup mechanism always executes in the security context of the caller. Trusted system code should typically invoke the methods in this class from within a privileged security context.


          介紹:

          在META-INF/services目錄下保存的是service provider的配置文件。 服務在應用中會是一個接口(更多的是抽象類)。
          一個類服務器提供者實現了一個服務類。這類的服務提供類可以以擴展的形式發布到平臺上。所以,jar文件引入了擴展目錄,同樣你也可以將服務提供者加入classpath提供訪問。

          服務都是表現為一個積累,而一個服務提供者通常是集成或實現了服務定義類。服務提供類通常不會像代理類一樣為了正常提供服務而包含了請求者的許多信息。服務提供類一般傾向于高集成。
          對這類服務提供類的唯一強制性要求就是必須有一個無參的構造函數。

          provider 配置文件
          META-INF/services目錄作為provider配置文件的存放路徑。provider配置文件中必須是全類名(包含package)。配置文件可以存在space tab 換行等字符,#作為注釋。
          注意:provider配置文件必須是以UTF-8編碼。

           


          總結:
                service provider機制為程序的動態擴展提供了契機,在應用中你可以針對接口編程,通過RTTI技術可以比較完美的解決程序之間的耦合性。相比于spring DIP機制,這也是一個不錯的嘗試,至少它不需要耦合spring包。

          Blog : http://agapple.javaeye.com/  歡迎訪問
          posted on 2008-10-31 11:32 agapple 閱讀(774) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 巴东县| 彩票| 平潭县| 河西区| 维西| 读书| 荣昌县| 巴林左旗| 上杭县| 留坝县| 麻阳| 和龙市| 水富县| 甘南县| 尤溪县| 随州市| 大新县| 翁源县| 荣昌县| 福建省| 九台市| 邮箱| 远安县| 通山县| 抚顺市| 米脂县| 安丘市| 邢台县| 阳西县| 汾阳市| 丰顺县| 秀山| 灌南县| 贺州市| 凉山| 蛟河市| 桦甸市| 曲阳县| 石柱| 温泉县| 玉龙|