BookShelf.java

Java for Small Spaces
Java on a chip
by Chris Carpenter
Listing 3. BookShelf.java.


/**
    BookShelf sets up the ports and runs the application
**/
package bookshelf.client;

import java.io.*;
import java.util.*;
import org.xml.sax.helpers.ParserFactory;
import bookshelf.utils.LcdTextHandler;
import com.dalsemi.onewire.adapter.DSPortAdapter;
import com.dalsemi.onewire.OneWireAccessProvider;
import org.xml.sax.Parser;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import bookshelf.messages.BookInformationRequest;

public class BookShelf implements BookObserver, Runnable {
    protected String currentBookInfo;
    protected LcdTextHandler lcdTextHandler;
    protected Dictionary books;

public BookShelf() {
    super();
}

public static void main(String[] args) {
    BookShelf bookShelf = new BookShelf();
    bookShelf.setServerURL(
        "http::bookServerHost:8080/servlet/server.bookshelf.server.Book-LibraryServerConnection");

    try {
        DSPortAdapter adapter = OneWireAccessProvider.getDefaultAdapter();
        // clear any previous search restrictions
        adapter.setSearchAllDevices();
        adapter.targetAllFamilies();
adapter.setSpeed(adapter.SPEED_REGULAR);

        BookListener bookListener = new BookListener(adapter, bookShelf);

        try {
            bookShelf.run();
        } catch (InterruptedException ie) {
System.out.println("Interrupted, cleaning up");
        }

        bookListener.killWatch();
        adapter.freePort();
    } catch (Exception owe) {
        System.out.println(owe);
    };
}

public void run() throws InterruptedException {
    wait();
}

protected void displayInformation(BookInformation info){
    LcdTextHandler.getCurrent().setText(info.toString());
}
protected Dictionary getBooks(){
    System.out.println("In getBooks()");
    if(books == null)
        books = new Hashtable();
    System.out.println("books null? " + (books == null));
    System.out.println("books empty? " + books.isEmpty());
    return books;
}
protected String getShelfId()
{
    return "001";
}

public void newBook(java.lang.String bookID) {
    Book book = (Clientbook) getbooks().get(bookID);
    if (book == null) {
        currentMedInfo = requestbookInfo(bookID.substring(0, 1));
        ResponseHandler handler = new ResponseHandler();

        try {
            Parser parser = ParserFactory.makeParser("com.docuverse.min.Parser");
            parser.setDocumentHandler(handler);
            parser.parse(new InputSource(new StringReader(current-MedInfo)));
        } catch (IOException e) {
            System.out.println(e);
        } catch (SAXException e) {
            System.out.println(e);
        } catch (InstantiationException e) {
            System.out.println(e);
        } catch (Exception e) {
            System.out.println(e);
        }

        book = handler.getbook();
        if (book == null)
            System.out.println("book object returned from handler is null.");
        getbooks().put(book.getId(), book);
    }
    if (book == null)
        System.out.println("book retrieved from store is null.");
    else
        displaybook(book);
}
public String requestBookInfo(String bookID) {
    Vector aVector = new Vector();
    aVector.addElement(bookID);
    return requestBookInfo(aVector);
}
public String requestBookInfo(Vector multipleBookIDs)
{
    BookInformationRequest request = new BookInformationRequest (getShelfId());
    Enumeration enumerator = multipleBookIDs.elements();
    while (enumerator.hasMoreElements())
        request.addBookId((String)enumerator.nextElement());

    BookLibraryConnection.getCurrent().write(request.toXML());
    return BookLibraryConnection.getCurrent().read();
}

public void setServerURL(String serverURL) {
    BookLibraryConnection.getCurrent().setServerURL(serverURL);
}
}

About the Author

Chris Carpenter is a software engineer and architect at RoleModel Software, Inc. in Holly Springs, NC. He can be contacted at ccarpenter@rolemodelsoft.com.

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.