DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Using SQS With JMS for Legacy Applications
  • Javac and Java Katas, Part 2: Module Path
  • Javac and Java Katas, Part 1: Class Path
  • Ways To Reduce JVM Docker Image Size

Trending

  • Vapi vs Retell AI vs LiveKit: Architecture, Latency, Pricing, and Developer Experience
  • Member Spotlight: Srinivas Chippagiri
  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack
  • Where the Model Stops and the Code Starts: Lessons From an AI Procurement Agent

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Migrating My Java Codebase to EE10

Migrating My Java Codebase to EE10

Learn how to connect EclipseLink to database drivers in an OSGi container while modernizing a Java codebase for Jakarta Persistence.

By 
Kees Pieters
·
Dec. 03, 26 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
19 Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In the past few months, I have been struggling to update my JAVA codebase to the newest libraries and standards. Now that I am seeing some light at the end of the tunnel, I thought I'd share my experience, because I'm sure I am not the only one dealing with incompatible, outdated libraries and multiple developments happening at the same time. This first contribution will be part of an irregular series that I aim to share when I've made progress I think may help the community.

The Problem

Let's start by setting the scene. I have been developing Eclipse RAP/RCP applications since the early 2000s in a pure OSGi setting. My Java codebase has therefore evolved with changing architectural views and developments, and has tended to rely on a few libraries that together allowed me to develop enterprise-based solutions. As of 2022, this was based on:

  • Eclipse RAP 3.15 for UI widgets, running on a Jetty Server
  • Javax.servlet 2.x for static resources and supporting web components
  • org.eclipse.http.registry for registration of Web resources
  • Javax.ws.rs and Jersey  2.x for RESTful microservices
  • EclipseLink  2.x for persistence
  • Gemini JPA and DBAccess for database connectivity (using MySQL connectors)
  • Eclipse Virgo as the server-side OSGI container
  • Eclipse Tycho (2.x) target definitions as a mix of Maven dependencies and JAR files stored locally on my computer  
  • My bundles support JAVA 8 and upward

Because of some personal circumstances, this codebase was dormant since the end of COVID in 2022, so last year I decided to embark on a mission to get my codebase updated to the newest standards:

  • Eclipse RAP 4.6 for UI widgets
  • Jakarta.servlet 6.x for static resources and supporting web components
  • Jakarta.annotations (and apache.felix.scr ) for Component HTTP resource registration 
  • Jakarta.ws.rs and Jersey 5.x for RESTful microservices
  • EclipseLink 5.x for persistence
  • Replace Gemini JPA and DBAccess, because they are no longer actively maintained 
  • Apache Karaf as the server-side OSGI container, for the same reasons as above 
  • Eclipse Tycho (5.x) target definitions purely as Maven dependencies

My original plan to go cold-turkey blew up in my face because, at every level, fundamental choices had been made in the Java architecture and, as noted above, some of the libraries I depended on were no longer maintained. In the end, I decided to first take an intermediate step and see if I could get my current setup to the most recent libraries. This would mean the following changes to my original setup:

  • Eclipse RAP 3.29 for UI widgets
  • Javax.servlet 4.x for static resources and supporting web components
  • org.osgi.service.http.whiteboard  for HTTP resource registration 
  • Replace Gemini JPA and DBAccess, because they are no longer actively maintained
  • Eclipse Tycho (5.x) target definitions purely as Maven dependencies

Even this more modest goal proved to be challenging in itself. One of the first lessons I learned was that, to make this migration work, I needed to start thinking in terms of Enterprise Edition Versions, or Java EE x. I had never really let it sink in that my current setup basically used the Java EE 8 architecture and that the different libraries tend to work well together for that reason. The developers of Eclipse RAP also adopted this approach over the years, so RAP 3.x was based on Java EE 8, while RAP 4.x was already preparing for Java EE 11. This means my migration strategy had to follow the RAP codebase as closely as possible. This approach is reflected in my target definition:

