5

I am learning basics of EJB 3.0. I have managed to get a sample code up and running. Now I am doing a line by line analysis to have in-depth knowledge. But I am stuck at few lines where there is a lookup to find the required bean.

Can anyone please explain me in simple language the meaning and the need of the following lines?

Properties properties = new Properties();
properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs", "org.jboss.naming rg.jnp.interfaces");
properties.setProperty(Context.PROVIDER_URL, "localhost:1099");

IniialContext context = null;
SamleEjbRemote cl = null;
try {
    context = new InitialContext(properties);
    cl = (SampleEjbRemote) context.lookup("SampleEjbBean/remote");
} catch (NamingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}


What is the exact meaning of each of the 'key' and 'value' that is used in properties?

Rest of it is to put the 'properties' in the initial context instance. I have had a very vague idea of the above, but I want to clarify it very clearly. I would be glad if anyone could point me to any links or insights about the above lines.

Thanks in advance.

informatik01
  • 16,038
  • 10
  • 74
  • 104
h-kach
  • 351
  • 2
  • 8
  • 25

1 Answers1

7

Both properties configures JBoss JNDI HTTP InitialContext Factory Implementation

Official document here : http://docs.jboss.org/jbossas/jboss4guide/r1/html/ch3.chapter.html

See chapter 3.2.1.2. The HTTP InitialContext Factory Implementation

java.naming.factory.initial: The name of the environment property for specifying the initial context factory, which must be org.jboss.naming.HttpNamingContextFactory.

java.naming.factory.url.pkgs: For all JBoss JNDI provider this must be org.jboss.naming:org.jnp.interfaces. This property is essential for locating the jnp: and java: URL context factories of the JBoss JNDI provider.

UPDATE:

I would recommend to use jndi.properties file in your class path

### JBossNS properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
rkosegi
  • 14,165
  • 5
  • 50
  • 83
  • Currently I am behind firewall and the site is blocked! Thanks for reply. But can I get better explanation as to why it is used? – h-kach Mar 30 '12 at 05:39
  • This is funny, URL is official RedHat Jboss documentation.I can sent you by mail if you want.This document you "must" read to undrestand why it is used. – rkosegi Mar 30 '12 at 05:44
  • yea I know. Sure, you can send it to hemanth94@gmail.com . Also, I have asked the admin guys to look into it! Thanks. – h-kach Mar 30 '12 at 05:59
  • Just sent, when you finish reading and have more questions, let me know – rkosegi Mar 30 '12 at 06:03
  • yea, got it now.. I will go through it and let you know. Thanks a lot. – h-kach Mar 30 '12 at 06:05
  • hey I went thorough the doc you sent me. Got a question, does package prefixes mean 'jnp:' in jnp://www.jboss.org:1099/ ?? I mean , first part of the URL provider? – h-kach Mar 31 '12 at 07:08
  • The jnp: portion of the URL is the protocol and refers to the socket/RMI based protocol used by JBoss.In other words is URL scheme prefix for java.naming.provider. – rkosegi Mar 31 '12 at 07:58
  • is it same as the package prefix used by java.naming.factory.url.pkgs? Also they have mentioned 2 package prefix, one is java: and other jnp: . is this the same used for java naming provider? – h-kach Mar 31 '12 at 09:02
  • No, it's fully qualified class name like org.jboss.naming:org.jnp.interfaces without any prefix.See my updated answer – rkosegi Mar 31 '12 at 10:00
  • The package prefix in org.jboss.naming:org.jnp.interfaces is used to locate jnp: , java: and the likes?? – h-kach Mar 31 '12 at 10:32
  • Well, I have gone through it again and this is my understanding, Please tell me if I am correct. 'org.jboss.naming' and/or 'org.jnp.interfaces' are the package prefixes used to search for the URL "SampleEjbBean/remote" passed to lookup() in the 'jnp:' and 'java:' URL context factories of the JBoss JNDI provider. Is this correct? P.S : Please see my updated code. – h-kach Mar 31 '12 at 14:22
  • Did you have anything to add?? I mean , what could have been your bigger answer ? :) – h-kach Mar 31 '12 at 14:31
  • In longer answer, yes. I'm just wondering why you need to know this detail.Do you have some problem with it?I mean some error, stacktrace, etc .. – rkosegi Mar 31 '12 at 14:35
  • 3
    @rkosegi.. oh, I am not facing any problem, Just for knowledge. I want to clearly know why I am using each line of my JAVA code. I do not want to just copy and paste my codes. Thanks a lot for your patience and support. – h-kach Mar 31 '12 at 14:39
  • 1
    You're welcome, it's best approach to know what your coding.Thumbs up. – rkosegi Mar 31 '12 at 14:40