Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

How is SPI (Service Provider Interface) works?

Thread view: 
jacksuyu@gmail.com - 19 Apr 2006 23:21 GMT
Can I define my own SPI?

I read some document, seems need to put service class under
META-INF/services directory

like
filename: com.my.SPIInterface

content:  com.my.myimplemeentationclass

But seems that doesn't work...

Any formal documents?

Thanks.
Thomas Fritsch - 19 Apr 2006 23:57 GMT
> Can I define my own SPI?
>
[quoted text clipped - 11 lines]
>
> Thanks.

There is a description (well-hidden) in the API doc:
<http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/spi/ServiceRegistry.html>.
Don't be irritated by the fact that this class is part of ImageIO. It is
applicable generally, not only for ImageIO.

See also
<http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#Service%20Provider>.
But that description is somewhat lousy, because the code-example given there
uses an undocumented class (sun.misc.Service). You can safely use a
documented class (javax.imageio.spi.ServiceRegistry, see above) instead.

Signature

"TFritsch$t-online:de".replace(':','.').replace('$','@')

jacksuyu@gmail.com - 20 Apr 2006 00:54 GMT
It appears different service take SPI values in different way....

I tried to write dummy SPI, and load from META-INF/services directory,
works fine.
but for this sample:
http://www.javaworld.com/javaworld/jw-11-2000/jw-1103-mp3.html

I tried the following code to call mp3.jar
import java.io.*;
import javax.sound.sampled.*;

public class A {

 public static void main(String[] args) {
         String fName = "a.mp3";

     new A(fName);
 }

 public A(String sound) {
   try {

     AudioInputStream stream = AudioSystem.getAudioInputStream(new
File(sound));

     // At present, ALAW and ULAW encodings must be converted
     // to PCM_SIGNED before it can be played
     AudioFormat format = stream.getFormat();

     if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
         format = new AudioFormat(
                  AudioFormat.Encoding.PCM_SIGNED,
                  format.getSampleRate(),
                  format.getSampleSizeInBits()*2,
                  format.getChannels(),
                  format.getFrameSize()*2,
                  format.getFrameRate(),
                  true);        // big endian
          stream = AudioSystem.getAudioInputStream(format, stream);
          System.out.println("\nConverted stream:");
          printFormat(format);
     }

     DataLine.Info info = new DataLine.Info(Clip.class,
stream.getFormat());
     Clip clip = (Clip) AudioSystem.getLine(info);

     // Add a listener for line events
     clip.addLineListener(new LineListener() {
       public void update(LineEvent evt) {
           if (evt.getType() == LineEvent.Type.STOP) {
              System.out.println("Sound stopped.");
              System.exit(0);
           }
       }
     });

     System.out.println("Playing sound...");
     clip.open(stream);
     clip.start();
  } catch (IOException e) {
    e.printStackTrace();
  } catch (LineUnavailableException e) {
    e.printStackTrace();
  } catch (UnsupportedAudioFileException e) {
    e.printStackTrace();
  }
 }
}

I always get javax.sound.sampled.UnsupportedAudioFileException: could
not get audio input stream from input file

error. And the class specified in META-INF/services directory never
work.....

I checked  com.sun.media.sound.JDK13Services.getProviders method
it appears need priviledge to return values, but I don't know which
priviledge and permission to run that process. And JDK13Services catch
throwable without print any message!!!!!

Any more suggestion?

Thanks.
Thomas Fritsch - 20 Apr 2006 09:56 GMT
> It appears different service take SPI values in different way....
I have absolutely no experience with Java audio. But nevertheless it
would surprise me, if Sun deviated from its canonical way of loading
SPIs here.

> I tried to write dummy SPI, and load from META-INF/services directory,
> works fine.
> but for this sample:
> http://www.javaworld.com/javaworld/jw-11-2000/jw-1103-mp3.html
>
> I tried the following code to call mp3.jar
[...code snipped...]

> I always get javax.sound.sampled.UnsupportedAudioFileException: could
> not get audio input stream from input file
[quoted text clipped - 8 lines]
>
> Any more suggestion?
In your situation I would check several things:
(1) Does your Java really see the jar file?
    You can verify this by
       System.out.println(System.getProperty("java.class.path"));
    somewhere in your application code.
    A safe and simple way to achieve this is to put the jar file
    into the ".../jre/lib/ext/" directory of your JRE. And of course,
    if you have more than 1 JREs on your system (for example:
    "C:\Programme\jdk1.5.0_04\jre\" and "C:\Programme\jre1.5.0_04\"),
    be sure to put into the correct JRE.
(2) Is your service provider class really loaded?
    You can check this by calling your application with
       java -verbose:class ...
And if encountering problems with a self-written SPI class:
(3) Is your class public? Has it a public no-arguments constructor?
(4) Does the SPI framework really load and instantiate your class?
    To verify this you can insert some System.out.println(...) lines
    into the class-initializer  static {....}
    and into the no-arguments constructor.

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

jacksuyu@gmail.com - 20 Apr 2006 15:57 GMT
Thanks for the suggestion.

After put in java -verbose:class... , I figured out the problem was
because that mp3.jar is not compatible with my mp3 files. Seems it is
just too old.

Nevertheless, the SPI loading works great.

Thank you.!!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.