FileDocCategorySizeDatePackage
EBookServiceServant.javaAPI DocExample6689Wed Mar 12 00:09:14 GMT 2003ora.jwsnut.chapter6.extendedbookservice

EBookServiceServant

public class EBookServiceServant extends Object implements EBookQuery
Implementation class for the extended books web service.

Fields Summary
private MediaTracker
tracker
MediaTracker used to load images
Constructors Summary
Methods Summary
public javax.mail.internet.MimeMultipartgetAllImages()
Gets the book images in MimeMultipart form

return
a MimeMultipart object containing the book images.

        String[] titles = EBookServiceServantData.getBookTitles();
        Image[] images = getImages(titles, false);
        
        try {
            MimeMultipart mp = new MimeMultipart();
            for (int i = 0; i < images.length; i++) {
                MimeBodyPart mbp = new MimeBodyPart();
                mbp.setContent(images[i], "image/jpeg");
                mbp.addHeader("Content-Type", "image/jpeg");
                mp.addBodyPart(mbp);
            }          
            return mp;
        } catch (MessagingException ex) {
            throw new EBookServiceException("Failed building MimeMultipart: " + ex.getMessage());
        }
    
public intgetBookCount()
Gets the number of books known to the service

return
the number of books known to the service.

        String[] titles = EBookServiceServantData.getBookTitles();
        return titles == null ? 0 : titles.length;
    
public javax.xml.transform.Source[]getBookDetails(java.lang.String[] titles)
Gets XML details for a given list of books.

param
titles the titles of the books.
return
an array of Source object containing XML details for the given list of books.
throws
EBookServiceException if the book title is unknown

        int length = titles.length;
        Source[] sources = new Source[length];
        
        for (int i = 0; i < length; i++) {
            String data = EBookServiceServantData.getXMLDetails(titles[i]);
            if (data == null) {
                throw new EBookServiceException("Unknown title: [" + titles[i] + "]");
            }
            try {
                data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + data;
                sources[i] = new StreamSource(new ByteArrayInputStream(data.getBytes("utf-8"))); 
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }         
        }
        
        return sources;
    
public java.lang.String[]getBookTitles()
Gets the set of book titles.

return
the set of book titles.

        return EBookServiceServantData.getBookTitles();        
    
public javax.activation.DataHandler[]getImageHandlers(java.lang.String[] titles, boolean gif)
Gets book images in the form of a DataHandler

param
titles the titles of the books
param
gif true to request GIF, false for JPEG
return
an array of DataHandlers for the cover images for the named books.
throws
EBookServiceException if any book title is unknown

        int length = titles.length;
        DataHandler[] handlers = new DataHandler[length];
        for (int i = 0; i < length; i++) {
            byte[] imageData = EBookServiceServantData.getImageData(titles[i], gif);
            if (imageData == null) {
                throw new EBookServiceException("Unknown title: [" + titles[i] + "]");
            }
            handlers[i] = new DataHandler(new ByteArrayDataSource("Image Data",
                                            imageData,
                                            gif ? "image/gif" : "image/jpeg"));
        }
        return handlers;
    
public java.awt.Image[]getImages(java.lang.String[] titles, boolean gif)
Gets the images for a books with given titles

param
titles the titles of the books
param
gif true to request a GIF, false for JPEG
return
an array of images for the named books.
throws
EBookServiceException if any book title is unknown

        int length = titles.length;
        Image[] images = new Image[length];
        for (int i = 0; i < length; i++) {
            byte[] imageData = EBookServiceServantData.getImageData(titles[i], gif);
            if (imageData == null) {
                throw new EBookServiceException("Unknown title: [" + titles[i] + "]");
            }
            if (tracker == null) {
                tracker = new MediaTracker(new Component() {});
            }
            Image image = Toolkit.getDefaultToolkit().createImage(imageData);
            tracker.addImage(image, 0);
            images[i] = image;            
        }
        try {
            tracker.waitForAll();
            for (int i = 0; i < length; i++) {
                tracker.removeImage(images[i]);
            }
        } catch (InterruptedException ex) {
        }

        return images;