XML
 
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde version="3.8"?>
<target name="commons-jpa.target" sequenceNumber="254">
<locations>
    <location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
        <repository location="https://download.eclipse.org/rt/rap/3.29"/>
        <unit id="org.eclipse.rap.equinox.target.feature.feature.group" version="3.29.0.20240605-1200"/>
    </location>
    <location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="true" missingManifest="generate" type="Maven">
        <dependencies>
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>1.0</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.14.0</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>com.sun.activation</groupId>
                <artifactId>javax.activation</artifactId>
                <version>1.2.0</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>com.sun.mail</groupId>
                <artifactId>javax.mail</artifactId>
                <version>1.6.2</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.22.0</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.4.0</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>javax.annotation</groupId>
                <artifactId>javax.annotation-api</artifactId>
                <version>1.3.2</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>javax.transaction</groupId>
                <artifactId>jta</artifactId>
                <version>1.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>javax.validation</groupId>
                <artifactId>validation-api</artifactId>
                <version>2.0.1.Final</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>javax.ws.rs</groupId>
                <artifactId>javax.ws.rs-api</artifactId>
                <version>2.1.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.4.0-b180830.0359</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.14</version>
                <type>jar</type>
            </dependency>
              <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.26.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.26.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.26.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j2-impl</artifactId>
                <version>2.24.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j2-impl</artifactId>
                <version>2.24.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>javax.persistence</artifactId>
                <version>2.2.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.antlr</artifactId>
                <version>2.7.16</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.asm</artifactId>
                <version>9.9.0</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.core</artifactId>
                <version>2.7.16</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.jpa.jpql</artifactId>
                <version>2.7.16</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.jpa</artifactId>
                <version>2.7.16</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.eclipse.platform</groupId>
                <artifactId>org.eclipse.equinox.security</artifactId>
                <version>1.4.700</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.hk2.external</groupId>
                <artifactId>aopalliance-repackaged</artifactId>
                <version>2.6.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.hk2</groupId>
                <artifactId>hk2-api</artifactId>
                <version>2.6.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.hk2</groupId>
                <artifactId>hk2-locator</artifactId>
                <version>2.6.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.hk2</groupId>
                <artifactId>hk2-utils</artifactId>
                <version>2.6.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.hk2</groupId>
                <artifactId>osgi-resource-locator</artifactId>
                <version>1.0.4</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet-core</artifactId>
                <version>2.48</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-client</artifactId>
                <version>2.48</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-common</artifactId>
                <version>2.48</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-server</artifactId>
                <version>2.48</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.inject</groupId>
                <artifactId>jersey-hk2</artifactId>
                <version>2.48</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.33.0-GA</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-commons</artifactId>
                <version>6.0.3</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-engine</artifactId>
                <version>6.0.3</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-launcher</artifactId>
                <version>6.0.3</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
                <version>6.0.3</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.mariadb.jdbc</groupId>
                <artifactId>mariadb-java-client</artifactId>
                <version>3.5.10</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.opentest4j</groupId>
                <artifactId>opentest4j</artifactId>
                <version>1.3.0</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.osgi</groupId>
                <artifactId>org.osgi.namespace.extender</artifactId>
                <version>1.0.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.osgi</groupId>
                <artifactId>org.osgi.service.component.annotations</artifactId>
                <version>1.5.1</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.osgi</groupId>
                <artifactId>org.osgi.service.jdbc</artifactId>
                <version>1.1.0</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.osgi</groupId>
                <artifactId>org.osgi.service.jndi</artifactId>
                <version>1.0.0</version>
                <type>jar</type>
            </dependency>
        </dependencies>
    </location>
