Oh ! Thanks ! Its good to know I dont need to know tomcat,etc. to use jndi in the simple way I want to.
I think my questions have been rather haphazard (I am confused), thats why I think you are not exactly able to know what I dont understand. I'll try to ask in a more organized manner this time, taking one problem at a time:
1. First step to use jndi has to be to register/bind a logical name with an object, right ? The object here is a datasource object. Say, I want to bind the logical name "jdbc/AcmeDB" to a datasource object. The following code should do it (taken from this
site) (see under the heading "4.1.3 Creating and Registering a DataSource Object")
The above code gives an Exception because I havent set the environment settings. Now, I can either set the environment settings through a .properties file or directly in this code by making a Hashtable object, setting the values, and passing the Hashtable object in the InitialContext's constructor. Though using a .properties file would be better, lets just use a Hashtable here to make things simpler. Following code should do that (shouldn't it?):
Now,
The Question is: What do I need to put in the place of "WHAT SHOULD I PUT HERE" 1 and 2 to make this code work ?
Also, are the values for Context.INITIAL_CONTEXT_FACTORY and Context.PROVIDER_URL right ?
Right now, no matter what I put in the place of "WHAT SHOULD I PUT HERE" 1 and 2, the above code gives the following Exceptions:
Exception in
thread "main" javax.naming.CommunicationException: localhost:389 [Root exception is java.net.ConnectException: Connection refused: connect]
at com.sun.jndi.ldap.Connection.<init>(Connection.java:207)
at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:118)
at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1580)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2616)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:287)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:197)
at Register.main(Register.java:21)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at com.sun.jndi.ldap.Connection.createSocket(Connection.java:349)
at com.sun.jndi.ldap.Connection.<init>(Connection.java:184)
... 13 more
I found the javadocs for these constants, and though they give me a good insight on what they are, I have no clue what to set their value to in my case. These are the javadocs :
Context.OBJECT_FACTORIES :
Constant that holds the name of the environment property for specifying the list of object factories to use. The value of the property should be a colon-separated list of the fully qualified class names of factory classes that will create an object given information about the object. This property may be specified in the environment, an
applet parameter, a system property, or one or more resource files.
The value of this constant is "java.naming.factory.object".
Context.STATE_FACTORIES
Constant that holds the name of the environment property for specifying the list of state factories to use. The value of the property should be a colon-separated list of the fully qualified class names of state factory classes that will be used to get an object's state given the object itself. This property may be specified in the environment, an applet parameter, a system property, or one or more resource files.
I hope my question is clear this time.
Thanks a ton again Freddy !