Jump to: navigation, search

EclipseLink/Development/DBWS/OSGi/GenerateDBWSFiles

< EclipseLink‎ | Development‎ | DBWS‎ | OSGi


Generating the DBWS file

The simple use-case is the creation of a Web service that exposes a database table's CRUD (Create/Read(findByPK,findAll)/Update/Delete) lifecycle operations. Here is the table SIMPLETABLE on my local MySql database:
EclipseLink DBWS with OSGi SimpleTableDescription.png

The DBWSBuilder utility requires a DBWS configuration file as input. NB - When running the DBWSBuilder, the setenv.{cmd, sh} file in .../eclipselink/utils/dbws needs to be updated with the path to JDBC driver jar:

@REM User MUST set DRIVER_CLASSPATH to point to their desired driver jar(s). For example:
set DRIVER_CLASSPATH=C:\external\libs\mysql-connector-java-5.1.13-bin.jar
<?xml version="1.0" encoding="UTF-8"?>
<dbws-builder xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <properties>
    <property name="projectName">simpleTable</property>
    <property name="logLevel">fine</property>
    <property name="username">user</property>
    <property name="password">password</property>
    <property name="url">jdbc:mysql://localhost:3306/emp</property>
    <property name="driver">com.mysql.jdbc.Driver</property>
    <property name="platformClassname">org.eclipse.persistence.platform.database.DerbyPlatform</property>
  </properties>
  <table
    tableNamePattern="SIMPLETABLE"
  />
</dbws-builder>
prompt > dbwsbuilder.cmd -builderFile dbws-builder.xml -stageDir $ROOT/output_directory -packageAs:noArchive javase

The above builder file is a little 'weird' as it specifies how to log in to my local MySql database, but it declares that the platform is Derby. The following files are generated:

$ROOT
│ 
├───output_directory
│       DBWSProvider.class
│       DBWSProvider.java
│       eclipselink-dbws-or.xml
│       eclipselink-dbws-ox.xml
│       eclipselink-dbws-schema.xsd
│       eclipselink-dbws-sessions.xml
│       eclipselink-dbws.wsdl
│       eclipselink-dbws.xml
│       ProviderListener.class
│       ProviderListener.java

Copy the eclipselink-dbws.xml, eclipselink-dbws-or.xml, eclipselink-dbws-ox.xml and eclipselink-dbws-sessions.xml to the META-INF directory; eclipselink-dbws-schema.xsd and eclipselink-dbws.wsdl to the wsdl directory.

The eclipselink-dbws-sessions.xml needs some manual updates:

<?xml version="1.0" encoding="UTF-8"?>
<sessions version="2.0.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <session xsi:type="database-session">
      <name>simpletable-dbws-or-session</name>
      <logging xsi:type="eclipselink-log">
         <log-level>fine</log-level>
      </logging>
      <!-- add the META-INF dir to the path to find the OR project -->
      <primary-project xsi:type="xml">META-INF/eclipselink-dbws-or.xml</primary-project>
      <login xsi:type="database-login">
         <platform-class>org.eclipse.persistence.platform.database.DerbyPlatform</platform-class>
         <!-- change the driver class to use Derby's EmbeddedDriver -->
         <driver-class>org.apache.derby.jdbc.EmbeddedDriver</driver-class>
         <!-- change the URL: use Derby's 'create' attribute to create database if it doesn't already exist -->
         <connection-url>jdbc:derby:test;create=true</connection-url>
         <byte-array-binding>false</byte-array-binding>
         <optimize-data-conversion>false</optimize-data-conversion>
         <trim-strings>false</trim-strings>
      </login>
   </session>
   <session xsi:type="database-session">
      <name>simpletable-dbws-ox-session</name>
      <logging xsi:type="eclipselink-log">
         <log-level>off</log-level>
      </logging>
      <!-- add the META-INF dir to the path to find the OX project -->
      <primary-project xsi:type="xml">META-INF/eclipselink-dbws-ox.xml</primary-project>
   </session>
</sessions>

Since the database won't exist the first time the service is run, need to create the table and populate a few rows:

public class Constants {
...
 