</locations>
    <includeBundles>
        <plugin id="com.google.gson"/>
        <plugin id="com.google.gson.source"/>
        <plugin id="com.ibm.icu"/>
        <plugin id="com.ibm.icu.source"/>
        <plugin id="org.apache.aries.spifly.dynamic.bundle"/>
        <plugin id="org.apache.commons.commons-codec"/>
        <plugin id="org.apache.commons.commons-codec.source"/>
        <plugin id="org.apache.commons.commons-io"/>
        <plugin id="org.apache.felix.gogo.command"/>
        <plugin id="org.apache.felix.gogo.command.source"/>
        <plugin id="org.apache.felix.gogo.runtime"/>
        <plugin id="org.apache.felix.gogo.runtime.source"/>
        <plugin id="org.apache.felix.gogo.shell"/>
        <plugin id="org.apache.felix.gogo.shell.source"/>
        <plugin id="org.apache.felix.scr"/>
        <plugin id="org.apache.felix.scr.source"/>
        <plugin id="org.eclipse.core.commands"/>
        <plugin id="org.eclipse.core.commands.source"/>
        <plugin id="org.eclipse.core.contenttype"/>
        <plugin id="org.eclipse.core.contenttype.source"/>
        <plugin id="org.eclipse.core.databinding"/>
        <plugin id="org.eclipse.core.databinding.beans"/>
        <plugin id="org.eclipse.core.databinding.beans.source"/>
        <plugin id="org.eclipse.core.databinding.observable"/>
        <plugin id="org.eclipse.core.databinding.observable.source"/>
        <plugin id="org.eclipse.core.databinding.property"/>
        <plugin id="org.eclipse.core.databinding.property.source"/>
        <plugin id="org.eclipse.core.databinding.source"/>
        <plugin id="org.eclipse.core.expressions"/>
        <plugin id="org.eclipse.core.expressions.source"/>
        <plugin id="org.eclipse.core.jobs"/>
        <plugin id="org.eclipse.core.jobs.source"/>
        <plugin id="org.eclipse.core.runtime"/>
        <plugin id="org.eclipse.core.runtime.source"/>
        <plugin id="org.eclipse.equinox.app"/>
        <plugin id="org.eclipse.equinox.app.source"/>
        <plugin id="org.eclipse.equinox.cm"/>
        <plugin id="org.eclipse.equinox.cm.source"/>
        <plugin id="org.eclipse.equinox.common"/>
        <plugin id="org.eclipse.equinox.common.source"/>
        <plugin id="org.eclipse.equinox.console"/>
        <plugin id="org.eclipse.equinox.console.source"/>
        <plugin id="org.eclipse.equinox.event"/>
        <plugin id="org.eclipse.equinox.event.source"/>
        <plugin id="org.eclipse.equinox.preferences"/>
        <plugin id="org.eclipse.equinox.preferences.source"/>
        <plugin id="org.eclipse.equinox.registry"/>
        <plugin id="org.eclipse.equinox.registry.source"/>
        <plugin id="org.eclipse.equinox.security"/>
        <plugin id="org.eclipse.equinox.security.source"/>
        <plugin id="org.eclipse.help"/>
        <plugin id="org.eclipse.help.source"/>
        <plugin id="org.eclipse.jdt.junit.runtime"/>
        <plugin id="org.eclipse.jdt.junit.runtime.source"/>
        <plugin id="org.eclipse.jetty.http"/>
        <plugin id="org.eclipse.jetty.http.source"/>
        <plugin id="org.eclipse.jetty.io"/>
        <plugin id="org.eclipse.jetty.io.source"/>
        <plugin id="org.eclipse.jetty.logging"/>
        <plugin id="org.eclipse.jetty.security"/>
        <plugin id="org.eclipse.jetty.security.source"/>
        <plugin id="org.eclipse.jetty.server"/>
        <plugin id="org.eclipse.jetty.server.source"/>
        <plugin id="org.eclipse.jetty.session"/>
        <plugin id="org.eclipse.jetty.util"/>
        <plugin id="org.eclipse.jetty.util.ajax"/>
        <plugin id="org.eclipse.jetty.util.ajax.source"/>
        <plugin id="org.eclipse.jetty.util.source"/>
        <plugin id="org.eclipse.osgi.util"/>
        <plugin id="org.eclipse.rap.filedialog"/>
        <plugin id="org.eclipse.rap.filedialog.source"/>
        <plugin id="org.eclipse.rap.fileupload"/>
        <plugin id="org.eclipse.rap.fileupload.source"/>
        <plugin id="org.eclipse.rap.jface"/>
        <plugin id="org.eclipse.rap.jface.databinding"/>
        <plugin id="org.eclipse.rap.jface.databinding.source"/>
        <plugin id="org.eclipse.rap.jface.source"/>
        <plugin id="org.eclipse.rap.nebula.jface.gridviewer"/>
        <plugin id="org.eclipse.rap.nebula.jface.gridviewer.source"/>
        <plugin id="org.eclipse.rap.nebula.widgets.grid"/>
        <plugin id="org.eclipse.rap.nebula.widgets.grid.source"/>
        <plugin id="org.eclipse.rap.nebula.widgets.richtext"/>
        <plugin id="org.eclipse.rap.nebula.widgets.richtext.source"/>
        <plugin id="org.eclipse.rap.rwt"/>
        <plugin id="org.eclipse.rap.rwt.osgi"/>
        <plugin id="org.eclipse.rap.rwt.osgi.source"/>
        <plugin id="org.eclipse.rap.rwt.source"/>
        <plugin id="org.eclipse.rap.ui"/>
        <plugin id="org.eclipse.rap.ui.cheatsheets"/>
        <plugin id="org.eclipse.rap.ui.cheatsheets.source"/>
        <plugin id="org.eclipse.rap.ui.forms"/>
        <plugin id="org.eclipse.rap.ui.forms.source"/>
        <plugin id="org.eclipse.rap.ui.source"/>
        <plugin id="org.eclipse.rap.ui.views"/>
        <plugin id="org.eclipse.rap.ui.views.source"/>
        <plugin id="org.eclipse.rap.ui.workbench"/>
        <plugin id="org.eclipse.rap.ui.workbench.source"/>
        <plugin id="org.hamcrest"/>
        <plugin id="org.hamcrest.core"/>
        <plugin id="org.hamcrest.core.source"/>
        <plugin id="org.junit"/>
        <plugin id="org.junit.source"/>
        <plugin id="org.objectweb.asm"/>
        <plugin id="org.objectweb.asm.commons"/>
        <plugin id="org.objectweb.asm.tree"/>
        <plugin id="org.objectweb.asm.tree.analysis"/>
        <plugin id="org.objectweb.asm.util"/>
        <plugin id="org.osgi.service.cm"/>
        <plugin id="org.osgi.service.cm.source"/>
        <plugin id="org.osgi.service.component"/>
        <plugin id="org.osgi.service.component.annotations"/>
        <plugin id="org.osgi.service.component.annotations.source"/>
        <plugin id="org.osgi.service.component.source"/>
        <plugin id="org.osgi.service.device"/>
        <plugin id="org.osgi.service.device.source"/>
        <plugin id="org.osgi.service.event"/>
        <plugin id="org.osgi.service.event.source"/>
        <plugin id="org.osgi.service.metatype"/>
        <plugin id="org.osgi.service.metatype.source"/>
        <plugin id="org.osgi.service.prefs"/>
        <plugin id="org.osgi.service.prefs.source"/>
        <plugin id="org.osgi.service.provisioning"/>
        <plugin id="org.osgi.service.provisioning.source"/>
        <plugin id="org.osgi.service.upnp"/>
        <plugin id="org.osgi.service.upnp.source"/>
        <plugin id="org.osgi.service.useradmin"/>
        <plugin id="org.osgi.service.useradmin.source"/>
        <plugin id="org.osgi.service.wireadmin"/>
        <plugin id="org.osgi.service.wireadmin.source"/>
        <plugin id="org.osgi.util.function"/>
        <plugin id="org.osgi.util.function.source"/>
        <plugin id="org.osgi.util.measurement"/>
        <plugin id="org.osgi.util.measurement.source"/>
        <plugin id="org.osgi.util.position"/>
        <plugin id="org.osgi.util.position.source"/>
        <plugin id="org.osgi.util.promise"/>
        <plugin id="org.osgi.util.promise.source"/>
        <plugin id="org.osgi.util.xml"/>
        <plugin id="org.osgi.util.xml.source"/>
        <plugin id="slf4j.api"/>
        <plugin id="org.osgi.service.jdbc"/>
        <plugin id="org.osgi.service.jdbc.source"/>
        <plugin id="org.osgi.service.jndi"/>
        <plugin id="org.osgi.service.jndi.source"/>
        <plugin id="org.eclipse.persistence.jpa"/>
        <plugin id="org.eclipse.persistence.jpa.source"/>
        <plugin id="org.apache.commons.commons-logging"/>
        <plugin id="org.apache.commons.commons-logging.source"/>
        <plugin id="org.glassfish.jersey.core.jersey-client.source"/>
        <plugin id="org.glassfish.jersey.core.jersey-client"/>
        <plugin id="org.glassfish.jersey.core.jersey-common.source"/>
        <plugin id="org.glassfish.jersey.core.jersey-common"/>
        <plugin id="org.glassfish.jersey.core.jersey-server.source"/>
        <plugin id="org.glassfish.jersey.core.jersey-server"/>
        <plugin id="org.glassfish.jersey.containers.jersey-container-servlet-core"/>
        <plugin id="org.glassfish.jersey.containers.jersey-container-servlet-core.source"/>
        <plugin id="org.eclipse.persistence.antlr.source"/>
        <plugin id="org.eclipse.persistence.antlr"/>
        <plugin id="org.eclipse.persistence.asm.source"/>
        <plugin id="org.eclipse.persistence.asm"/>
        <plugin id="org.eclipse.persistence.core.source"/>
        <plugin id="org.eclipse.persistence.core"/>
        <plugin id="org.opentest4j.source"/>
        <plugin id="org.opentest4j"/>
        <plugin id="org.osgi.namespace.extender.source"/>
        <plugin id="org.osgi.namespace.extender"/>
        <plugin id="org.osgi.service.coordinator"/>
        <plugin id="wrapped.junit.junit.source"/>
        <plugin id="wrapped.junit.junit"/>
        <plugin id="org.glassfish.hk2.osgi-resource-locator"/>
        <plugin id="org.glassfish.hk2.osgi-resource-locator.source"/>
        <plugin id="org.glassfish.jersey.inject.jersey-hk2"/>
        <plugin id="org.glassfish.jersey.inject.jersey-hk2.source"/>
        <plugin id="org.glassfish.hk2.api.source"/>
        <plugin id="org.glassfish.hk2.api"/>
        <plugin id="org.glassfish.hk2.utils.source"/>
        <plugin id="org.glassfish.hk2.utils"/>
        <plugin id="org.glassfish.hk2.external.aopalliance-repackaged"/>
        <plugin id="org.glassfish.hk2.external.aopalliance-repackaged.source"/>
        <plugin id="wrapped.aopalliance.aopalliance.source"/>
        <plugin id="wrapped.aopalliance.aopalliance"/>
        <plugin id="javassist"/>
        <plugin id="javassist.source"/>
        <plugin id="org.eclipse.osgi"/>
        <plugin id="org.eclipse.osgi.source"/>
        <plugin id="com.sun.activation.javax.activation"/>
        <plugin id="com.sun.activation.javax.activation.source"/>
        <plugin id="javax.validation.api"/>
        <plugin id="javax.validation.api.source"/>
        <plugin id="wrapped.javax.transaction.jta"/>
        <plugin id="wrapped.javax.transaction.jta.source"/>
        <plugin id="javax.ws.rs-api"/>
        <plugin id="javax.ws.rs-api.source"/>
        <plugin id="wrapped.javax.inject.javax.inject.source"/>
        <plugin id="wrapped.javax.inject.javax.inject"/>
        <plugin id="javax.annotation-api.source"/>
        <plugin id="javax.annotation-api"/>
        <plugin id="org.glassfish.hk2.locator.source"/>
        <plugin id="org.glassfish.hk2.locator"/>
        <plugin id="com.sun.mail.javax.mail.source"/>
        <plugin id="com.sun.mail.javax.mail"/>
        <plugin id="jaxb-api.source"/>
        <plugin id="jaxb-api"/>
        <plugin id="javax.persistence"/>
        <plugin id="javax.persistence.source"/>
        <plugin id="junit-platform-commons"/>
        <plugin id="junit-platform-commons.source"/>
        <plugin id="junit-platform-engine"/>
        <plugin id="junit-platform-engine.source"/>
        <plugin id="junit-platform-launcher"/>
        <plugin id="junit-platform-launcher.source"/>
        <plugin id="junit-vintage-engine"/>
        <plugin id="junit-vintage-engine.source"/>
        <plugin id="org.apache.logging.log4j.api.source"/>
        <plugin id="org.apache.logging.log4j.api"/>
        <plugin id="org.apache.logging.log4j.core.source"/>
        <plugin id="org.apache.logging.log4j.core"/>
        <plugin id="org.apache.logging.log4j.slf4j2.impl.source"/>
        <plugin id="org.apache.logging.log4j.slf4j2.impl"/>
        <plugin id="org.osgi.service.http.whiteboard"/>
        <plugin id="org.apache.commons.commons-fileupload"/>
        <plugin id="org.eclipse.equinox.http.jetty"/>
        <plugin id="org.eclipse.equinox.http.jetty.source"/>
        <plugin id="org.eclipse.equinox.http.registry"/>
        <plugin id="org.eclipse.equinox.http.registry.source"/>
        <plugin id="org.eclipse.equinox.http.service.api"/>
        <plugin id="org.eclipse.equinox.http.servlet"/>
        <plugin id="org.eclipse.equinox.http.servlet.source"/>
        <plugin id="org.eclipse.equinox.http.servletbridge"/>
        <plugin id="org.eclipse.equinox.servletbridge"/>
        <plugin id="org.eclipse.jetty.ee8.security"/>
        <plugin id="org.eclipse.jetty.ee8.server"/>
        <plugin id="org.eclipse.jetty.ee8.servlet"/>
        <plugin id="org.eclipse.jetty.ee8.servlet.source"/>
        <plugin id="org.eclipse.jetty.servlet-api"/>
        <plugin id="org.eclipse.osgi.services"/>
        <plugin id="org.eclipse.osgi.services.source"/>
        <plugin id="org.eclipse.osgi.util.source"/>
        <plugin id="org.eclipse.persistence.jpa.jpql"/>
        <plugin id="org.eclipse.persistence.jpa.jpql.source"/>
        <plugin id="mariadb-java-client.source"/>
        <plugin id="mariadb-java-client"/>
    </includeBundles>
    <targetJRE path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-22"/>
