BookWrapper クラスを作成する

BookWrapper という名前で CobolBookReader プロジェクトに COBOL JVM クラスを作成します。その内容を次のコードに置き換えます。

      $set sourceformat"variable"
      $set ilnamespace"com.microfocus.book" 
      $set ilusing"java.util" 
      $set ilusing"java.io"
       class-id Book.

       working-storage section.
       copy "book-error-codes.cpy".
       copy "book-rec-jvm.cpy" replacing == (prefix) == by == book ==.
       
       01 filenameSet          condition-value static value false.
       78 filenamePropFile     value "filename.properties".
       78 filenameProp         value "filename".
       78 defaultFilename      value "C:\bookfile.dat".
       
       01 bookFunction         pic x static.
           78 readRecord       value "1".
           78 addRecord        value "2".
           78 deleteRecord     value "3".
           78 nextRecord       value "4".
           78 allRecords       value "5".

       *>> <summary>
       *>> When the method is called, it reads book details
       *>> from a file by a given "stock number".
       *>> </summary>
       method-id Read static.
       local-storage section.
       01 file-status pic xx.
       procedure division using by value stockno-in as string
                          returning      myBook     as type Book.
           set myBook to new Book()
           set myBook::StockNumber to stockno-in
           invoke self::SetupFilename(myBook::getClass()::getClassLoader())

           call "BookLegacy" using by value readRecord
                                   by value myBook::BookDetails
                                   by reference file-status
           invoke self::RaiseExceptionIfError(file-status)

       end method.

       *>> <summary>
       *>> This method records information about a book into the data file
       *>> </summary>
       *>> <param name="stockno-in">The stock number of the book</param>
       *>> <param name="title-in">The title of the book</param>
       *>> <param name="author-in">The author of the book</param>
       *>> <param name="type-in">The type of the book</param>
       *>> <param name="price-in">The price of the book</param>
       *>> <param name="onhand-in">The number of the books in stock</param>
       *>> <param name="sold-in">The number of books sold</param>
       *>> <returns></returns>
       method-id Add static.
       local-storage section.
       01 file-status pic xx.
       procedure division using by value  stockno-in as string
                                by value  title-in   as string
                                by value  author-in  as string
                                by value  type-in    as string
                                by value  price-in   as decimal
                                by value  onhand-in  as binary-long
                                by value  sold-in    as binary-long
                                returning myBook     as type Book.

           set myBook               to new Book()
           set myBook::StockNumber  to stockno-in
           set myBook::Title        to title-in
           set myBook::Author       to author-in
           set myBook::Type         to type-in
           set myBook::RetailPrice  to price-in
           set myBook::NumberOnHand to onhand-in
           set myBook::NumberSold   to sold-in
           invoke self::SetupFilename(myBook::getClass()::getClassLoader())

           call "BookLegacy" using by value addRecord
                                   by value myBook::BookDetails
                                   by reference file-status

           invoke self::RaiseExceptionIfError(file-status)

       end method.
       
       method-id Add static.
       local-storage section.
       procedure division using by value  inBook as type Book
                                returning outBook as type Book.

           set outBook to type Book::Add(
               inBook::StockNumber
               inBook::Title
               inBook::Author
               inBook::Type
               inBook::RetailPrice
               inBook::NumberOnHand
               inBook::NumberSold
           )

       end method.

       *>> <summary>
       *>> Reads details about the next book following the given stock number
       *>> </summary>
       *>> <param name="stockno">The stock number of the last book</param>
       *>> <returns>Details about the next book</returns>
       method-id Next static.
       local-storage section.
       01 file-status pic xx.
       procedure division using by value stockno as string
                          returning      myBook  as type Book.

           set myBook to new Book()
           set myBook::StockNumber to stockno
           invoke self::SetupFilename(myBook::getClass()::getClassLoader())

           call "BookLegacy" using by value nextRecord
                                   by value myBook::BookDetails
                                   by reference file-status

           invoke self::RaiseExceptionIfError(file-status)

       end method.

       *>> <summary>
       *>> Deletes a book from the data file given the book stock number
       *>> </summary>
       *>> <param name="stockno">The stock number of the book to delete</param>
       *>> <returns>The book that was deleted from the data file</returns>
       method-id Delete static.
       local-storage section.
       01 file-status pic xx.
       procedure division using by value stockno as string
                          returning      myBook  as type Book.

           set myBook to new Book()
           set myBook::StockNumber to stockno
           invoke self::SetupFilename(myBook::getClass()::getClassLoader())

           call "BookLegacy" using by value deleteRecord
                                   by value myBook::BookDetails
                                   by reference file-status

           invoke self::RaiseExceptionIfError(file-status)

       end method.

       *>> <summary>
       *>> Raises a specific exception given a file status
       *>> </summary>
       *>> <param name="file-status">The file status that determines the exception to raise</param>
       method-id RaiseExceptionIfError static.
       linkage section.
       01 file-status pic xx.
       procedure division using file-status.
           if file-status <> "00" and file-status <> "02"
               evaluate file-status
                   when "35"
                       raise new BookException(DataFileNotFound)
                   when "23"
                       raise new BookException(StockItemNotFound)
                   when "46"
                       raise new BookException(NoMoreItemsLeft)
                   when "99"
                       raise new BookException(ItemAlreadyExists)
                   when other
                       raise new BookException(FileError)
               end-evaluate
           end-if
       end method.
       
       method-id SetupFilename static.
       01 filename     pic x(256).
       procedure division using by value cl as type ClassLoader.
           if not filenameSet
               set filename to self::ReadFilename(cl)
               
               call "SET_FILENAME" using filename
               
               set filenameSet to true
           end-if
       end method.
       
       method-id ReadFilename static.
       01 props        type Properties.
       01 inputStream  type InputStream.
       procedure division using by value cl as type ClassLoader
                              returning ret as string.
           set props to new Properties()
           set inputStream to cl::getResourceAsStream(filenamePropFile)
           invoke props::load(inputStream)
           invoke inputStream::close()
           
           set ret to props::getProperty(filenameProp defaultFilename)
       end method.


       *>> <summary>
       *>> Returns a pointer to the book details
       *>> </summary>
       *>> <returns>Pointer to the book details</returns>
       method-id get property BookDetails.
       procedure division returning bookDetailsAddress as pointer.
           set bookDetailsAddress to address of book-details
       end method.

       *>> <summary>
       *>> Returns the computed stock value of the book.
       *>> </summary>
       *>> <returns>The computed stock value of the book</returns>
       method-id get property StockValue.
       procedure division returning stockvalue as float-short.
           compute stockvalue = book-retail * book-onhand

       end method.
       end class.

これは、ブックを表すマネージ COBOL クラスであり、ファイル・システムへのアクセスとブック情報の内部管理を行うための静的メソッドを提供します。

注:リソースの読み取りに使用されるクラス・ローダは、JVM 基本クラスではなくアプリケーション内のクラスから取得されることが重要です。Tomcat クラス・ローディングの癖は、すべての基本クラスがクラス・ローダとして null を返すことを意味します。この例では、各関数は Book クラスから取得したクラス・ローダを渡します。