Trouble using #Resource and JNDI lookup - java

I'm having trouble applying the concept of injection and also JNDI with EJBs and I'd like your help. I am learning this and I really want to understand and apply the techniques of #Resource and/or JNDI lookups with XML configuration. I can't seem to find any of the initial parameters in my JNDI lookups. Now, before I continue, if I manually enter the JNDI name of my datasource, everything works great. What I'm trying to do (again as an exercise) is use #Resource or JNDI to get the JNDI datasource name itself and then do a JNDI lookup on the datasource. I know that you can directly inject a DataSource object but if I can't inject a String, I gotta start with this.
First, here's the important part of my ejb-jar.xml:
<env-entry>
<description>DataSource JNDI lookup name</description>
<env-entry-name>datasourceName</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>myDataSource</env-entry-value>
</env-entry>
In terms of injection using #Resource I've tried I've tried the following field in the EJB
#Resource(name = "datasourceName")
String dsName;
I've also tried with mappedName and lookup (using JNDI lookup) nothing is coming in. Then I've tried JNDI lookup as follows:
// get JNDI name from environment entry in EJB Context and use to lookup DataSource
Context context = new InitialContext();
String dsName = (String) context.lookup("java:comp/env/SRS/datasourceName");
(The name of the application is SRS) - This comes up with a NamingException that the JNDI lookup did not find anything. I've tried the following lookups:
String datasourceName = (String) context.lookup("java:comp/env/SRS/Status/datasourceName"); // name of EJB is Status
String datasourceName = (String) context.lookup("datasourceName");
String datasourceName = (String) context.lookup("SRS/datasourceName");
String datasourceName = (String) context.lookup("SRS/Status/datasourceName");
String datasourceName = (String) context.lookup("java:global/SRS/datasourceName");
String datasourceName = (String) context.lookup("java:global/SRS/Status/datasourceName");
My first post here so I hope I asked properly. Thanks for the help!

Ok. Two days later I saw that my mistake was nowhere in the code of the EJB itself but in a configuration of the JSP which invoked the EJB!! My mistake is that I had somehow invoked the methods of the EJB class as a POJO and NOT through the container. I had assumed since the EJB methods were being called that the container had correctly injected the EJB to the JSP. But it didn't. I put in a JNDI lookup in the JSP and everything resolved itself. Thanks everyone for your responses!

Related

Remote lookup to another Glassfish from a Servlet

I'm trying to run a remote lookup to another Glassfish from a Servlet. So, I was following the link documentation (http://docs.oracle.com/cd/E19798-01/821-1752/beanv/index.html). First I created a Sateless Session Ben called CalculatorBean, packaged in an EJB JAR of the same name (CalculatorBean), the JNDI name was java:global/CalculatorBean/CalculatorBean.
According to the documentation, I created a Web project and declared my EJB in sub-web.xml the following file:
<ejb-ref>
<ejb-ref-name>ejb/CalculatorBean</ejb-ref-name>
<jndi-name>corbaname:iiop:127.0.0.1:3700#CalculatorBean/CalculatorBean</jndi-name>
</ejb-ref>
where 127.0.0.1 is the host of the machine (local!), 3700 is the default port for querying and, CalculatorBean/CalculatorBean is the global JNDI name. First question, theoretically the JNDI name passes into an interoperable String "CalculatorBean/CalculatorBean" instead of "java: global/CalculatorBean/CalculatorBean", right?
After that, I created a Servlet and put the following code snippet:
ctx = new InitialContext ();
bean = (CalculatorRemote) ctx.lookup ("java:comp/env/ejb/CalculatorBean");
Where, CalculatorRemote is the name of the remote interface that we included in the java project:comp/env/ is the directory section to access Java EE components and ejb/CalculatorBean is the name of my bean in the configuration of the sun-web.xml file
 
When put to run my Servlet I'm getting the exception:
Caused by: javax.naming.NameNotFoundException: No object bound to name java:comp/env/ejb/CalculatorBean
Obviously, it's not finding the name, however, do not really know what name I should use to set the lookup.
I had the same problem, and I solved it.
By default, your EJB is not visilbe into java:comp/env/ and you can not lookup for an EJB into InitialContext instance. But, you can successful lookup for an EJB after when at least one EJB instance is injected using #EJB annotation, like, for your example:
#EJB(name = "ejb/CalculatorBean")
private CalculatorRemote calc;
After that, CalculatorRemote EJB is visible in InitialContext instance.