</target>


This setup is not the minimal required version, but it does provide a fully functional EE8 base, because this is provided by the RAP 3.29 software site. Removing the dependencies on org.eclipse.equinox.http.registry in the plugin.xml file of my bundles, and replacing them with OSGI Whiteboard annotations proved relatively simple (you can see in the target definition above that this bundle is still present in RAP 3.29), and after some experimenting I could also implement my REST endpoints with this mechanism:

Java
 
import java.util.logging.Logger;

import javax.servlet.Servlet;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.servlet.ServletContainer;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;

@Component(service = Servlet.class, 
  scope=ServiceScope.PROTOTYPE,
  property= "osgi.http.whiteboard.servlet.pattern=/" + RestServlet.S_CONTEXT_PATH + "/*")
public class RestServlet extends ServletContainer {
	private static final long serialVersionUID = -478694508797364227L;

	public static final String S_CONTEXT_PATH = "my-context-path";

	private Logger logger = Logger.getLogger(RestServlet.class.getName());

	public RestServlet() {
		super( new RestApplication());
		logger.info("STARTING SERVLET: " + this.getClass().getName()); 
	}
	
	private static class RestApplication extends ResourceConfig {

		public RestApplication() {
			super();
			property(ServerProperties.WADL_FEATURE_DISABLE, true);
			register( MyRestClass1.class );
			register( MyRestClass2.class);
            ...
		}
	}
}


