7 Replies Latest reply on May 21, 2004 7:44 AM by johanwa

    Dynamically accessing EJB at runtime

    johanwa

      I'm working on EJB based Application resources from struts and heva the following problem;
      I'd want to pass the jndiName and path in as text var's and the instantiate my object with that.

      I will mail my full code listing and results on request.

      I'm trying this:
      -------------------
      Context ctx = new InitialContext(env);
      Object obj = ctx.lookup( "acme/Application" );
      ...
      Class bcHome = Class.forName("app.resources.ApplicationHome");
      ...
      Object boHome;
      boHome = PortableRemoteObject.narrow(obj, bcHome); //<-LINE 35
      ...

      And get the following:
      ------------------------------
      java.lang.InstantiationException: app.resources.Application
      at java.lang.Class.newInstance0(Unknown Source)
      at java.lang.Class.newInstance(Unknown Source)
      at CallResourcesEjb.main(CallResourcesEjb.java:35)

        • 1. Re: Dynamically accessing EJB at runtime

          Moved to the users forum along with your other post. Can you not read the warning
          at the top of the forum?

          Anyway, this was fixed in 3.2.4RC1 - something you would have discovered had you
          used search.

          • 2. Re: Dynamically accessing EJB at runtime
            johanwa

            Thanks for the reply (about the fix).

            Sorry to be such a bug in your obviously overstressed life. I am new to JBoss and to the forum (any forum for that matter), so once again, sorry for posting in the wrong place or not searching first. But I'm not trying to find excuses, I'm overworked to, so I blame myself for being such a dic, and bringing that out in you.

            • 3. Re: Dynamically accessing EJB at runtime

              Actually I posted on the wrong thread (a slip of the mouse :-)
              You are in the correct forum, and my response is meant for somebody else.

              • 4. Re: Dynamically accessing EJB at runtime

                Your problem is here:
                at CallResourcesEjb.main(CallResourcesEjb.java:35)

                Where you are trying to do Class.newInstance() on an interface.

                • 5. Re: Dynamically accessing EJB at runtime
                  johanwa

                  Phew, I was getting a bit nervous on that comment, I'm not all that self asured. But thanks for the nice reply and sorry for my comments.

                  Code:

                  Context ctx = new InitialContext(env);
                  Object obj = ctx.lookup( "acme/Application" );
                  ApplicationHome home = (ApplicationHome)PortableRemoteObject.narrow(obj, ApplicationHome.class);
                  Application app = (Application)home.findBaseKey("logon.label");
                  System.out.println("1. logon.label : " + app.getValue());
                  app = (Application)home.findKeyByLocale("en_ZA", "logon.label");
                  System.out.println("2. logon.label : " + app.getValue());Class bcHome = Class.forName("app.resources.ApplicationHome");
                  Method findBaseKey = bcHome.getMethod("findBaseKey", new Class[] { String.class });
                  Class bClass = Class.forName("app.resources.Application");
                  Method getValue = bClass.getMethod("getValue", null);
                  Object boHome; // = bcHome.newInstance();
                  Object bObject = bClass.newInstance();
                  boHome = PortableRemoteObject.narrow(obj, bcHome); <-LINE 35
                  Object[] baseKeyVal = null;
                  baseKeyVal[0] = "logon.label";
                  bObject = findBaseKey.invoke(boHome, baseKeyVal);
                  System.out.println("3. logon.label : " + getValue.invoke(bObject, null));

                  And I get:
                  1. logon.label : Please log on.
                  2. logon.label : Kom gerus binne
                  Exception: app.resources.Application
                  java.lang.InstantiationException: app.resources.Application
                  at java.lang.Class.newInstance0(Unknown Source)
                  at java.lang.Class.newInstance(Unknown Source)
                  at CallResourcesEjb.main(CallResourcesEjb.java:35)


                  Thanks for the help, I'm quite new to Java and EJB's.

                  Appreciated.

                  • 6. Re: Dynamically accessing EJB at runtime

                    If it really is line 35, your source code does not match your class file.

                    This is the line in error:
                    Object bObject = bClass.newInstance();

                    Application (bClass) is an interface.

                    • 7. Re: Dynamically accessing EJB at runtime
                      johanwa

                      You clever dog you! Thanks a mil, it works now. Struts can now be EJB enabled thanks to JBoss and the JBoss team. I'll send the complete solution on to the struts team when I'm done, maybe they'll want to publish it.

                      Thanks again.