Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support external config file for eclipselink #88

Merged
merged 1 commit into from
Aug 7, 2024

Conversation

aihuaxu
Copy link
Contributor

@aihuaxu aihuaxu commented Aug 5, 2024

Description

Due to the limitation of eclipselink which requires the config file to be a resource inside a jar so a config file like /tmp/persistence.xml can't be supported directly. Persistence.createEntityManagerFactory() implementation searches the configuration file, checks if it's an archive (zip or jar file) and fails if it's not.

To workaround the issue, the PR supports the external configuration file format /tmp/conf.jar!/persistence.xml, adds /tmp/conf.jar in the classpath and then load the configuration. Here are the steps:

  1. create a jar file from persistence.xml through jar -cf /tmp/conf.jar ./persistence.xml
  2. configure eclipselink in polaris-server.yml
       metaStoreManager:
            type: eclipse-link
             persistence-unit: polaris
             conf-file: /tmp/conf.jar!/persistence.xml

The extra step is to run is jar -cf /tmp/conf.jar ./persistence.xml after updating the file.

Also updated persistence_unit name that the one for test should be polaris-dev and the default one for production should be polaris in persistence.xml.

Fixes #45

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • By generating a conf.jar, run test PolarisEclipseLinkMetaStoreTest.java with confFile =/tmp/ conf.jar!/persistence.xml
  • Add test coverage in PolarisEclipseLinkMetaStoreTest.java

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

Please delete options that are not relevant.

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • If adding new functionality, I have discussed my implementation with the community using the linked GitHub issue
  • I have signed and submitted the ICLA and if needed, the CCLA. See Contributing for details.

@aihuaxu aihuaxu requested a review from a team as a code owner August 5, 2024 00:45
@eric-maynard
Copy link
Contributor

If we can't support persistence.xml files outside of a JAR on the classpath, I'm skeptical as to whether we should do this. It seems like this is asking users to have a pretty strange configuration

@sfc-gh-aixu
Copy link
Contributor

If we can't support persistence.xml files outside of a JAR on the classpath, I'm skeptical as to whether we should do this. It seems like this is asking users to have a pretty strange configuration

Yeah. It's awkward to do such configuration and it can only be used for workaround. One more improvement we can do is to dynamically add the jar to classpath so the user doesn't need to do that.

InputStream input = null;
int splitPosition = confFile.indexOf("!/");
if (splitPosition == -1) {
input = this.getClass().getClassLoader().getResourceAsStream(confFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically, I use Thread.currentThread().contextClassLoader() so that if the plugin jar happens to be loaded by a child classloader, rather than this one, we can still find it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I made the change to create a new ClassLoader with the conf jar and I'm using Thread.currentThread().contextClassLoader() now.

Comment on lines 135 to 140
String jarPrefixPath = confFile.substring(0, splitPosition);
String descPath = confFile.substring(splitPosition + 2);
URL prefixUrl = this.getClass().getClassLoader().getResource(jarPrefixPath);
URLClassLoader child =
new URLClassLoader(new URL[] {prefixUrl}, this.getClass().getClassLoader());
input = child.getResourceAsStream(descPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I don't understand what's going on. This InputStream is not being used to setup eclipselink? Cause this InputStream could be coming from anywhere. Why is there a restriction that the resource is on the classpath?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used to parse it, but then EclispeLink again tries to load the resource directly when we call Persistence.createEntityManagerFactory

Copy link
Contributor

@sfc-gh-aixu sfc-gh-aixu Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since each realm has its only database, I have the following configuration in persistence.xml. I read this configuration and replace the realm then pass it to Persistence.createEntityManagerFactory() which will again read persistence.xml for the rest of the configurations. Persistence.createEntityManagerFactory() assumes the persistence.xml is a resource file inside a jar.

<property name="jakarta.persistence.jdbc.url" value="jdbc:h2:file:/tmp/eclipseLink/{realm}"/>

    Due to the limitation of eclipselink, the config file has to be a resource inside a jar so a config file like /tmp/persistence.xml can't be supported directly.
    To workaround the issue,
    1. create a jar file from persistence.xml through 'jar /tmp/cf conf.jar ./persistence.xml'
    3. configure eclipselink in polaris-server.yml
           metaStoreManager:
             type: eclipse-link
             persistence-unit: polaris
             conf-file: /tmp/conf.jar!/persistence.xml
@aihuaxu aihuaxu force-pushed the aixu-external-eclipselink-conf-file branch from 19667fe to 023bb41 Compare August 6, 2024 15:38
@guitcastro
Copy link

I was able to fix this in #95 .

Copy link
Contributor

@collado-mike collado-mike left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, hacky but... it works I guess

}
URLClassLoader currentClassLoader =
new URLClassLoader(new URL[] {prefixUrl}, this.getClass().getClassLoader());
Thread.currentThread().setContextClassLoader(currentClassLoader);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a logging statement here stating that we're changing the classloader temporarily

Comment on lines +128 to +133
String jarPrefixPath = confFile.substring(0, splitPosition);
confFile = confFile.substring(splitPosition + 2);
URL prefixUrl = this.getClass().getClassLoader().getResource(jarPrefixPath);
if (prefixUrl == null) {
prefixUrl = new File(jarPrefixPath).toURI().toURL();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add logs here specifying the jar we found and how we're altering the classpath

Copy link
Contributor Author

@aihuaxu aihuaxu Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't see the comments before merging. Let me add that in a follow up.

Comment on lines +149 to +151
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch RuntimeException separately and just rethrow

String.format(
"Cannot find or parse the configuration file %s for persistence-unit %s",
confFile, persistenceUnitName);
LOG.error(str);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log the original exception so we can see the stacktrace

@aihuaxu aihuaxu merged commit 4af03b6 into apache:main Aug 7, 2024
3 checks passed
@snazy
Copy link
Member

snazy commented Aug 7, 2024

@aihuaxu @collado-mike I think, we need a follow-up of this one, because it adds binary content (a jar file).
We can generate a jar in a temp directory - but having binaries in Git isn't great.

@sfc-gh-aixu
Copy link
Contributor

@aihuaxu @collado-mike I think, we need a follow-up of this one, because it adds binary content (a jar file). We can generate a jar in a temp directory - but having binaries in Git isn't great.

Ack. Let me think of a way to address that since that is only used for testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DOCUMENTATION]: missing documentation on how to load conf-file from other locations
7 participants