comp/env/pool not found in context "java:"?

I have a web application trying to access a JNDI declared in WebSphere Application Server.
The JNDI is declared under Object pool managers. However, I'm receiving an error when I access the pool. The error says that comp/env/pool is not found in context "java:".
My code is written as follows:
InitialContext initialContext = new InitialContext();
ObjectPoolManager opm = (ObjectPoolManager)initialContext.lookup("java:comp/env/pool");
Accessing the pool via the code below works:
ObjectPoolManager opm = (ObjectPoolManager)initialContext.lookup("pool");
I'm confused because according to what I've found on the internet, java:comp/env/ is a default prefix for JNDI. So why does it cause an error in my case?
Thank you!
you can only use java:comp/env if you have declared a reference to the Object Pool in your web.xml under the resource-ref section.
See What is resource-ref in web.xml used for? for further explanation.

Context is read only

Helo masters, I have to create a JNDI Datasource dynamically, I tried to do it with a listener called SetupApplicationListener. Here is the beginning of WEB-LIB/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee">
<display-name>pri-web</display-name>
<!-- Listeners -->
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<listener>
<listener-class>myapp.SetupApplicationListener</listener-class>
</listener>
The code of the listener:
public class SetupApplicationListener implements ServletContextListener {
public static Log LOG = null;
public void contextInitialized(ServletContextEvent ctx){
try {
createOracleDataSource();
.....
}
}
private void createOracleDataSource() throws SQLException, NamingException {
OracleDataSource ds = new OracleDataSource();
ds.setDriverType(...);
ds.setServerName(...);
ds.setPortNumber(...);
ds.setDatabaseName(...);
ds.setUser(...);
ds.setPassword(...);
new InitialContext().bind("java:comp/env/jdbc/myDS", ds);
}
.....
}
And there is the error:
[ERROR] 29/01/2013 09:44:50,517 (SetupApplicationListener.java:86) -> Error
javax.naming.NamingException: Context is read only
at org.apache.naming.NamingContext.checkWritable(NamingContext.java:903)
at org.apache.naming.NamingContext.bind(NamingContext.java:831)
at org.apache.naming.NamingContext.bind(NamingContext.java:171)
at org.apache.naming.NamingContext.bind(NamingContext.java:187)
at org.apache.naming.SelectorContext.bind(SelectorContext.java:186)
at javax.naming.InitialContext.bind(InitialContext.java:359)
at myapp.SetupApplicationListener.createOracleDataSource(SetupApplicationListener.java:102)
Can I set the read-only properties of the Context to "true"? Thanks! :)
Tomcat 6.0
Oracle 11g
jdk1.5
EDIT: Don't need to be dynamically, i have to define a jndi datasource internally I can't modify the server files because it is a shared server. It must be jndi because other modules use it in that way, thanks.
If you need to create a datasource dynamically is there really any need for a JNDI lookup? JNDI is designed to make the connection external to the application, while in your scenario its tightly coupled to the application due to a legitimate requirement. Why not just use a JDBC connection?
You need to create a ServletContextListener and there you can make the InitialContext writable - it's not the way it should be done, but if you really need it, this is one way you can do it.
This also works with Java Melody!
protected void makeJNDIContextWritable(ServletContextEvent sce) {
try {
Class<?> contextAccessControllerClass = sce.getClass().getClassLoader().loadClass("org.apache.naming.ContextAccessController");
Field readOnlyContextsField = contextAccessControllerClass.getDeclaredField("readOnlyContexts");
readOnlyContextsField.setAccessible(true);
Hashtable readOnlyContexts = (Hashtable) readOnlyContextsField.get(null);
String context = null;
for (Object key : readOnlyContexts.keySet()) {
String keyString = key + "";
if (keyString.endsWith(sce.getServletContext().getContextPath())) {
context = keyString;
}
}
readOnlyContexts.remove(context);
} catch (Exception ex) {
ex.printStackTrace();
}
}
I haven't got this problem before since I usually defined JNDI in application server(tomcat, weblogic and etc). Just like what Kevin said, this is exactly what JNDI was designed for; separating datasource config from your source code and retrieving JNDI resources through lookup and inject;
Back to your question, I think tomcat has every strict rules on modifying JNDI at runtime. In another word, you cannot re-bind or remove jndi from Context. If you go through the tomcat specification you will probably see some thing about jndi lookup but no re-bind.
From section EE.5.3.4 of the EE 6 platform specification (JSR 316):
The container must ensure that the application component instances
have only read access to their naming context. The container must
throw the javax.naming.OperationNotSupportedException from all the
methods of the javax.naming.Context interface that modify the
environment naming context and its subcontexts.
Note that "their naming context" in this section is referring to java:comp.
I solved this problem when found that I was closing environmentContext object
For example:
Context context=new InitialContext();
Context environmentContext=(Context) context.lookup("java:comp/env");
And my code was:
environmentContext.close();
After removing close function from environmentContext problem was solded for me;
I also had this problem, but being new to Tomee, I didn't know that there is a simple solution. When I deployed my web app to the webapps folder, the app worked fine, but when I deployed it to a service folder, I got the same abort. The problem was that the folder name did not match the war name (minus the .war). Once I fixed that, the app worked fine. Make sure the war name, folder name and service name are identical. This problem produces several different errors, including Context is read only and Error merging Java EE JNDI entries.
I solved this issue by setting useNaming="false" in my context.xml.
From the documentation:
useNaming : Set to true (the default) to have Catalina enable a JNDI InitialContext for this web application that is compatible with Java2 Enterprise Edition (J2EE) platform conventions.

