FileDocCategorySizeDatePackage
EBookServiceServantData.javaAPI DocExample4397Wed Oct 09 14:49:00 BST 2002ora.jwsnut.chapter6.extendedbookservice

EBookServiceServantData

public class EBookServiceServantData extends Object
A class that loads the data for the extended book service

Fields Summary
private static HashMap
bookImageMap
The image data in the form of a HashMap, created when it is first requested.
private static HashMap
bookDetailsMap
HashMap containing the XML details for each book.
private static String[]
bookTitles
List of book titles.
private static byte[]
buffer
Buffer used when reading image data
private static ByteArrayOutputStream
os
Stream used when reading image data
Constructors Summary
Methods Summary
public static java.lang.String[]getBookTitles()
Gets a list of all known books.

        loadData();
        return bookTitles;
    
private static byte[]getImage(java.lang.String path)
Reads an image file and returns the content.

        InputStream is = EBookServiceServantData.class.getResourceAsStream(path);
        int count;
        while ((count = is.read(buffer, 0, buffer.length)) > 0) {
            os.write(buffer, 0, count);
        }
        byte[] content = os.toByteArray();
        os.reset();
        return content;       
    
public static byte[]getImageData(java.lang.String title, boolean gif)
Gets the image data for a book with a given title.

        byte[][] data = (byte[][])bookImageMap.get(title.trim().toUpperCase());
        return data == null ? null : data[gif ? 1 : 0];
    
public static java.lang.StringgetXMLDetails(java.lang.String title)
Gets the XML details for a book with a given title.

        return (String)bookDetailsMap.get(title.trim().toUpperCase());
    
private static voidloadData()
Loads the book data from information contained in the booklist.txt file.

        if (bookImageMap == null) {
            
            // First request - create the data
            bookImageMap = new HashMap();
            bookDetailsMap = new HashMap();
            ArrayList list = new ArrayList();

            try {
                buffer = new byte[1024];
                os = new ByteArrayOutputStream();                
                InputStream is = 
                    EBookServiceServantData.class.getResourceAsStream("booklist.txt");
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = reader.readLine()) != null) {
                    StringTokenizer st = new StringTokenizer(line, "!");
                    if (st.countTokens() == 4) {
                        String title = st.nextToken();
                        String canonicalTitle = title.trim().toUpperCase();
                        String jpegImagePath = st.nextToken();
                        String gifImagePath = st.nextToken();
                        String xmlDetails = st.nextToken();
                        
                        // Load the images
                        byte[][] imageData = new byte[2][];
                        imageData[0] = getImage(jpegImagePath);
                        imageData[1] = getImage(gifImagePath); 
                        
                        // Store the details and the images
                        bookImageMap.put(canonicalTitle, imageData);
                        bookDetailsMap.put(canonicalTitle, xmlDetails);         
                        
                        // Add a new book title
                        list.add(title);
                    }
                }
                
                bookTitles = new String[list.size()];
                list.toArray(bookTitles);
            } catch (Exception ex) {
                // Just return an empty or partial list                
                ex.printStackTrace();
            }
        }