The biggest challenge, however, was replacing Gemini JPA and DBAccess to secure the database connection to EclipseLink in an OSGi container. Because this problem exposes a fundamental challenge in the entire migration to Java EE 11, I decided to share my solution with the DZone community to support others facing the same issues.

EclipseLink and OSGI

To understand the fundamental problem in this migration, it helps to briefly recap what sets OSGi apart from other container architectures. From the start, OSGI was based on strict sandboxing policies that other container architectures don't necessarily follow. Both EclipseLink and Jakarta EE don't specifically follow this regime, which means the code tends to cause problems when used in an OSGi environment. This can be very frustrating, because, yes, the libraries are OSGI bundles — basically JAR files with a META-INF/MANIFEST-MF file attached in order to instruct the container how the JAR is used- but this does not address the strict sandboxing policy provided by OSGI. In OSGi, every functionality that the bundle offers is invisible to other bundles unless it is specifically made available, usually through a declarative service. 

As EclipseLink does not use this mechanism, a bundle implementing a database client, such as for MySQL, H2, or MariaDB, will not be visible to EclipseLink, even though it is included in the target definition. This can be frustrating because everything looks fine, yet the system keeps telling you something is wrong with the database connection. There are a few ways to address this problem; a common approach is to wrap the bundle in a custom one that includes the declarative services needed to expose the required functionality, such as a database driver.  Gemini JPA and DBaccess basically provide this service for EclipseLink. This approach, however, tends to become a maintenance hazard, as every updated version of the used bundle needs to be exposed in a corresponding wrapper bundle, so it is very hard to keep this up to date. 