    //database smts
    public static final String CREATE_TABLE = 
    	"CREATE TABLE SIMPLETABLE (\n" +
    	"  id NUMERIC NOT NULL,\n" +
    	"  name VARCHAR(25),\n" +
    	"  since DATE,\n" +
    	"  PRIMARY KEY (id)\n" +
    	")";
    public static final String INS1 =
    	"INSERT INTO SIMPLETABLE (id, name, since) VALUES (1, 'mike', '2001-12-25')";
    public static final String INS2 =
	"INSERT INTO SIMPLETABLE (id, name, since) VALUES (2, 'blaise','2001-12-25')";
    public static final String INS3 =
	"INSERT INTO SIMPLETABLE (id, name, since) VALUES (3, 'rick','2001-12-25')";
}
 
...
import static simpletable.Constants.CREATE_TABLE;
import static simpletable.Constants.INS1;
import static simpletable.Constants.INS2;
import static simpletable.Constants.INS3;
...
 
@ServiceMode(MESSAGE)
public class Activator extends ProviderHelper implements BundleActivator, Provider<SOAPMessage> {
...
 
@Override
public void loginSessions() {
    super.loginSessions();
    Session orSession = xrService.getORSession();
    try {
        orSession.executeNonSelectingSQL(CREATE_TABLE);
        orSession.executeNonSelectingSQL(INS1);
        orSession.executeNonSelectingSQL(INS2);
        orSession.executeNonSelectingSQL(INS3);
    }
    catch (Exception e) {
        // table & rows already exist - ignore
    }
}
...

Running the Server target should now work:

osgi> [EL Info]: 2010-08-16 16:02:10.68--DatabaseSessionImpl(17240206)--Thread(Thread[Start Level Event Dispatcher,5,main])--EclipseLink, version: Eclipse Persistence Services - 2.1.1.v20100805-r7986
[EL Config]: 2010-08-16 16:02:10.68--DatabaseSessionImpl(17240206)--Connection(13623369)--Thread(Thread[Start Level Event Dispatcher,5,main])--connecting(DatabaseLogin(
	platform=>DerbyPlatform
	user name=> ""
	datasource URL=> "jdbc:derby:test;create=true"
))
[EL Config]: 2010-08-16 16:02:11.305--DatabaseSessionImpl(17240206)--Connection(9656129)--Thread(Thread[Start Level Event Dispatcher,5,main])--Connected: jdbc:derby:test
	User: APP
	Database: Apache Derby  Version: 10.5.1.1 - (764942)
	Driver: Apache Derby Embedded JDBC Driver  Version: 10.5.1.1 - (764942)
[EL Info]: 2010-08-16 16:02:11.305--DatabaseSessionImpl(17240206)--Thread(Thread[Start Level Event Dispatcher,5,main])--simpletable-dbws-or-session login successful
[EL Fine]: 2010-08-16 16:02:11.305--DatabaseSessionImpl(17240206)--Connection(9656129)--Thread(Thread[Start Level Event Dispatcher,5,main])--CREATE TABLE SIMPLETABLE (
  id NUMERIC NOT NULL,
  name VARCHAR(25),
  since DATE,
  PRIMARY KEY (id)
)
[EL Fine]: 2010-08-16 16:02:11.383--DatabaseSessionImpl(17240206)--Connection(9656129)--Thread(Thread[Start Level Event Dispatcher,5,main])--INSERT INTO SIMPLETABLE (id, name, since) VALUES (1, mike, 2001-12-25)
[EL Fine]: 2010-08-16 16:02:11.43--DatabaseSessionImpl(17240206)--Connection(9656129)--Thread(Thread[Start Level Event Dispatcher,5,main])--INSERT INTO SIMPLETABLE (id, name, since) VALUES (2, blaise,2001-12-25)
[EL Fine]: 2010-08-16 16:02:11.43--DatabaseSessionImpl(17240206)--Connection(9656129)--Thread(Thread[Start Level Event Dispatcher,5,main])--INSERT INTO SIMPLETABLE (id, name, since) VALUES (3, rick,2001-12-25)
Hello, javax.xml.ws.Service@1bd06bf