ProgrammaticLogin

I use ProgrammaticLogin in Glassfish 3.1.2 with username,password and realm and it works good. Anyway when I call context.getCallerPrincipal() I get org.glassfish.security.common.PrincipalImpl with context.getCallerPrincipal().getName() which returns the username.
Is there any way that I can pass Principle via ProgrammaticLogin
so it is passed in my ejb and available from SessionContext - context.getCallerPrincipal()?
I guess you are in a context where resource-injection is not possible via annotations.
You can try to get an instance of the desired EJB via a manual JNDI lookup.
Example:
InitialContext init = new InitialContext();
YourBean bean = (YourBean) init.lookup(JNDI_NAME_OF_YOUR_BEAN);
Now you should be able to pass the Principal to your bean.

Where is the JNDI name in my code?

I have created a EJB2.0 using Eclipse 3.7 IDE, and deployed it in JBoss 5 application server (my bean name is product). I am doing normal context lookup (and other stuff to call ejb), and I am able to call EJB successfully. Now my question is what is JNDI name exactly, and where did it get used in all this. Is my bean name the JNDI name, or is this my JNDI name -> org.jnp.interfaces.NamingContextFactory. Where is the JNDI name in this?????
my code:-
// initial code.............
Context ctx = getContext();
Object obj=ctx.lookup("Product");
ProductHome home =(ProductHome) javax.rmi.PortableRemoteObject.narrow(obj,ProductHome.class);
ProductRemote remote=home.create();
Product prd = new rohit.Product("PRDCamera",001,50.50) ;
remote.addProduct(prd);
remote.updateProduct(prd);
remote.removeProduct(001);
remote.findProduct(001);
remote.findAllProduct();
// getContext Method
public static InitialContext getContext() throws Exception{
Properties pro = new Properties();
pro.put(javax.naming.InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
pro.put(javax.naming.InitialContext.PROVIDER_URL,"localhost:1099");
return new InitialContext(pro);
}
There is no JNDI name in your code.
This is how you look up EJBs in EJB 2.0:
Object ejbHome = initialContext.lookup("java:comp/env/com/mycorp/MyEJB");
MyHome myHome = (MyHome)javax.rmi.PortableRemoteObject.narrow(
(org.omg.CORBA.Object)ejbHome, MyHome.class);
The JNDI name is java:comp/env/com/mycorp/MyEJB in this case.
In the much saner EJB 3.0, you just do
MyEJB myEJB = initialContext.lookup("java:comp/env/com/mycorp/MyEJB")
and do away with the terrible home interface idea.

Categories