Just to give the basic idea of what we need to achieve. EclipseLink scans for bundles on its classpath that have a persistence.xml file present (typically in the bundle's META-INF folder), which contains the necessary information about the datasource it should access. Most importantly, the file tells EclipseLink what database driver to look for.  If another bundle implements this driver, EclipseLink can load it and open a connection to the database. In an OSGI container, the strict sandboxing policy creates two problems;

  • EclipseLink cannot access the persistence.xml file 
  • EclipseLink cannot find a database client bundle

Initially, I thought I would tackle this problem by updating the Git repos of Gemini JPA and DBAccess myself, but when I noticed the potential maintenance hazard of especially DBAccess, I decided to rethink this strategy. Since this problem is also potentially present in the migration of javax code to jakarta code, I decided to take a different approach better geared toward the eventual goal of this migration effort, and to my surprise, it proved simpler than I thought!

The Solution

What I wanted to achieve was to facilitate the normal behavior of EclipseLink as much as possible. This meant trying to expose the persistence.xml file to EclipseLink with as few modifications as possible, in order to prepare for the migration of javax.persistence to jakarta.persistence, which is going to affect the structure of this file. 

Secondly, I needed to expose the database client bundle (using MariaDB in this case) to EclipseLink in such a way that the solution could also be used for other clients, without any hard-coded references to them. Ideally, the bridge would get all the necessary information from the persistence.xml file and facilitate EclipseLink's normal discovery operation. The eventual bridge could be implemented as an abstract class that I could add to my bundle with core Java utilities:

Java
 
package org.mybundle.core.persistence

import java.io.Closeable;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

import javax.persistence.EntityManagerFactory;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.sessions.DirectConnector;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.wiring.BundleWiring;

public class AbstractEntityManagerFactoryService implements Closeable{

	public static final String S_PERSISTENCE_LOCATION = "META-INF/persistence.xml";
	public static final String S_TARGET_DATABASE = "MySQL";
	
	public static final String S_INFO_ENTITYMANAGER_CREATED = "EntityManagerFactory created";
	public static final String S_INFO_MANAGED_ENTITY = "Managed entity: ";
	
	public static final String S_ERR_LOCATION_NOT_VISIBLE ="persistence.xml not visible to class loader";
	public static final String S_ERR_LOCATION_NOT_FOUND =  "Persistence unit myPU not found";
	
    private String persistenceUnitName;
    
	private EntityManagerFactory emf;

    private Logger logger = Logger.getLogger( this.getClass().getName() );
    
    protected AbstractEntityManagerFactoryService(String persistenceUnitName) {
		super();
		this.persistenceUnitName = persistenceUnitName;
	}

	public EntityManagerFactory getFactory() {
		return emf;
	}

	/**
	 * this method reads the persistence.xml provided by the bundle, and parses this. then it checks for corresponding
	 * implementation of the databases driver (e.g mysql, mariadb or h2) and tries to offer this to the EclipseLink classloader
	 * so that the regular service discovery can find it. 
	 * 
	 * @param context
	 * @throws Exception
	 */
	protected void activate(BundleContext context) throws Exception {
        Bundle bundle = context.getBundle();

        // Check 1: Is persistence.xml inside the bundle?
        URL entry = bundle.getEntry( S_PERSISTENCE_LOCATION);
        logger.info("Bundle resource: " + entry);

        // Check 2: Can the bundle class loader discover it?
        ClassLoader loader = bundle.adapt(
            BundleWiring.class).getClassLoader();

        Enumeration<URL> resources =
            loader.getResources( S_PERSISTENCE_LOCATION );

        int count = 0;
        while (resources.hasMoreElements()) {
        	logger.info("Persistence descriptor: " + resources.nextElement());
            count++;
        }

        if (count == 0) 
            throw new IllegalStateException( S_ERR_LOCATION_NOT_VISIBLE);

        Properties jdbcProperties = PersistencePropertiesHandler.readPersistenceProperties(entry, persistenceUnitName);
        String driverName = jdbcProperties.getProperty( PersistenceUnitProperties.JDBC_DRIVER); 
        emf = OsgiJdbcClassLoader.createFactory(context, driverName, persistenceUnitName, jdbcProperties);
    }

    public void close() {
        if (emf != null) {
            emf.close();
            emf = null;
        }
    }
}


The neat trick is mainly carried out after Check 3. First we read the  persistence.xml file in order to extract the properties:

Java
 
package org.mybundle.persistence;

import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Logger;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class PersistencePropertiesHandler {

	private static final String S_ERR_XML_MISSING =
		    "Persistence descriptor not found";

		private static final String S_ERR_UNIT_MISSING =
		    "Persistence unit not found: ";

		private static final String S_INFO_PROPERTIES =
		    "Persistence properties loaded for: ";

	private static Logger logger = Logger.getLogger(PersistencePropertiesHandler.class.getName());

	public static Properties readPersistenceProperties(
	        URL descriptor,
	        String unitName) throws Exception {

	    if (descriptor == null) {
	        throw new IllegalStateException(S_ERR_XML_MISSING);
	    }

	    DocumentBuilderFactory factory =
	        DocumentBuilderFactory.newInstance();

	    factory.setNamespaceAware(true);
	    factory.setFeature(
	        XMLConstants.FEATURE_SECURE_PROCESSING, true);
	    factory.setFeature(
	        "http://apache.org/xml/features/disallow-doctype-decl",
	        true);
	    factory.setXIncludeAware(false);
	    factory.setExpandEntityReferences(false);

	    try (InputStream input = descriptor.openStream()) {
	        Document document =
	            factory.newDocumentBuilder().parse(input);
	        NodeList units = document.getElementsByTagNameNS(
	            "*", "persistence-unit");
	        for (int i = 0; i < units.getLength(); i++) {

	            Element unit = (Element) units.item(i);

	            if (!unitName.equals(unit.getAttribute("name"))) {
	                continue;
	            }

	            Properties properties = new Properties();
	            NodeList entries =
	                unit.getElementsByTagNameNS("*", "property");
	            for (int j = 0; j < entries.getLength(); j++) {

	                Element property = (Element) entries.item(j);

	                properties.setProperty(
	                    property.getAttribute("name"),
	                    property.getAttribute("value")
	                );
	            }
	            logger.info(S_INFO_PROPERTIES + unitName);
	            return properties;
	        }
	    }
	    throw new IllegalStateException(
	        S_ERR_UNIT_MISSING + unitName);
	}
}


Then the properties of the  persistence.xml file are offered to a ClassLoaderFactory that tries to add the bundle of the database client to the classpath of EclipseLink:

Java
 
package org.mybundle.core.persistence;

import java.sql.Driver;
import java.util.Objects;
import java.util.Properties;
import java.util.logging.Logger;

import javax.persistence.EntityManagerFactory;

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.jpa.PersistenceProvider;
import org.eclipse.persistence.sessions.DirectConnector;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.wiring.BundleWiring;

public final class OsgiJdbcClassLoader extends ClassLoader {

	private static final String S_INFO_DRIVER_LOADED = "The requested driver was successfully loaded: ";

    private static final String S_ERR_DRIVER_BUNDLE_NOT_FOUND = "The requested driver bundle wasn't found: ";

	private final Bundle driverBundle;
    private final String driverClassName;

    private static Logger logger = Logger.getLogger( OsgiJdbcClassLoader.class.getName() );
    
    public OsgiJdbcClassLoader( ClassLoader parent, Bundle driverBundle, String driverClassName) {
        super(parent);
        this.driverBundle =
            Objects.requireNonNull(driverBundle);

        this.driverClassName =
            Objects.requireNonNull(driverClassName);
    }

    @Override
    protected Class<?> findClass(String name)
            throws ClassNotFoundException {

        if (driverClassName.equals(name)) {
            return driverBundle.loadClass(name);
        }

        throw new ClassNotFoundException(name);
    }
    
    public static EntityManagerFactory createFactory(BundleContext context, String driverName, String persistenceUnitName, Properties properties ){
    	DatasourceInspector inspector = new DatasourceInspector();
    	ClassLoader combinedLoader = null;
    	EntityManagerFactory emf = null;
    	try {

    		Driver driver = inspector.findDriver(context, driverName);
    		Bundle driverBundle =
    				FrameworkUtil.getBundle(driver.getClass());
    		if (driverBundle == null)
    			throw new IllegalStateException( S_ERR_DRIVER_BUNDLE_NOT_FOUND + driverName );

    		//Retrieve the parent classloader
    		Bundle bundle = context.getBundle();
    		ClassLoader loader = bundle.adapt(BundleWiring.class).getClassLoader();
    		combinedLoader = new OsgiJdbcClassLoader( loader, driverBundle, driverName );

    		// Verify the bridge before starting EclipseLink.
    		Class<?> resolvedDriver;
    		resolvedDriver = Class.forName( driverName, true, combinedLoader);
    		logger.info(S_INFO_DRIVER_LOADED + resolvedDriver.getName());

    	} catch (ReflectiveOperationException e) {
    		e.printStackTrace();
    		return emf;
    	}

    	// Pass the combined loader to EclipseLink.
    	properties.put( PersistenceUnitProperties.CLASSLOADER, combinedLoader );
    	// Tell EclipseLink to bypass DriverManager
    	properties.put( PersistenceUnitProperties.JDBC_CONNECTOR, DirectConnector.class.getName());
 
    	Thread thread = Thread.currentThread();
    	ClassLoader previous = thread.getContextClassLoader();

    	try {
    	    thread.setContextClassLoader(combinedLoader);

    	    emf = new PersistenceProvider()
    	        .createEntityManagerFactory(
    	            persistenceUnitName,
    	            properties
    	        );

    	} finally {
    	    thread.setContextClassLoader(previous);
    	}    	
    	return emf;
    }
}


Here, we use an option provided by EclipseLink to use a custom classloader, which bypasses the default restrictions of OSGI. If a database driver is found, it will be present in the loader that we have just made and is offered to the classpath of EclipseLink. We need to overwrite two properties of the persistence.xml file in order to achieve this, but unless you have very specific requirements, this should not be a problem. You can just provide the structure that is recommended by the specifications, regardless of whether it's from OSGI or Jakarta.

Lastly, a utility class is provided to assist in the workflow:

Java
 
package org.mybundle.persistence;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Properties;
import java.util.logging.Logger;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.jdbc.DataSourceFactory;

public class DatasourceInspector {

	private static final String S_INFO_JDBC_FACTORY = "Available JDBC factory: ";

	private static final String S_INFO_DRIVER_FOUND =
			"Found JDBC driver in bundle: ";

	private static final String S_ERR_DRIVER_NOT_FOUND =
			"JDBC driver not found: ";

	private static final String S_INFO_CONNECTED =
			"JDBC connection established with: ";

	private static final String S_ERR_CONNECTION =
			"JDBC driver did not accept the connection URL";


	private Logger logger = Logger.getLogger(DatasourceInspector.class.getName());


	/**
	 * First find all the JDBC factories that are registered 
	 * @param context
	 * @throws InvalidSyntaxException
	 */
	public void inspectJdbcFactories(BundleContext context)
			throws InvalidSyntaxException {

		Collection<ServiceReference<DataSourceFactory>> refs =
				context.getServiceReferences(
						DataSourceFactory.class, null);

		if ((refs == null) || ( refs.size() == 0)) {
			logger.info("No JDBC factories registered");
			return;
		}

		for (ServiceReference<DataSourceFactory> ref : refs) {
			Object driver = ref.getProperty(
					DataSourceFactory.OSGI_JDBC_DRIVER_CLASS);

			logger.info(S_INFO_JDBC_FACTORY + driver);
		}
	}

	/**
	 * Then find the JDBC driver with the given name 
	 * @param context
	 * @param driverName
	 * @return
	 * @throws ReflectiveOperationException
	 */
	public Driver findDriver(
			BundleContext context,
			String driverName)
					throws ReflectiveOperationException {

		for (Bundle bundle : context.getBundles()) {

			if (bundle.getState() == Bundle.UNINSTALLED) 
				continue;

			try {
				Class<?> type = bundle.loadClass(driverName);
				if (!Driver.class.isAssignableFrom(type))
					continue;

				Bundle owner = FrameworkUtil.getBundle(type);
				if (owner != null && !owner.equals(bundle))
					continue;

				logger.info(S_INFO_DRIVER_FOUND + bundle.getSymbolicName()	);
				return (Driver) type
						.getDeclaredConstructor()
						.newInstance();

			} catch (ClassNotFoundException ex) {
				//logger.warning( ex.getMessage());
			}
		}

		throw new IllegalStateException( S_ERR_DRIVER_NOT_FOUND + driverName );
	}

	/**
	 * Test the connection with the data source
	 * @param driver
	 * @param url
	 * @param jdbcProperties
	 * @throws SQLException
	 */
	public void testConnection(
			Driver driver,
			String url,
			Properties jdbcProperties) throws SQLException {

		try (Connection connection =
				driver.connect(url, jdbcProperties)) {

			if (connection == null)
				throw new SQLException(S_ERR_CONNECTION);

			logger.info( S_INFO_CONNECTED +	connection.getMetaData().getDatabaseProductName());
		}
	}
}


Lastly, we can now use the abstract class to gain access to the database in the bundle that provides the persistence.xml file, using a simple Component:

Java
 
package com.myorganisation.myservice;

import javax.persistence.EntityManagerFactory;

import org.mybundle.persistence.AbstractEntityManagerFactoryService;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;

/**
 * Create the entity manager factory
 * @author Kees
 *
 */
@Component(immediate = true)
public class EntityManagerService extends AbstractEntityManagerFactoryService{

	public static final String BUNDLE_ID = "com.condast.indymo.authentication";
	
	//Passes the entitymanager to other classes
    Dispatcher service = Dispatcher.getInstance();

	public EntityManagerService() {
		super( BUNDLE_ID );
	}

	@Activate
	public void activate( BundleContext context ) throws Exception {
		super.activate( context );
	    EntityManagerFactory emf = super.getFactory();
	    service.setEMF(emf);
	}

	@Deactivate
	public synchronized void close() {
		super.close();
		service.disconnect();
	}
}


Conclusion

Although this approach has currently only been tested with MariaDB, I'm pretty sure it should also work with other database clients. It offers a lightweight solution that lets EclipseLink access database clients in an OSGi environment, bypassing the original solution offered by Gemini JPA and DBAccess. Moreover, it should already be prepared for the next step of the migration to jakarta.persistence.

EclipseLink JAR (file format) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Using SQS With JMS for Legacy Applications
  • Javac and Java Katas, Part 2: Module Path
  • Javac and Java Katas, Part 1: Class Path
  • Ways To Reduce JVM Docker Image Size

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook