Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6946503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:34:25+00:00 2026-05-27T13:34:25+00:00

I have an application which is part JavaEE (the server side) part JavaSE (the

  • 0

I have an application which is part JavaEE (the server side) part JavaSE (the client side). As I want that client to be well architectured, I use Weld in it to inject various components. Some of these components should be server-side @EJB.

What I plan to do is to extend Weld architecture to provide the “component” allowing Weld to perform JNDI lookup to load instances of EJBs when client tries to reference them. But how do I do that ?

In other worrds, I want to have

on the client side

public class ClientCode {
    public @Inject @EJB MyEJBInterface;
}

on the server-side

@Stateless
public class MyEJB implements MyEJBInterface {
}

With Weld “implicitely” performing the JNDI lookup when ClientCode objects are created. How can I do that ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:34:25+00:00Added an answer on May 27, 2026 at 1:34 pm

    Basically, doing so requires write a so-called portable CDI extension.

    But, as it is quite long and requires a few tweaks, let me explain it further.

    Portable extension

    Like weld doc explains, first step is to create a class that implements the Extension tagging interface, in which one will write code corresponding to interesting CDI events. In that precise case, the most interesting event is, to my mind, AfterBeanDiscovery. Indeed, this event occurs after all “local” beans have been found by CDI impl.

    So, writing extension is, more opr less, writing a handler for that event :

    public void loadJndiBeansFromServer(
            @Observes AfterBeanDiscovery beanDiscovery, BeanManager beanManager)
            throws NamingException, ClassNotFoundException, IOException {
        // Due to my inability to navigate in server JNDI naming (a weird issue in Glassfish naming)
        // This props maps interface class to JNDI name for its server-side
        Properties interfacesToNames = extractInterfacesToNames();
    
        // JNDI properties
        Properties jndiProperties = new Properties();
        Context context = new InitialContext();
        for (Entry<?, ?> entry : interfacesToNames.entrySet()) {
            String interfaceName = entry.getKey().toString();
            Class<?> interfaceClass = Class.forName(interfaceName);
            String jndiName = entry.getValue().toString();
            Bean<?> jndiBean = createJndIBeanFor(beanManager, interfaceClass, jndiName, jndiProperties);
            beanDiscovery.addBean(jndiBean);
        }
    }
    

    Creating the bean is not a trivial operation : it requires transforming “basic” Java reflection objects into more advanced weld ones (well, in my case)

    private <Type> Bean<Type> createJndIBeanFor(BeanManager beanManager, Class<Type> interfaceClass,
            String jndiName, Properties p) {
        AnnotatedType<Type> annotatedType = beanManager
                .createAnnotatedType(interfaceClass);
        // Creating injection target in a classical way will fail, as interfaceClass is the interface of an EJB
        JndiBean<Type> beanToAdd = new JndiBean<Type>(interfaceClass, jndiName, p);
        return beanToAdd;
    }
    

    Finally, one has to write the JndiBean class. But before, a small travel in annotations realm is required.

    Defining the used annotation

    At first, I used the @EJB one. A bad idea : Weld uses qualifier annotation method calls result to build hashcode of bean ! So, I created my very own @JndiClient annotation, which holds no methods, neither constants, in order for it to be as simple as possible.

    Constructing a JNDI client bean

    Two notions merge here.

    • On one side, the Bean interface seems (to me) to define what the bean is.
    • On the other side, the InjectionTarget defines, to a certain extend, the lifecycle of that very bean.

    From the literature I was able to find, those two interfaces implementations often share at least some of their state. So I’ve decided to impelment them using a unique class : the JndiBean !

    In that bean, most of the methods are left empty (or to a default value) excepted

    • Bean#getTypes, which must return the EJB remote interface and all extended @Remote interfaces (as methods from these interfaces can be called through this interface)
    • Bean#getQualifiers which returns a Set containing only one element : an AnnotationLiteral corresponding to @JndiClient interface.
    • Contextual#create (you forgot Bean extended Contextual, didn’t you ?) which performs the lookup :

      @Override
      public T create(CreationalContext<T> arg0) {
          // Some classloading confusion occurs here in my case, but I guess they're of no interest to you
      
          try {
              Hashtable contextProps = new Hashtable();
              contextProps.putAll(jndiProperties);
              Context context = new InitialContext(contextProps);
              Object serverSide = context.lookup(jndiName);
              return interfaceClass.cast(serverSide);
          } catch (NamingException e) {
              // An unchecked exception to go through weld and break the world appart
              throw new LookupFailed(e);
          }
      }
      

    And that’s all

    Usage ?

    Well, now, in my glassfish java client code, I can write things such as

    private @Inject @JndiClient MyRemoteEJB instance;
    

    And it works without any problems

    A future ?

    Well, for now, user credentials are not managed, but I guess it could be totally possible using the C of CDI : Contexts … oh no ! Not contexts : scopes !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application with redis part which I need to debug. I want
I have an application which needs to use Eastern Time for a certain part
I have application which needs to use a dll (also written by me) which
We have an application which needs to use Direct3D. Specifically, it needs at least
I have an application which is a portal application and I want to allow
I have an application which get copied and run on client machines. The program
I have a C# application which sometimes cost the CPU very high. I want
We have a .NET application which, on a certain server, keeps making various TCP
I have an application which has a loop, part of a Scheduler, which runs
I have an ASP.NET MVC 2 application which in part allows a user to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.