Converting non-OSGi JDBC drivers into OSGi-compliant bundles

In Chapter 3, Advanced JPA Support in Spring Roo we discussed that the database reverse engineering process of Roo requires the JDBC driver for the database to be available as an OSGi bundle. In this recipe, we'll look at how to convert H2 database's JDBC driver into a OSGi bundle using the addon create wrapper command and use it in database reverse engineering.

Getting ready

Download the H2 database bundled as a ZIP file from , and unzip it to the C: oo-cookbook directory. Extracting the H2 database ZIP file will create a directory named h2 inside the C: oo-cookbook directory.

How to do it...

The following steps will demonstrate how to convert non-OSGi JDBC drivers into OSGi compliant bundles:

  1. Go to the C: oo-cookbookh2in directory and double-click the h2.bat file. This will start the H2 database and also open H2 Console in your default web browser, as shown in the following screenshot:
    How to do it...

    Make sure that you select Generic H2 (Server) as the value of the Saved Settings option and the value of the JDBC URL field is jdbc:h2:tcp://localhost/~/myflightappdb. Click the Connect button to log in to the H2 Console. This will automatically create the myflightappdb in H2 database.

  2. After logging in to H2 Console, you will see the myflightappdb details, as shown here:
    How to do it...

    Now, paste the following SQL statement in the text area shown in the given screenshot and execute it by clicking the Run (Ctrl+Enter) button:

    DROP TABLE IF EXISTS 'customer_tbl';
    CREATE TABLE IF NOT EXISTS 'customer_tbl' (
      'cust_id' int(10) NOT NULL,
      'cust_dob' date NOT NULL,
      'cust_name' varchar(50) NOT NULL,
      PRIMARY KEY ('cust_id','cust_dob')
    )

    Executing the SQL statement will create the CUSTOMER_TBL table in the myflightappdb database.

  3. Create the C: oo-cookbookch07-recipesdriver directory and copy the h2*.jar file from the C: oo-cookbookh2in directory to the driver directory.
  4. Open the command prompt and go to C: oo-cookbookch07-recipesdriver and execute the maven install command:
    C:
    oo-cookbookdriver> mvn install:install-file -Dfile=h2-1.3.160.jar -DgroupId=com.h2database -DartifactId=h2 -Dversion=1.3.60 -Dpackaging=jar
    

    Here, it is assumed that H2 database driver JAR file is the h2-1.3.160.jar file.

  5. Create the C: oo-cookbookch07-recipeswrapper directory and start the Roo shell from it.
  6. Execute the addon create wrapper command to create a Roo add-on that wraps the h2-1.3.160.jar maven artifact:
    ... roo> addon create wrapper --topLevelPackage com.h2.roo.jdbc --groupId com.h2database --artifactId h2 --version 1.3.60 --vendorName H2 --licenseUrl http://www.h2database.com
    
  7. Modify the pom.xml file and add the following <Import-Package> element to the configuration of Maven Bundle Plugin:
    <instructions>
      <Import-Package>javax.servlet.*;resolution:=optional,
        org.apache.lucene.*;resolution:=optional,
        org.slf4j;resolution:=optional,*
      </Import-Package>
      ...
    </instructions>
  8. Now, exit the Roo shell and execute the following maven goal to generate the OSGi version of the H2 database driver:
    C:
    oo-cookbookch07-recipeswrapper> mvn bundle:bundle
    

    This maven goal creates the OSGi-compliant H2 database driver in the target directory of the project with name com.h2.roo.jdbc.h2-1.3.60.0001.jar.

  9. Create the C: oo-cookbookch07-recipesflight-app directory and start the Roo shell from it.
  10. Create a new Roo project inside the flight-app directory:
    ... roo> project --topLevelPackage sample.roo.flightapp --java 6 --projectName flight-app
    
  11. Set up Hibernate as the persistence provider for the myflightappdb H2 database:
    ... roo> persistence setup --provider HIBERNATE --database H2_IN_MEMORY --databaseName myFlightAppDB
    
  12. Set 'sa' as the username to use for connecting with the H2 database:
    ... roo> database properties set --key database.username --value sa
    
  13. Now, execute the perform eclipse command to create Eclipse IDE-specific configuration files:
    ... roo> perform eclipse
    
  14. Import the flight-app project into Eclipse IDE and modify the database.url property in the SRC_MAIN_RESOURCES/META-INF/spring/database.properties file to point to the myflightappdb H2 database, as shown here:
    database.password=
    database.url=jdbc:h2:tcp://localhost/~/myflightappdb
    database.username=sa
    database.driverClassName=org.h2.Driver
  15. Now, install the OSGi-compliant H2 database driver that we created earlier:
    ... roo> osgi start --url file:///C:/roo-cookbook/ch07-recipes /wrapper/target/com.h2.roo.jdbc.h2-1.3.60.0001.jar
    
  16. Execute database reverse engineer command to instruct Roo to create the JPA entity corresponding to the CUSTOMER_TBL in the H2 database:
    roo> database reverse engineer --schema PUBLIC
    

How it works...

The addon create wrapper command creates an add-on that wraps a Maven artifact. The Apache Felix Maven Bundle Plugin's bundle goal creates an OSGi-compliant JAR for the add-on project.

The <Import-Package> element specifies the packages that are required or optional for the bundle.

See also

  • Refer to the Creating entities from database recipe of Chapter 3 to see how the database reverse engineer command works
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.134.17