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

How to modify java:comp/BeanManager to java:comp/env/BeanManager? #74

Closed
iwangxiaodong opened this issue Oct 12, 2017 · 13 comments
Closed
Assignees
Labels

Comments

@iwangxiaodong
Copy link

InitialContext.doLookup("java:comp/BeanManager") in org.mybatis.cdi.CDIUtils.getBeanManager()

Jetty only allows you to bind entries to java:comp/env, so the BeanManager will be available at java:comp/env/BeanManager

How to modify java:comp/BeanManager to java:comp/env/BeanManager?

Thanks!

@hazendaz
Copy link
Member

Good catch! This would affect tomcat too as its jndi is read only as well. So bean manager is going to be in the location you noted above. I'd say drop back to 1.0.0 release for time being.

@iwangxiaodong
Copy link
Author

I copied CDIUtils.java and modified it as follows:

private static BeanManager getBeanManager() {
    try {
        return InitialContext.doLookup("java:comp/BeanManager");
    } catch (NamingException e) {
        try {
            return InitialContext.doLookup("java:comp/env/BeanManager");
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }
    }
}

It works fine!

@hazendaz
Copy link
Member

@iwangxiaodong Can you kindly send a PR for this. Thanks. I've used this exact same thing for JNDI many times in the past. While not the most efficient, given it hits multiple container types it's probably the easiest.

@mnesarco
Copy link
Member

@iwangxiaodong @hazendaz It would be nice if we can cache at least the jndi name. but maybe it requires some synchronization. I think throwing exceptions and catching them every time this method is called is a lot of overhead. We have to decide what is less bad: synchronization vs try/catch. Or maybe we can statically do a lookup when the class is loaded and save the correct jndi name, so we will not require sync or try/catch inside the method.

@mnesarco
Copy link
Member

@iwangxiaodong @hazendaz What do you think about something like this:

  private static final String DEFAULT_JNDI_NAME = "java:comp/BeanManager";
  private static final AtomicBoolean INITIALIZED = new AtomicBoolean(false);
  private static String jndiName = DEFAULT_JNDI_NAME; 

  private static void init() {
    if (!INITIALIZED.getAndSet(true)) {
      synchronized (DEFAULT_JNDI_NAME) {
        try {
          if (InitialContext.doLookup("java:comp/env/BeanManager") != null) {
            jndiName = "java:comp/env/BeanManager";
          }
        } catch (NamingException e) {
          // Fallback to default, do nothing
        }
      }
    }
  }

  private static BeanManager getBeanManager() {
    init();
    try {
      return InitialContext.doLookup(jndiName);
    } catch (NamingException e) {
      throw new RuntimeException(e);
    }
  }

@mnesarco
Copy link
Member

@iwangxiaodong @hazendaz Or even better:

public final class CDIUtils {

  private static class JNDI {

    private static final String DEFAULT_JNDI_NAME = "java:comp/BeanManager";
    static final String NAME;

    static {
      String jndiName = DEFAULT_JNDI_NAME;
      try {
        if (InitialContext.doLookup("java:comp/env/BeanManager") != null) {
          jndiName = "java:comp/env/BeanManager";
        }
      } catch (NamingException e) {
        // Fallback to default, do nothing
      }
      NAME = jndiName;
    }

  }

  private static BeanManager getBeanManager() {
    try {
      return InitialContext.doLookup(JNDI.NAME);
    } catch (NamingException e) {
      throw new RuntimeException(e);
    }
  }


@iwangxiaodong
Copy link
Author

@mnesarco @hazendaz This code is very good performance!

@mnesarco mnesarco added the bug label Oct 12, 2017
@mnesarco mnesarco self-assigned this Oct 12, 2017
@mnesarco
Copy link
Member

@iwangxiaodong Thanks for the catch. I will submit the patch just now.

@iwangxiaodong
Copy link
Author

@mnesarco Yeah! Nice!

@hazendaz
Copy link
Member

hazendaz commented Oct 12, 2017 via email

@hazendaz
Copy link
Member

@iwangxiaodong @mnesarco I have released version 1.0.2 with this change. It should show in central in about 2 hours but is otherwise available for download now.

@mnesarco
Copy link
Member

mnesarco commented Oct 14, 2017 via email

@iwangxiaodong
Copy link
Author

@hazendaz

compile 'org.mybatis:mybatis-cdi:1.0.2'

I tested OK!

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

No branches or pull requests

3 participants