Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/Examples/DBWS/AdvancedOSGi"
(→Generating the DBWS file) |
(→Generating the DBWS file) |
||
Line 325: | Line 325: | ||
Running the <b>SimpleTable</b> target should now work: | Running the <b>SimpleTable</b> target should now work: | ||
<source lang="text"> | <source lang="text"> | ||
− | + | 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 | |
− | osgi> [EL Info]: 2010-08-16 16: | + | [EL Config]: 2010-08-16 16:02:10.68--DatabaseSessionImpl(17240206)--Connection(13623369)--Thread(Thread[Start Level Event Dispatcher,5,main])--connecting(DatabaseLogin( |
− | [EL Config]: 2010-08-16 16: | + | |
platform=>DerbyPlatform | platform=>DerbyPlatform | ||
user name=> "" | user name=> "" | ||
datasource URL=> "jdbc:derby:test;create=true" | datasource URL=> "jdbc:derby:test;create=true" | ||
)) | )) | ||
− | [EL Config]: 2010-08-16 16: | + | [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 | User: APP | ||
Database: Apache Derby Version: 10.5.1.1 - (764942) | Database: Apache Derby Version: 10.5.1.1 - (764942) | ||
Driver: Apache Derby Embedded JDBC Driver Version: 10.5.1.1 - (764942) | Driver: Apache Derby Embedded JDBC Driver Version: 10.5.1.1 - (764942) | ||
− | [EL Info]: 2010-08-16 16: | + | [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: | + | [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, | id NUMERIC NOT NULL, | ||
name VARCHAR(25), | name VARCHAR(25), | ||
Line 343: | Line 342: | ||
PRIMARY KEY (id) | PRIMARY KEY (id) | ||
) | ) | ||
− | Hello, javax.xml.ws.Service@ | + | [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 | ||
</source> | </source> | ||
=== A SimpleTable REST Client === | === A SimpleTable REST Client === |
Revision as of 16:33, 16 August 2010
DBWS in an OSGi Environment
An Eclipselink DBWS service can be run in an OSGi environment using Javase 6's 'containerless' javax.xml.ws.Endpoint API.
In this example, Eclipse Equinox is the OSGi environment.
Environment Setup
Download a version of the Eclipse IDE that includes the Plug-in Development Environment (PDE) (Eclipse IDE for Java EE Developers, Eclipse Classic 3.6.0 or
Eclipse for RCP and RAP Developers). We will need an OSGi-friendly JDBC driver - for this example, we will the Apache Derby bundle:
- Download Apache Derby (org.apache.derby) bundle from Orbit.
- Place the org.apache.derby bundle into your $ECLIPSE_HOME/dropins folder.
- Install the EclipseLink and Jetty target Components:
{Note - the following will be changed once the EclipseLink P2 Update Repository has the correct version of EclipseLink that supports running DBWS with OSGi}
Accept the license and proceed through the prompts for re-starting Eclipse ...
Under 'Preferences' -> 'Plug-in Development' -> 'Target Platform', reload the Running Platform (Active)
Create a new Plug-in project
From the 'Plug-in Development' Perspective, create a new Plug-in project SimpleTable:
{Note the choice of 'standard' for OSGi framework}
Proceed to the next panel of the wizard where the (OSGi bundle) Activator for SimpleTable is defined:
Click 'Finish' and open the generated Activator:
package simpletable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { private static BundleContext context; static BundleContext getContext() { return context; } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; } }
Modifying Activator to implement javax.xml.ws.Provider
In order for our Activator to provide a Web service, we need to update the EclipseLink bundles. Builds after
2.1.1.v20100805-r7986 have EclipseLink DBWS packaged as a 'fragment' bundle that extends the core
org.eclipse.persistence
bundle. Download the updated bundles and extract the following 6 bundles to some directory:
$ ls updatedBundles/ org.eclipse.persistence.asm.source_2.1.1.v20100805-r7986.jar org.eclipse.persistence.asm_2.1.1.v20100805-r7986.jar org.eclipse.persistence.core.source_2.1.1.v20100805-r7986.jar org.eclipse.persistence.core_2.1.1.v20100805-r7986.jar org.eclipse.persistence.dbws.source_2.1.1.v20100805-r7986.jar org.eclipse.persistence.dbws_2.1.1.v20100805-r7986.jar
Edit the Target Platform, adding the directory with the above bundles:
{see note above about EclipseLink P2 Update Repository}
Change the Activator to extend org.eclipse.persistence.internal.dbws.ProviderHelper
(NB - will show error):
In order to correct the error, the SimpleTable project needs to update its list of required bundles. Open the MANIFEST.MF file in the 'Plug-in Manifest Editor' and select the 'Dependencies' tab and add the following bundles:
Note: you do not have to directly reference the DBWS bundle since its 'host' bundle org.eclipse.persistence.core is included in the list of 'Required Plug-ins'
The Activator needs to be annotated to support a (dynamic) Web services Endpoint:
public class Constants { //JAX-WS properties public static final String TEST_NAMESPACE = "urn:" + TEST_PROJECT; public static final String TEST_SERVICE = TEST_PROJECT + "Service"; public static final String TEST_SERVICE_NAMESPACE = "urn:" + TEST_SERVICE; public static final String TEST_PORT = TEST_SERVICE + "Port"; public static final String ENDPOINT_ADDRESS = "http://localhost:9999/" + TEST_PROJECT; } package simpletable; //java eXtension imports import javax.xml.namespace.QName; import javax.xml.soap.SOAPMessage; import javax.xml.ws.Endpoint; import javax.xml.ws.Provider; import javax.xml.ws.Service; import javax.xml.ws.WebServiceProvider; import javax.xml.ws.ServiceMode; import static javax.xml.ws.Service.Mode.MESSAGE; import static javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING; //EclipseLink imports import org.eclipse.persistence.internal.dbws.ProviderHelper; import org.eclipse.persistence.sessions.Session; //OSGi/PDE imports import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; //test imports import static simpletable.Constants.ENDPOINT_ADDRESS; import static simpletable.Constants.TEST_PORT; import static simpletable.Constants.TEST_SERVICE_NAMESPACE; import static simpletable.Constants.TEST_SERVICE; @WebServiceProvider( targetNamespace = TEST_SERVICE_NAMESPACE, serviceName = TEST_SERVICE, portName = TEST_PORT ) @ServiceMode(MESSAGE) public class Activator extends ProviderHelper implements BundleActivator, Provider<SOAPMessage> { private static BundleContext context; private static Endpoint endpoint = null; private static QName portQName = null; private static Service testService = null; static BundleContext getContext() { return context; } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; super.init(getClass().getClassLoader(), null, false); endpoint = Endpoint.create(this); endpoint.publish(ENDPOINT_ADDRESS); QName serviceQName = new QName(TEST_SERVICE_NAMESPACE, TEST_SERVICE); portQName = new QName(TEST_SERVICE_NAMESPACE, TEST_PORT); testService = Service.create(serviceQName); testService.addPort(portQName, SOAP11HTTP_BINDING, ENDPOINT_ADDRESS); System.out.println("Hello, " + testService.toString()); } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext bundleContext) throws Exception { Activator.context = null; if (endpoint != null) { endpoint.stop(); } super.destroy(); System.out.println("Goodbye"); } }
Running the DBWS Provider-Activator
In the Eclipse IDE, select the MANIFEST.MF
file and bring up the context menu for 'Run As' -> 'Run Configurations' -> 'OSGi Framework' to create a new launch configuration:
Deselect the 'Include optional dependencies when computing required bundles' and the
'Add new workspace bundles to this launch configuration automatically' check-boxes.
Deselect all bundles and re-select 'SimpleTable' and then click 'Add Required Bundles'. This simplifies the list of Required Bundles (approx. ~16):
When this launch configuration is run, an exception is thrown indicating that some DBWS files cannot be found. The SimpleTable project needs two additional source-folders. The first folder already exists - add the META-INF
folder as a source-folder. The second needs to be created:
In the next section, we will re-use a previous example (Basic Table) to generate the required files.
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:
The DBWSBuilder utility requires a DBWS configuration file as input.
<?xml version="1.0" encoding="UTF-8"?> <dbws-builder xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <properties> <property name="projectName">simpleTtble</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 SimpleTable 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