Here follows a part of the code I used from a class running under
Tomcat(running as a separate bundle) to retrieve a reference to a session
bean deployed under Jboss:
try {
InitialContext context;
//setJbossProperties returns a Properties object loaded with jndi.properties
(see at bottom its implementation)
context = new InitialContext(setJbossProperties());
Object node = context.lookup(sessionName);
UsersDbManagerHome home =
(UsersDbManagerHome)PortableRemoteObject.narrow(node,
UsersDbManagerHome.class);
bean = home.create();
} catch (RemoteException e) {
System.err.println("RemoteException in creating the DbManagerService...");
return null;
} catch (CreateException e) {
System.err.println("CreateException in creating the DbManagerService...");
return null;
}
catch (NamingException e1) {
System.err.println("NamingException in creating the DbManagerService...");
System.err.println("This exception could mean that the application server
may be not running or" +
"\nthe information provided in the jndi.properties file are not of the right
" +
"type. \nPlease ensure that the application server is up and running and the
" +
"\ninformation provided in the jndi.properties file are of the right
type.");
return null;
}
Here follows the setJbossProperties() method:
public Properties setJbossProperties() {
ClassLoader loader = getClass().getClassLoader();
InputStream is = loader.getResourceAsStream("jndi.properties");
Properties properties = new Properties();
logger.info("Trying to load the jndi.properties file");
try {
properties.load(is);
setJbossProperties(properties);
logger.info("jndi.properties file loaded");
} catch (IOException e) {
logger.error("Problem during loading of jndi.properties");
}
return properties;
}
Hope it will help,
Marco
----- Original Message -----
From: jonathan wong
To: [EMAIL PROTECTED]
Sent: Tuesday, June 10, 2003 3:42 AM
Subject: Re: [JBoss-user] Hello ! The problem of JNDI in JSP !
Dear all ,
Hello ! Thank you for all your help . I try to rewrite the JSP like follow
:
<%@ page
session="false"
isThreadSafe="true"
isErrorPage="false"
import="javax.naming.*,java.util.*,
com.sample.*"
%>
<%
try {
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
properties.put(Context.PROVIDER_URL, "localhost:1099");
Context lContext = new InitialContext(properties);
HelloWorldHome lHome = (HelloWorldHome) lContext.lookup(
"java:/com/sample/HelloWorld"
);
HelloWorld lSession = lHome.create();
out.println( "" + lSession.hello() );
}
catch( Exception e ) {
out.println( "Caugth exception: " + e.getMessage() );
e.printStackTrace();
}
%>
Also , the "jndi.properties" resides the WEB-INF\classes , the structure as
follow :
E:\jboss\tomcat-4.1.x\webapps\test\WEB-INF\classes
E:\jboss\tomcat-4.1.x\webapps\test\WEB-INF\classes\jndi.properties
However , the Tomcat still says "Name com is not bound in this Context " if
I load the JSP ? Therefore , should I have the futher configurations ? ( I
so confuse to place and use jndi.properties and JNDI ) Thank you !
Marco Tedone <[EMAIL PROTECTED]> wrote:
A couple of considerations:
1) If your JSP is running from a context different from Jboss, then you
should create the InitialContext using the jndi.properties file like:
InitialContext context = new InitialContext(properties);
2) The jndi name of your bean could be probably looked up as:
com/sample/HelloWorld (jboss.xml <jndi-name> element)
3) The jndi.properties file could(should?) reside under WEB-INF/classes (but
I could be wrong on it!)
Hope it will help,
Marco
----- Original Message -----
From: jonathan wong
To: [EMAIL PROTECTED]
Sent: Monday, June 09, 2003 6:56 PM
Subject: [JBoss-user] Hello ! The problem of JNDI in JSP !
Dear all ,
Hello ! I am a beginner of JBoss 3.0.7 . I encounter a problem of JNDI in
JSP . The file structure like follow :
E:\JBoss3T\test_EJB\com\sample\HelloWorld.class
E:\JBoss3T\test_EJB\com\sample\HelloWorldBean.class
E:\JBoss3T\test_EJB\com\sample\HelloWorldHome.class
And the ejb-jar.xml like follow :
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<description>JBoss Hello World Application</description>
<display-name>Hello World EJB</display-name>
<enterprise-beans>
<session>
<ejb-name>HelloWorld</ejb-name>
<home>com.sample.HelloWorldHome</home>
<remote>com.sample.HelloWorld</remote>
<ejb-class>com.sample.HelloWorldBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
The jboss.xml like follow :
<jboss>
<enterprise-beans>
<session>
<ejb-name>HelloWorld</ejb-name>
<jndi-name>com/sample/HelloWorld</jndi-name>
</session>
</enterprise-beans>
</jboss>
The JSP is as follow :
<%@ page
session="false"
isThreadSafe="true"
isErrorPage="false"
import="javax.naming.*,
com.sample.*"
%>
<%
try {
Context lContext = new InitialContext();
HelloWorldHome lHome = (HelloWorldHome) lContext.lookup(
"java:comp/env/com/sample/HelloWorld"
);
HelloWorld lSession = lHome.create();
out.println( "" + lSession.hello() );
}
catch( Exception e ) {
out.println( "Caugth exception: " + e.getMessage() );
e.printStackTrace();
}
%>
I also reside the jndi.properties with WEB-INF ( I use the standalone
Tomcat ) . The JNDI is displayed in global JNDI Namespace in jmx-console .
However , the Tomcat says "Name com is not bound in this Context" .
Therefore , what should I further configue ?
Jonathan
(Wong Yat Sing)
Jonathan Studio
M.P (852) - 91235947
NetMeeting : [EMAIL PROTECTED]
ICQ# 57646152
Do you Yahoo!?
Free online calendar with sync to Outlook(TM).
Jonathan
(Wong Yat Sing)
Jonathan Studio
M.P (852) - 91235947
NetMeeting : [EMAIL PROTECTED]
ICQ# 57646152
Do you Yahoo!?
Free online calendar with sync to Outlook(TM).
-------------------------------------------------------
This SF.net email is sponsored by: Etnus, makers of TotalView, The best
thread debugger on the planet. Designed with thread debugging features
you've never dreamed of, try TotalView 6 free at www.etnus.com.
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user