Introduction to the Service Provider Interfaces--官方文档
地址:https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html What Are Services?Services are units of sound-handling functionality that are automatically available when an application program makes use of an implementation of the Java Sound API. They consist of objects that do the work of reading,writing,mixing,processing,and converting audio and MIDI data. An implementation of the Java Sound API generally supplies a basic set of services,but mechanisms are also included in the API to support the development of new sound services by third-party developers (or by the vendor of the implementation itself). These new services can be "plugged into" an existing installed implementation to expand its functionality without requiring a new release. In the Java Sound API architecture,third-party services are integrated into the system in such a way that an application program's interface to them is the same as the interface to the "built-in" services. In some cases,application developers who use the Examples of potential third-party,sampled-audio services include:
Third-party MIDI services might consist of:
How Services WorkThe? Developers of new sound services implement concrete subclasses of the appropriate classes in the SPI packages. These subclasses,along with any additional classes required to support the new service,are placed in a Java Archive (JAR) archive file with a description of the included service or services. When this JAR file is installed in the user's? Once the new service is installed,it can be accessed just like any previously installed service. Consumers of services can get information about the new service,or obtain instances of the new service class itself,by invoking methods of the? For example,suppose a hypothetical service provider called Acme Software,Inc. is interested in supplying a package that allows application programs to read a new format of sound file (but one whose audio data is in a standard data format). The SPI class? What's the point of having these "factory" classes? Why not permit the application developer to get access directly to newly provided services? That is a possible approach,but having all management and instantiation of services pass through gatekeeper system objects shields the application developer from having to know anything about the identity of installed services. Application developers just use services of value to them,perhaps without even realizing it. At the same time this architecture permits service providers to effectively manage the available resources in their packages. Often the use of new sound services is transparent to the application program. For example,imagine a situation where an application developer wants to read in a stream of audio from a file. Assuming that? File theInFile = new File(thePathName);
AudioInputStream theInStream = AudioSystem.getAudioInputStream(theInFile);
Behind the scenes,the? How Providers Prepare New ServicesService providers supply their new services in specially formatted JAR files,which are to be installed in a directory on the user's system where the Java runtime will find them. JAR files are archive files,each containing sets of files that might be organized in hierarchical directory structures within the archive. Details about the preparation of the class files that go into these archives are discussed in the next few pages,which describe the specifics of the audio and MIDI SPI packages; here we'll just give an overview of the process of JAR file creation. The JAR file for a new service or services should contain a class file for each service supported in the JAR file. Following the Java platform's convention,each class file has the name of the newly defined class,which is a concrete subclass of one of the abstract service provider classes. The JAR file also must include any supporting classes required by the new service implementation. So that the new service or services can be located by the runtime system's service provider mechanism,the JAR file must also contain special files (described below) that map the SPI class names to the new subclasses being defined. To continue from our example above,say Acme Software,Inc. is distributing a package of new sampled-audio services. Let's suppose this package consists of two new services:
Starting from an arbitrary directory—let's call it? com/acme/AcmeAudioFileReader.class
com/acme/AcmeAudioFileWriter.class
In addition,for each new SPI class being subclassed,we create a mapping file in a specially named directory? We create the file META-INF/services/javax.sound.sampled.spi.AudioFileReader
which consists of # Providers of sound file-reading services
# (a comment line begins with a pound sign)
com.acme.AcmeAudioFileReader
and also the file META-INF/services/javax.sound.sampled.spi.AudioFileWriter
which consists of # Providers of sound file-writing services
com.acme.AcmeAudioFileWriter
Now we run? jar cvf acme.jar -C /devel .
The? This run will create the file? com/acme/AcmeAudioFileReader.class
com/acme/AcmeAudioFileWriter.class
META-INF/services/javax.sound.sampled.spi.AudioFileReader
META-INF/services/javax.sound.sampled.spi.AudioFileWriter
META-INF/Manifest.mf
The file? How Users Install New ServicesFor end users (or system administrators) who wish to get access to a new service through their application programs,installation is simple. They place the provided JAR file in a directory in their It's not an error to install more than one provider for the same service. For example,two different service providers might supply support for reading the same type of sound file. In such a case,the system arbitrarily chooses one of the providers. Users who care which provider is chosen should install only the desired one. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |