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

fail early in RInChIGeneratorFactory when there is an issue with the native RInChI lib? #19

Open
uli-f opened this issue Oct 24, 2022 · 3 comments

Comments

@uli-f
Copy link

uli-f commented Oct 24, 2022

I left a TODO for us in RInChIGeneratorFactory.

RInChIGeneratorFactory declared a CDKException in its private constructor and stated in the documentation that this exception is thrown when the native library cannot be loaded. However, the native library wasn't loaded when RInChIGeneratorFactory was instantiated.

InChIGeneratorFactory does the same thing: declaring a CDKException in its private constructor and stating in the documentation that this exception is thrown when the native library cannot be loaded. So my guess is that this is where this originates from.

The code in RInChIGeneratorFactory is now changed due to my refactoring of the singleton pattern. However, the question remains: Shouldn't RInChIGeneratorFactory fail as early as possible if there is an issue with the loading of the native library?

The following code loads the native library by explicitly loading the class io.github.dan2097.jnarinchi.JnaRinchi thereby making sure that there is an exception as soon as the the method getInstance() of RInChIGeneratorFactory is called:

private static class SingletonInstanceHolder {
  public final static RInChIGeneratorFactory INSTANCE;

  static {
    INSTANCE = new RInChIGeneratorFactory();
    // TODO do we want to load the rinchi native library at this point so that we get an exception early on?
    try {
      RInChIGeneratorFactory.class.getClassLoader().loadClass("io.github.dan2097.jnarinchi.JnaRinchi");
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
  }
}

However, the main drawback is that the thrown exception must be a RuntimeException (or subclass thereof) as this happens in a static block.

@ntk73 Any thoughts?

@vedina
Copy link
Member

vedina commented Nov 11, 2022

@uli-f I agree with your proposal, it's better to fail early. I am not sure why RuntimeException should be a concern, we are not implementing interface requiring CDKException to worry about it.

Tests might need to be updated to throw RuntimeException .

@uli-f
Copy link
Author

uli-f commented Nov 11, 2022

Okay. I made a PR for this change: PR #21

@ntk73 Would you please merge this one as well? Thanks.

Whether to fail early - and if so, how - might be something that I will discuss with the CDK developers once we open a PR to merge the rinchi code.

As a reminder for myself I would like to leave this open for now.

@uli-f uli-f changed the title TODO in RInChIGeneratorFactory fail early in RInChIGeneratorFactory when there is an issue with the native RInChI lib? Nov 11, 2022
@ntk73
Copy link

ntk73 commented Nov 11, 2022

PR #21 is already merged.

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

No branches or pull requests

3 participants